From 340ba2dc7c9696cc6b88f488c1c7dc2f18fc3d7b Mon Sep 17 00:00:00 2001 From: mezz Date: Sun, 9 Aug 2015 21:02:26 -0700 Subject: [PATCH] Fix #680 and Fix #750 worktable handling of tools and ItemCraftedEvent Cleanup some derped refactoring (cherry picked from commit 9f1b1cc4746d7ce883bebe117ff159c37d91eb98) --- application.properties | 2 +- .../forestry/core/gui/slots/SlotCrafter.java | 26 +- .../core/interfaces/ICrafterWorktable.java | 14 ++ .../forestry/core/inventory/InvTools.java | 40 +++- .../java/forestry/core/utils/StackUtils.java | 117 ++------- .../factory/gadgets/MachineCarpenter.java | 2 +- .../factory/gadgets/MachineFabricator.java | 2 +- .../factory/gadgets/MachineSqueezer.java | 2 +- .../factory/gadgets/TileWorktable.java | 56 +++-- .../factory/recipes/RecipeMemory.java | 223 +++++++++--------- 10 files changed, 228 insertions(+), 256 deletions(-) create mode 100644 src/main/java/forestry/core/interfaces/ICrafterWorktable.java mode change 100755 => 100644 src/main/java/forestry/core/inventory/InvTools.java mode change 100755 => 100644 src/main/java/forestry/factory/gadgets/MachineSqueezer.java diff --git a/application.properties b/application.properties index 12a9ace64..d2e26734f 100644 --- a/application.properties +++ b/application.properties @@ -2,4 +2,4 @@ mcversion=1.7.10 forgeversion=10.13.3.1370-1.7.10 version_major=3 version_minor=6 -version_patch=5 +version_patch=6 diff --git a/src/main/java/forestry/core/gui/slots/SlotCrafter.java b/src/main/java/forestry/core/gui/slots/SlotCrafter.java index dbab9854a..7b7693fa6 100755 --- a/src/main/java/forestry/core/gui/slots/SlotCrafter.java +++ b/src/main/java/forestry/core/gui/slots/SlotCrafter.java @@ -17,22 +17,17 @@ import net.minecraft.item.ItemStack; import cpw.mods.fml.common.FMLCommonHandler; -import forestry.core.interfaces.ICrafter; +import forestry.core.interfaces.ICrafterWorktable; public class SlotCrafter extends SlotCrafting { - private final ICrafter crafter; private final IInventory craftMatrix; + private final ICrafterWorktable crafter; - public SlotCrafter(EntityPlayer player, IInventory craftMatrix, ICrafter crafter, int slot, int xPos, int yPos) { + public SlotCrafter(EntityPlayer player, IInventory craftMatrix, ICrafterWorktable crafter, int slot, int xPos, int yPos) { super(player, craftMatrix, craftMatrix, slot, xPos, yPos); - this.crafter = crafter; this.craftMatrix = craftMatrix; - } - - @Override - public boolean isItemValid(ItemStack par1ItemStack) { - return false; + this.crafter = crafter; } @Override @@ -51,18 +46,23 @@ public class SlotCrafter extends SlotCrafting { @Override public ItemStack getStack() { - return this.crafter.getResult(); + return crafter.getResult(); } @Override public boolean getHasStack() { - return this.getStack() != null && crafter.canTakeStack(getSlotIndex()); + return getStack() != null && crafter.canTakeStack(getSlotIndex()); } @Override public void onPickupFromSlot(EntityPlayer player, ItemStack itemStack) { + if (!crafter.onCraftingStart(player)) { + return; + } + FMLCommonHandler.instance().firePlayerCraftingEvent(player, itemStack, craftMatrix); - this.onCrafting(itemStack, itemStack.stackSize); // handles crafting achievements, maps, and statistics - crafter.takenFromSlot(getSlotIndex(), player); + this.onCrafting(itemStack); // handles crafting achievements, maps, and statistics + + crafter.onCraftingComplete(player); } } diff --git a/src/main/java/forestry/core/interfaces/ICrafterWorktable.java b/src/main/java/forestry/core/interfaces/ICrafterWorktable.java new file mode 100644 index 000000000..61d4bcfa9 --- /dev/null +++ b/src/main/java/forestry/core/interfaces/ICrafterWorktable.java @@ -0,0 +1,14 @@ +package forestry.core.interfaces; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +public interface ICrafterWorktable { + ItemStack getResult(); + + boolean canTakeStack(int slotIndex); + + boolean onCraftingStart(EntityPlayer player); + + void onCraftingComplete(EntityPlayer player); +} diff --git a/src/main/java/forestry/core/inventory/InvTools.java b/src/main/java/forestry/core/inventory/InvTools.java old mode 100755 new mode 100644 index 795888845..45fb71a53 --- a/src/main/java/forestry/core/inventory/InvTools.java +++ b/src/main/java/forestry/core/inventory/InvTools.java @@ -927,33 +927,46 @@ public abstract class InvTools { * If the inventory doesn't have all the required items, returns false without removing anything. * If stowContainer is true, items with containers will have their container stowed. */ - public static boolean removeSets(IInventory inventory, int count, ItemStack[] set, int firstSlotIndex, int slotCount, EntityPlayer player, boolean stowContainer, boolean oreDictionary, boolean craftingTools) { + public static boolean removeSets(IInventory inventory, int count, ItemStack[] set, EntityPlayer player, boolean stowContainer, boolean oreDictionary) { + return removeSets(inventory, count, set, 0, inventory.getSizeInventory(), player, stowContainer, oreDictionary, false) != null; + } - ItemStack[] condensedSet = StackUtils.condenseStacks(set, -1, false); + public static boolean removeSets(IInventory inventory, int count, ItemStack[] set, int firstSlotIndex, int slotCount, EntityPlayer player, boolean stowContainer, boolean oreDictionary) { + return removeSets(inventory, count, set, firstSlotIndex, slotCount, player, stowContainer, oreDictionary, false) != null; + } + public static ItemStack[] removeSets(IInventory inventory, int count, ItemStack[] set, int firstSlotIndex, int slotCount, EntityPlayer player, boolean stowContainer, boolean oreDictionary, boolean craftingTools) { + + ItemStack[] removed = new ItemStack[set.length]; ItemStack[] stock = getStacks(inventory, firstSlotIndex, slotCount); - if (StackUtils.containsSets(condensedSet, stock, oreDictionary, craftingTools) < count) { - return false; + + if (StackUtils.containsSets(set, stock, oreDictionary, craftingTools) < count) { + return null; } - for (ItemStack stackToRemove : condensedSet) { + for (int i = 0; i < set.length; i++) { + if (set[i] == null) { + continue; + } + ItemStack stackToRemove = set[i].copy(); stackToRemove.stackSize *= count; // try to remove the exact stack first - removeStack(inventory, stackToRemove, firstSlotIndex, slotCount, player, stowContainer, false, false); - - // remove crafting equivalents next - if (stackToRemove.stackSize > 0) { - removeStack(inventory, stackToRemove, firstSlotIndex, slotCount, player, stowContainer, oreDictionary, craftingTools); + ItemStack removedStack = removeStack(inventory, stackToRemove, firstSlotIndex, slotCount, player, stowContainer, false, false); + if (removedStack == null) { + // remove crafting equivalents next + removedStack = removeStack(inventory, stackToRemove, firstSlotIndex, slotCount, player, stowContainer, oreDictionary, craftingTools); } + + removed[i] = removedStack; } - return true; + return removed; } /** * Private Helper for removeSetsFromInventory. Assumes removal is possible. */ - private static void removeStack(IInventory inventory, ItemStack stackToRemove, int firstSlotIndex, int slotCount, EntityPlayer player, boolean stowContainer, boolean oreDictionary, boolean craftingTools) { + private static ItemStack removeStack(IInventory inventory, ItemStack stackToRemove, int firstSlotIndex, int slotCount, EntityPlayer player, boolean stowContainer, boolean oreDictionary, boolean craftingTools) { for (int j = firstSlotIndex; j < firstSlotIndex + slotCount; j++) { ItemStack stackInSlot = inventory.getStackInSlot(j); if (stackInSlot == null) { @@ -972,9 +985,10 @@ public abstract class InvTools { } if (stackToRemove.stackSize == 0) { - return; + return removed; } } + return null; } /* CONTAINS */ diff --git a/src/main/java/forestry/core/utils/StackUtils.java b/src/main/java/forestry/core/utils/StackUtils.java index 6892a2fc3..e0f8c823f 100755 --- a/src/main/java/forestry/core/utils/StackUtils.java +++ b/src/main/java/forestry/core/utils/StackUtils.java @@ -116,54 +116,6 @@ public class StackUtils { } return added; - - } - - public static int addToInventory(ItemStack itemstack, IInventory inventory, boolean doAdd, int slot1, int count) { - - int added = 0; - - for (int i = slot1; i < slot1 + count; i++) { - ItemStack inventoryStack = inventory.getStackInSlot(i); - - // Grab those free slots - if (inventoryStack == null) { - if (doAdd) { - inventory.setInventorySlotContents(i, itemstack.copy()); - } - return itemstack.stackSize; - } - - // Already full - if (inventoryStack.stackSize >= inventoryStack.getMaxStackSize()) { - continue; - } - - // Not same type - if (!inventoryStack.isItemEqual(itemstack)) { - continue; - } - - int space = inventoryStack.getMaxStackSize() - inventoryStack.stackSize; - - // Enough space to add all - if (space > itemstack.stackSize - added) { - if (doAdd) { - inventoryStack.stackSize += itemstack.stackSize; - } - return itemstack.stackSize; - // Only part can be added - } else { - if (doAdd) { - inventoryStack.stackSize = inventoryStack.getMaxStackSize(); - } - added += space; - } - - } - - return added; - } /** @@ -189,18 +141,6 @@ public class StackUtils { temp.stackSize = 0; } - public static boolean freeSpaceInStack(ItemStack stack, int maxSize) { - if (stack == null) { - return true; - } - - if (stack.stackSize >= maxSize) { - return false; - } - - return true; - } - /** * Creates a split stack of the specified amount, preserving NBT data, * without decreasing the source stack. @@ -215,14 +155,7 @@ public class StackUtils { } public static ItemStack[] condenseStacks(ItemStack[] stacks) { - return condenseStacks(stacks, -1, false); - } - - /** - * @param maxCountedPerStack The maximum stacksize counted in a single stack. -1 for unlimited. - */ - public static ItemStack[] condenseStacks(ItemStack[] stacks, int maxCountedPerStack, boolean craftingEquivalency) { - ArrayList condensed = new ArrayList(); + List condensed = new ArrayList(); for (ItemStack stack : stacks) { if (stack == null) { @@ -234,17 +167,14 @@ public class StackUtils { boolean matched = false; for (ItemStack cached : condensed) { - if ((cached.isItemEqual(stack) && ItemStack.areItemStackTagsEqual(cached, stack)) || (craftingEquivalency && isCraftingEquivalent(cached, stack, true, false))) { - cached.stackSize += maxCountedPerStack > 0 && stack.stackSize > maxCountedPerStack ? maxCountedPerStack : stack.stackSize; + if ((cached.isItemEqual(stack) && ItemStack.areItemStackTagsEqual(cached, stack))) { + cached.stackSize += stack.stackSize; matched = true; } } if (!matched) { ItemStack cached = stack.copy(); - if (maxCountedPerStack > 0) { - cached.stackSize = maxCountedPerStack; - } condensed.add(cached); } @@ -273,34 +203,31 @@ public class StackUtils { * Counts how many full sets are contained in the passed stock */ public static int containsSets(ItemStack[] set, ItemStack[] stock, boolean oreDictionary, boolean craftingTools) { - int count = 0; + int totalSets = 0; - ItemStack[] condensedRequired = StackUtils.condenseStacks(set, -1, oreDictionary); - ItemStack[] condensedOffered = StackUtils.condenseStacks(stock, -1, oreDictionary); + ItemStack[] condensedRequired = StackUtils.condenseStacks(set); + ItemStack[] condensedOffered = StackUtils.condenseStacks(stock); for (ItemStack req : condensedRequired) { - boolean matched = false; + int reqCount = 0; for (ItemStack offer : condensedOffered) { - if (isCraftingEquivalent(req, offer, oreDictionary, craftingTools)) { - int stackCount = (int) Math.floor(offer.stackSize / req.stackSize); - matched |= (stackCount > 0); - - if (count == 0) { - count = stackCount; - } else if (count > stackCount) { - count = stackCount; - } + reqCount = Math.max(reqCount, stackCount); } } - if (!matched) { + + if (reqCount == 0) { return 0; + } else if (totalSets == 0) { + totalSets = reqCount; + } else if (totalSets > reqCount) { + totalSets = reqCount; } } - return count; + return totalSets; } /** @@ -439,20 +366,6 @@ public class StackUtils { return created; } - public static ItemStack consumeItem(ItemStack stack) { - if (stack.stackSize == 1) { - if (stack.getItem().hasContainerItem(stack)) { - return stack.getItem().getContainerItem(stack); - } else { - return null; - } - } else { - stack.splitStack(1); - - return stack; - } - } - public static Block getBlock(ItemStack stack) { Item item = stack.getItem(); diff --git a/src/main/java/forestry/factory/gadgets/MachineCarpenter.java b/src/main/java/forestry/factory/gadgets/MachineCarpenter.java index 0987ef083..88c3d5166 100644 --- a/src/main/java/forestry/factory/gadgets/MachineCarpenter.java +++ b/src/main/java/forestry/factory/gadgets/MachineCarpenter.java @@ -297,7 +297,7 @@ public class MachineCarpenter extends TilePowered implements ISidedInventory, IL private boolean removeSets(int count, ItemStack[] set) { EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwner()); - return InvTools.removeSets(getInternalInventory(), count, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true, true); + return InvTools.removeSets(getInternalInventory(), count, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true); } private boolean tryAddPending() { diff --git a/src/main/java/forestry/factory/gadgets/MachineFabricator.java b/src/main/java/forestry/factory/gadgets/MachineFabricator.java index ec636a69d..d8919fdb9 100755 --- a/src/main/java/forestry/factory/gadgets/MachineFabricator.java +++ b/src/main/java/forestry/factory/gadgets/MachineFabricator.java @@ -439,7 +439,7 @@ public class MachineFabricator extends TilePowered implements ICrafter, ILiquidT private boolean removeFromInventory(ItemStack[] set, EntityPlayer player, boolean doRemove) { IInventoryAdapter inventory = getInternalInventory(); if (doRemove) { - return InvTools.removeSets(inventory, 1, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true, true); + return InvTools.removeSets(inventory, 1, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true); } else { ItemStack[] stock = InvTools.getStacks(inventory, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT); return StackUtils.containsSets(set, stock) >= 1; diff --git a/src/main/java/forestry/factory/gadgets/MachineSqueezer.java b/src/main/java/forestry/factory/gadgets/MachineSqueezer.java old mode 100755 new mode 100644 index 8afa38983..573a468b3 --- a/src/main/java/forestry/factory/gadgets/MachineSqueezer.java +++ b/src/main/java/forestry/factory/gadgets/MachineSqueezer.java @@ -291,7 +291,7 @@ public class MachineSqueezer extends TilePowered implements ISidedInventory, ILi private boolean removeResources(ItemStack[] stacks) { EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwner()); - return InvTools.removeSets(getInternalInventory(), 1, stacks, SLOT_RESOURCE_1, SLOTS_RESOURCE_COUNT, player, false, true, true); + return InvTools.removeSets(getInternalInventory(), 1, stacks, SLOT_RESOURCE_1, SLOTS_RESOURCE_COUNT, player, false, true); } @Override diff --git a/src/main/java/forestry/factory/gadgets/TileWorktable.java b/src/main/java/forestry/factory/gadgets/TileWorktable.java index 242097fd8..a6fdb240f 100755 --- a/src/main/java/forestry/factory/gadgets/TileWorktable.java +++ b/src/main/java/forestry/factory/gadgets/TileWorktable.java @@ -19,12 +19,15 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; + import forestry.api.core.ForestryAPI; import forestry.core.gadgets.TileBase; -import forestry.core.interfaces.ICrafter; +import forestry.core.interfaces.ICrafterWorktable; import forestry.core.inventory.InvTools; -import forestry.core.inventory.InventoryAdapter; import forestry.core.inventory.TileInventoryAdapter; +import forestry.core.inventory.wrappers.InventoryMapper; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.GuiId; @@ -33,7 +36,7 @@ import forestry.core.utils.GuiUtil; import forestry.core.utils.RecipeUtil; import forestry.factory.recipes.RecipeMemory; -public class TileWorktable extends TileBase implements ICrafter { +public class TileWorktable extends TileBase implements ICrafterWorktable { /* CONSTANTS */ public final static int SLOT_CRAFTING_1 = 0; @@ -161,11 +164,6 @@ public class TileWorktable extends TileBase implements ICrafter { return RecipeUtil.canCraftRecipe(worldObj, recipeItems, recipeOutput, inventory); } - private boolean removeResources(EntityPlayer player) { - ItemStack[] set = InvTools.getStacks(craftingInventory, SLOT_CRAFTING_1, SLOT_CRAFTING_COUNT); - return InvTools.removeSets(getInternalInventory(), 1, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true, true); - } - @Override public boolean canTakeStack(int slotIndex) { if (slotIndex == SLOT_CRAFTING_RESULT) { @@ -175,17 +173,47 @@ public class TileWorktable extends TileBase implements ICrafter { } @Override - public ItemStack takenFromSlot(int slotIndex, EntityPlayer player) { - if (!removeResources(player)) { - return null; + public boolean onCraftingStart(EntityPlayer player) { + ItemStack[] set = InvTools.getStacks(currentRecipe.getMatrix(), SLOT_CRAFTING_1, SLOT_CRAFTING_COUNT); + ItemStack[] removed = InvTools.removeSets(this, 1, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, false, true, true); + + for (int i = 0; i < removed.length; i++) { + craftingInventory.setInventorySlotContents(i, removed[i]); } + return removed != null; + } + + @Override + public void onCraftingComplete(EntityPlayer player) { + IInventory craftingInventory = getCraftingInventory(); + for (int i = 0; i < craftingInventory.getSizeInventory(); ++i) { + ItemStack itemStack = craftingInventory.getStackInSlot(i); + if (itemStack == null) { + continue; + } + + if (!itemStack.getItem().hasContainerItem(itemStack)) { + continue; + } + + ItemStack container = itemStack.getItem().getContainerItem(itemStack); + + if (container != null && container.isItemStackDamageable() && container.getItemDamage() > container.getMaxDamage()) { + MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, container)); + continue; + } + + if (!InvTools.tryAddStack(this, container, true)) { + player.dropPlayerItemWithRandomChoice(container, false); + } + } + if (Proxies.common.isSimulating(worldObj)) { memorized.memorizeRecipe(worldObj, currentRecipe, currentCrafting); } updateCraftResult(); - return currentRecipe.getRecipeOutput(worldObj).copy(); } @Override @@ -203,8 +231,8 @@ public class TileWorktable extends TileBase implements ICrafter { /** * @return Inaccessible crafting inventory for the craft grid. */ - public InventoryAdapter getCraftingInventory() { - return craftingInventory; + public IInventory getCraftingInventory() { + return new InventoryMapper(craftingInventory, SLOT_CRAFTING_1, SLOT_CRAFTING_COUNT); } private static class WorktableInventoryAdapter extends TileInventoryAdapter { diff --git a/src/main/java/forestry/factory/recipes/RecipeMemory.java b/src/main/java/forestry/factory/recipes/RecipeMemory.java index 83d7be43f..46a73ae13 100644 --- a/src/main/java/forestry/factory/recipes/RecipeMemory.java +++ b/src/main/java/forestry/factory/recipes/RecipeMemory.java @@ -36,115 +36,7 @@ import forestry.core.utils.PlainInventory; public class RecipeMemory implements INBTTagable, IStreamable { - public static final class Recipe implements INBTTagable, IStreamable { - - private InventoryAdapter matrix; - private long lastUsed; - private boolean locked; - private ItemStack cachedRecipeOutput; - - public Recipe() { - } - - public Recipe(InventoryCrafting crafting) { - this.matrix = new InventoryAdapter(new PlainInventory(crafting)); - } - - public Recipe(NBTTagCompound nbttagcompound) { - readFromNBT(nbttagcompound); - } - - public void updateLastUse(long lastUsed) { - this.lastUsed = lastUsed; - } - - public void toggleLock() { - locked = !locked; - } - - public InventoryAdapter getMatrix() { - return matrix; - } - - public ItemStack getRecipeOutput(World world) { - if (cachedRecipeOutput == null) { - InventoryCrafting crafting = new InventoryCrafting(DUMMY_CONTAINER, 3, 3); - for (int i = 0; i < crafting.getSizeInventory(); i++) { - crafting.setInventorySlotContents(i, matrix.getStackInSlot(i)); - } - - cachedRecipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, world); - } - return cachedRecipeOutput; - } - - public long getLastUsed() { - return this.lastUsed; - } - - public boolean isLocked() { - return this.locked; - } - - public boolean hasSameOutput(InventoryCrafting crafting, World world) { - ItemStack recipeOutput = getRecipeOutput(world); - if (recipeOutput == null) { - return false; - } - - ItemStack matchingRecipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, world); - return ItemStack.areItemStacksEqual(recipeOutput, matchingRecipeOutput); - } - - @Override - public final void readFromNBT(NBTTagCompound nbttagcompound) { - matrix = new InventoryAdapter(new InventoryCrafting(DUMMY_CONTAINER, 3, 3)); - matrix.readFromNBT(nbttagcompound); - sanitizeMatrix(); - lastUsed = nbttagcompound.getLong("LastUsed"); - locked = nbttagcompound.getBoolean("Locked"); - } - - @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - sanitizeMatrix(); - matrix.writeToNBT(nbttagcompound); - nbttagcompound.setLong("LastUsed", lastUsed); - nbttagcompound.setBoolean("Locked", locked); - } - - @Override - public void writeData(DataOutputStreamForestry data) throws IOException { - sanitizeMatrix(); - matrix.writeData(data); - data.writeLong(lastUsed); - data.writeBoolean(locked); - } - - @Override - public void readData(DataInputStreamForestry data) throws IOException { - matrix = new InventoryAdapter(new InventoryCrafting(DUMMY_CONTAINER, 3, 3)); - matrix.readData(data); - sanitizeMatrix(); - lastUsed = data.readLong(); - locked = data.readBoolean(); - } - - /** - * This is a nasty hack to hide a bug where the recipe stacks somehow - * get the stackSize set to -1. Which makes the recipe no longer work, - * ever. I've seen the bug, even inspected the NBT data of an afflicted - * worktable, but can't duplicate it, and can't trace it. -CovertJaguar - */ - private void sanitizeMatrix() { - for (int slot = 0; slot < matrix.getSizeInventory(); slot++) { - ItemStack stack = matrix.getStackInSlot(slot); - if (stack != null) { - stack.stackSize = 1; - } - } - } - } + public static final int capacity = 9; private static final Container DUMMY_CONTAINER = new ContainerDummy(); private static final List> memoryBlacklist = new ArrayList>(); @@ -155,7 +47,6 @@ public class RecipeMemory implements INBTTagable, IStreamable { private LinkedList recipes = new LinkedList(); private long lastUpdate; - public final int capacity = 9; private static boolean isValid(World world, Recipe recipe) { if (recipe == null) { @@ -310,4 +201,116 @@ public class RecipeMemory implements INBTTagable, IStreamable { List newRecipes = data.readStreamables(Recipe.class); recipes.addAll(newRecipes); } + + public static final class Recipe implements INBTTagable, IStreamable { + + private InventoryAdapter matrix; + private long lastUsed; + private boolean locked; + private ItemStack cachedRecipeOutput; + + public Recipe() { + // required for serialization + } + + public Recipe(InventoryCrafting crafting) { + this.matrix = new InventoryAdapter(new PlainInventory(crafting)); + } + + public Recipe(NBTTagCompound nbttagcompound) { + readFromNBT(nbttagcompound); + } + + public void updateLastUse(long lastUsed) { + this.lastUsed = lastUsed; + } + + public void toggleLock() { + locked = !locked; + } + + public InventoryAdapter getMatrix() { + return matrix; + } + + public ItemStack getRecipeOutput(World world) { + if (cachedRecipeOutput == null) { + InventoryCrafting crafting = new InventoryCrafting(DUMMY_CONTAINER, 3, 3); + for (int i = 0; i < crafting.getSizeInventory(); i++) { + crafting.setInventorySlotContents(i, matrix.getStackInSlot(i)); + } + + cachedRecipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, world); + } + return cachedRecipeOutput; + } + + public long getLastUsed() { + return this.lastUsed; + } + + public boolean isLocked() { + return this.locked; + } + + public boolean hasSameOutput(InventoryCrafting crafting, World world) { + ItemStack recipeOutput = getRecipeOutput(world); + if (recipeOutput == null) { + return false; + } + + ItemStack matchingRecipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, world); + return ItemStack.areItemStacksEqual(recipeOutput, matchingRecipeOutput); + } + + @Override + public final void readFromNBT(NBTTagCompound nbttagcompound) { + matrix = new InventoryAdapter(new InventoryCrafting(DUMMY_CONTAINER, 3, 3)); + matrix.readFromNBT(nbttagcompound); + sanitizeMatrix(); + lastUsed = nbttagcompound.getLong("LastUsed"); + locked = nbttagcompound.getBoolean("Locked"); + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + sanitizeMatrix(); + matrix.writeToNBT(nbttagcompound); + nbttagcompound.setLong("LastUsed", lastUsed); + nbttagcompound.setBoolean("Locked", locked); + } + + @Override + public void writeData(DataOutputStreamForestry data) throws IOException { + sanitizeMatrix(); + matrix.writeData(data); + data.writeLong(lastUsed); + data.writeBoolean(locked); + } + + @Override + public void readData(DataInputStreamForestry data) throws IOException { + matrix = new InventoryAdapter(new InventoryCrafting(DUMMY_CONTAINER, 3, 3)); + matrix.readData(data); + sanitizeMatrix(); + lastUsed = data.readLong(); + locked = data.readBoolean(); + } + + /** + * This is a nasty hack to hide a bug where the recipe stacks somehow + * get the stackSize set to -1. Which makes the recipe no longer work, + * ever. I've seen the bug, even inspected the NBT data of an afflicted + * worktable, but can't duplicate it, and can't trace it. -CovertJaguar + */ + private void sanitizeMatrix() { + for (int slot = 0; slot < matrix.getSizeInventory(); slot++) { + ItemStack stack = matrix.getStackInSlot(slot); + if (stack != null) { + stack.stackSize = 1; + } + } + } + } + }