Fix serious issues with the worktable

Fix #1677 Fix #1768 Fix #1558 Fix #1519 Fix #1403
This commit is contained in:
mezz committed 2017-07-29 00:52:48 -07:00
1 parent 1034111985
commit f50e7281c5
16 files changed
+331 -308

No files matched your search

+6 -6
View File
@@ -1,6 +1,6 @@
#Mon Sep 14 12:28:28 PDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-bin.zip
#Sat Jul 29 00:23:29 PDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-all.zip
@@ -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 {
@@ -10,6 +10,7 @@
******************************************************************************/
package forestry.core.gui.slots;
import forestry.factory.inventory.InventoryCraftingForestry;
import forestry.factory.tiles.ICrafterWorktable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
@@ -29,7 +30,7 @@ public class SlotCrafter extends Slot {
/**
* The craft matrix inventory linked to this result slot.
*/
private final IInventory craftMatrix;
private final InventoryCraftingForestry craftMatrix;
private final ICrafterWorktable crafter;
/**
@@ -40,9 +41,9 @@ public class SlotCrafter extends Slot {
* The number of items that have been crafted so far. Gets passed to ItemStack.onCrafting before being reset.
*/
private int amountCrafted;
public SlotCrafter(EntityPlayer player, IInventory craftMatrix, ICrafterWorktable crafter, int slot, int xPos, int yPos) {
super(craftMatrix, slot, xPos, yPos);
public SlotCrafter(EntityPlayer player, InventoryCraftingForestry craftMatrix, IInventory craftingDisplay, ICrafterWorktable crafter, int slot, int xPos, int yPos) {
super(craftingDisplay, slot, xPos, yPos);
this.craftMatrix = craftMatrix;
this.crafter = crafter;
this.player = player;
@@ -136,7 +137,7 @@ public class SlotCrafter extends Slot {
@Override
public ItemStack getStack() {
return crafter.getResult();
return crafter.getResult(craftMatrix, player.world);
}
@Override
@@ -70,119 +70,76 @@ public abstract class RecipeUtil {
}
@Nullable
public static InventoryCraftingForestry getCraftRecipe(NonNullList<ItemStack> recipeItems, NonNullList<ItemStack> availableItems, World world, ItemStack recipeOutput) {
// 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<ItemStack> outputs = findMatchingRecipes(crafting, world);
if (!ItemStackUtil.containsItemStack(outputs, recipeOutput)) {
return null;
}
return crafting;
crafting.setInventorySlotContents(slot, originalStack);
return ItemStack.EMPTY;
}
public static NonNullList<ItemStack> findMatchingRecipes(InventoryCrafting inventory, World world) {
NonNullList<ItemStack> matchingRecipes = NonNullList.create();
ItemStack repairRecipe = findRepairRecipe(inventory);
if (!repairRecipe.isEmpty()) {
matchingRecipes.add(repairRecipe);
return matchingRecipes;
}
for (Object recipe : CraftingManager.getInstance().getRecipeList()) {
IRecipe irecipe = (IRecipe) recipe;
if (irecipe.matches(inventory, world)) {
ItemStack result = irecipe.getCraftingResult(inventory);
if (!ItemStackUtil.containsItemStack(matchingRecipes, result)) {
matchingRecipes.add(result);
}
public static NonNullList<IRecipe> findMatchingRecipes(InventoryCrafting inventory, World world) {
NonNullList<IRecipe> matchingRecipes = NonNullList.create();
for (IRecipe recipe : CraftingManager.getInstance().getRecipeList()) {
if (recipe.matches(inventory, world)) {
matchingRecipes.add(recipe);
}
}
return matchingRecipes;
}
private static ItemStack findRepairRecipe(InventoryCrafting inventory) {
int craftIngredientCount = 0;
ItemStack itemstack0 = ItemStack.EMPTY;
ItemStack itemstack1 = ItemStack.EMPTY;
for (int j = 0; j < inventory.getSizeInventory(); j++) {
ItemStack itemstack = inventory.getStackInSlot(j);
if (!itemstack.isEmpty()) {
if (craftIngredientCount == 0) {
itemstack0 = itemstack;
}
if (craftIngredientCount == 1) {
itemstack1 = itemstack;
}
++craftIngredientCount;
}
}
if (craftIngredientCount == 2 && itemstack0.getItem() == itemstack1.getItem() && itemstack0.getCount() == 1 && itemstack1.getCount() == 1 && itemstack0.getItem().isRepairable()) {
int damage0 = itemstack0.getMaxDamage() - itemstack0.getItemDamage();
int damage1 = itemstack0.getMaxDamage() - itemstack1.getItemDamage();
int repairAmount = damage0 + damage1 + itemstack0.getMaxDamage() * 5 / 100;
int repairedDamage = itemstack0.getMaxDamage() - repairAmount;
if (repairedDamage < 0) {
repairedDamage = 0;
}
return new ItemStack(itemstack0.getItem(), 1, repairedDamage);
}
return ItemStack.EMPTY;
}
public static void addRecipe(Block block, Object... obj) {
addRecipe(new ItemStack(block), obj);
}
@@ -215,6 +172,7 @@ public abstract class RecipeUtil {
GameRegistry.addSmelting(res, prod, xp);
}
@Nullable
public static String[][] matches(IDescriptiveRecipe recipe, IInventory inventoryCrafting) {
NonNullList<NonNullList<ItemStack>> recipeIngredients = recipe.getIngredients();
NonNullList<String> oreDicts = recipe.getOreDicts();
@@ -223,6 +181,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();
}
@@ -61,7 +61,7 @@ public class ContainerWorktable extends ContainerTile<TileWorktable> implements
}
// CraftResult display
addSlotToContainer(new SlotCrafter(player, craftingDisplay, tile, InventoryGhostCrafting.SLOT_CRAFTING_RESULT, 77, 38));
addSlotToContainer(new SlotCrafter(player, craftMatrix, craftingDisplay, tile, InventoryGhostCrafting.SLOT_CRAFTING_RESULT, 77, 38));
for (int i = 0; i < craftMatrix.getSizeInventory(); i++) {
onCraftMatrixChanged(tile.getCraftingDisplay(), i);
@@ -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;
}
}
@@ -53,7 +53,7 @@ public class PacketWorktableRecipeRequest extends ForestryPacket implements IFor
@Override
public void onPacketData(PacketBufferForestry data, EntityPlayerMP player) throws IOException {
BlockPos pos = data.readBlockPos();
MemorizedRecipe recipe = new MemorizedRecipe(data);
MemorizedRecipe recipe = new MemorizedRecipe(data, player.world);
TileUtil.actOnTile(player.world, pos, TileWorktable.class, worktable -> {
worktable.setCurrentRecipe(recipe);
@@ -55,7 +55,7 @@ public class PacketWorktableRecipeUpdate extends ForestryPacket implements IFore
@Override
public void onPacketData(PacketBufferForestry data, EntityPlayer player) throws IOException {
BlockPos pos = data.readBlockPos();
MemorizedRecipe recipe = data.readStreamable(MemorizedRecipe::new);
MemorizedRecipe recipe = data.readStreamable(data1 -> new MemorizedRecipe(data1, player.world));
TileUtil.actOnTile(player.world, pos, TileWorktable.class, tile -> tile.setCurrentRecipe(recipe));
}
@@ -10,7 +10,20 @@
******************************************************************************/
package forestry.factory.recipes;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.core.INbtReadable;
import forestry.api.core.INbtWritable;
@@ -18,112 +31,129 @@ import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
import forestry.core.recipes.RecipeUtil;
import forestry.core.utils.InventoryUtil;
import forestry.core.utils.ItemStackUtil;
import forestry.factory.inventory.InventoryCraftingForestry;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
public final class MemorizedRecipe implements INbtWritable, INbtReadable, IStreamable {
private final InventoryCraftingForestry craftMatrix = new InventoryCraftingForestry();
private NonNullList<ItemStack> recipeOutputs = NonNullList.create();
private List<IRecipe> recipes = NonNullList.create();
private int selectedRecipe;
private long lastUsed;
private boolean locked;
public MemorizedRecipe(PacketBufferForestry data) throws IOException {
readData(data);
public MemorizedRecipe(PacketBufferForestry data, World world) throws IOException {
readData(data, world);
}
public MemorizedRecipe(NBTTagCompound nbt) {
readFromNBT(nbt);
}
public MemorizedRecipe(InventoryCraftingForestry craftMatrix, NonNullList<ItemStack> recipeOutputs) {
public MemorizedRecipe(InventoryCraftingForestry craftMatrix, List<IRecipe> recipes) {
InventoryUtil.deepCopyInventoryContents(craftMatrix, this.craftMatrix);
this.recipeOutputs = recipeOutputs;
this.recipes = recipes;
}
public InventoryCraftingForestry getCraftMatrix() {
return craftMatrix;
}
public void calculateRecipeOutput(World world) {
recipeOutputs = RecipeUtil.findMatchingRecipes(craftMatrix, world);
if (selectedRecipe >= recipeOutputs.size()) {
public void validate(World world) {
recipes = RecipeUtil.findMatchingRecipes(craftMatrix, world);
if (selectedRecipe > recipes.size()) {
selectedRecipe = 0;
}
if (hasRecipeConflict()) {
removeRecipeConflicts();
}
}
public void incrementRecipe() {
selectedRecipe++;
if (selectedRecipe >= recipeOutputs.size()) {
if (selectedRecipe >= recipes.size()) {
selectedRecipe = 0;
}
}
public void decrementRecipe() {
selectedRecipe--;
if (selectedRecipe < 0) {
selectedRecipe = recipeOutputs.size() - 1;
selectedRecipe = recipes.size() - 1;
}
}
public boolean hasRecipeConflict() {
return recipeOutputs.size() > 1;
return recipes.size() > 1;
}
public void removeRecipeConflicts() {
ItemStack recipeOutput = getRecipeOutput();
recipeOutputs.clear();
recipeOutputs.add(recipeOutput);
IRecipe recipe = getSelectedRecipe();
recipes.clear();
recipes.add(recipe);
selectedRecipe = 0;
}
public ItemStack getRecipeOutput() {
if (recipeOutputs.isEmpty()) {
return ItemStack.EMPTY;
public ItemStack getOutputIcon() {
IRecipe selectedRecipe = getSelectedRecipe();
if (selectedRecipe != null) {
ItemStack recipeOutput = selectedRecipe.getCraftingResult(craftMatrix);
if (!recipeOutput.isEmpty()) {
return recipeOutput;
}
}
return ItemStack.EMPTY;
}
public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting, World world) {
IRecipe selectedRecipe = getSelectedRecipe();
if (selectedRecipe != null && selectedRecipe.matches(inventoryCrafting, world)) {
ItemStack recipeOutput = selectedRecipe.getCraftingResult(inventoryCrafting);
if (!recipeOutput.isEmpty()) {
return recipeOutput;
}
}
return ItemStack.EMPTY;
}
@Nullable
public IRecipe getSelectedRecipe() {
if (recipes.isEmpty()) {
return null;
} else {
return recipeOutputs.get(selectedRecipe);
return recipes.get(selectedRecipe);
}
}
public boolean hasRecipeOutput(ItemStack output) {
return ItemStackUtil.containsItemStack(recipeOutputs, output);
public boolean hasRecipe(@Nullable IRecipe recipe) {
return this.recipes.contains(recipe);
}
public void updateLastUse(long lastUsed) {
this.lastUsed = lastUsed;
}
public long getLastUsed() {
return lastUsed;
}
public void toggleLock() {
locked = !locked;
}
public boolean isLocked() {
return locked;
}
/* INbtWritable */
@Override
public final void readFromNBT(NBTTagCompound nbttagcompound) {
InventoryUtil.readFromNBT(craftMatrix, nbttagcompound);
lastUsed = nbttagcompound.getLong("LastUsed");
locked = nbttagcompound.getBoolean("Locked");
if (nbttagcompound.hasKey("SelectedRecipe")) {
selectedRecipe = nbttagcompound.getInteger("SelectedRecipe");
}
recipes.clear();
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
InventoryUtil.writeToNBT(craftMatrix, nbttagcompound);
@@ -138,15 +168,21 @@ public final class MemorizedRecipe implements INbtWritable, INbtReadable, IStrea
public void writeData(PacketBufferForestry data) {
data.writeInventory(craftMatrix);
data.writeBoolean(locked);
data.writeItemStacks(recipeOutputs);
data.writeVarInt(selectedRecipe);
}
@Override
@SideOnly(Side.CLIENT)
public void readData(PacketBufferForestry data) throws IOException {
readData(data, Minecraft.getMinecraft().world);
}
public void readData(PacketBufferForestry data, World world) throws IOException {
data.readInventory(craftMatrix);
locked = data.readBoolean();
recipeOutputs = data.readItemStacks();
selectedRecipe = data.readVarInt();
recipes.clear();
validate(world);
}
}
@@ -17,26 +17,32 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import forestry.api.core.INbtWritable;
import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
import net.minecraft.item.Item;
import net.minecraft.item.ItemMap;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.RecipesMapCloning;
import net.minecraft.item.crafting.RecipesMapExtending;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.core.INbtWritable;
import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
public class RecipeMemory implements INbtWritable, IStreamable {
private static final int capacity = 9;
private static final List<Class<? extends Item>> memoryBlacklist = new ArrayList<>();
private static final List<Class<? extends IRecipe>> memoryBlacklist = new ArrayList<>();
static {
memoryBlacklist.add(ItemMap.class); // almost every ItemMap is unique
// almost every ItemMap is unique
memoryBlacklist.add(RecipesMapCloning.class);
memoryBlacklist.add(RecipesMapExtending.class);
}
private final LinkedList<MemorizedRecipe> memorizedRecipes;
@@ -64,12 +70,8 @@ public class RecipeMemory implements INbtWritable, IStreamable {
if (recipe == null) {
return false;
}
ItemStack recipeOutput = recipe.getRecipeOutput();
if (recipeOutput.isEmpty()) {
return false;
}
Item item = recipeOutput.getItem();
return !memoryBlacklist.contains(item.getClass());
IRecipe recipeOutput = recipe.getSelectedRecipe();
return recipeOutput != null && !memoryBlacklist.contains(recipeOutput.getClass());
}
public void validate(World world) {
@@ -77,7 +79,7 @@ public class RecipeMemory implements INbtWritable, IStreamable {
while (iterator.hasNext()) {
MemorizedRecipe recipe = iterator.next();
if (recipe != null) {
recipe.calculateRecipeOutput(world);
recipe.validate(world);
if (!isValid(recipe)) {
iterator.remove();
}
@@ -88,26 +90,26 @@ public class RecipeMemory implements INbtWritable, IStreamable {
public long getLastUpdate() {
return lastUpdate;
}
public void memorizeRecipe(long worldTime, MemorizedRecipe recipe) {
if (!isValid(recipe)) {
if (recipe.getSelectedRecipe() == null) {
return;
}
lastUpdate = worldTime;
recipe.updateLastUse(lastUpdate);
if (recipe.hasRecipeConflict()) {
recipe.removeRecipeConflicts();
}
// update existing matching recipes
MemorizedRecipe memory = getExistingMemorizedRecipe(recipe.getRecipeOutput());
MemorizedRecipe memory = getExistingMemorizedRecipe(recipe.getSelectedRecipe());
if (memory != null) {
updateExistingRecipe(memory, recipe);
return;
}
// add a new recipe
if (memorizedRecipes.size() < capacity) {
memorizedRecipes.add(recipe);
@@ -150,13 +152,13 @@ public class RecipeMemory implements INbtWritable, IStreamable {
}
return memorizedRecipes.get(recipeIndex);
}
public ItemStack getRecipeDisplayOutput(int recipeIndex) {
MemorizedRecipe recipe = getRecipe(recipeIndex);
if (recipe == null) {
return ItemStack.EMPTY;
}
return recipe.getRecipeOutput();
return recipe.getOutputIcon();
}
public boolean isLocked(int recipeIndex) {
@@ -170,15 +172,17 @@ public class RecipeMemory implements INbtWritable, IStreamable {
memorizedRecipes.get(recipeIndex).toggleLock();
}
}
@Nullable
private MemorizedRecipe getExistingMemorizedRecipe(ItemStack craftingRecipeOutput) {
for (MemorizedRecipe memorizedRecipe : memorizedRecipes) {
if (memorizedRecipe.hasRecipeOutput(craftingRecipeOutput)) {
return memorizedRecipe;
private MemorizedRecipe getExistingMemorizedRecipe(@Nullable IRecipe recipe) {
if (recipe != null) {
for (MemorizedRecipe memorizedRecipe : memorizedRecipes) {
if (memorizedRecipe.hasRecipe(recipe)) {
return memorizedRecipe;
}
}
}
return null;
}
@@ -204,6 +208,6 @@ public class RecipeMemory implements INbtWritable, IStreamable {
@Override
@SideOnly(Side.CLIENT)
public void readData(PacketBufferForestry data) throws IOException {
data.readStreamables(memorizedRecipes, MemorizedRecipe::new);
data.readStreamables(memorizedRecipes, data1 -> new MemorizedRecipe(data1, Minecraft.getMinecraft().world));
}
}
@@ -4,6 +4,7 @@ import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import forestry.core.recipes.RecipeUtil;
import forestry.factory.gui.ContainerWorktable;
import forestry.factory.inventory.InventoryCraftingForestry;
import forestry.factory.recipes.MemorizedRecipe;
@@ -13,6 +14,7 @@ import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -27,27 +29,30 @@ class WorktableRecipeTransferHandler implements IRecipeTransferHandler<Container
@Nullable
@Override
public IRecipeTransferError transferRecipe(ContainerWorktable container, IRecipeLayout recipeLayout, EntityPlayer player, boolean maxTransfer, boolean doTransfer) {
Map<Integer, ? extends IGuiIngredient<ItemStack>> guiIngredients = recipeLayout.getItemStacks().getGuiIngredients();
if (doTransfer) {
Map<Integer, ? extends IGuiIngredient<ItemStack>> guiIngredients = recipeLayout.getItemStacks().getGuiIngredients();
InventoryCraftingForestry inventory = new InventoryCraftingForestry(container);
InventoryCraftingForestry inventory = new InventoryCraftingForestry(container);
NonNullList<ItemStack> recipeOutputs = NonNullList.create();
for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> entry : guiIngredients.entrySet()) {
int recipeSlot = entry.getKey();
List<ItemStack> allIngredients = entry.getValue().getAllIngredients();
if (!allIngredients.isEmpty()) {
if (recipeSlot == 0) {
recipeOutputs.addAll(allIngredients);
} else {
ItemStack firstIngredient = allIngredients.get(0);
inventory.setInventorySlotContents(recipeSlot - 1, firstIngredient);
NonNullList<ItemStack> recipeOutputs = NonNullList.create();
for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> entry : guiIngredients.entrySet()) {
int recipeSlot = entry.getKey();
List<ItemStack> allIngredients = entry.getValue().getAllIngredients();
if (!allIngredients.isEmpty()) {
if (recipeSlot == 0) {
recipeOutputs.addAll(allIngredients);
} else {
ItemStack firstIngredient = allIngredients.get(0);
inventory.setInventorySlotContents(recipeSlot - 1, firstIngredient);
}
}
}
}
if (!recipeOutputs.isEmpty() && doTransfer) {
MemorizedRecipe recipe = new MemorizedRecipe(inventory, recipeOutputs);
container.sendWorktableRecipeRequest(recipe);
List<IRecipe> matchingRecipes = RecipeUtil.findMatchingRecipes(inventory, player.world);
if (!matchingRecipes.isEmpty()) {
MemorizedRecipe recipe = new MemorizedRecipe(inventory, matchingRecipes);
container.sendWorktableRecipeRequest(recipe);
}
}
return null;
@@ -1,10 +1,12 @@
package forestry.factory.tiles;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public interface ICrafterWorktable {
ItemStack getResult();
ItemStack getResult(InventoryCrafting inventoryCrafting, World world);
boolean canTakeStack(int slotIndex);
@@ -12,6 +12,7 @@ package forestry.factory.tiles;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import com.google.common.base.Preconditions;
import forestry.core.inventory.InventoryAdapterTile;
@@ -33,10 +34,13 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
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;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -134,15 +138,18 @@ public class TileWorktable extends TileBase implements ICrafterWorktable {
if (currentRecipe == null) {
return false;
}
IRecipe selectedRecipe = currentRecipe.getSelectedRecipe();
if (selectedRecipe == null) {
return false;
}
NonNullList<ItemStack> recipeItems = InventoryUtil.getStacks(currentRecipe.getCraftMatrix());
NonNullList<ItemStack> inventoryStacks = InventoryUtil.getStacks(this);
InventoryCraftingForestry crafting = RecipeUtil.getCraftRecipe(recipeItems, inventoryStacks, world, currentRecipe.getRecipeOutput());
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) {
@@ -152,13 +159,13 @@ 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);
}
@@ -169,13 +176,17 @@ 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.getInstance().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);
}
@@ -186,18 +197,13 @@ public class TileWorktable extends TileBase implements ICrafterWorktable {
recipeMemory.memorizeRecipe(world.getTotalWorldTime(), currentRecipe);
}
}
@Override
public ItemStack getResult() {
if (currentRecipe == null) {
return ItemStack.EMPTY;
public ItemStack getResult(InventoryCrafting inventoryCrafting, World world) {
if (currentRecipe != null) {
return currentRecipe.getCraftingResult(inventoryCrafting, world);
}
ItemStack result = currentRecipe.getRecipeOutput();
if (!result.isEmpty()) {
result = result.copy();
}
return result;
return ItemStack.EMPTY;
}
/* Crafting Container methods */
@@ -225,13 +231,13 @@ public class TileWorktable extends TileBase implements ICrafterWorktable {
craftingDisplay.setInventorySlotContents(slot, ItemStack.EMPTY);
}
}
public void setCurrentRecipe(InventoryCraftingForestry crafting) {
NonNullList<ItemStack> recipeOutputs = RecipeUtil.findMatchingRecipes(crafting, world);
MemorizedRecipe recipe = recipeOutputs.isEmpty() ? null : new MemorizedRecipe(crafting, recipeOutputs);
List<IRecipe> recipes = RecipeUtil.findMatchingRecipes(crafting, world);
MemorizedRecipe recipe = recipes.isEmpty() ? null : new MemorizedRecipe(crafting, recipes);
if (currentRecipe != null && recipe != null) {
if (recipe.hasRecipeOutput(currentRecipe.getRecipeOutput())) {
if (recipe.hasRecipe(currentRecipe.getSelectedRecipe())) {
NonNullList<ItemStack> stacks = InventoryUtil.getStacks(crafting);
NonNullList<ItemStack> currentStacks = InventoryUtil.getStacks(currentRecipe.getCraftMatrix());
if (ItemStackUtil.equalSets(stacks, currentStacks)) {
@@ -239,7 +245,7 @@ public class TileWorktable extends TileBase implements ICrafterWorktable {
}
}
}
setCurrentRecipe(recipe);
}