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

@@ -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);
}