diff --git a/src/main/java/forestry/apiculture/gui/ContainerAlvearySieve.java b/src/main/java/forestry/apiculture/gui/ContainerAlvearySieve.java index 54cebec1b..eab1b48d1 100755 --- a/src/main/java/forestry/apiculture/gui/ContainerAlvearySieve.java +++ b/src/main/java/forestry/apiculture/gui/ContainerAlvearySieve.java @@ -17,20 +17,20 @@ import forestry.apiculture.multiblock.TileAlvearySieve; import forestry.core.gui.ContainerTile; import forestry.core.gui.slots.SlotFiltered; import forestry.core.gui.slots.SlotOutput; -import forestry.core.tiles.ICrafter; +import forestry.core.inventory.watchers.ISlotPickupWatcher; public class ContainerAlvearySieve extends ContainerTile { public ContainerAlvearySieve(InventoryPlayer player, TileAlvearySieve tile) { super(tile, player, 8, 87); - ICrafter crafter = tile.getCrafter(); + ISlotPickupWatcher crafter = tile.getCrafter(); - addSlotToContainer(new SlotOutput(tile, 0, 94, 52).setCrafter(crafter)); - addSlotToContainer(new SlotOutput(tile, 1, 115, 39).setCrafter(crafter)); - addSlotToContainer(new SlotOutput(tile, 2, 73, 39).setCrafter(crafter)); - addSlotToContainer(new SlotOutput(tile, 3, 94, 26).setCrafter(crafter)); + addSlotToContainer(new SlotOutput(tile, 0, 94, 52).setPickupWatcher(crafter)); + addSlotToContainer(new SlotOutput(tile, 1, 115, 39).setPickupWatcher(crafter)); + addSlotToContainer(new SlotOutput(tile, 2, 73, 39).setPickupWatcher(crafter)); + addSlotToContainer(new SlotOutput(tile, 3, 94, 26).setPickupWatcher(crafter)); - addSlotToContainer(new SlotFiltered(tile, InventoryAlvearySieve.SLOT_SIEVE, 43, 39).setCrafter(crafter)); + addSlotToContainer(new SlotFiltered(tile, InventoryAlvearySieve.SLOT_SIEVE, 43, 39).setPickupWatcher(crafter)); } } diff --git a/src/main/java/forestry/apiculture/inventory/InventoryAlvearySieve.java b/src/main/java/forestry/apiculture/inventory/InventoryAlvearySieve.java index e8d9ae229..e9450f352 100644 --- a/src/main/java/forestry/apiculture/inventory/InventoryAlvearySieve.java +++ b/src/main/java/forestry/apiculture/inventory/InventoryAlvearySieve.java @@ -16,10 +16,10 @@ import net.minecraft.item.ItemStack; import forestry.apiculture.multiblock.TileAlvearySieve; import forestry.core.config.ForestryItem; import forestry.core.inventory.InventoryAdapterTile; -import forestry.core.tiles.ICrafter; +import forestry.core.inventory.watchers.ISlotPickupWatcher; import forestry.core.utils.ItemStackUtil; -public class InventoryAlvearySieve extends InventoryAdapterTile implements ICrafter { +public class InventoryAlvearySieve extends InventoryAdapterTile implements ISlotPickupWatcher { public static final int SLOT_POLLEN_1 = 0; public static final int SLOTS_POLLEN_COUNT = 4; public static final int SLOT_SIEVE = 4; @@ -56,17 +56,15 @@ public class InventoryAlvearySieve extends InventoryAdapterTile implement this.player = player; // Analyze slot - addSlotToContainer(new SlotFiltered(this.tile, InventoryEscritoire.SLOT_ANALYZE, 97, 67).setCrafter(this.tile).setStackLimit(1)); + addSlotToContainer(new SlotFiltered(this.tile, InventoryEscritoire.SLOT_ANALYZE, 97, 67).setPickupWatcher(this.tile).setStackLimit(1)); for (int i = 0; i < InventoryEscritoire.SLOTS_INPUT_COUNT; i++) { addSlotToContainer(new SlotFiltered(this.tile, InventoryEscritoire.SLOT_INPUT_1 + i, 17, 49 + i * 18).setBlockedTexture("slots/blocked_2")); diff --git a/src/main/java/forestry/core/gui/ContainerForestry.java b/src/main/java/forestry/core/gui/ContainerForestry.java index f09d09346..261e85d95 100644 --- a/src/main/java/forestry/core/gui/ContainerForestry.java +++ b/src/main/java/forestry/core/gui/ContainerForestry.java @@ -52,8 +52,11 @@ public abstract class ContainerForestry extends Container { } Slot slot = (slotIndex < 0) ? null : (Slot) this.inventorySlots.get(slotIndex); - if (slot instanceof SlotForestry && ((SlotForestry) slot).isPhantom()) { - return SlotUtil.slotClickPhantom(slot, button, modifier, player); + if (slot instanceof SlotForestry) { + SlotForestry slotForestry = (SlotForestry) slot; + if (slotForestry.isPhantom()) { + return SlotUtil.slotClickPhantom(slotForestry, button, modifier, player); + } } return super.slotClick(slotIndex, button, modifier, player); diff --git a/src/main/java/forestry/core/gui/slots/SlotFiltered.java b/src/main/java/forestry/core/gui/slots/SlotFiltered.java index e34f0ff03..b400ddc16 100644 --- a/src/main/java/forestry/core/gui/slots/SlotFiltered.java +++ b/src/main/java/forestry/core/gui/slots/SlotFiltered.java @@ -12,23 +12,24 @@ package forestry.core.gui.slots; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; +import net.minecraft.util.IIcon; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +import forestry.core.render.TextureManager; import forestry.core.tiles.IFilterSlotDelegate; /** * Slot which only takes specific items, specified by the IFilterSlotDelegate. */ public class SlotFiltered extends SlotWatched { - + private String blockedTexture = "slots/blocked"; private final IFilterSlotDelegate filterSlotDelegate; - public SlotFiltered(T filterSlotDelegateInventory, int slotIndex, int xPos, int yPos) { - this(filterSlotDelegateInventory, filterSlotDelegateInventory, slotIndex, xPos, yPos); - } - - public SlotFiltered(IFilterSlotDelegate filterSlotDelegate, IInventory inventory, int slotIndex, int xPos, int yPos) { + public SlotFiltered(T inventory, int slotIndex, int xPos, int yPos) { super(inventory, slotIndex, xPos, yPos); - this.filterSlotDelegate = filterSlotDelegate; + this.filterSlotDelegate = inventory; } @Override @@ -42,4 +43,20 @@ public class SlotFiltered extends SlotWatched { } return true; } + + public SlotFiltered setBlockedTexture(String ident) { + blockedTexture = ident; + return this; + } + + @SideOnly(Side.CLIENT) + @Override + public IIcon getBackgroundIconIndex() { + ItemStack stack = getStack(); + if (!isItemValid(stack)) { + return TextureManager.getInstance().getDefault(blockedTexture); + } else { + return null; + } + } } diff --git a/src/main/java/forestry/core/gui/slots/SlotWatched.java b/src/main/java/forestry/core/gui/slots/SlotWatched.java index dc6d14fd8..c98012e23 100644 --- a/src/main/java/forestry/core/gui/slots/SlotWatched.java +++ b/src/main/java/forestry/core/gui/slots/SlotWatched.java @@ -1,52 +1,54 @@ +/******************************************************************************* + * 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.core.gui.slots; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; -import net.minecraft.util.IIcon; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -import forestry.core.render.TextureManager; -import forestry.core.tiles.ICrafter; +import forestry.core.inventory.watchers.FakeSlotChangeWatcher; +import forestry.core.inventory.watchers.FakeSlotPickupWatcher; +import forestry.core.inventory.watchers.ISlotChangeWatcher; +import forestry.core.inventory.watchers.ISlotPickupWatcher; /** - * Slot with an ICrafter callback. + * Slot with a watcher callbacks. */ -public abstract class SlotWatched extends SlotForestry { - private ICrafter crafter; - private String blockedTexture = "slots/blocked"; +public class SlotWatched extends SlotForestry { + private ISlotPickupWatcher pickupWatcher = FakeSlotPickupWatcher.instance; + private ISlotChangeWatcher changeWatcher = FakeSlotChangeWatcher.instance; - protected SlotWatched(IInventory inventory, int slotIndex, int xPos, int yPos) { + public SlotWatched(IInventory inventory, int slotIndex, int xPos, int yPos) { super(inventory, slotIndex, xPos, yPos); } - public SlotWatched setCrafter(ICrafter crafter) { - this.crafter = crafter; + public SlotWatched setPickupWatcher(ISlotPickupWatcher pickupWatcher) { + this.pickupWatcher = pickupWatcher; + return this; + } + + public SlotWatched setChangeWatcher(ISlotChangeWatcher changeWatcher) { + this.changeWatcher = changeWatcher; return this; } @Override public void onPickupFromSlot(EntityPlayer player, ItemStack itemStack) { - if (crafter != null) { - crafter.takenFromSlot(getSlotIndex(), player); - } + super.onPickupFromSlot(player, itemStack); + pickupWatcher.onPickupFromSlot(getSlotIndex(), player); } - public SlotWatched setBlockedTexture(String ident) { - blockedTexture = ident; - return this; - } - - @SideOnly(Side.CLIENT) @Override - public IIcon getBackgroundIconIndex() { - ItemStack stack = getStack(); - if (!isItemValid(stack)) { - return TextureManager.getInstance().getDefault(blockedTexture); - } else { - return null; - } + public void onSlotChanged() { + super.onSlotChanged(); + changeWatcher.onSlotChanged(inventory, getSlotIndex()); } } diff --git a/src/main/java/forestry/core/gui/slots/SlotCraftAuto.java b/src/main/java/forestry/core/inventory/watchers/FakeSlotChangeWatcher.java old mode 100755 new mode 100644 similarity index 51% rename from src/main/java/forestry/core/gui/slots/SlotCraftAuto.java rename to src/main/java/forestry/core/inventory/watchers/FakeSlotChangeWatcher.java index d1ff76e4e..ee67e5b9d --- a/src/main/java/forestry/core/gui/slots/SlotCraftAuto.java +++ b/src/main/java/forestry/core/inventory/watchers/FakeSlotChangeWatcher.java @@ -1,38 +1,26 @@ -/******************************************************************************* - * 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.core.gui.slots; - -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.Slot; - -import forestry.core.gui.IContainerCrafting; - -/** - * Informs the passed container of slot changes. - */ -public class SlotCraftAuto extends Slot { - - private final IContainerCrafting eventHandler; - private final int slot; - - public SlotCraftAuto(IContainerCrafting container, IInventory iinventory, int slotNumber, int x, int y) { - super(iinventory, slotNumber, x, y); - this.eventHandler = container; - this.slot = slotNumber; - } - - @Override - public void onSlotChanged() { - super.onSlotChanged(); - eventHandler.onCraftMatrixChanged(inventory, slot); - } - -} +/******************************************************************************* + * 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.core.inventory.watchers; + +import net.minecraft.inventory.IInventory; + +public class FakeSlotChangeWatcher implements ISlotChangeWatcher { + public static final FakeSlotChangeWatcher instance = new FakeSlotChangeWatcher(); + + private FakeSlotChangeWatcher() { + + } + + @Override + public void onSlotChanged(IInventory inventory, int slot) { + + } +} diff --git a/src/main/java/forestry/core/inventory/watchers/FakeSlotPickupWatcher.java b/src/main/java/forestry/core/inventory/watchers/FakeSlotPickupWatcher.java new file mode 100644 index 000000000..aea0999ce --- /dev/null +++ b/src/main/java/forestry/core/inventory/watchers/FakeSlotPickupWatcher.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * 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.core.inventory.watchers; + +import net.minecraft.entity.player.EntityPlayer; + +public class FakeSlotPickupWatcher implements ISlotPickupWatcher { + + public static final FakeSlotPickupWatcher instance = new FakeSlotPickupWatcher(); + + private FakeSlotPickupWatcher() { + + } + + @Override + public void onPickupFromSlot(int slotIndex, EntityPlayer player) { + + } +} diff --git a/src/main/java/forestry/core/inventory/watchers/ISlotChangeWatcher.java b/src/main/java/forestry/core/inventory/watchers/ISlotChangeWatcher.java new file mode 100644 index 000000000..922202be3 --- /dev/null +++ b/src/main/java/forestry/core/inventory/watchers/ISlotChangeWatcher.java @@ -0,0 +1,17 @@ +/******************************************************************************* + * 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.core.inventory.watchers; + +import net.minecraft.inventory.IInventory; + +public interface ISlotChangeWatcher { + void onSlotChanged(IInventory inventory, int slot); +} diff --git a/src/main/java/forestry/core/tiles/ICrafter.java b/src/main/java/forestry/core/inventory/watchers/ISlotPickupWatcher.java similarity index 80% rename from src/main/java/forestry/core/tiles/ICrafter.java rename to src/main/java/forestry/core/inventory/watchers/ISlotPickupWatcher.java index 19aecb602..c8d8650f9 100644 --- a/src/main/java/forestry/core/tiles/ICrafter.java +++ b/src/main/java/forestry/core/inventory/watchers/ISlotPickupWatcher.java @@ -8,11 +8,10 @@ * Various Contributors including, but not limited to: * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges ******************************************************************************/ -package forestry.core.tiles; +package forestry.core.inventory.watchers; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -public interface ICrafter { - ItemStack takenFromSlot(int slotIndex, EntityPlayer player); +public interface ISlotPickupWatcher { + void onPickupFromSlot(int slotIndex, EntityPlayer player); } diff --git a/src/main/java/forestry/core/network/DataInputStreamForestry.java b/src/main/java/forestry/core/network/DataInputStreamForestry.java index f0fb0a2ec..9dad78abb 100644 --- a/src/main/java/forestry/core/network/DataInputStreamForestry.java +++ b/src/main/java/forestry/core/network/DataInputStreamForestry.java @@ -5,6 +5,7 @@ import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InvalidObjectException; +import java.util.Collection; import java.util.List; import net.minecraft.inventory.IInventory; @@ -19,8 +20,6 @@ import net.minecraftforge.fluids.FluidStack; import cpw.mods.fml.common.registry.GameData; -import forestry.core.utils.Log; - public class DataInputStreamForestry extends DataInputStream { public DataInputStreamForestry(InputStream in) { @@ -56,6 +55,15 @@ public class DataInputStreamForestry extends DataInputStream { return itemStacks; } + public void readItemStacks(Collection itemStacks) throws IOException { + itemStacks.clear(); + + int stackCount = readVarInt(); + for (int i = 0; i < stackCount; i++) { + itemStacks.add(readItemStack()); + } + } + public void readInventory(IInventory inventory) throws IOException { int size = readVarInt(); @@ -72,7 +80,7 @@ public class DataInputStreamForestry extends DataInputStream { streamable.readData(this); return streamable; } catch (ReflectiveOperationException e) { - Log.severe("Failed to read Streamable for class " + streamableClass + " with error " + e); + throw new InvalidObjectException("Failed to read Streamable for class " + streamableClass + " with error " + e); } } return null; diff --git a/src/main/java/forestry/core/network/DataOutputStreamForestry.java b/src/main/java/forestry/core/network/DataOutputStreamForestry.java index 0b55ed927..9caff858d 100644 --- a/src/main/java/forestry/core/network/DataOutputStreamForestry.java +++ b/src/main/java/forestry/core/network/DataOutputStreamForestry.java @@ -3,6 +3,7 @@ package forestry.core.network; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Collection; import java.util.List; import net.minecraft.inventory.IInventory; @@ -41,6 +42,13 @@ public class DataOutputStreamForestry extends DataOutputStream { } } + public void writeItemStacks(Collection itemStacks) throws IOException { + writeVarInt(itemStacks.size()); + for (ItemStack itemstack : itemStacks) { + writeItemStack(itemstack); + } + } + public void writeInventory(IInventory inventory) throws IOException { int size = inventory.getSizeInventory(); writeVarInt(size); diff --git a/src/main/java/forestry/core/network/PacketGuiSelect.java b/src/main/java/forestry/core/network/PacketGuiSelect.java index f72a47e32..28eeaace4 100644 --- a/src/main/java/forestry/core/network/PacketGuiSelect.java +++ b/src/main/java/forestry/core/network/PacketGuiSelect.java @@ -28,15 +28,15 @@ public abstract class PacketGuiSelect extends ForestryPacket { @Override public void writeData(DataOutputStreamForestry data) throws IOException { super.writeData(data); - data.writeInt(primaryIndex); - data.writeInt(secondaryIndex); + data.writeVarInt(primaryIndex); + data.writeVarInt(secondaryIndex); } @Override public void readData(DataInputStreamForestry data) throws IOException { super.readData(data); - primaryIndex = data.readInt(); - secondaryIndex = data.readInt(); + primaryIndex = data.readVarInt(); + secondaryIndex = data.readVarInt(); } public int getPrimaryIndex() { diff --git a/src/main/java/forestry/core/network/PacketIdClient.java b/src/main/java/forestry/core/network/PacketIdClient.java index 99eccf599..c46f20529 100644 --- a/src/main/java/forestry/core/network/PacketIdClient.java +++ b/src/main/java/forestry/core/network/PacketIdClient.java @@ -37,6 +37,7 @@ public enum PacketIdClient implements IPacketId { // Factory WORKTABLE_MEMORY_UPDATE, + WORKTABLE_CRAFTING_UPDATE, // Apiculture TILE_FORESTRY_ACTIVE, diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index 4e2f35a5b..590794bba 100644 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -34,9 +34,9 @@ import forestry.api.recipes.RecipeManagers; import forestry.core.config.ForestryBlock; import forestry.core.config.ForestryItem; import forestry.core.fluids.Fluids; -import forestry.core.gui.ContainerDummy; import forestry.core.utils.ItemStackUtil; import forestry.core.utils.Log; +import forestry.factory.inventory.InventoryCraftingForestry; public abstract class RecipeUtil { @@ -97,10 +97,10 @@ public abstract class RecipeUtil { return result; } - public static boolean canCraftRecipe(World world, ItemStack[] recipeItems, ItemStack recipeOutput, ItemStack[] availableItems) { + public static InventoryCraftingForestry getCraftRecipe(ItemStack[] recipeItems, ItemStack[] availableItems, World world, ItemStack recipeOutput) { // Need at least one matched set if (ItemStackUtil.containsSets(recipeItems, availableItems, true, true) == 0) { - return false; + return null; } // Check that it doesn't make a different recipe. @@ -112,7 +112,7 @@ public abstract class RecipeUtil { // 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. - InventoryCrafting crafting = new InventoryCrafting(ContainerDummy.instance, 3, 3); + InventoryCraftingForestry crafting = new InventoryCraftingForestry(); ItemStack[] stockCopy = ItemStackUtil.condenseStacks(availableItems); for (int slot = 0; slot < recipeItems.length; slot++) { @@ -143,9 +143,12 @@ public abstract class RecipeUtil { } } } - ItemStack output = CraftingManager.getInstance().findMatchingRecipe(crafting, world); - return ItemStack.areItemStacksEqual(output, recipeOutput); + List outputs = findMatchingRecipes(crafting, world); + if (!ItemStackUtil.containsItemStack(outputs, recipeOutput)) { + return null; + } + return crafting; } public static List findMatchingRecipes(InventoryCrafting inventory, World world) { @@ -161,7 +164,9 @@ public abstract class RecipeUtil { if (irecipe.matches(inventory, world)) { ItemStack result = irecipe.getCraftingResult(inventory); - matchingRecipes.add(result); + if (!ItemStackUtil.containsItemStack(matchingRecipes, result)) { + matchingRecipes.add(result); + } } } diff --git a/src/main/java/forestry/core/tiles/TileEscritoire.java b/src/main/java/forestry/core/tiles/TileEscritoire.java index 6499bf421..862f4b5b0 100644 --- a/src/main/java/forestry/core/tiles/TileEscritoire.java +++ b/src/main/java/forestry/core/tiles/TileEscritoire.java @@ -23,13 +23,14 @@ import forestry.api.genetics.AlleleManager; import forestry.api.genetics.IAlleleSpecies; import forestry.api.genetics.IIndividual; import forestry.core.inventory.InventoryEscritoire; +import forestry.core.inventory.watchers.ISlotPickupWatcher; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.GuiId; import forestry.core.network.IStreamableGui; import forestry.core.utils.InventoryUtil; -public class TileEscritoire extends TileBase implements ISidedInventory, ICrafter, IStreamableGui { +public class TileEscritoire extends TileBase implements ISidedInventory, ISlotPickupWatcher, IStreamableGui { private final EscritoireGame game = new EscritoireGame(); @@ -112,23 +113,11 @@ public class TileEscritoire extends TileBase implements ISidedInventory, ICrafte game.readData(data); } - // @Override - // public TankRenderInfo getResourceTankInfo() { - // return TankRenderInfo.EMPTY; - // } - // - // @Override - // public TankRenderInfo getProductTankInfo() { - // return TankRenderInfo.EMPTY; - // } - - /* ICrafter */ + /* ISlotPickupWatcher */ @Override - public ItemStack takenFromSlot(int slotIndex, EntityPlayer player) { + public void onPickupFromSlot(int slotIndex, EntityPlayer player) { if (slotIndex == InventoryEscritoire.SLOT_ANALYZE) { game.reset(); } - return null; } - } diff --git a/src/main/java/forestry/core/utils/ItemStackUtil.java b/src/main/java/forestry/core/utils/ItemStackUtil.java index 17ea6848d..1f5830eb2 100644 --- a/src/main/java/forestry/core/utils/ItemStackUtil.java +++ b/src/main/java/forestry/core/utils/ItemStackUtil.java @@ -33,6 +33,10 @@ public abstract class ItemStackUtil { * Compares item id, damage and NBT. Accepts wildcard damage. */ public static boolean isIdenticalItem(ItemStack lhs, ItemStack rhs) { + if (lhs == rhs) { + return true; + } + if (lhs == null || rhs == null) { return false; } @@ -164,6 +168,28 @@ public abstract class ItemStackUtil { return totalSets; } + public static boolean equalSets(ItemStack[] set1, ItemStack[] set2) { + if (set1 == set2) { + return true; + } + + if (set1 == null || set2 == null) { + return false; + } + + if (set1.length != set2.length) { + return false; + } + + for (int i = 0; i < set1.length; i++) { + if (!isIdenticalItem(set1[i], set2[i])) { + return false; + } + } + + return true; + } + /** * Compare two item stacks for crafting equivalency without oreDictionary or craftingTools */ diff --git a/src/main/java/forestry/core/utils/SlotUtil.java b/src/main/java/forestry/core/utils/SlotUtil.java index b7360148e..cdc9ea3f4 100644 --- a/src/main/java/forestry/core/utils/SlotUtil.java +++ b/src/main/java/forestry/core/utils/SlotUtil.java @@ -15,6 +15,7 @@ import java.util.List; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Slot; +import net.minecraft.inventory.SlotCrafting; import net.minecraft.item.ItemStack; import forestry.core.gui.slots.SlotForestry; @@ -25,7 +26,7 @@ public abstract class SlotUtil { return (slotIndex >= start) && (slotIndex < start + count); } - public static ItemStack slotClickPhantom(Slot slot, int mouseButton, int modifier, EntityPlayer player) { + public static ItemStack slotClickPhantom(SlotForestry slot, int mouseButton, int modifier, EntityPlayer player) { ItemStack stack = null; ItemStack stackSlot = slot.getStack(); @@ -69,11 +70,13 @@ public abstract class SlotUtil { return null; } + boolean fromCraftingSlot = (slot instanceof SlotCrafting); + int numSlots = inventorySlots.size(); ItemStack stackInSlot = slot.getStack(); ItemStack originalStack = stackInSlot.copy(); - if (!shiftItemStack(inventorySlots, stackInSlot, slotIndex, numSlots)) { + if (!shiftItemStack(inventorySlots, stackInSlot, slotIndex, numSlots, fromCraftingSlot)) { return null; } @@ -92,7 +95,7 @@ public abstract class SlotUtil { return originalStack; } - private static boolean shiftItemStack(List inventorySlots, ItemStack stackInSlot, int slotIndex, int numSlots) { + private static boolean shiftItemStack(List inventorySlots, ItemStack stackInSlot, int slotIndex, int numSlots, boolean fromCraftingSlot) { if (isInPlayerInventory(slotIndex)) { if (shiftToMachineInventory(inventorySlots, stackInSlot, numSlots)) { return true; @@ -104,12 +107,17 @@ public abstract class SlotUtil { return shiftToHotbar(inventorySlots, stackInSlot); } } else { + if (fromCraftingSlot) { + if (shiftToMachineInventory(inventorySlots, stackInSlot, numSlots)) { + return true; + } + } return shiftToPlayerInventory(inventorySlots, stackInSlot); } } - private static void adjustPhantomSlot(Slot slot, int mouseButton, int modifier) { - if (!((SlotForestry) slot).canAdjustPhantom()) { + private static void adjustPhantomSlot(SlotForestry slot, int mouseButton, int modifier) { + if (!slot.canAdjustPhantom()) { return; } ItemStack stackSlot = slot.getStack(); @@ -133,8 +141,8 @@ public abstract class SlotUtil { slot.putStack(stackSlot); } - private static void fillPhantomSlot(Slot slot, ItemStack stackHeld, int mouseButton) { - if (!((SlotForestry) slot).canAdjustPhantom()) { + private static void fillPhantomSlot(SlotForestry slot, ItemStack stackHeld, int mouseButton) { + if (!slot.canAdjustPhantom()) { return; } diff --git a/src/main/java/forestry/factory/gui/ContainerMoistener.java b/src/main/java/forestry/factory/gui/ContainerMoistener.java index 8dc729762..ea9de6197 100755 --- a/src/main/java/forestry/factory/gui/ContainerMoistener.java +++ b/src/main/java/forestry/factory/gui/ContainerMoistener.java @@ -15,13 +15,13 @@ import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.IInventory; import forestry.core.gui.ContainerLiquidTanks; -import forestry.core.gui.IContainerCrafting; -import forestry.core.gui.slots.SlotCraftAuto; import forestry.core.gui.slots.SlotFiltered; +import forestry.core.gui.slots.SlotWatched; import forestry.core.gui.slots.SlotWorking; +import forestry.core.inventory.watchers.ISlotChangeWatcher; import forestry.factory.tiles.TileMoistener; -public class ContainerMoistener extends ContainerLiquidTanks implements IContainerCrafting { +public class ContainerMoistener extends ContainerLiquidTanks implements ISlotChangeWatcher { public ContainerMoistener(InventoryPlayer player, TileMoistener tile) { super(tile, player, 8, 84); @@ -43,12 +43,12 @@ public class ContainerMoistener extends ContainerLiquidTanks impl // Product slot this.addSlotToContainer(new SlotFiltered(tile, 10, 143, 55)); // Boxes - this.addSlotToContainer(new SlotCraftAuto(this, tile, 11, 143, 19)); + this.addSlotToContainer(new SlotWatched(tile, 11, 143, 19).setChangeWatcher(this)); } @Override - public void onCraftMatrixChanged(IInventory iinventory, int slot) { - tile.setInventorySlotContents(slot, iinventory.getStackInSlot(slot)); + public void onSlotChanged(IInventory inventory, int slot) { + tile.setInventorySlotContents(slot, inventory.getStackInSlot(slot)); tile.checkRecipe(); } diff --git a/src/main/java/forestry/factory/gui/ContainerWorktable.java b/src/main/java/forestry/factory/gui/ContainerWorktable.java index 651298e75..be73a9831 100755 --- a/src/main/java/forestry/factory/gui/ContainerWorktable.java +++ b/src/main/java/forestry/factory/gui/ContainerWorktable.java @@ -24,16 +24,18 @@ import forestry.core.gui.slots.SlotCrafter; import forestry.core.network.PacketGuiSelectRequest; import forestry.core.proxy.Proxies; import forestry.core.utils.ItemStackUtil; +import forestry.factory.inventory.InventoryCraftingForestry; import forestry.factory.inventory.InventoryGhostCrafting; import forestry.factory.inventory.InventoryWorktable; -import forestry.factory.inventory.InventoryWorktableCrafting; import forestry.factory.network.PacketWorktableMemoryUpdate; +import forestry.factory.network.PacketWorktableRecipeUpdate; +import forestry.factory.recipes.RecipeMemory; import forestry.factory.tiles.TileWorktable; public class ContainerWorktable extends ContainerTile implements IContainerCrafting, IGuiSelectable { - - private final InventoryWorktableCrafting craftMatrix = new InventoryWorktableCrafting(this); - private long lastUpdate; + private final InventoryCraftingForestry craftMatrix = new InventoryCraftingForestry(this); + private long lastMemoryUpdate; + private boolean craftMatrixChanged = false; public ContainerWorktable(EntityPlayer player, TileWorktable tile) { super(tile, player.inventory, 8, 136); @@ -58,28 +60,36 @@ public class ContainerWorktable extends ContainerTile implements // CraftResult display addSlotToContainer(new SlotCrafter(player, craftingDisplay, tile, InventoryGhostCrafting.SLOT_CRAFTING_RESULT, 77, 38)); - // Update crafting matrix with current contents of tileentity. - updateCraftMatrix(); + for (int i = 0; i < craftMatrix.getSizeInventory(); i++) { + onCraftMatrixChanged(tile.getCraftingDisplay(), i); + } } @Override public void detectAndSendChanges() { - super.detectAndSendChanges(); - if (lastUpdate == tile.getMemory().getLastUpdate()) { - return; + if (craftMatrixChanged) { + craftMatrixChanged = false; + tile.setCurrentRecipe(craftMatrix); + sendPacketToCrafters(new PacketWorktableRecipeUpdate(tile)); } - lastUpdate = tile.getMemory().getLastUpdate(); + super.detectAndSendChanges(); - PacketWorktableMemoryUpdate packet = new PacketWorktableMemoryUpdate(tile); - sendPacketToCrafters(packet); + if (lastMemoryUpdate != tile.getMemory().getLastUpdate()) { + lastMemoryUpdate = tile.getMemory().getLastUpdate(); + sendPacketToCrafters(new PacketWorktableMemoryUpdate(tile)); + } } - @Override - public void onCraftMatrixChanged(IInventory iinventory) { - updateRecipe(); + private void updateCraftMatrix() { + IInventory crafting = tile.getCraftingDisplay(); + for (int i = 0; i < crafting.getSizeInventory(); i++) { + onCraftMatrixChanged(crafting, i); + } } + // Fired when SlotCraftMatrix detects a change. + // Direct changes to the underlying inventory are not detected, only slot changes. @Override public void onCraftMatrixChanged(IInventory iinventory, int slot) { if (slot >= craftMatrix.getSizeInventory()) { @@ -94,18 +104,15 @@ public class ContainerWorktable extends ContainerTile implements } } - private void updateCraftMatrix() { - for (int i = 0; i < craftMatrix.getSizeInventory(); i++) { - craftMatrix.setInventorySlotContents(i, tile.getCraftingDisplay().getStackInSlot(i)); - } - } - - private void updateRecipe() { - tile.setRecipe(craftMatrix); + // Fired when this container's craftMatrix detects a change + @Override + public void onCraftMatrixChanged(IInventory iinventory) { + craftMatrixChanged = true; } + /* Gui Selection Handling */ public static void clearRecipe() { - sendRecipeClick(-1, -1); + sendRecipeClick(-1, 0); } public static void sendRecipeClick(int mouseButton, int recipeIndex) { @@ -114,17 +121,38 @@ public class ContainerWorktable extends ContainerTile implements @Override public void handleSelectionRequest(EntityPlayerMP player, PacketGuiSelectRequest packet) { - int mouseButton = packet.getPrimaryIndex(); + int primary = packet.getPrimaryIndex(); int secondary = packet.getSecondaryIndex(); - if (mouseButton < 0) { - tile.clearCraftMatrix(); - } else if (mouseButton == 0) { - tile.chooseRecipe(secondary); - updateCraftMatrix(); - updateRecipe(); - } else { - tile.getMemory().toggleLock(player.worldObj.getTotalWorldTime(), secondary); + switch (primary) { + case -1: { // clicked clear button + tile.clearCraftMatrix(); + updateCraftMatrix(); + sendPacketToCrafters(new PacketWorktableRecipeUpdate(tile)); + break; + } + case 0: { // clicked a memorized recipe + tile.chooseRecipeMemory(secondary); + updateCraftMatrix(); + sendPacketToCrafters(new PacketWorktableRecipeUpdate(tile)); + break; + } + case 1: { // right clicked a memorized recipe + long time = player.worldObj.getTotalWorldTime(); + RecipeMemory memory = tile.getMemory(); + memory.toggleLock(time, secondary); + break; + } + case 100: { // clicked previous recipe conflict button + tile.choosePreviousConflictRecipe(); + sendPacketToCrafters(new PacketWorktableRecipeUpdate(tile)); + break; + } + case 101: { // clicked next recipe conflict button + tile.chooseNextConflictRecipe(); + sendPacketToCrafters(new PacketWorktableRecipeUpdate(tile)); + break; + } } } } diff --git a/src/main/java/forestry/factory/gui/GuiWorktable.java b/src/main/java/forestry/factory/gui/GuiWorktable.java index 87fc90b7c..f90e77b69 100755 --- a/src/main/java/forestry/factory/gui/GuiWorktable.java +++ b/src/main/java/forestry/factory/gui/GuiWorktable.java @@ -10,10 +10,15 @@ ******************************************************************************/ package forestry.factory.gui; +import net.minecraft.client.gui.GuiButton; import net.minecraft.entity.player.EntityPlayer; import forestry.core.config.Constants; import forestry.core.gui.GuiForestryTitled; +import forestry.core.gui.buttons.GuiBetterButton; +import forestry.core.gui.buttons.StandardButtonTextureSets; +import forestry.core.network.PacketGuiSelectRequest; +import forestry.core.proxy.Proxies; import forestry.factory.gui.widgets.ClearWorktable; import forestry.factory.gui.widgets.MemorizedRecipeSlot; import forestry.factory.recipes.RecipeMemory; @@ -21,6 +26,7 @@ import forestry.factory.tiles.TileWorktable; public class GuiWorktable extends GuiForestryTitled { private static final int SPACING = 18; + private boolean hasRecipeConflict = false; public GuiWorktable(EntityPlayer player, TileWorktable tile) { super(Constants.TEXTURE_PATH_GUI + "/worktable2.png", new ContainerWorktable(player, tile), tile); @@ -41,4 +47,30 @@ public class GuiWorktable extends GuiForestryTitled recipeOutputs = new ArrayList<>(); + private int selectedRecipe; private long lastUsed; private boolean locked; @@ -34,28 +38,62 @@ public final class MemorizedRecipe implements INBTTagable, IStreamable { // required for IStreamable serialization } - public MemorizedRecipe(InventoryWorktableCrafting craftMatrix, ItemStack recipeOutput) { + public MemorizedRecipe(InventoryCraftingForestry craftMatrix, List recipeOutputs) { InventoryUtil.deepCopyInventoryContents(craftMatrix, this.craftMatrix); - this.recipeOutput = recipeOutput; + this.recipeOutputs.addAll(recipeOutputs); } - public InventoryWorktableCrafting getCraftMatrix() { + public InventoryCraftingForestry getCraftMatrix() { return craftMatrix; } - /** - * Calculate the recipe output. - * For older memorized recipes that were not created with an output. - * @since Forestry 4.2 - */ - public void updateRecipeOutputLegacy(World world) { - if (recipeOutput == null) { - recipeOutput = CraftingManager.getInstance().findMatchingRecipe(craftMatrix, world); + public void calculateRecipeOutput(World world) { + recipeOutputs.clear(); + List matching = RecipeUtil.findMatchingRecipes(craftMatrix, world); + recipeOutputs.addAll(matching); + if (selectedRecipe >= recipeOutputs.size()) { + selectedRecipe = 0; + } + if (hasRecipeConflict()) { + removeRecipeConflicts(); } } + public void incrementRecipe() { + selectedRecipe++; + if (selectedRecipe >= recipeOutputs.size()) { + selectedRecipe = 0; + } + } + + public void decrementRecipe() { + selectedRecipe--; + if (selectedRecipe < 0) { + selectedRecipe = recipeOutputs.size() - 1; + } + } + + public boolean hasRecipeConflict() { + return recipeOutputs.size() > 1; + } + + public void removeRecipeConflicts() { + ItemStack recipeOutput = getRecipeOutput(); + recipeOutputs.clear(); + recipeOutputs.add(recipeOutput); + selectedRecipe = 0; + } + public ItemStack getRecipeOutput() { - return recipeOutput; + if (recipeOutputs.size() == 0) { + return null; + } else { + return recipeOutputs.get(selectedRecipe); + } + } + + public boolean hasRecipeOutput(ItemStack output) { + return ItemStackUtil.containsItemStack(recipeOutputs, output); } public void updateLastUse(long lastUsed) { @@ -81,9 +119,8 @@ public final class MemorizedRecipe implements INBTTagable, IStreamable { lastUsed = nbttagcompound.getLong("LastUsed"); locked = nbttagcompound.getBoolean("Locked"); - if (nbttagcompound.hasKey("Output")) { - NBTTagCompound recipeOutputNbt = nbttagcompound.getCompoundTag("Output"); - recipeOutput = ItemStack.loadItemStackFromNBT(recipeOutputNbt); + if (nbttagcompound.hasKey("SelectedRecipe")) { + selectedRecipe = nbttagcompound.getInteger("SelectedRecipe"); } } @@ -92,26 +129,23 @@ public final class MemorizedRecipe implements INBTTagable, IStreamable { InventoryUtil.writeToNBT(craftMatrix, nbttagcompound); nbttagcompound.setLong("LastUsed", lastUsed); nbttagcompound.setBoolean("Locked", locked); - - NBTTagCompound recipeOutputNbt = new NBTTagCompound(); - recipeOutput.writeToNBT(recipeOutputNbt); - nbttagcompound.setTag("Output", recipeOutputNbt); + nbttagcompound.setInteger("SelectedRecipe", selectedRecipe); } /* IStreamable */ @Override public void writeData(DataOutputStreamForestry data) throws IOException { data.writeInventory(craftMatrix); - data.writeItemStack(recipeOutput); - data.writeLong(lastUsed); data.writeBoolean(locked); + data.writeItemStacks(recipeOutputs); + data.writeVarInt(selectedRecipe); } @Override public void readData(DataInputStreamForestry data) throws IOException { data.readInventory(craftMatrix); - recipeOutput = data.readItemStack(); - lastUsed = data.readLong(); locked = data.readBoolean(); + data.readItemStacks(recipeOutputs); + selectedRecipe = data.readVarInt(); } } diff --git a/src/main/java/forestry/factory/recipes/RecipeMemory.java b/src/main/java/forestry/factory/recipes/RecipeMemory.java index 54949f2c7..d8c267909 100644 --- a/src/main/java/forestry/factory/recipes/RecipeMemory.java +++ b/src/main/java/forestry/factory/recipes/RecipeMemory.java @@ -15,7 +15,6 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; -import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemMap; import net.minecraft.item.ItemStack; @@ -57,7 +56,7 @@ public class RecipeMemory implements INBTTagable, IStreamable { LinkedList validRecipes = new LinkedList<>(); for (MemorizedRecipe recipe : memorizedRecipes) { if (recipe != null) { - recipe.updateRecipeOutputLegacy(world); + recipe.calculateRecipeOutput(world); if (isValid(recipe)) { validRecipes.add(recipe); } @@ -78,6 +77,10 @@ public class RecipeMemory implements INBTTagable, IStreamable { lastUpdate = worldTime; recipe.updateLastUse(lastUpdate); + if (recipe.hasRecipeConflict()) { + recipe.removeRecipeConflicts(); + } + // update existing matching recipes MemorizedRecipe memory = getExistingMemorizedRecipe(recipe.getRecipeOutput()); if (memory != null) { @@ -119,23 +122,14 @@ public class RecipeMemory implements INBTTagable, IStreamable { return oldest; } - private MemorizedRecipe getRecipe(int recipeIndex) { + public MemorizedRecipe getRecipe(int recipeIndex) { if (recipeIndex < 0 || recipeIndex >= memorizedRecipes.size()) { return null; } return memorizedRecipes.get(recipeIndex); - } - public IInventory getRecipeCraftMatrix(int recipeIndex) { - MemorizedRecipe recipe = getRecipe(recipeIndex); - if (recipe == null) { - return null; - } - return recipe.getCraftMatrix(); - } - - public ItemStack getRecipeOutput(int recipeIndex) { + public ItemStack getRecipeDisplayOutput(int recipeIndex) { MemorizedRecipe recipe = getRecipe(recipeIndex); if (recipe == null) { return null; @@ -160,8 +154,7 @@ public class RecipeMemory implements INBTTagable, IStreamable { private MemorizedRecipe getExistingMemorizedRecipe(ItemStack craftingRecipeOutput) { for (MemorizedRecipe memorizedRecipe : memorizedRecipes) { - ItemStack memorizedRecipeOutput = memorizedRecipe.getRecipeOutput(); - if (ItemStack.areItemStacksEqual(memorizedRecipeOutput, craftingRecipeOutput)) { + if (memorizedRecipe.hasRecipeOutput(craftingRecipeOutput)) { return memorizedRecipe; } } diff --git a/src/main/java/forestry/factory/tiles/TileFabricator.java b/src/main/java/forestry/factory/tiles/TileFabricator.java index 4c827b688..93c7df9f9 100644 --- a/src/main/java/forestry/factory/tiles/TileFabricator.java +++ b/src/main/java/forestry/factory/tiles/TileFabricator.java @@ -39,12 +39,12 @@ import forestry.core.fluids.tanks.StandardTank; import forestry.core.inventory.IInventoryAdapter; import forestry.core.inventory.InventoryAdapter; import forestry.core.inventory.InventoryAdapterTile; +import forestry.core.inventory.watchers.ISlotPickupWatcher; import forestry.core.inventory.wrappers.InventoryMapper; import forestry.core.items.ICraftingPlan; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.GuiId; -import forestry.core.tiles.ICrafter; import forestry.core.tiles.ILiquidTankTile; import forestry.core.tiles.TilePowered; import forestry.core.utils.InventoryUtil; @@ -53,7 +53,7 @@ import forestry.factory.inventory.InventoryGhostCrafting; import forestry.factory.recipes.FabricatorRecipeManager; import forestry.factory.recipes.FabricatorSmeltingRecipeManager; -public class TileFabricator extends TilePowered implements ICrafter, ILiquidTankTile, IFluidHandler, ISidedInventory { +public class TileFabricator extends TilePowered implements ISlotPickupWatcher, ILiquidTankTile, IFluidHandler, ISidedInventory { private static final int MAX_HEAT = 5000; private final InventoryAdapterTile craftingInventory; @@ -193,14 +193,12 @@ public class TileFabricator extends TilePowered implements ICrafter, ILiquidTank return myRecipe.getRecipeOutput().copy(); } - /* ICrafter */ + /* ISlotPickupWatcher */ @Override - public ItemStack takenFromSlot(int slotIndex, EntityPlayer player) { - if (slotIndex != InventoryFabricator.SLOT_RESULT) { - return null; + public void onPickupFromSlot(int slotIndex, EntityPlayer player) { + if (slotIndex == InventoryFabricator.SLOT_RESULT) { + decrStackSize(InventoryFabricator.SLOT_RESULT, 1); } - - return getInternalInventory().decrStackSize(InventoryFabricator.SLOT_RESULT, 1); } private void craftResult() { @@ -214,9 +212,7 @@ public class TileFabricator extends TilePowered implements ICrafter, ILiquidTank return; } - IInventoryAdapter inventory = getInternalInventory(); - - if (inventory.getStackInSlot(InventoryFabricator.SLOT_RESULT) != null) { + if (getStackInSlot(InventoryFabricator.SLOT_RESULT) != null) { return; } @@ -236,14 +232,14 @@ public class TileFabricator extends TilePowered implements ICrafter, ILiquidTank moltenTank.drain(liquid.amount, true); // Damage plan - if (inventory.getStackInSlot(InventoryFabricator.SLOT_PLAN) != null) { - Item planItem = inventory.getStackInSlot(InventoryFabricator.SLOT_PLAN).getItem(); + if (getStackInSlot(InventoryFabricator.SLOT_PLAN) != null) { + Item planItem = getStackInSlot(InventoryFabricator.SLOT_PLAN).getItem(); if (planItem instanceof ICraftingPlan) { - inventory.setInventorySlotContents(InventoryFabricator.SLOT_PLAN, ((ICraftingPlan) planItem).planUsed(inventory.getStackInSlot(InventoryFabricator.SLOT_PLAN), result)); + setInventorySlotContents(InventoryFabricator.SLOT_PLAN, ((ICraftingPlan) planItem).planUsed(getStackInSlot(InventoryFabricator.SLOT_PLAN), result)); } } - inventory.setInventorySlotContents(InventoryFabricator.SLOT_RESULT, result); + setInventorySlotContents(InventoryFabricator.SLOT_RESULT, result); } private boolean removeFromInventory(ItemStack[] set, EntityPlayer player, boolean doRemove) { diff --git a/src/main/java/forestry/factory/tiles/TileWorktable.java b/src/main/java/forestry/factory/tiles/TileWorktable.java index c1ce499bd..04ae6d4b6 100644 --- a/src/main/java/forestry/factory/tiles/TileWorktable.java +++ b/src/main/java/forestry/factory/tiles/TileWorktable.java @@ -11,11 +11,11 @@ package forestry.factory.tiles; import java.io.IOException; +import java.util.List; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.CraftingManager; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.MinecraftForge; @@ -30,9 +30,9 @@ import forestry.core.recipes.RecipeUtil; import forestry.core.tiles.TileBase; import forestry.core.utils.InventoryUtil; import forestry.core.utils.ItemStackUtil; +import forestry.factory.inventory.InventoryCraftingForestry; import forestry.factory.inventory.InventoryGhostCrafting; import forestry.factory.inventory.InventoryWorktable; -import forestry.factory.inventory.InventoryWorktableCrafting; import forestry.factory.recipes.MemorizedRecipe; import forestry.factory.recipes.RecipeMemory; @@ -89,49 +89,30 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { recipeMemory.validate(worldObj); } - /* RECIPE SELECTION */ - public RecipeMemory getMemory() { - return recipeMemory; + /* Recipe Conflicts */ + public boolean hasRecipeConflict() { + return currentRecipe != null && currentRecipe.hasRecipeConflict(); } - public void clearCraftMatrix() { - for (int slot = 0; slot < craftingDisplay.getSizeInventory(); slot++) { - craftingDisplay.setInventorySlotContents(slot, null); - } - } - - public void chooseRecipe(int recipeIndex) { - IInventory craftMatrix = recipeMemory.getRecipeCraftMatrix(recipeIndex); - if (craftMatrix == null) { - return; - } - - for (int slot = 0; slot < craftMatrix.getSizeInventory(); slot++) { - craftingDisplay.setInventorySlotContents(slot, craftMatrix.getStackInSlot(slot)); - } - } - - /* CRAFTING */ - public void setRecipe(InventoryWorktableCrafting crafting) { - ItemStack recipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, worldObj); - if (recipeOutput == null) { - currentRecipe = null; - } else { - currentRecipe = new MemorizedRecipe(crafting, recipeOutput); - } - updateCraftResult(); - } - - private void updateCraftResult() { + public void chooseNextConflictRecipe() { if (currentRecipe != null) { - ItemStack result = currentRecipe.getRecipeOutput(); - if (result != null) { - craftingDisplay.setInventorySlotContents(InventoryGhostCrafting.SLOT_CRAFTING_RESULT, result.copy()); - return; - } + currentRecipe.incrementRecipe(); } + } - craftingDisplay.setInventorySlotContents(InventoryGhostCrafting.SLOT_CRAFTING_RESULT, null); + public void choosePreviousConflictRecipe() { + if (currentRecipe != null) { + currentRecipe.decrementRecipe(); + } + } + + /* ICrafterWorktable */ + @Override + public boolean canTakeStack(int craftingSlotIndex) { + if (craftingSlotIndex == InventoryGhostCrafting.SLOT_CRAFTING_RESULT) { + return canCraftCurrentRecipe(); + } + return true; } private boolean canCraftCurrentRecipe() { @@ -139,35 +120,35 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { return false; } - ItemStack[] recipeItems = InventoryUtil.getStacks(craftingDisplay, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT); + ItemStack[] recipeItems = InventoryUtil.getStacks(currentRecipe.getCraftMatrix()); ItemStack[] inventory = InventoryUtil.getStacks(this); - ItemStack recipeOutput = currentRecipe.getRecipeOutput(); - - return RecipeUtil.canCraftRecipe(worldObj, recipeItems, recipeOutput, inventory); - } - - @Override - public boolean canTakeStack(int slotIndex) { - if (slotIndex == InventoryGhostCrafting.SLOT_CRAFTING_RESULT) { - return canCraftCurrentRecipe(); - } - return true; + InventoryCraftingForestry crafting = RecipeUtil.getCraftRecipe(recipeItems, inventory, worldObj, currentRecipe.getRecipeOutput()); + return crafting != null; } @Override public boolean onCraftingStart(EntityPlayer player) { - ItemStack[] set = InventoryUtil.getStacks(currentRecipe.getCraftMatrix()); + if (currentRecipe == null) { + return false; + } - IInventory inventory = new InventoryMapper(this, InventoryWorktable.SLOT_INVENTORY_1, InventoryWorktable.SLOT_INVENTORY_COUNT); - ItemStack[] removed = InventoryUtil.removeSets(inventory, 1, set, player, false, true, true); + ItemStack[] recipeItems = InventoryUtil.getStacks(currentRecipe.getCraftMatrix()); + ItemStack[] inventory = InventoryUtil.getStacks(this); + InventoryCraftingForestry crafting = RecipeUtil.getCraftRecipe(recipeItems, inventory, worldObj, currentRecipe.getRecipeOutput()); + if (crafting == null) { + return false; + } + recipeItems = InventoryUtil.getStacks(crafting); + + // craft recipe should exactly match ingredients here, so no oreDict or tool matching + ItemStack[] removed = InventoryUtil.removeSets(this, 1, recipeItems, player, false, false, false); if (removed == null) { return false; } - for (int i = 0; i < removed.length; i++) { - craftingDisplay.setInventorySlotContents(i, removed[i]); - } + // update crafting display to match the ingredients that were actually used + setCraftingDisplay(crafting); return true; } @@ -208,8 +189,6 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { if (!worldObj.isRemote) { recipeMemory.memorizeRecipe(worldObj.getTotalWorldTime(), currentRecipe); } - - updateCraftResult(); } @Override @@ -225,7 +204,62 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { return result; } + /* Crafting Container methods */ + public RecipeMemory getMemory() { + return recipeMemory; + } + + public void chooseRecipeMemory(int recipeIndex) { + MemorizedRecipe recipe = recipeMemory.getRecipe(recipeIndex); + setCurrentRecipe(recipe); + } + + private void setCraftingDisplay(IInventory craftMatrix) { + if (craftMatrix == null) { + return; + } + + for (int slot = 0; slot < craftMatrix.getSizeInventory(); slot++) { + craftingDisplay.setInventorySlotContents(slot, craftMatrix.getStackInSlot(slot)); + } + } + public IInventory getCraftingDisplay() { return new InventoryMapper(craftingDisplay, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT); } + + public void clearCraftMatrix() { + for (int slot = 0; slot < craftingDisplay.getSizeInventory(); slot++) { + craftingDisplay.setInventorySlotContents(slot, null); + } + } + + public void setCurrentRecipe(InventoryCraftingForestry crafting) { + List recipeOutputs = RecipeUtil.findMatchingRecipes(crafting, worldObj); + MemorizedRecipe recipe = recipeOutputs.size() == 0 ? null : new MemorizedRecipe(crafting, recipeOutputs); + + if (currentRecipe != null && recipe != null) { + if (recipe.hasRecipeOutput(currentRecipe.getRecipeOutput())) { + ItemStack[] stacks = InventoryUtil.getStacks(crafting); + ItemStack[] currentStacks = InventoryUtil.getStacks(currentRecipe.getCraftMatrix()); + if (ItemStackUtil.equalSets(stacks, currentStacks)) { + return; + } + } + } + + setCurrentRecipe(recipe); + } + + /* Network Sync with PacketWorktableRecipeUpdate */ + public MemorizedRecipe getCurrentRecipe() { + return currentRecipe; + } + + public void setCurrentRecipe(MemorizedRecipe recipe) { + this.currentRecipe = recipe; + if (currentRecipe != null) { + setCraftingDisplay(currentRecipe.getCraftMatrix()); + } + } } diff --git a/src/main/java/forestry/plugins/PluginFactory.java b/src/main/java/forestry/plugins/PluginFactory.java index 3a0dde92a..78d5ccb21 100644 --- a/src/main/java/forestry/plugins/PluginFactory.java +++ b/src/main/java/forestry/plugins/PluginFactory.java @@ -59,6 +59,7 @@ import forestry.factory.DummyManagers; import forestry.factory.GuiHandlerFactory; import forestry.factory.circuits.CircuitSpeedUpgrade; import forestry.factory.network.PacketWorktableMemoryUpdate; +import forestry.factory.network.PacketWorktableRecipeUpdate; import forestry.factory.recipes.CarpenterRecipeManager; import forestry.factory.recipes.CentrifugeRecipeManager; import forestry.factory.recipes.FabricatorRecipeManager; @@ -153,6 +154,7 @@ public class PluginFactory extends ForestryPlugin { PacketIdServer.WORKTABLE_NEI_SELECT.setPacketHandler(new PacketWorktableNEISelect()); PacketIdClient.WORKTABLE_MEMORY_UPDATE.setPacketHandler(new PacketWorktableMemoryUpdate()); + PacketIdClient.WORKTABLE_CRAFTING_UPDATE.setPacketHandler(new PacketWorktableRecipeUpdate()); } @Override