From 4cc318bc1d7b594bc6745616f6acb40036623864 Mon Sep 17 00:00:00 2001 From: mezz Date: Sat, 6 Dec 2014 23:31:56 -0800 Subject: [PATCH] Fix crash when crafting with a Map on a Worktable --- .../factory/gadgets/TileWorktable.java | 339 ++---------------- .../forestry/factory/gui/GuiWorktable.java | 9 +- .../factory/recipes/RecipeMemory.java | 229 ++++++++++++ 3 files changed, 256 insertions(+), 321 deletions(-) create mode 100644 src/main/java/forestry/factory/recipes/RecipeMemory.java diff --git a/src/main/java/forestry/factory/gadgets/TileWorktable.java b/src/main/java/forestry/factory/gadgets/TileWorktable.java index 6f5bb7282..931537582 100755 --- a/src/main/java/forestry/factory/gadgets/TileWorktable.java +++ b/src/main/java/forestry/factory/gadgets/TileWorktable.java @@ -10,21 +10,7 @@ ******************************************************************************/ package forestry.factory.gadgets; -import java.util.LinkedList; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.Container; -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.InventoryCrafting; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.CraftingManager; -import net.minecraft.item.crafting.IRecipe; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.world.World; - import forestry.api.core.ForestryAPI; -import forestry.api.core.INBTTagable; import forestry.core.gadgets.TileBase; import forestry.core.gui.ContainerDummy; import forestry.core.interfaces.ICrafter; @@ -35,9 +21,16 @@ import forestry.core.network.PacketTileNBT; import forestry.core.network.PacketTileUpdate; import forestry.core.proxy.Proxies; import forestry.core.utils.InventoryAdapter; -import forestry.core.utils.PlainInventory; import forestry.core.utils.StackUtils; import forestry.core.utils.TileInventoryAdapter; +import forestry.factory.recipes.RecipeMemory; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.nbt.NBTTagCompound; public class TileWorktable extends TileBase implements ICrafter { @@ -48,297 +41,9 @@ public class TileWorktable extends TileBase implements ICrafter { public final static short SLOT_INVENTORY_1 = 0; public final static short SLOT_INVENTORY_COUNT = 18; private final static Container DUMMY_CONTAINER = new ContainerDummy(); - private final static RecipeBridge RECIPE_BRIDGE = new RecipeBridge(); - private static class RecipeBridge { - public ItemStack findMatchingRecipe(InventoryCrafting crafting, World world) { - return CraftingManager.getInstance().findMatchingRecipe(crafting, world); - } - } - - /* RECIPE MEMORY */ - public static final class Recipe implements INBTTagable { - - private InventoryAdapter matrix; - private long lastUsed; - private boolean locked; - private World cachedWorld; - private ItemStack cachedRecipeOutput; - - public Recipe(InventoryCrafting crafting) { - this.matrix = new InventoryAdapter(new PlainInventory(crafting)); - } - - public Recipe(NBTTagCompound nbttagcompound) { - readFromNBT(nbttagcompound); - } - - public void setCacheWorld(World world) { - this.cachedWorld = world; - } - - private boolean updateCachedRecipeOutput() { - - if (cachedRecipeOutput != null) - return true; - - InventoryCrafting crafting = new InventoryCrafting(DUMMY_CONTAINER, 3, 3); - for (int i = 0; i < crafting.getSizeInventory(); i++) { - crafting.setInventorySlotContents(i, matrix.getStackInSlot(i)); - } - - cachedRecipeOutput = RECIPE_BRIDGE.findMatchingRecipe(crafting, cachedWorld); - return cachedRecipeOutput != null; - } - - public void updateLastUse(World world) { - lastUsed = world.getTotalWorldTime(); - setCacheWorld(world); - } - - public void toogleLock() { - locked = !locked; - } - - public InventoryAdapter getMatrix() { - return matrix; - } - - public ItemStack getCraftingResult(InventoryCrafting inventorycrafting) { - if (updateCachedRecipeOutput()) - return cachedRecipeOutput; - - return null; - } - - public ItemStack getRecipeOutput() { - if (updateCachedRecipeOutput()) - return cachedRecipeOutput; - - return null; - } - - public long getLastUsed() { - return this.lastUsed; - } - - public boolean isLocked() { - return this.locked; - } - - public int getRecipeSize() { - return matrix.getSizeInventory(); - } - - public boolean hasSameOutput(InventoryCrafting crafting, World world) { - - if (!updateCachedRecipeOutput()) - return false; - - ItemStack recipeOutput = RECIPE_BRIDGE.findMatchingRecipe(crafting, world); - return ItemStack.areItemStacksEqual(cachedRecipeOutput, recipeOutput); - } - - @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); - } - - /** - * 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 class RecipeMemory implements INBTTagable, IInventory { - - private LinkedList recipes = new LinkedList(); - private long lastUpdate; - public final int capacity = 9; - - public long getLastUpdate() { - return lastUpdate; - } - - public void memorizeRecipe(World world, Recipe recipe, InventoryCrafting crafting) { - - lastUpdate = world.getTotalWorldTime(); - recipe.updateLastUse(world); - - Recipe memory = getMemorized(crafting, world); - if (memory != null) { - int index = recipes.indexOf(memory); - recipes.set(index, recipe); - return; - } - - if (recipes.size() < capacity) { - recipes.add(recipe); - return; - } - - Recipe oldest = null; - for (Recipe existing : recipes) { - if (oldest != null && oldest.getLastUsed() < existing.getLastUsed()) - continue; - if (existing.isLocked()) - continue; - - oldest = existing; - } - if (oldest != null) { - recipes.remove(oldest); - recipes.add(recipe); - } - - } - - public Recipe getRecipe(int recipeIndex) { - if (recipes.size() > recipeIndex) - return recipes.get(recipeIndex); - - return null; - } - - public ItemStack getRecipeOutput(int recipeIndex) { - if (recipes.size() > recipeIndex) - return recipes.get(recipeIndex).getRecipeOutput(); - else - return null; - } - - public boolean isLocked(int recipeIndex) { - if (recipes.size() > recipeIndex) - return recipes.get(recipeIndex).isLocked(); - - return false; - } - - public void toggleLock(World world, int recipeIndex) { - lastUpdate = world.getTotalWorldTime(); - if (recipes.size() > recipeIndex) - recipes.get(recipeIndex).toogleLock(); - } - - private Recipe getMemorized(InventoryCrafting crafting, World world) { - for (Recipe recipe : recipes) { - if (recipe.hasSameOutput(crafting, world)) - return recipe; - } - - return null; - } - - @Override - public void readFromNBT(NBTTagCompound nbttagcompound) { - recipes = new LinkedList(); - if (!nbttagcompound.hasKey("RecipeMemory")) - return; - - NBTTagList nbttaglist = nbttagcompound.getTagList("RecipeMemory", 10); - for (int j = 0; j < nbttaglist.tagCount(); ++j) { - NBTTagCompound nbttagcompound2 = nbttaglist.getCompoundTagAt(j); - recipes.add(new Recipe(nbttagcompound2)); - } - } - - @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - NBTTagList nbttaglist = new NBTTagList(); - for (Recipe recipe : recipes) { - if (recipe != null) { - NBTTagCompound nbttagcompound2 = new NBTTagCompound(); - recipe.writeToNBT(nbttagcompound2); - nbttaglist.appendTag(nbttagcompound2); - } - } - nbttagcompound.setTag("RecipeMemory", nbttaglist); - } - - @Override - public int getSizeInventory() { - return recipes.size(); - } - - @Override - public ItemStack getStackInSlot(int i) { - return getRecipeOutput(i); - } - - @Override - public ItemStack decrStackSize(int i, int j) { - return null; - } - - @Override - public ItemStack getStackInSlotOnClosing(int i) { - return null; - } - - @Override - public void setInventorySlotContents(int i, ItemStack itemstack) { - } - - @Override - public String getInventoryName() { - return ""; - } - - @Override - public boolean hasCustomInventoryName() { - return false; - } - - @Override - public int getInventoryStackLimit() { - return 64; - } - - @Override - public void markDirty() { - } - - @Override - public boolean isUseableByPlayer(EntityPlayer entityplayer) { - return true; - } - - @Override - public void openInventory() { - } - - @Override - public void closeInventory() { - } - - @Override - public boolean isItemValidForSlot(int i, ItemStack itemstack) { - return false; - } - } /* MEMBERS */ - private Recipe currentRecipe; + private RecipeMemory.Recipe currentRecipe; private InventoryCrafting currentCrafting; private final RecipeMemory memorized; private final TileInventoryAdapter craftingInventory; @@ -406,9 +111,7 @@ public class TileWorktable extends TileBase implements ICrafter { return; } - Recipe recipe = memorized.getRecipe(recipeIndex); - recipe.sanitizeMatrix(); - IInventory matrix = recipe.getMatrix(); + IInventory matrix = memorized.getRecipeMatrix(recipeIndex); if (matrix == null) return; @@ -420,20 +123,20 @@ public class TileWorktable extends TileBase implements ICrafter { /* CRAFTING */ public void setRecipe(InventoryCrafting crafting) { - ItemStack recipeOutput = RECIPE_BRIDGE.findMatchingRecipe(crafting, worldObj); + ItemStack recipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, worldObj); if (recipeOutput == null) { currentRecipe = null; currentCrafting = null; } else { - currentRecipe = new Recipe(crafting); + currentRecipe = new RecipeMemory.Recipe(crafting, worldObj); currentCrafting = crafting; } - updateCraftResult(currentCrafting); + updateCraftResult(); } - private void updateCraftResult(InventoryCrafting inventorycrafting) { + private void updateCraftResult() { if (currentRecipe != null) { - ItemStack result = currentRecipe.getCraftingResult(inventorycrafting); + ItemStack result = currentRecipe.getRecipeOutput(worldObj); if (result != null) { craftingInventory.setInventorySlotContents(SLOT_CRAFTING_RESULT, result.copy()); return; @@ -492,11 +195,11 @@ public class TileWorktable extends TileBase implements ICrafter { } } } - ItemStack recipeOutput = RECIPE_BRIDGE.findMatchingRecipe(crafting, worldObj); + ItemStack recipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, worldObj); if (recipeOutput == null) return false; - return ItemStack.areItemStacksEqual(recipeOutput, currentRecipe.getRecipeOutput()); + return ItemStack.areItemStacksEqual(recipeOutput, currentRecipe.getRecipeOutput(worldObj)); } private boolean removeResources(EntityPlayer player) { @@ -519,8 +222,8 @@ public class TileWorktable extends TileBase implements ICrafter { if (Proxies.common.isSimulating(worldObj)) memorized.memorizeRecipe(worldObj, currentRecipe, currentCrafting); - updateCraftResult(currentCrafting); - return currentRecipe.getRecipeOutput().copy(); + updateCraftResult(); + return currentRecipe.getRecipeOutput(worldObj).copy(); } @Override @@ -528,8 +231,8 @@ public class TileWorktable extends TileBase implements ICrafter { if (currentRecipe == null) return null; - if (currentRecipe.getRecipeOutput() != null) - return currentRecipe.getRecipeOutput().copy(); + if (currentRecipe.getRecipeOutput(worldObj) != null) + return currentRecipe.getRecipeOutput(worldObj).copy(); return null; } diff --git a/src/main/java/forestry/factory/gui/GuiWorktable.java b/src/main/java/forestry/factory/gui/GuiWorktable.java index 59dd3ff3c..36248e8b5 100755 --- a/src/main/java/forestry/factory/gui/GuiWorktable.java +++ b/src/main/java/forestry/factory/gui/GuiWorktable.java @@ -22,6 +22,7 @@ import net.minecraft.client.renderer.OpenGlHelper; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.world.World; import org.lwjgl.opengl.GL11; public class GuiWorktable extends GuiForestryTitled { @@ -29,14 +30,16 @@ public class GuiWorktable extends GuiForestryTitled { private class MemorizedSlot extends Widget { private final int slotNumber; + private final World world; - public MemorizedSlot(WidgetManager manager, int xPos, int yPos, int slot) { + public MemorizedSlot(World world, WidgetManager manager, int xPos, int yPos, int slot) { super(manager, xPos, yPos); this.slotNumber = slot; + this.world = world; } private ItemStack getOutputStack() { - return worktable.getMemory().getRecipeOutput(slotNumber); + return worktable.getMemory().getRecipeOutput(world, slotNumber); } @Override @@ -111,7 +114,7 @@ public class GuiWorktable extends GuiForestryTitled { int yPos = 20 + (y * spacing); for (int x = 0; x < 3; x++) { int xPos = 110 + (x * spacing); - widgetManager.add(new MemorizedSlot(widgetManager, xPos, yPos, slot)); + widgetManager.add(new MemorizedSlot(player.getEntityWorld(), widgetManager, xPos, yPos, slot)); slot += 1; } } diff --git a/src/main/java/forestry/factory/recipes/RecipeMemory.java b/src/main/java/forestry/factory/recipes/RecipeMemory.java new file mode 100644 index 000000000..b027b3769 --- /dev/null +++ b/src/main/java/forestry/factory/recipes/RecipeMemory.java @@ -0,0 +1,229 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.factory.recipes; + +import forestry.api.core.INBTTagable; +import forestry.core.gui.ContainerDummy; +import forestry.core.utils.InventoryAdapter; +import forestry.core.utils.PlainInventory; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.world.World; + +import java.util.LinkedList; + +public class RecipeMemory implements INBTTagable { + + private final static Container DUMMY_CONTAINER = new ContainerDummy(); + + public static final class Recipe implements INBTTagable { + + private InventoryAdapter matrix; + private long lastUsed; + private boolean locked; + private ItemStack cachedRecipeOutput; + + 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); + } + + /** + * 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; + } + } + } + + private LinkedList recipes = new LinkedList(); + private long lastUpdate; + public final int capacity = 9; + + public long getLastUpdate() { + return lastUpdate; + } + + public void memorizeRecipe(World world, Recipe recipe, InventoryCrafting crafting) { + + lastUpdate = world.getTotalWorldTime(); + recipe.updateLastUse(lastUpdate); + + Recipe memory = getMemorized(crafting, world); + if (memory != null) { + int index = recipes.indexOf(memory); + recipes.set(index, recipe); + return; + } + + if (recipes.size() < capacity) { + recipes.add(recipe); + return; + } + + Recipe oldest = null; + for (Recipe existing : recipes) { + if (oldest != null && oldest.getLastUsed() < existing.getLastUsed()) + continue; + if (existing.isLocked()) + continue; + + oldest = existing; + } + if (oldest != null) { + recipes.remove(oldest); + recipes.add(recipe); + } + + } + + public Recipe getRecipe(int recipeIndex) { + if (recipes.size() > recipeIndex) + return recipes.get(recipeIndex); + + return null; + } + + public IInventory getRecipeMatrix(int recipeIndex) { + RecipeMemory.Recipe recipe = getRecipe(recipeIndex); + recipe.sanitizeMatrix(); + return recipe.getMatrix(); + } + + public ItemStack getRecipeOutput(World world, int recipeIndex) { + if (recipes.size() > recipeIndex) + return recipes.get(recipeIndex).getRecipeOutput(world); + else + return null; + } + + public boolean isLocked(int recipeIndex) { + if (recipes.size() > recipeIndex) + return recipes.get(recipeIndex).isLocked(); + + return false; + } + + public void toggleLock(World world, int recipeIndex) { + lastUpdate = world.getTotalWorldTime(); + if (recipes.size() > recipeIndex) + recipes.get(recipeIndex).toggleLock(); + } + + private Recipe getMemorized(InventoryCrafting crafting, World world) { + for (Recipe recipe : recipes) { + if (recipe.hasSameOutput(crafting, world)) + return recipe; + } + + return null; + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + recipes = new LinkedList(); + if (!nbttagcompound.hasKey("RecipeMemory")) + return; + + NBTTagList nbttaglist = nbttagcompound.getTagList("RecipeMemory", 10); + for (int j = 0; j < nbttaglist.tagCount(); ++j) { + NBTTagCompound nbttagcompound2 = nbttaglist.getCompoundTagAt(j); + recipes.add(new Recipe(nbttagcompound2)); + } + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + NBTTagList nbttaglist = new NBTTagList(); + for (Recipe recipe : recipes) { + if (recipe != null) { + NBTTagCompound nbttagcompound2 = new NBTTagCompound(); + recipe.writeToNBT(nbttagcompound2); + nbttaglist.appendTag(nbttagcompound2); + } + } + nbttagcompound.setTag("RecipeMemory", nbttaglist); + } + +}