From 0e6d635e8dd42e515297fa8ec5de1e35a1b53ebe Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sat, 11 Apr 2015 11:53:27 +0100 Subject: [PATCH] Fixed shift clicking crash, now saves the inventory of the chest --- .../container/ContainerQuantumChest.java | 9 +- .../container/ContainerQuantumTank.java | 3 +- .../container/ContainerThermalGenerator.java | 3 +- .../client/container/TechRebornContainer.java | 94 +++++++++++++++++++ .../client/gui/GuiQuantumChest.java | 2 - .../techreborn/tiles/TileQuantumChest.java | 33 ++++++- .../techreborn/tiles/TileQuantumTank.java | 2 +- 7 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 src/main/java/techreborn/client/container/TechRebornContainer.java diff --git a/src/main/java/techreborn/client/container/ContainerQuantumChest.java b/src/main/java/techreborn/client/container/ContainerQuantumChest.java index a4e8dd019..2bf608be4 100644 --- a/src/main/java/techreborn/client/container/ContainerQuantumChest.java +++ b/src/main/java/techreborn/client/container/ContainerQuantumChest.java @@ -1,14 +1,12 @@ package techreborn.client.container; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; -import net.minecraft.item.ItemStack; import techreborn.client.SlotFake; import techreborn.client.SlotOutput; import techreborn.tiles.TileQuantumChest; -public class ContainerQuantumChest extends Container { +public class ContainerQuantumChest extends TechRebornContainer { public TileQuantumChest tileQuantumChest; public EntityPlayer player; @@ -39,9 +37,4 @@ public class ContainerQuantumChest extends Container { return true; } - //TODO enable shift-clicking - public ItemStack transferStackInSlot(EntityPlayer player, int slot) { - ItemStack stack = null; - return stack; - } } diff --git a/src/main/java/techreborn/client/container/ContainerQuantumTank.java b/src/main/java/techreborn/client/container/ContainerQuantumTank.java index 2f0721471..46328ef13 100644 --- a/src/main/java/techreborn/client/container/ContainerQuantumTank.java +++ b/src/main/java/techreborn/client/container/ContainerQuantumTank.java @@ -1,13 +1,12 @@ package techreborn.client.container; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import techreborn.client.SlotFake; import techreborn.client.SlotOutput; import techreborn.tiles.TileQuantumTank; -public class ContainerQuantumTank extends Container { +public class ContainerQuantumTank extends TechRebornContainer { public TileQuantumTank tileQuantumTank; public EntityPlayer player; diff --git a/src/main/java/techreborn/client/container/ContainerThermalGenerator.java b/src/main/java/techreborn/client/container/ContainerThermalGenerator.java index f9a876e54..8f11ef29f 100644 --- a/src/main/java/techreborn/client/container/ContainerThermalGenerator.java +++ b/src/main/java/techreborn/client/container/ContainerThermalGenerator.java @@ -1,13 +1,12 @@ package techreborn.client.container; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import techreborn.client.SlotFake; import techreborn.client.SlotOutput; import techreborn.tiles.TileThermalGenerator; -public class ContainerThermalGenerator extends Container { +public class ContainerThermalGenerator extends TechRebornContainer { public TileThermalGenerator tileThermalGenerator; public EntityPlayer player; diff --git a/src/main/java/techreborn/client/container/TechRebornContainer.java b/src/main/java/techreborn/client/container/TechRebornContainer.java new file mode 100644 index 000000000..8d6bc6e50 --- /dev/null +++ b/src/main/java/techreborn/client/container/TechRebornContainer.java @@ -0,0 +1,94 @@ +package techreborn.client.container; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import techreborn.client.SlotFake; +import techreborn.util.FluidUtils; + + +public abstract class TechRebornContainer extends Container { + + public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { + ItemStack originalStack = null; + Slot slot = (Slot) inventorySlots.get(slotIndex); + int numSlots = inventorySlots.size(); + if (slot != null && slot.getHasStack()) { + ItemStack stackInSlot = slot.getStack(); + originalStack = stackInSlot.copy(); + if (slotIndex >= numSlots - 9 * 4 && tryShiftItem(stackInSlot, numSlots)) { + // NOOP + } else if (slotIndex >= numSlots - 9 * 4 && slotIndex < numSlots - 9) { + if (!shiftItemStack(stackInSlot, numSlots - 9, numSlots)) + return null; + } else if (slotIndex >= numSlots - 9 && slotIndex < numSlots) { + if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots - 9)) + return null; + } else if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots)) + return null; + slot.onSlotChange(stackInSlot, originalStack); + if (stackInSlot.stackSize <= 0) + slot.putStack(null); + else + slot.onSlotChanged(); + if (stackInSlot.stackSize == originalStack.stackSize) + return null; + slot.onPickupFromSlot(player, stackInSlot); + } + return originalStack; + } + + protected boolean shiftItemStack(ItemStack stackToShift, int start, int end) { + boolean changed = false; + if (stackToShift.isStackable()) + for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) { + Slot slot = (Slot) inventorySlots.get(slotIndex); + ItemStack stackInSlot = slot.getStack(); + if (stackInSlot != null && FluidUtils.isItemEqual(stackInSlot, stackToShift, true, true)) { + int resultingStackSize = stackInSlot.stackSize + stackToShift.stackSize; + int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit()); + if (resultingStackSize <= max) { + stackToShift.stackSize = 0; + stackInSlot.stackSize = resultingStackSize; + slot.onSlotChanged(); + changed = true; + } else if (stackInSlot.stackSize < max) { + stackToShift.stackSize -= max - stackInSlot.stackSize; + stackInSlot.stackSize = max; + slot.onSlotChanged(); + changed = true; + } + } + } + if (stackToShift.stackSize > 0) + for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) { + Slot slot = (Slot) inventorySlots.get(slotIndex); + ItemStack stackInSlot = slot.getStack(); + if (stackInSlot == null) { + int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit()); + stackInSlot = stackToShift.copy(); + stackInSlot.stackSize = Math.min(stackToShift.stackSize, max); + stackToShift.stackSize -= stackInSlot.stackSize; + slot.putStack(stackInSlot); + slot.onSlotChanged(); + changed = true; + } + } + return changed; + } + + private boolean tryShiftItem(ItemStack stackToShift, int numSlots) { + for (int machineIndex = 0; machineIndex < numSlots - 9 * 4; machineIndex++) { + Slot slot = (Slot) inventorySlots.get(machineIndex); + if (slot instanceof SlotFake) { + continue; + } + if (!slot.isItemValid(stackToShift)) + continue; + if (shiftItemStack(stackToShift, machineIndex, machineIndex + 1)) + return true; + } + return false; + } +} diff --git a/src/main/java/techreborn/client/gui/GuiQuantumChest.java b/src/main/java/techreborn/client/gui/GuiQuantumChest.java index 2d709edd5..70baf9d1c 100644 --- a/src/main/java/techreborn/client/gui/GuiQuantumChest.java +++ b/src/main/java/techreborn/client/gui/GuiQuantumChest.java @@ -6,9 +6,7 @@ import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ResourceLocation; import techreborn.client.container.ContainerQuantumChest; -import techreborn.client.container.ContainerQuantumTank; import techreborn.tiles.TileQuantumChest; -import techreborn.tiles.TileQuantumTank; public class GuiQuantumChest extends GuiContainer { diff --git a/src/main/java/techreborn/tiles/TileQuantumChest.java b/src/main/java/techreborn/tiles/TileQuantumChest.java index 291411ff0..fee8d4c87 100644 --- a/src/main/java/techreborn/tiles/TileQuantumChest.java +++ b/src/main/java/techreborn/tiles/TileQuantumChest.java @@ -1,10 +1,10 @@ package techreborn.tiles; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; @@ -73,6 +73,37 @@ public class TileQuantumChest extends TileEntity implements IInventory { readFromNBT(packet.func_148857_g()); } + @Override + public void readFromNBT(NBTTagCompound tagCompound) { + super.readFromNBT(tagCompound); + inventory.readFromNBT(tagCompound); + + storedItem = null; + + if (tagCompound.hasKey("storedStack")) + { + storedItem = ItemStack. + loadItemStackFromNBT((NBTTagCompound)tagCompound.getTag("storedStack")); + } + + if(storedItem != null){ + storedItem.stackSize = tagCompound.getInteger("storedQuantity"); + } + } + + @Override + public void writeToNBT(NBTTagCompound tagCompound) { + super.writeToNBT(tagCompound); + inventory.writeToNBT(tagCompound); + if (storedItem != null) + { + tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound())); + tagCompound.setInteger("storedQuantity", storedItem.stackSize); + } + else + tagCompound.setInteger("storedQuantity", 0); + } + @Override public int getSizeInventory() { return inventory.getSizeInventory(); diff --git a/src/main/java/techreborn/tiles/TileQuantumTank.java b/src/main/java/techreborn/tiles/TileQuantumTank.java index 2920b0cc3..8f5c7d984 100644 --- a/src/main/java/techreborn/tiles/TileQuantumTank.java +++ b/src/main/java/techreborn/tiles/TileQuantumTank.java @@ -19,7 +19,7 @@ import techreborn.util.Tank; public class TileQuantumTank extends TileEntity implements IFluidHandler, IInventory { - public Tank tank = new Tank("TileQuantumTank", 2000000000, this); + public Tank tank = new Tank("TileQuantumTank", Integer.MAX_VALUE, this); public Inventory inventory = new Inventory(3, "TileQuantumTank", 64); @Override