From 13cef01a2d5b1b8ce7e67f741814248c213b1496 Mon Sep 17 00:00:00 2001 From: bdew Date: Mon, 17 Feb 2014 12:26:20 +0200 Subject: [PATCH] Added helper classes for items that have and inventory and a gui --- .../inventory/ContainerItemInventory.scala | 29 +++++++++ .../inventory/InventoryItemAdapter.scala | 47 ++++++++++++++ .../lib/items/inventory/ItemInventory.scala | 61 +++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/net/bdew/lib/items/inventory/ContainerItemInventory.scala create mode 100644 src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala create mode 100644 src/net/bdew/lib/items/inventory/ItemInventory.scala diff --git a/src/net/bdew/lib/items/inventory/ContainerItemInventory.scala b/src/net/bdew/lib/items/inventory/ContainerItemInventory.scala new file mode 100644 index 0000000..1694510 --- /dev/null +++ b/src/net/bdew/lib/items/inventory/ContainerItemInventory.scala @@ -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) + } +} diff --git a/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala b/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala new file mode 100644 index 0000000..7f031e4 --- /dev/null +++ b/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala @@ -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) + } +} diff --git a/src/net/bdew/lib/items/inventory/ItemInventory.scala b/src/net/bdew/lib/items/inventory/ItemInventory.scala new file mode 100644 index 0000000..285712c --- /dev/null +++ b/src/net/bdew/lib/items/inventory/ItemInventory.scala @@ -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) + } +}