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 d8b2686e..e6d06fcf 100644 --- a/src/resonantinduction/battery/TileEntityBattery.java +++ b/src/resonantinduction/battery/TileEntityBattery.java @@ -82,6 +82,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { super.readFromNBT(nbtTags); + //Main inventory NBTTagList tagList = nbtTags.getTagList("Items"); structure.inventory = new HashSet(); @@ -91,6 +92,24 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver structure.inventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); } + + //Visible inventory + if(nbtTags.hasKey("VisibleItems")) + { + 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)); + } + } + } } @Override @@ -98,6 +117,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { super.writeToNBT(nbtTags); + //Inventory NBTTagList tagList = new NBTTagList(); for(ItemStack itemStack : structure.inventory) @@ -111,6 +131,26 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } nbtTags.setTag("Items", tagList); + + //Visible inventory + if(!structure.wroteVisibleInventory) + { + 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; + } } public void update() @@ -246,9 +286,32 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } @Override - public ItemStack decrStackSize(int i, int j) + public ItemStack decrStackSize(int slotID, int amount) { - return null; + if(getStackInSlot(slotID) != null) + { + ItemStack tempStack; + + if(getStackInSlot(slotID).stackSize <= amount) + { + tempStack = getStackInSlot(slotID); + setInventorySlotContents(slotID, null); + return tempStack; + } + else { + tempStack = getStackInSlot(slotID).splitStack(amount); + + if(getStackInSlot(slotID).stackSize == 0) + { + setInventorySlotContents(slotID, null); + } + + return tempStack; + } + } + else { + return null; + } } @Override