From 86549f51bcbf14a2f713ab766d01b361caeaa242 Mon Sep 17 00:00:00 2001 From: CrazyPants Date: Tue, 15 Oct 2013 17:43:41 +0800 Subject: [PATCH] Made validation for AlloySmelter input much stricter to fix automation issues. --- .../machine/AbstractMachineContainer.java | 83 ++++++++++++++++++- .../machine/alloy/BasicAlloyRecipe.java | 29 +++++-- .../enderio/machine/alloy/IAlloyRecipe.java | 9 ++ .../machine/alloy/TileAlloySmelter.java | 82 ++++++++++++------ .../enderio/material/FusedQuartzRecipe.java | 13 ++- 5 files changed, 180 insertions(+), 36 deletions(-) create mode 100644 common/crazypants/enderio/machine/alloy/IAlloyRecipe.java diff --git a/common/crazypants/enderio/machine/AbstractMachineContainer.java b/common/crazypants/enderio/machine/AbstractMachineContainer.java index 1d80998b..3ce9815b 100644 --- a/common/crazypants/enderio/machine/AbstractMachineContainer.java +++ b/common/crazypants/enderio/machine/AbstractMachineContainer.java @@ -74,8 +74,8 @@ public abstract class AbstractMachineContainer extends Container { } else { //Check from inv->input then inv->upgrade then inv->hotbar or hotbar->inv if(slotIndex >= startPlayerSlot) { - if(!tileEntity.isValidInput(origStack) || !mergeItemStack(origStack, slotDef.getMinInputSlot(), slotDef.getMaxInputSlot() + 1, false)) { - if(!tileEntity.isValidUpgrade(origStack) || !mergeItemStack(origStack, slotDef.getMinUpgradeSlot(), slotDef.getMaxUpgradeSlot() + 1, false)) { + if(!mergeItemStack(origStack, slotDef.getMinInputSlot(), slotDef.getMaxInputSlot() + 1, false)) { + if(!mergeItemStack(origStack, slotDef.getMinUpgradeSlot(), slotDef.getMaxUpgradeSlot() + 1, false)) { if(slotIndex <= endPlayerSlot) { if(!mergeItemStack(origStack, startHotBarSlot, endHotBarSlot, false)) { return null; @@ -108,4 +108,83 @@ public abstract class AbstractMachineContainer extends Container { return copystack; } + /** + * Added validation of slot input + */ + @Override + protected boolean mergeItemStack(ItemStack par1ItemStack, int fromIndex, int toIndex, boolean reversOrder) { + + boolean result = false; + int checkIndex = fromIndex; + + if(reversOrder) { + checkIndex = toIndex - 1; + } + + Slot slot; + ItemStack itemstack1; + + if(par1ItemStack.isStackable()) { + + while (par1ItemStack.stackSize > 0 && (!reversOrder && checkIndex < toIndex || reversOrder && checkIndex >= fromIndex)) { + slot = (Slot) this.inventorySlots.get(checkIndex); + itemstack1 = slot.getStack(); + + if(itemstack1 != null && itemstack1.itemID == par1ItemStack.itemID + && (!par1ItemStack.getHasSubtypes() || par1ItemStack.getItemDamage() == itemstack1.getItemDamage()) + && ItemStack.areItemStackTagsEqual(par1ItemStack, itemstack1) + && slot.isItemValid(par1ItemStack)) { + + int mergedSize = itemstack1.stackSize + par1ItemStack.stackSize; + if(mergedSize <= par1ItemStack.getMaxStackSize()) { + par1ItemStack.stackSize = 0; + itemstack1.stackSize = mergedSize; + slot.onSlotChanged(); + result = true; + } else if(itemstack1.stackSize < par1ItemStack.getMaxStackSize()) { + par1ItemStack.stackSize -= par1ItemStack.getMaxStackSize() - itemstack1.stackSize; + itemstack1.stackSize = par1ItemStack.getMaxStackSize(); + slot.onSlotChanged(); + result = true; + } + } + + if(reversOrder) { + --checkIndex; + } else { + ++checkIndex; + } + } + } + + if(par1ItemStack.stackSize > 0) { + if(reversOrder) { + checkIndex = toIndex - 1; + } else { + checkIndex = fromIndex; + } + + while (!reversOrder && checkIndex < toIndex || reversOrder && checkIndex >= fromIndex) { + slot = (Slot) this.inventorySlots.get(checkIndex); + itemstack1 = slot.getStack(); + + if(itemstack1 == null && slot.isItemValid(par1ItemStack)) { + slot.putStack(par1ItemStack.copy()); + slot.onSlotChanged(); + par1ItemStack.stackSize = 0; + result = true; + break; + } + + if(reversOrder) { + --checkIndex; + } else { + ++checkIndex; + } + } + } + + return result; + } + } diff --git a/common/crazypants/enderio/machine/alloy/BasicAlloyRecipe.java b/common/crazypants/enderio/machine/alloy/BasicAlloyRecipe.java index 3847ff74..2a19eda2 100644 --- a/common/crazypants/enderio/machine/alloy/BasicAlloyRecipe.java +++ b/common/crazypants/enderio/machine/alloy/BasicAlloyRecipe.java @@ -16,9 +16,9 @@ import crazypants.enderio.crafting.impl.RecipeOutput; import crazypants.enderio.machine.IMachineRecipe; import crazypants.enderio.machine.MachineRecipeInput; -public class BasicAlloyRecipe implements IMachineRecipe { +public class BasicAlloyRecipe implements IMachineRecipe, IAlloyRecipe { - public static final int DEFAULT_ENERGY_USE = 1600; + public static final int DEFAULT_ENERGY_USE = 1400; private final String uid; @@ -55,6 +55,21 @@ public class BasicAlloyRecipe implements IMachineRecipe { } + @Override + public boolean isValidRecipeComponents(ItemStack... items) { + Set remainingInputs = new HashSet(inputKeys); + for (ItemStack item : items) { + if(item != null) { + InputKey key = new InputKey(item.itemID, item.getItemDamage()); + if(!remainingInputs.contains(key)) { + return false; + } + remainingInputs.remove(key); + } + } + return true; + } + public ItemStack[] getInputs() { return inputs; } @@ -82,7 +97,7 @@ public class BasicAlloyRecipe implements IMachineRecipe { Set keys = new HashSet(inputKeys); for (MachineRecipeInput input : checking) { - ItemStack ing = getIngrediantForInput(input.item); + ItemStack ing = getRecipeComponentFromInput(input.item); if(ing == null || ing.stackSize > input.item.stackSize) { return false; } @@ -119,7 +134,7 @@ public class BasicAlloyRecipe implements IMachineRecipe { if(input == null) { return false; } - return getIngrediantForInput(input.item) != null; + return getRecipeComponentFromInput(input.item) != null; } @Override @@ -128,16 +143,16 @@ public class BasicAlloyRecipe implements IMachineRecipe { } public int getQuantityConsumed(MachineRecipeInput input) { - ItemStack ing = getIngrediantForInput(input.item); + ItemStack ing = getRecipeComponentFromInput(input.item); return ing == null ? 0 : ing.stackSize; } - private ItemStack getIngrediantForInput(ItemStack input) { + private ItemStack getRecipeComponentFromInput(ItemStack input) { if(input == null) { return null; } for (ItemStack st : inputs) { - if(st != null && st.itemID == input.itemID) { + if(st != null && st.isItemEqual(input)) { return st; } } diff --git a/common/crazypants/enderio/machine/alloy/IAlloyRecipe.java b/common/crazypants/enderio/machine/alloy/IAlloyRecipe.java new file mode 100644 index 00000000..14e5c345 --- /dev/null +++ b/common/crazypants/enderio/machine/alloy/IAlloyRecipe.java @@ -0,0 +1,9 @@ +package crazypants.enderio.machine.alloy; + +import net.minecraft.item.ItemStack; + +public interface IAlloyRecipe { + + boolean isValidRecipeComponents(ItemStack... items); + +} diff --git a/common/crazypants/enderio/machine/alloy/TileAlloySmelter.java b/common/crazypants/enderio/machine/alloy/TileAlloySmelter.java index 592d8068..ad9d03b4 100644 --- a/common/crazypants/enderio/machine/alloy/TileAlloySmelter.java +++ b/common/crazypants/enderio/machine/alloy/TileAlloySmelter.java @@ -4,6 +4,7 @@ import java.util.List; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import crazypants.enderio.Log; import crazypants.enderio.ModObject; import crazypants.enderio.machine.AbstractPoweredTaskEntity; import crazypants.enderio.machine.IMachineRecipe; @@ -73,11 +74,15 @@ public class TileAlloySmelter extends AbstractPoweredTaskEntity { @Override public boolean isMachineItemValidForSlot(int slot, ItemStack itemstack) { - if(slot >= slotDefinition.getNumSlots()) { + if(!slotDefinition.isInputSlot(slot)) { return false; } - //if we are already processing a recipe and have more ingredients for it, only allow more items for that same recipe to be added + //We will assume anything that is in a slot is valid, so just return whether the new input can be stacked with the current one + ItemStack currentStackInSlot = inventory[slot]; + if(currentStackInSlot != null) { + return currentStackInSlot.isItemEqual(itemstack); + } int numSlotsFilled = 0; for (int i = slotDefinition.getMinInputSlot(); i <= slotDefinition.getMaxInputSlot(); i++) { @@ -87,38 +92,63 @@ public class TileAlloySmelter extends AbstractPoweredTaskEntity { } } } + List recipes = MachineRecipeRegistry.instance.getRecipesForInput(getMachineName(), MachineRecipeInput.create(slot, itemstack)); - //No task or all the slots are empty so just check for a new recipe - if(numSlotsFilled == 0 || currentTask == null) { - List recipes = MachineRecipeRegistry.instance.getRecipesForInput(getMachineName(), MachineRecipeInput.create(slot, itemstack)); - if(mode == Mode.ALLOY) { - return containsAlloyRecipe(recipes); - } else if(mode == mode.FURNACE) { - return containsFurnaceRecipe(recipes); - } - return !recipes.isEmpty(); + if(mode == Mode.FURNACE) { + return isValidInputForFurnaceRecipe(itemstack, numSlotsFilled, recipes); + } else if(mode == Mode.ALLOY) { + return isValidInputForAlloyRecipe(slot, itemstack, numSlotsFilled, recipes); } + return isValidInputForFurnaceRecipe(itemstack, numSlotsFilled, recipes) || isValidInputForAlloyRecipe(slot, itemstack, numSlotsFilled, recipes); + } - //If we are processing as vanilla recipe, allow all the slots to be filled with a single item - if(currentTask.getRecipe() instanceof VanillaSmeltingRecipe || mode == Mode.FURNACE) { - ItemStack currentStackType = null; - for (int i = slotDefinition.getMinInputSlot(); i <= slotDefinition.getMaxInputSlot() && currentStackType == null; i++) { - currentStackType = inventory[i]; + private boolean isValidInputForAlloyRecipe(int slot, ItemStack itemstack, int numSlotsFilled, List recipes) { + if(numSlotsFilled == 0) { + return containsAlloyRecipe(recipes); + } + for (IMachineRecipe recipe : recipes) { + if(!(recipe instanceof VanillaSmeltingRecipe)) { + + if(recipe instanceof IAlloyRecipe) { + ItemStack[] resultInv = new ItemStack[slotDefinition.getNumInputSlots()]; + for (int i = slotDefinition.getMinInputSlot(); i <= slotDefinition.getMaxInputSlot(); i++) { + if(i >= 0 && i < inventory.length) { + if(i == slot) { + resultInv[i] = itemstack; + } else { + resultInv[i] = inventory[i]; + } + } + } + if(((IAlloyRecipe) recipe).isValidRecipeComponents(resultInv)) { + return true; + } + + } else { + Log.warn("TileAlloySmelter.isMachineItemValidForSlot: A non alloy recipe was returned for the alloy smelter"); + return true; + } } + } + return false; + } + + private boolean isValidInputForFurnaceRecipe(ItemStack itemstack, int numSlotsFilled, List recipes) { + if(numSlotsFilled == 0) { + return containsFurnaceRecipe(recipes); + } + return containsFurnaceRecipe(recipes) && isItemAlreadyInASlot(itemstack); + } + + private boolean isItemAlreadyInASlot(ItemStack itemstack) { + ItemStack currentStackType = null; + for (int i = slotDefinition.getMinInputSlot(); i <= slotDefinition.getMaxInputSlot() && currentStackType == null; i++) { + currentStackType = inventory[i]; if(currentStackType != null && currentStackType.isItemEqual(itemstack)) { return true; } - return false; - - } else { - //Its an alloy so only allow existing stacks to be added to - ItemStack stackInSlot = inventory[slot]; - if(stackInSlot == null) { - return false; - } - - return stackInSlot.isItemEqual(itemstack); } + return false; } private boolean containsFurnaceRecipe(List recipes) { diff --git a/common/crazypants/enderio/material/FusedQuartzRecipe.java b/common/crazypants/enderio/material/FusedQuartzRecipe.java index 786817b3..c97d0af9 100644 --- a/common/crazypants/enderio/material/FusedQuartzRecipe.java +++ b/common/crazypants/enderio/material/FusedQuartzRecipe.java @@ -12,11 +12,22 @@ import crazypants.enderio.crafting.impl.EnderIoRecipe; import crazypants.enderio.machine.IMachineRecipe; import crazypants.enderio.machine.MachineRecipeInput; import crazypants.enderio.machine.alloy.BasicAlloyRecipe; +import crazypants.enderio.machine.alloy.IAlloyRecipe; -public class FusedQuartzRecipe implements IMachineRecipe { +public class FusedQuartzRecipe implements IMachineRecipe, IAlloyRecipe { private static final int NUM_QUARTZ = 4; + @Override + public boolean isValidRecipeComponents(ItemStack... items) { + for (ItemStack item : items) { + if(item != null && item.itemID != Item.netherQuartz.itemID) { + return false; + } + } + return true; + } + @Override public String getUid() { return ModObject.blockFusedQuartz.unlocalisedName;