Made inventory trait a bit more flexible.

This commit is contained in:
Florian Nücke 2015-02-24 02:31:13 +01:00
parent b34ec529bd
commit cefa0464dc
4 changed files with 14 additions and 8 deletions

View File

@ -11,6 +11,8 @@ import net.minecraftforge.common.util.Constants.NBT
trait Inventory extends IInventory { trait Inventory extends IInventory {
def items: Array[Option[ItemStack]] def items: Array[Option[ItemStack]]
def updateItems(slot: Int, stack: ItemStack) = items(slot) = Option(stack)
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //
override def getStackInSlot(slot: Int) = override def getStackInSlot(slot: Int) =
@ -45,7 +47,7 @@ trait Inventory extends IInventory {
} }
val oldStack = items(slot) val oldStack = items(slot)
items(slot) = None updateItems(slot, null)
if (oldStack.isDefined) { if (oldStack.isDefined) {
onItemRemoved(slot, oldStack.get) onItemRemoved(slot, oldStack.get)
} }
@ -53,7 +55,7 @@ trait Inventory extends IInventory {
if (stack.stackSize > getInventoryStackLimit) { if (stack.stackSize > getInventoryStackLimit) {
stack.stackSize = getInventoryStackLimit stack.stackSize = getInventoryStackLimit
} }
items(slot) = Some(stack) updateItems(slot, stack)
} }
if (items(slot).isDefined) { if (items(slot).isDefined) {
@ -84,7 +86,7 @@ trait Inventory extends IInventory {
nbt.getTagList(Settings.namespace + "items", NBT.TAG_COMPOUND).foreach((slotNbt: NBTTagCompound) => { nbt.getTagList(Settings.namespace + "items", NBT.TAG_COMPOUND).foreach((slotNbt: NBTTagCompound) => {
val slot = slotNbt.getByte("slot") val slot = slotNbt.getByte("slot")
if (slot >= 0 && slot < items.length) { if (slot >= 0 && slot < items.length) {
items(slot) = Option(ItemUtils.loadStack(slotNbt.getCompoundTag("item"))) updateItems(slot, ItemUtils.loadStack(slotNbt.getCompoundTag("item")))
} }
}) })
} }

View File

@ -11,7 +11,9 @@ trait ItemStackInventory extends Inventory {
// The item stack that provides the inventory. // The item stack that provides the inventory.
def container: ItemStack def container: ItemStack
lazy val items = Array.fill[Option[ItemStack]](getSizeInventory)(None) private lazy val inventory = Array.fill[Option[ItemStack]](getSizeInventory)(None)
override def items = inventory
// Initialize the list automatically if we have a container. // Initialize the list automatically if we have a container.
if (container != null) { if (container != null) {
@ -24,14 +26,14 @@ trait ItemStackInventory extends Inventory {
container.setTagCompound(new NBTTagCompound()) container.setTagCompound(new NBTTagCompound())
} }
for (i <- 0 until items.length) { for (i <- 0 until items.length) {
items(i) = None updateItems(i, null)
} }
if (container.getTagCompound.hasKey(Settings.namespace + "items")) { if (container.getTagCompound.hasKey(Settings.namespace + "items")) {
val list = container.getTagCompound.getTagList(Settings.namespace + "items", NBT.TAG_COMPOUND) val list = container.getTagCompound.getTagList(Settings.namespace + "items", NBT.TAG_COMPOUND)
for (i <- 0 until (list.tagCount min items.length)) { for (i <- 0 until (list.tagCount min items.length)) {
val tag = list.getCompoundTagAt(i) val tag = list.getCompoundTagAt(i)
if (!tag.hasNoTags) { if (!tag.hasNoTags) {
items(i) = Option(ItemUtils.loadStack(tag)) updateItems(i, ItemUtils.loadStack(tag))
} }
} }
} }

View File

@ -83,7 +83,7 @@ class Assembler extends traits.Environment with traits.PowerAcceptor with traits
requiredEnergy = totalRequiredEnergy requiredEnergy = totalRequiredEnergy
ServerPacketSender.sendRobotAssembling(this, assembling = true) ServerPacketSender.sendRobotAssembling(this, assembling = true)
for (slot <- 0 until getSizeInventory) items(slot) = None for (slot <- 0 until getSizeInventory) updateItems(slot, null)
markDirty() markDirty()
true true

View File

@ -9,7 +9,9 @@ import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.common.util.ForgeDirection import net.minecraftforge.common.util.ForgeDirection
trait Inventory extends TileEntity with inventory.Inventory { trait Inventory extends TileEntity with inventory.Inventory {
lazy val items = Array.fill[Option[ItemStack]](getSizeInventory)(None) private lazy val inventory = Array.fill[Option[ItemStack]](getSizeInventory)(None)
def items = inventory
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //