From 75b1a9804d1a19016de2ee0ead98ccf23e8bf040 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Mon, 5 Aug 2013 01:29:39 -0400 Subject: [PATCH] Battery bugfixes --- src/resonantinduction/base/SetUtil.java | 7 +- .../battery/BatteryUpdateProtocol.java | 1 + .../battery/ContainerBattery.java | 22 ++++++ .../battery/SynchronizedBatteryData.java | 6 +- .../battery/TileEntityBattery.java | 76 ++++++++++++------- 5 files changed, 81 insertions(+), 31 deletions(-) diff --git a/src/resonantinduction/base/SetUtil.java b/src/resonantinduction/base/SetUtil.java index b9dce3c9..724cbac4 100644 --- a/src/resonantinduction/base/SetUtil.java +++ b/src/resonantinduction/base/SetUtil.java @@ -18,7 +18,7 @@ public class SetUtil Set toReturn = new HashSet(); List list = Arrays.asList(set.toArray()); - for(int i = list.size()-1; i >= 0; i--) + for(int i = list.size(); i >= 0; i--) { toReturn.add((V)list.get(i)); } @@ -189,7 +189,10 @@ public class SetUtil { for(V obj : set) { - return obj; + if(obj != null) + { + return obj; + } } return null; diff --git a/src/resonantinduction/battery/BatteryUpdateProtocol.java b/src/resonantinduction/battery/BatteryUpdateProtocol.java index 33697016..80c53f41 100644 --- a/src/resonantinduction/battery/BatteryUpdateProtocol.java +++ b/src/resonantinduction/battery/BatteryUpdateProtocol.java @@ -235,6 +235,7 @@ public class BatteryUpdateProtocol if(!didVisibleInventory) { tile.structure.visibleInventory = oldStructure.visibleInventory; + didVisibleInventory = true; } } } diff --git a/src/resonantinduction/battery/ContainerBattery.java b/src/resonantinduction/battery/ContainerBattery.java index 1b9df965..1db9612b 100644 --- a/src/resonantinduction/battery/ContainerBattery.java +++ b/src/resonantinduction/battery/ContainerBattery.java @@ -3,7 +3,9 @@ package resonantinduction.battery; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; import resonantinduction.battery.BatteryManager.SlotBattery; import resonantinduction.battery.BatteryManager.SlotOut; @@ -37,6 +39,26 @@ public class ContainerBattery extends Container tileEntity.openChest(); tileEntity.playersUsing.add(inventory.player); } + + @Override + public ItemStack slotClick(int slotID, int par2, int par3, EntityPlayer par4EntityPlayer) + { + ItemStack stack = super.slotClick(slotID, par2, par3, par4EntityPlayer); + + if(slotID == 1) + { + ItemStack itemstack = ((Slot)inventorySlots.get(slotID)).getStack(); + ItemStack itemstack1 = itemstack == null ? null : itemstack.copy(); + inventoryItemStacks.set(slotID, itemstack1); + + for (int j = 0; j < this.crafters.size(); ++j) + { + ((ICrafting)this.crafters.get(j)).sendSlotContents(this, slotID, itemstack1); + } + } + + return stack; + } @Override public void onContainerClosed(EntityPlayer entityplayer) diff --git a/src/resonantinduction/battery/SynchronizedBatteryData.java b/src/resonantinduction/battery/SynchronizedBatteryData.java index 9a613f9d..9e4d8f4a 100644 --- a/src/resonantinduction/battery/SynchronizedBatteryData.java +++ b/src/resonantinduction/battery/SynchronizedBatteryData.java @@ -27,11 +27,13 @@ public class SynchronizedBatteryData public int height; + public ItemStack tempStack; + public boolean isMultiblock; public boolean didTick; - public boolean wroteVisibleInventory; + public boolean wroteInventory; public int getVolume() { @@ -49,7 +51,7 @@ public class SynchronizedBatteryData ItemStack[] toSort = new ItemStack[array.length]; - for(int i = 0; i < array.length-1; i++) + for(int i = 0; i < array.length; i++) { toSort[i] = (ItemStack)array[i]; } diff --git a/src/resonantinduction/battery/TileEntityBattery.java b/src/resonantinduction/battery/TileEntityBattery.java index b4fb2e12..a8f2a907 100644 --- a/src/resonantinduction/battery/TileEntityBattery.java +++ b/src/resonantinduction/battery/TileEntityBattery.java @@ -89,14 +89,17 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver super.readFromNBT(nbtTags); //Main inventory - NBTTagList tagList = nbtTags.getTagList("Items"); - structure.inventory = new HashSet(); - - for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) + if(nbtTags.hasKey("Items")) { - NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); - - structure.inventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); + NBTTagList tagList = nbtTags.getTagList("Items"); + structure.inventory = new HashSet(); + + for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) + { + NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); + + structure.inventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); + } } //Visible inventory @@ -123,24 +126,24 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { super.writeToNBT(nbtTags); - //Inventory - NBTTagList tagList = new NBTTagList(); - - for(ItemStack itemStack : structure.inventory) - { - if(itemStack != null) - { - NBTTagCompound tagCompound = new NBTTagCompound(); - itemStack.writeToNBT(tagCompound); - tagList.appendTag(tagCompound); - } - } - - nbtTags.setTag("Items", tagList); - - //Visible inventory - if(!structure.wroteVisibleInventory) + if(!structure.wroteInventory) { + //Inventory + NBTTagList tagList = new NBTTagList(); + + for(ItemStack itemStack : structure.inventory) + { + if(itemStack != null) + { + NBTTagCompound tagCompound = new NBTTagCompound(); + itemStack.writeToNBT(tagCompound); + tagList.appendTag(tagCompound); + } + } + + nbtTags.setTag("Items", tagList); + + //Visible inventory NBTTagList tagList1 = new NBTTagList(); for(int slotCount = 0; slotCount < structure.visibleInventory.length; slotCount++) @@ -155,7 +158,8 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } nbtTags.setTag("VisibleItems", tagList1); - structure.wroteVisibleInventory = true; + + structure.wroteInventory = true; } } @@ -286,7 +290,13 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } else if(i == 1) { - return SetUtil.getTop(structure.inventory); + if(!worldObj.isRemote) + { + return SetUtil.getTop(structure.inventory); + } + else { + return structure.tempStack; + } } else { return structure.visibleInventory[i-1]; @@ -339,7 +349,19 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { if(itemstack == null) { - structure.inventory.remove(SetUtil.getTop(structure.inventory)); + if(!worldObj.isRemote) + { + structure.inventory.remove(SetUtil.getTop(structure.inventory)); + } + else { + structure.tempStack = null; + } + } + else { + if(worldObj.isRemote) + { + structure.tempStack = itemstack; + } } } else {