From ab02a09159e631c01fd5f2c041f7261642913aff Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sun, 4 Aug 2013 21:27:56 -0400 Subject: [PATCH] Visible inventory logistical management (hopefully) complete --- src/resonantinduction/base/SetUtil.java | 10 ++++ .../battery/BatteryUpdateProtocol.java | 12 +++++ .../battery/SynchronizedBatteryData.java | 20 +++++++ .../battery/TileEntityBattery.java | 53 +++++++++++-------- 4 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/resonantinduction/base/SetUtil.java b/src/resonantinduction/base/SetUtil.java index 67dad1f7..740ff1ec 100644 --- a/src/resonantinduction/base/SetUtil.java +++ b/src/resonantinduction/base/SetUtil.java @@ -59,6 +59,11 @@ public class SetUtil return toReturn; } + /** + * Copies a set. + * @param set - set to copy + * @return copied set + */ public static Set copy(Set set) { Set toReturn = new HashSet(); @@ -145,6 +150,11 @@ public class SetUtil return toReturn; } + /** + * Gets the top object in a set. + * @param set - set to get the object from + * @return top object in the set + */ public static V getTop(Set set) { for(V obj : set) diff --git a/src/resonantinduction/battery/BatteryUpdateProtocol.java b/src/resonantinduction/battery/BatteryUpdateProtocol.java index 5f19ff35..a30c459c 100644 --- a/src/resonantinduction/battery/BatteryUpdateProtocol.java +++ b/src/resonantinduction/battery/BatteryUpdateProtocol.java @@ -219,11 +219,17 @@ public class BatteryUpdateProtocol { ArrayList> inventories = SetUtil.split(oldStructure.inventory, iteratedNodes.size()); List iterList = SetUtil.asList(iteratedNodes); + boolean didVisibleInventory = false; for(int i = 0; i < iterList.size(); i++) { TileEntityBattery tile = iterList.get(i); tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i)); + + if(!didVisibleInventory) + { + tile.structure.visibleInventory = oldStructure.visibleInventory; + } } } } @@ -258,6 +264,12 @@ public class BatteryUpdateProtocol TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj); structureFound.inventory = SetUtil.merge(structureFound.inventory, tileEntity.structure.inventory); + + if(tileEntity.structure.hasVisibleInventory()) + { + structureFound.visibleInventory = tileEntity.structure.visibleInventory; + } + tileEntity.structure = structureFound; } diff --git a/src/resonantinduction/battery/SynchronizedBatteryData.java b/src/resonantinduction/battery/SynchronizedBatteryData.java index 78e5f102..c0ca748c 100644 --- a/src/resonantinduction/battery/SynchronizedBatteryData.java +++ b/src/resonantinduction/battery/SynchronizedBatteryData.java @@ -14,6 +14,11 @@ public class SynchronizedBatteryData public Set inventory = new HashSet(); + /** + * Slot 0: Cell input slot + * Slot 1: Battery charge slot + * Slot 2: Battery discharge slot + */ public ItemStack[] visibleInventory = new ItemStack[3]; public int length; @@ -26,6 +31,8 @@ public class SynchronizedBatteryData public boolean didTick; + public boolean wroteVisibleInventory; + public int getVolume() { return length*width*height; @@ -67,6 +74,19 @@ public class SynchronizedBatteryData } } + public boolean hasVisibleInventory() + { + for(ItemStack itemStack : visibleInventory) + { + if(itemStack != null) + { + return true; + } + } + + return false; + } + public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, Set inventory) { SynchronizedBatteryData structure = getBase(tileEntity); diff --git a/src/resonantinduction/battery/TileEntityBattery.java b/src/resonantinduction/battery/TileEntityBattery.java index 7fe31ed5..e6d06fcf 100644 --- a/src/resonantinduction/battery/TileEntityBattery.java +++ b/src/resonantinduction/battery/TileEntityBattery.java @@ -94,18 +94,21 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } //Visible inventory - NBTTagList tagList1 = nbtTags.getTagList("VisibleItems"); - structure.visibleInventory = new ItemStack[3]; - - for(int tagCount = 0; tagCount < tagList1.tagCount(); tagCount++) + if(nbtTags.hasKey("VisibleItems")) { - NBTTagCompound tagCompound = (NBTTagCompound)tagList1.tagAt(tagCount); - byte slotID = tagCompound.getByte("Slot"); - - if(slotID >= 0 && slotID < structure.visibleInventory.length) - { - setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound)); - } + NBTTagList tagList1 = nbtTags.getTagList("VisibleItems"); + structure.visibleInventory = new ItemStack[3]; + + for(int tagCount = 0; tagCount < tagList1.tagCount(); tagCount++) + { + NBTTagCompound tagCompound = (NBTTagCompound)tagList1.tagAt(tagCount); + byte slotID = tagCompound.getByte("Slot"); + + if(slotID >= 0 && slotID < structure.visibleInventory.length) + { + setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound)); + } + } } } @@ -130,20 +133,24 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver nbtTags.setTag("Items", tagList); //Visible inventory - NBTTagList tagList1 = new NBTTagList(); - - for(int slotCount = 0; slotCount < structure.visibleInventory.length; slotCount++) + if(!structure.wroteVisibleInventory) { - if(getStackInSlot(slotCount) != null) - { - NBTTagCompound tagCompound = new NBTTagCompound(); - tagCompound.setByte("Slot", (byte)slotCount); - getStackInSlot(slotCount).writeToNBT(tagCompound); - tagList1.appendTag(tagCompound); - } + NBTTagList tagList1 = new NBTTagList(); + + for(int slotCount = 0; slotCount < structure.visibleInventory.length; slotCount++) + { + if(getStackInSlot(slotCount) != null) + { + NBTTagCompound tagCompound = new NBTTagCompound(); + tagCompound.setByte("Slot", (byte)slotCount); + getStackInSlot(slotCount).writeToNBT(tagCompound); + tagList1.appendTag(tagCompound); + } + } + + nbtTags.setTag("VisibleItems", tagList1); + structure.wroteVisibleInventory = true; } - - nbtTags.setTag("VisibleItems", tagList1); } public void update()