From 09c445cf5c956c18e0e66393d159394ae7de4b98 Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Wed, 4 Mar 2015 13:25:33 -0500 Subject: [PATCH] Improve chisel container/slot code to prevent crashes/dupes Fixes #257 #236 --- .../chisel/inventory/ContainerChisel.java | 13 +++-- .../chisel/inventory/SlotChiselSelection.java | 48 ++++--------------- 2 files changed, 16 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/cricketcraft/chisel/inventory/ContainerChisel.java b/src/main/java/com/cricketcraft/chisel/inventory/ContainerChisel.java index f6962945..93cd72af 100644 --- a/src/main/java/com/cricketcraft/chisel/inventory/ContainerChisel.java +++ b/src/main/java/com/cricketcraft/chisel/inventory/ContainerChisel.java @@ -16,7 +16,7 @@ public class ContainerChisel extends Container { public final InventoryChiselSelection inventory; public InventoryPlayer playerInventory; - int currentIndex; + int chiselSlot; public ItemStack chisel; public boolean finished; public ICarvingRegistry carving; @@ -24,7 +24,7 @@ public class ContainerChisel extends Container { public ContainerChisel(InventoryPlayer inventoryplayer, InventoryChiselSelection inv) { inventory = inv; playerInventory = inventoryplayer; - currentIndex = playerInventory.currentItem; + chiselSlot = playerInventory.currentItem; inv.container = this; int top = 8, left = 62; @@ -64,7 +64,10 @@ public class ContainerChisel extends Container { @Override public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer) { // we need to subtract away all the other slots - if (par1 - inventory.getSizeInventory() - 27 == currentIndex) + int clickedSlot = par1 - inventory.getSizeInventory() - 27; + + // if the player has clicked on the chisel or is trying to use a number key to force an itemstack into the slot the chisel is in + if (clickedSlot == chiselSlot || (par3 == 2 && par2 == chiselSlot)) return null; return super.slotClick(par1, par2, par3, par4EntityPlayer); @@ -128,7 +131,7 @@ public class ContainerChisel extends Container { } public void onChiselSlotChanged() { - ItemStack stack = playerInventory.mainInventory[currentIndex]; + ItemStack stack = playerInventory.mainInventory[chiselSlot]; if (stack == null || !stack.isItemEqual(chisel)) finished = true; @@ -137,6 +140,6 @@ public class ContainerChisel extends Container { General.setChiselTarget(chisel, inventory.getStackInSpecialSlot()); - playerInventory.mainInventory[currentIndex] = chisel; + playerInventory.mainInventory[chiselSlot] = chisel; } } diff --git a/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java b/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java index 2170a7f3..eb7d4a57 100644 --- a/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java +++ b/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java @@ -13,6 +13,9 @@ import com.cricketcraft.chisel.utils.GeneralClient; public class SlotChiselSelection extends Slot { + private final ContainerChisel container; + private final InventoryChiselSelection selInventory; + public SlotChiselSelection(ContainerChisel container, InventoryChiselSelection inv, IInventory iinventory, int i, int j, int k) { super(iinventory, i, j, k); @@ -35,18 +38,13 @@ public class SlotChiselSelection extends Slot { @Override public void onPickupFromSlot(EntityPlayer player, ItemStack itemstack) { - ItemStack stack = player.inventory.getItemStack(); + ItemStack heldStack = player.inventory.getItemStack(); ItemStack crafted = selInventory.inventory[InventoryChiselSelection.normalSlots]; - if (stack == null) { - if (crafted != null && crafted.stackSize > 0) - crafted.stackSize--; - if (crafted.stackSize == 0) - crafted = null; - - selInventory.setInventorySlotContents(InventoryChiselSelection.normalSlots, crafted); + if (heldStack == null) { + selInventory.decrStackSize(InventoryChiselSelection.normalSlots, 1); } else { - putStack(new ItemStack(itemstack.getItem(), itemstack.stackSize, itemstack.getItemDamage())); + putStack(itemstack.copy()); player.inventory.setItemStack(null); @@ -58,7 +56,7 @@ public class SlotChiselSelection extends Slot { } selInventory.updateItems(); - + if (((IChiselItem) container.chisel.getItem()).onChisel(player.worldObj, container.chisel, General.getVariation(crafted))) { container.chisel.damageItem(1, player); if (container.chisel.stackSize <= 0) { @@ -70,35 +68,5 @@ public class SlotChiselSelection extends Slot { String sound = Carving.chisel.getVariationSound(crafted.getItem(), crafted.getItemDamage()); GeneralClient.playChiselSound(player.worldObj, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ), sound); } - - /* - * ItemStack stack=player.inventory.getItemStack(); - * - * putStack(new ItemStack(itemstack.itemID, itemstack.stackSize, - * itemstack.getItemDamage())); - * - * super.onPickupFromSlot(player,itemstack); - * - * player.inventory.setItemStack(null); - * - * if(selInventory.items[InventoryChiselSelection.normalSlots]==null) - * return; - * - * player.inventory.setItemStack(new ItemStack(itemstack.itemID, - * selInventory.items[InventoryChiselSelection.normalSlots].stackSize, - * itemstack.getItemDamage())); - * selInventory.setInventorySlotContents(InventoryChiselSelection - * .normalSlots,null); - * - * selInventory.updateItems(); - * - * String sound=Carving.chisel.getVariationSound(itemstack.itemID, - * itemstack.getItemDamage()); player.worldObj.playSoundAtEntity(player, - * sound, 0.3f + 0.7f * General.rand.nextFloat(), 0.6f + 0.4f * - * General.rand.nextFloat()); - */ } - - ContainerChisel container; - InventoryChiselSelection selInventory; }