Added helper classes for items that have and inventory and a gui

This commit is contained in:
bdew committed 2014-02-17 12:26:20 +02:00
1 parent 1296b32110
commit 13cef01a2d
3 files changed
+137

No files matched your search

@@ -0,0 +1,29 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.items.inventory
import net.minecraft.entity.player.EntityPlayer
import net.bdew.lib.gui.BaseContainer
import net.minecraft.item.ItemStack
import net.minecraft.inventory.Slot
class ContainerItemInventory(inv: InventoryItemAdapter, player: EntityPlayer) extends BaseContainer(inv) {
def canInteractWith(entityplayer: EntityPlayer) = entityplayer == player
override def slotClick(slotnum: Int, button: Int, modifiers: Int, player: EntityPlayer): ItemStack = {
if (slotnum > 0 && slotnum < inventorySlots.size()) {
val slot = inventorySlots.asInstanceOf[java.util.List[Slot]].get(slotnum)
if (slot.isSlotInInventory(inv.player.inventory, inv.slot))
return null
}
if (modifiers == 2 && button == inv.slot) return null
return super.slotClick(slotnum, button, modifiers, player)
}
}
@@ -0,0 +1,47 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.items.inventory
import net.minecraft.item.ItemStack
import net.bdew.lib.tile.inventory.BaseInventory
import net.bdew.lib.Misc
import net.minecraft.nbt.{NBTTagList, NBTTagCompound}
import net.minecraft.entity.player.EntityPlayer
class InventoryItemAdapter(val player: EntityPlayer, val slot: Int, val size: Int, val tagName: String) extends BaseInventory {
def getSizeInventory = size
def getStack = player.inventory.getStackInSlot(slot)
if (getStack.hasTagCompound)
load()
def load() {
val tag = getStack.getTagCompound
for (nbtItem <- Misc.iterNbtList[NBTTagCompound](tag.getTagList(tagName))) {
val slot = nbtItem.getByte("Slot")
if (slot >= 0 && slot < inv.size) {
inv(slot) = ItemStack.loadItemStackFromNBT(nbtItem)
}
}
}
def onInventoryChanged() {
if (!getStack.hasTagCompound) getStack.setTagCompound(new NBTTagCompound)
val tag = getStack.getTagCompound
val itemList = new NBTTagList
for ((item, i) <- inv.view.zipWithIndex if item != null) {
val itemNbt: NBTTagCompound = new NBTTagCompound
itemNbt.setByte("Slot", i.asInstanceOf[Byte])
item.writeToNBT(itemNbt)
itemList.appendTag(itemNbt)
}
tag.setTag(tagName, itemList)
}
}
@@ -0,0 +1,61 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.items.inventory
import net.minecraft.item.Item
import net.bdew.lib.gui.{BaseScreen, GuiProvider}
import cpw.mods.fml.common.Loader
import cpw.mods.fml.relauncher.{Side, SideOnly}
import net.minecraft.entity.player.EntityPlayer
import net.bdew.lib.BdLib
import net.minecraft.world.World
/**
* Mixin for items that have an internal inventory
* Subclasses are responsible for registering themself with the GuiHandler of their mod
*/
trait ItemInventory extends Item with GuiProvider {
type TEClass = Any
def guiId: Int
val invSize: Int
val invTagName: String
val modObj = Loader.instance().activeModContainer().getMod
@SideOnly(Side.CLIENT)
def makeGui(inv: InventoryItemAdapter, player: EntityPlayer): BaseScreen
def makeContainer(inv: InventoryItemAdapter, player: EntityPlayer): ContainerItemInventory
def makeAdaptor(player: EntityPlayer) = new InventoryItemAdapter(player, player.inventory.currentItem, invSize, invTagName)
@SideOnly(Side.CLIENT)
def getGui(te: TEClass, player: EntityPlayer) = {
val stack = player.inventory.getCurrentItem
if (stack.getItem == this) {
makeGui(makeAdaptor(player), player)
} else {
BdLib.logWarn("Attempt to open item container GUI without active item (%s)", this)
null
}
}
def getContainer(te: TEClass, player: EntityPlayer) = {
val stack = player.inventory.getCurrentItem
if (stack.getItem == this) {
makeContainer(makeAdaptor(player), player)
} else {
BdLib.logWarn("Attempt to open item container GUI without active item by '%s' (%s)", player.getCommandSenderName, this)
null
}
}
def doOpenGui(player: EntityPlayer, world: World) {
player.openGui(modObj, guiId, world, player.posX.toInt, player.posY.toInt, player.posZ.toInt)
}
}