mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-20 12:09:39 +00:00
Fix #1768 fix serious issues with the worktable
This commit is contained in:
1 parent
b897056824
commit
a1bc9144bd
8 files changed
+141
-130
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -57,58 +57,58 @@ public abstract class RecipeUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static InventoryCraftingForestry getCraftRecipe(NonNullList<ItemStack> recipeItems, NonNullList<ItemStack> 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<ItemStack> 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<ItemStack> 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<ItemStack> 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<IRecipe> outputs = findMatchingRecipes(crafting, world);
|
||||
if (outputs.contains(recipe)) {
|
||||
return crafting;
|
||||
}
|
||||
return null;
|
||||
crafting.setInventorySlotContents(slot, originalStack);
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
public static List<IRecipe> 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<NonNullList<ItemStack>> recipeIngredients = recipe.getRawIngredients();
|
||||
NonNullList<String> oreDicts = recipe.getOreDicts();
|
||||
@@ -155,6 +156,7 @@ public abstract class RecipeUtil {
|
||||
return matches(recipeIngredients, oreDicts, width, height, inventoryCrafting);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String[][] matches(NonNullList<NonNullList<ItemStack>> recipeIngredients, NonNullList<String> oreDicts, int width, int height, IInventory inventoryCrafting) {
|
||||
ItemStack[][] resources = getResources(inventoryCrafting);
|
||||
return matches(recipeIngredients, oreDicts, width, height, resources);
|
||||
|
||||
@@ -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<ItemStack> 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<ItemStack> required) {
|
||||
NonNullList<ItemStack> offered = getStacks(inventory);
|
||||
NonNullList<ItemStack> condensedRequired = ItemStackUtil.condenseStacks(required);
|
||||
NonNullList<ItemStack> 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<ItemStack> 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<ItemStack> removeSets(IInventory inventory, int count, NonNullList<ItemStack> 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<ItemStack> stacks, int startSlot, int slots, boolean all) {
|
||||
|
||||
for (ItemStack stack : stacks) {
|
||||
if (stack == null) {
|
||||
if (stack == null || stack.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<ItemStack> recipeItems = InventoryUtil.getStacks(currentRecipe.getCraftMatrix());
|
||||
NonNullList<ItemStack> 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<ItemStack> 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<ItemStack> 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<ItemStack> remainingItems = CraftingManager.getRemainingItems(currentRecipe.getCraftMatrix(), player.world);
|
||||
InventoryCraftingForestry craftMatrix = currentRecipe.getCraftMatrix();
|
||||
NonNullList<ItemStack> 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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user