From c20a7b969e765a80b89d6ec4eb32d1a795ba23f2 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 22 Sep 2015 23:48:27 +0200 Subject: [PATCH] Fixed tool table system bugging when multiple stacks of the same item are present --- .../actuallyadditions/blocks/BlockToolTable.java | 13 ++++++++++++- .../actuallyadditions/event/TooltipEvent.java | 5 +++-- .../inventory/ContainerToolTable.java | 11 ++++++++--- .../ellpeck/actuallyadditions/util/ItemUtil.java | 15 +++++++++++---- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/java/ellpeck/actuallyadditions/blocks/BlockToolTable.java b/src/main/java/ellpeck/actuallyadditions/blocks/BlockToolTable.java index 0beb8082c..6a0d4e5bd 100644 --- a/src/main/java/ellpeck/actuallyadditions/blocks/BlockToolTable.java +++ b/src/main/java/ellpeck/actuallyadditions/blocks/BlockToolTable.java @@ -14,6 +14,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import ellpeck.actuallyadditions.ActuallyAdditions; import ellpeck.actuallyadditions.inventory.GuiHandler; +import ellpeck.actuallyadditions.tile.TileEntityInventoryBase; import ellpeck.actuallyadditions.tile.TileEntityToolTable; import ellpeck.actuallyadditions.util.INameableItem; import ellpeck.actuallyadditions.util.ModUtil; @@ -96,7 +97,17 @@ public class BlockToolTable extends BlockContainerBase implements INameableItem{ @Override public void breakBlock(World world, int x, int y, int z, Block block, int par6){ - this.dropInventory(world, x, y, z); + if(!world.isRemote){ + TileEntity aTile = world.getTileEntity(x, y, z); + if(aTile instanceof TileEntityInventoryBase){ + TileEntityInventoryBase tile = (TileEntityInventoryBase)aTile; + if(tile.getSizeInventory() > 0){ + for(int i = 0; i < tile.getSizeInventory()-1; i++){ + this.dropSlotFromInventory(i, tile, world, x, y, z); + } + } + } + } super.breakBlock(world, x, y, z, block, par6); } diff --git a/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java b/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java index 3638063b9..10b124322 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java +++ b/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java @@ -64,8 +64,9 @@ public class TooltipEvent{ if(page.getItemStackForPage() != null && page.getItemStackForPage().isItemEqual(stack)){ int keyCode = KeyBinds.keybindOpenBooklet.getKeyCode(); if(!ConfigBoolValues.NEED_BOOKLET_FOR_KEYBIND_INFO.isEnabled() || Minecraft.getMinecraft().thePlayer.inventory.hasItem(InitItems.itemLexicon)){ - event.toolTip.add(EnumChatFormatting.GOLD+StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".keyToSeeRecipe", keyCode > 0 && keyCode < Keyboard.KEYBOARD_SIZE ? "'"+Keyboard.getKeyName(keyCode)+"'" : "[NONE]")); - + if(ConfigBoolValues.SHOW_NEED_BOOKLET_FOR_KEYBIND_INFO.isEnabled()){ + event.toolTip.add(EnumChatFormatting.GOLD+StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".keyToSeeRecipe", keyCode > 0 && keyCode < Keyboard.KEYBOARD_SIZE ? "'"+Keyboard.getKeyName(keyCode)+"'" : "[NONE]")); + } if(Keyboard.isKeyDown(KeyBinds.keybindOpenBooklet.getKeyCode())){ GuiBooklet book = new GuiBooklet(); Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F)); diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/ContainerToolTable.java b/src/main/java/ellpeck/actuallyadditions/inventory/ContainerToolTable.java index fc4370602..df9371bae 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/ContainerToolTable.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/ContainerToolTable.java @@ -52,9 +52,14 @@ public class ContainerToolTable extends Container{ if(this.inventory instanceof TileEntityToolTable){ TileEntityToolTable table = (TileEntityToolTable)this.inventory; ToolTableHandler.Recipe recipe = ToolTableHandler.getRecipeFromSlots(table.slots); - for(int i = 0; i < TileEntityToolTable.INPUT_SLOT_AMOUNT; i++){ - if(ItemUtil.contains(recipe.itemsNeeded, table.getStackInSlot(i))){ - table.decrStackSize(i, 1); + if(recipe != null){ + ItemStack[] stacks = recipe.itemsNeeded.clone(); + for(int i = 0; i < TileEntityToolTable.INPUT_SLOT_AMOUNT; i++){ + int place = ItemUtil.getPlaceAt(stacks, table.getStackInSlot(i)); + if(place != -1){ + table.decrStackSize(i, 1); + stacks[place] = null; + } } } diff --git a/src/main/java/ellpeck/actuallyadditions/util/ItemUtil.java b/src/main/java/ellpeck/actuallyadditions/util/ItemUtil.java index 9903aab8c..62b1fb021 100644 --- a/src/main/java/ellpeck/actuallyadditions/util/ItemUtil.java +++ b/src/main/java/ellpeck/actuallyadditions/util/ItemUtil.java @@ -60,11 +60,18 @@ public class ItemUtil{ * Returns true if array contains stack or if both contain null */ public static boolean contains(ItemStack[] array, ItemStack stack){ - for(ItemStack aStack : array){ - if((stack == null && aStack == null) || (stack != null && aStack != null && aStack.isItemEqual(stack))){ - return true; + return getPlaceAt(array, stack) != -1; + } + + /** + * Returns the place of stack in array, -1 if not present + */ + public static int getPlaceAt(ItemStack[] array, ItemStack stack){ + for(int i = 0; i < array.length; i++){ + if((stack == null && array[i] == null) || (stack != null && array[i] != null && array[i].isItemEqual(stack))){ + return i; } } - return false; + return -1; } }