From a1bc9144bd41833e15d6defaffdcab7027e69ecf Mon Sep 17 00:00:00 2001 From: mezz Date: Thu, 27 Jul 2017 22:36:01 -0700 Subject: [PATCH] Fix #1768 fix serious issues with the worktable --- gradle.properties | 4 +- .../forestry/core/fluids/FluidHelper.java | 3 +- .../forestry/core/recipes/RecipeUtil.java | 82 ++++++------- .../forestry/core/utils/InventoryUtil.java | 112 +++++++----------- .../forestry/core/utils/ItemStackUtil.java | 21 +++- .../java/forestry/core/utils/SlotUtil.java | 5 +- .../inventory/InventoryCraftingForestry.java | 21 +++- .../forestry/factory/tiles/TileWorktable.java | 23 ++-- 8 files changed, 141 insertions(+), 130 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5a4b387bb..a45526679 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,9 @@ mcversion=1.12 -forgeversion=14.21.1.2395 +forgeversion=14.21.1.2424 mcp_mappings=snapshot_20170703 tesla_version=1.0.60 -jei_version=4.7.0.67 +jei_version=4.7.2.77 ic2_version=2.7.30-ex111 version_major=5 diff --git a/src/main/java/forestry/core/fluids/FluidHelper.java b/src/main/java/forestry/core/fluids/FluidHelper.java index 271f70887..df10b82b4 100644 --- a/src/main/java/forestry/core/fluids/FluidHelper.java +++ b/src/main/java/forestry/core/fluids/FluidHelper.java @@ -12,7 +12,6 @@ package forestry.core.fluids; import javax.annotation.Nullable; -import forestry.core.utils.InventoryUtil; import forestry.core.utils.ItemStackUtil; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -107,7 +106,7 @@ public final class FluidHelper { boolean moveToOutput = fluidInContainer.amount >= containerCapacity; if (moveToOutput) { - if (!output.isEmpty() && (output.getCount() >= output.getMaxStackSize() || !InventoryUtil.isItemEqual(filled, output))) { + if (!output.isEmpty() && (output.getCount() >= output.getMaxStackSize() || !ItemStackUtil.areItemStacksEqualIgnoreCount(filled, output))) { return FillStatus.NO_SPACE; } } else { diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index ccb0d7495..ffb28f526 100755 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -57,58 +57,58 @@ public abstract class RecipeUtil { } @Nullable - public static InventoryCraftingForestry getCraftRecipe(NonNullList recipeItems, NonNullList availableItems, World world, IRecipe recipe) { - // Need at least one matched set - if (ItemStackUtil.containsSets(recipeItems, availableItems, true, true) == 0) { + public static InventoryCraftingForestry getCraftRecipe(InventoryCrafting originalCrafting, NonNullList availableItems, World world, IRecipe recipe) { + if (!recipe.matches(originalCrafting, world)) { return null; } - - // Check that it doesn't make a different recipe. - // For example: - // Wood Logs are all ore dictionary equivalent with each other, - // but an Oak Log shouldn't be able to make Ebony Wood Planks - // because it makes Oak Wood Planks using the same recipe. - // Strategy: - // Create a fake crafting inventory using items we have in availableItems - // in place of the ones in the saved crafting inventory. - // Check that the recipe it makes is the same as the currentRecipe. + + ItemStack expectedOutput = recipe.getCraftingResult(originalCrafting); + if (expectedOutput.isEmpty()) { + return null; + } + InventoryCraftingForestry crafting = new InventoryCraftingForestry(); NonNullList stockCopy = ItemStackUtil.condenseStacks(availableItems); - for (int slot = 0; slot < recipeItems.size(); slot++) { - ItemStack recipeStack = recipeItems.get(slot); - if (recipeStack.isEmpty()) { - continue; - } - - // Use crafting equivalent (not oredict) items first - for (ItemStack stockStack : stockCopy) { - if (stockStack.getCount() > 0 && ItemStackUtil.isCraftingEquivalent(recipeStack, stockStack, false, false)) { - ItemStack stack = ItemStackUtil.createCopyWithCount(stockStack, 1); - stockStack.shrink(1); - crafting.setInventorySlotContents(slot, stack); - break; + for (int slot = 0; slot < originalCrafting.getSizeInventory(); slot++) { + ItemStack stackInSlot = originalCrafting.getStackInSlot(slot); + if (!stackInSlot.isEmpty()) { + ItemStack equivalent = getCraftingEquivalent(stockCopy, originalCrafting, slot, world, recipe, expectedOutput); + if (equivalent.isEmpty()) { + return null; + } else { + crafting.setInventorySlotContents(slot, equivalent); } } + } + + if (recipe.matches(crafting, world)) { + ItemStack output = recipe.getCraftingResult(crafting); + if (ItemStack.areItemStacksEqual(output, expectedOutput)) { + return crafting; + } + } - // Use oredict items if crafting equivalent items aren't available - if (crafting.getStackInSlot(slot).isEmpty()) { - for (ItemStack stockStack : stockCopy) { - if (stockStack.getCount() > 0 && ItemStackUtil.isCraftingEquivalent(recipeStack, stockStack, true, true)) { - ItemStack stack = ItemStackUtil.createCopyWithCount(stockStack, 1); - stockStack.shrink(1); - crafting.setInventorySlotContents(slot, stack); - break; + return null; + } + + private static ItemStack getCraftingEquivalent(NonNullList stockCopy, InventoryCrafting crafting, int slot, World world, IRecipe recipe, ItemStack expectedOutput) { + ItemStack originalStack = crafting.getStackInSlot(slot); + for (ItemStack stockStack : stockCopy) { + if (!stockStack.isEmpty()) { + ItemStack singleStockStack = ItemStackUtil.createCopyWithCount(stockStack, 1); + crafting.setInventorySlotContents(slot, singleStockStack); + if (recipe.matches(crafting, world)) { + ItemStack output = recipe.getCraftingResult(crafting); + if (ItemStack.areItemStacksEqual(output, expectedOutput)) { + crafting.setInventorySlotContents(slot, originalStack); + return stockStack.splitStack(1); } } } } - - List outputs = findMatchingRecipes(crafting, world); - if (outputs.contains(recipe)) { - return crafting; - } - return null; + crafting.setInventorySlotContents(slot, originalStack); + return ItemStack.EMPTY; } public static List findMatchingRecipes(InventoryCrafting inventory, World world) { @@ -147,6 +147,7 @@ public abstract class RecipeUtil { GameRegistry.addSmelting(res, prod, xp); } + @Nullable public static String[][] matches(IDescriptiveRecipe recipe, IInventory inventoryCrafting) { NonNullList> recipeIngredients = recipe.getRawIngredients(); NonNullList oreDicts = recipe.getOreDicts(); @@ -155,6 +156,7 @@ public abstract class RecipeUtil { return matches(recipeIngredients, oreDicts, width, height, inventoryCrafting); } + @Nullable public static String[][] matches(NonNullList> recipeIngredients, NonNullList oreDicts, int width, int height, IInventory inventoryCrafting) { ItemStack[][] resources = getResources(inventoryCrafting); return matches(recipeIngredients, oreDicts, width, height, resources); diff --git a/src/main/java/forestry/core/utils/InventoryUtil.java b/src/main/java/forestry/core/utils/InventoryUtil.java index 650395430..5565161ae 100644 --- a/src/main/java/forestry/core/utils/InventoryUtil.java +++ b/src/main/java/forestry/core/utils/InventoryUtil.java @@ -11,7 +11,6 @@ package forestry.core.utils; import javax.annotation.Nullable; -import java.util.Collection; import forestry.api.core.ForestryAPI; import forestry.core.circuits.ISocketable; @@ -32,75 +31,8 @@ import net.minecraft.world.World; import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.fml.common.Optional; import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.oredict.OreDictionary; public abstract class InventoryUtil { - - public static boolean isWildcard(ItemStack stack) { - return stack.getItemDamage() == OreDictionary.WILDCARD_VALUE; - } - - /** - * A more robust item comparison function. Supports items with damage = -1 - * matching any sub-type. - * - * @param a An ItemStack - * @param b An ItemStack - * @return True if equal - */ - public static boolean isItemEqual(ItemStack a, ItemStack b) { - return isItemEqual(a, b, true, true); - } - - public static boolean isItemEqual(final ItemStack a, final ItemStack b, final boolean matchDamage, final boolean matchNBT) { - if (a.isEmpty() || b.isEmpty()) { - return false; - } - if (a.getItem() != b.getItem()) { - return false; - } - if (matchNBT && !ItemStack.areItemStackTagsEqual(a, b)) { - return false; - } - if (matchDamage && a.getHasSubtypes()) { - if (isWildcard(a) || isWildcard(b)) { - return true; - } - if (a.getItemDamage() != b.getItemDamage()) { - return false; - } - } - return true; - } - - /** - * Returns true if the item is equal to any one of several possible matches. - */ - public static boolean isItemEqual(ItemStack stack, ItemStack... matches) { - for (ItemStack match : matches) { - if (isItemEqual(stack, match)) { - return true; - } - } - return false; - } - - /** - * Returns true if the item is equal to any one of several possible matches. - * - * @param stack the ItemStack to test - * @param matches the ItemStacks to test against - * @return true if a match is found - */ - public static boolean isItemEqual(ItemStack stack, Collection matches) { - for (ItemStack match : matches) { - if (isItemEqual(stack, match)) { - return true; - } - } - return false; - } - /** * Attempts to move an ItemStack from one inventory to another. * @@ -218,6 +150,48 @@ public abstract class InventoryUtil { return ItemStackUtil.containsSets(set, stock, oreDicts, craftingTools) >= count; } } + + public static boolean deleteExactSet(IInventory inventory, NonNullList required) { + NonNullList offered = getStacks(inventory); + NonNullList condensedRequired = ItemStackUtil.condenseStacks(required); + NonNullList condensedOffered = ItemStackUtil.condenseStacks(offered); + + for (ItemStack req : condensedRequired) { + if (!containsExactStack(req, condensedOffered)) { + return false; + } + } + + for (ItemStack itemStack : condensedRequired) { + deleteExactStack(inventory, itemStack); + } + return true; + } + + private static boolean containsExactStack(ItemStack req, NonNullList condensedOffered) { + for (ItemStack offer : condensedOffered) { + if (offer.getCount() >= req.getCount() && ItemStackUtil.areItemStacksEqualIgnoreCount(req, offer)) { + return true; + } + } + return false; + } + + private static void deleteExactStack(IInventory inventory, ItemStack itemStack) { + int count = itemStack.getCount(); + for (int j = 0; j < inventory.getSizeInventory(); j++) { + ItemStack stackInSlot = inventory.getStackInSlot(j); + if (!stackInSlot.isEmpty()) { + if (ItemStackUtil.areItemStacksEqualIgnoreCount(itemStack, stackInSlot)) { + ItemStack removed = inventory.decrStackSize(j, count); + count -= removed.getCount(); + if (count == 0) { + return; + } + } + } + } + } @Nullable public static NonNullList removeSets(IInventory inventory, int count, NonNullList set, @Nullable EntityPlayer player, boolean stowContainer, boolean oreDictionary, boolean craftingTools) { @@ -394,7 +368,7 @@ public abstract class InventoryUtil { public static boolean tryAddStacksCopy(IInventory inventory, NonNullList stacks, int startSlot, int slots, boolean all) { for (ItemStack stack : stacks) { - if (stack == null) { + if (stack == null || stack.isEmpty()) { continue; } diff --git a/src/main/java/forestry/core/utils/ItemStackUtil.java b/src/main/java/forestry/core/utils/ItemStackUtil.java index c8e4cff3d..1fa184ae4 100755 --- a/src/main/java/forestry/core/utils/ItemStackUtil.java +++ b/src/main/java/forestry/core/utils/ItemStackUtil.java @@ -594,5 +594,24 @@ public abstract class ItemStackUtil { } return null; } - + + /** + * Checks like {@link ItemStack#areItemStacksEqual(ItemStack, ItemStack)} + * but ignores stack size (count). + */ + public static boolean areItemStacksEqualIgnoreCount(ItemStack a, ItemStack b) { + if (a.isEmpty() && b.isEmpty()) { + return true; + } else if (a.isEmpty() || b.isEmpty()) { + return false; + } else if (a.getItem() != b.getItem()) { + return false; + } else if (a.getItemDamage() != b.getItemDamage()) { + return false; + } else if (a.getTagCompound() == null && b.getTagCompound() != null) { + return false; + } else { + return (a.getTagCompound() == null || a.getTagCompound().equals(b.getTagCompound())) && a.areCapsCompatible(b); + } + } } diff --git a/src/main/java/forestry/core/utils/SlotUtil.java b/src/main/java/forestry/core/utils/SlotUtil.java index e3e178649..cf30fa431 100644 --- a/src/main/java/forestry/core/utils/SlotUtil.java +++ b/src/main/java/forestry/core/utils/SlotUtil.java @@ -12,6 +12,7 @@ package forestry.core.utils; import java.util.List; +import forestry.core.gui.slots.SlotCrafter; import forestry.core.gui.slots.SlotForestry; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; @@ -70,7 +71,7 @@ public abstract class SlotUtil { return ItemStack.EMPTY; } - boolean fromCraftingSlot = slot instanceof SlotCrafting; + boolean fromCraftingSlot = slot instanceof SlotCrafting || slot instanceof SlotCrafter; int numSlots = inventorySlots.size(); ItemStack stackInSlot = slot.getStack(); @@ -82,7 +83,7 @@ public abstract class SlotUtil { slot.onSlotChange(stackInSlot, originalStack); if (stackInSlot.isEmpty()) { - slot.putStack(stackInSlot); + slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } diff --git a/src/main/java/forestry/factory/inventory/InventoryCraftingForestry.java b/src/main/java/forestry/factory/inventory/InventoryCraftingForestry.java index 8a8aed0b1..e486a8f15 100644 --- a/src/main/java/forestry/factory/inventory/InventoryCraftingForestry.java +++ b/src/main/java/forestry/factory/inventory/InventoryCraftingForestry.java @@ -12,14 +12,31 @@ package forestry.factory.inventory; import forestry.core.gui.ContainerDummy; import forestry.factory.gui.ContainerWorktable; + +import net.minecraft.inventory.Container; import net.minecraft.inventory.InventoryCrafting; public class InventoryCraftingForestry extends InventoryCrafting { + private final Container eventHandlerIn; + public InventoryCraftingForestry(ContainerWorktable containerWorktable) { - super(containerWorktable, 3, 3); + this(containerWorktable, 3, 3); } public InventoryCraftingForestry() { - super(ContainerDummy.instance, 3, 3); + this(ContainerDummy.instance, 3, 3); + } + + private InventoryCraftingForestry(Container eventHandlerIn, int width, int height) { + super(eventHandlerIn, width, height); + this.eventHandlerIn = eventHandlerIn; + } + + public InventoryCraftingForestry copy() { + InventoryCraftingForestry copy = new InventoryCraftingForestry(this.eventHandlerIn, getWidth(), getHeight()); + for (int slot = 0; slot < getSizeInventory(); slot++) { + copy.setInventorySlotContents(slot, getStackInSlot(slot).copy()); + } + return copy; } } diff --git a/src/main/java/forestry/factory/tiles/TileWorktable.java b/src/main/java/forestry/factory/tiles/TileWorktable.java index 4d3056874..f023b7dc5 100755 --- a/src/main/java/forestry/factory/tiles/TileWorktable.java +++ b/src/main/java/forestry/factory/tiles/TileWorktable.java @@ -23,7 +23,6 @@ import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryBasic; 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.util.NonNullList; @@ -150,14 +149,13 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { return false; } - NonNullList recipeItems = InventoryUtil.getStacks(currentRecipe.getCraftMatrix()); NonNullList inventoryStacks = InventoryUtil.getStacks(this); - InventoryCraftingForestry crafting = RecipeUtil.getCraftRecipe(recipeItems, inventoryStacks, world, selectedRecipe); + InventoryCraftingForestry crafting = RecipeUtil.getCraftRecipe(currentRecipe.getCraftMatrix(), inventoryStacks, world, selectedRecipe); if (crafting == null) { return false; } - - recipeItems = InventoryUtil.getStacks(crafting); + + NonNullList recipeItems = InventoryUtil.getStacks(crafting); IInventory inventory; if (simulate) { @@ -167,13 +165,11 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { inventory = this; } - // craft recipe should exactly match ingredients here, so no oreDict or tool matching - NonNullList removed = InventoryUtil.removeSets(inventory, 1, recipeItems, null, false, false, false); - if (removed == null) { + if (!InventoryUtil.deleteExactSet(inventory, recipeItems)) { return false; } - if (!simulate) { + if (!simulate && !world.isRemote) { // update crafting display to match the ingredients that were actually used setCraftingDisplay(crafting); } @@ -184,13 +180,16 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { @Override public void onCraftingComplete(EntityPlayer player) { Preconditions.checkState(currentRecipe != null); - + IRecipe selectedRecipe = currentRecipe.getSelectedRecipe(); + Preconditions.checkState(selectedRecipe != null); + ForgeHooks.setCraftingPlayer(player); - NonNullList remainingItems = CraftingManager.getRemainingItems(currentRecipe.getCraftMatrix(), player.world); + InventoryCraftingForestry craftMatrix = currentRecipe.getCraftMatrix(); + NonNullList remainingItems = selectedRecipe.getRemainingItems(craftMatrix.copy()); ForgeHooks.setCraftingPlayer(null); for (ItemStack remainingItem : remainingItems) { - if (remainingItem != null) { + if (remainingItem != null && !remainingItem.isEmpty()) { if (!InventoryUtil.tryAddStack(this, remainingItem, true)) { player.dropItem(remainingItem, false); }