From 49e790e6672c23b175940c52e4418bd6d6a0d701 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Wed, 26 Feb 2014 00:53:49 +0800 Subject: [PATCH] Engineering table now can auto-craft --- .../engineering/TileEngineeringTable.java | 58 +++++++++++- .../core/prefab/fluid/PipeNetwork.java | 92 ------------------- 2 files changed, 53 insertions(+), 97 deletions(-) diff --git a/archaic/src/main/java/resonantinduction/archaic/engineering/TileEngineeringTable.java b/archaic/src/main/java/resonantinduction/archaic/engineering/TileEngineeringTable.java index 22d6bcb4..58bb1aa4 100644 --- a/archaic/src/main/java/resonantinduction/archaic/engineering/TileEngineeringTable.java +++ b/archaic/src/main/java/resonantinduction/archaic/engineering/TileEngineeringTable.java @@ -156,6 +156,9 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive } } + /** + * DO NOT USE THIS INTERNALLY. FOR EXTERNAL USE ONLY! + */ @Override public ItemStack getStackInSlot(int slot) { @@ -165,7 +168,7 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive } else if (slot < CRAFTING_OUTPUT_END) { - return this.inventory[slot - CRAFTING_MATRIX_END]; + return inventory[slot - CRAFTING_MATRIX_END]; } else if (slot < PLAYER_OUTPUT_END && invPlayer != null) { @@ -204,6 +207,12 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive } else if (slot < CRAFTING_OUTPUT_END) { + /** + * An external inventory is attempting to craft the item from the engineering table. + */ + if (itemStack == null) + onPickUpFromSlot(null, slot, this.inventory[slot - CRAFTING_MATRIX_END]); + this.inventory[slot - CRAFTING_MATRIX_END] = itemStack; } else if (slot < PLAYER_OUTPUT_END && this.invPlayer != null) @@ -356,7 +365,7 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive } @Override - public void onPickUpFromSlot(EntityPlayer entityPlayer, int s, ItemStack itemStack) + public void onPickUpFromSlot(EntityPlayer entityPlayer, int slotID, ItemStack itemStack) { if (!worldObj.isRemote) { @@ -458,19 +467,58 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive @Override public int[] getAccessibleSlotsFromSide(int side) { - return this.getCraftingInv(); + return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; } + /** + * Auto-crafting methods. + */ @Override public boolean canInsertItem(int slot, ItemStack itemstack, int side) { - return this.isItemValidForSlot(slot, itemstack); + if (getStackInSlot(4) != null && getStackInSlot(4).getItem() instanceof ItemImprint) + return true; + + int minSize = 64; + int optimalSlot = -1; + + for (int i = 0; i < craftingMatrix.length; i++) + { + ItemStack checkStack = getStackInSlot(i); + + if (checkStack != null && checkStack.isItemEqual(itemstack)) + { + if (checkStack.stackSize < minSize || optimalSlot < 0) + { + optimalSlot = i; + minSize = checkStack.stackSize; + } + } + } + + return slot == optimalSlot; } @Override public boolean canExtractItem(int slot, ItemStack itemstack, int side) { - return this.isItemValidForSlot(slot, itemstack); + ItemStack outputStack = getStackInSlot(CRAFTING_MATRIX_END); + + if (outputStack != null) + { + /** + * Only allow take out crafting result when it can be crafted twice! + */ + Pair idealRecipeItem = this.getCraftingManager().getIdealRecipe(outputStack); + ItemStack[] doubleResults = ArrayUtils.addAll(idealRecipeItem.right(), idealRecipeItem.right()); + + if (!getCraftingManager().consumeItems(false, doubleResults)) + { + return false; + } + } + + return slot == CRAFTING_MATRIX_END; } @Override diff --git a/src/main/java/resonantinduction/core/prefab/fluid/PipeNetwork.java b/src/main/java/resonantinduction/core/prefab/fluid/PipeNetwork.java index 58a9dd56..cc3f802c 100644 --- a/src/main/java/resonantinduction/core/prefab/fluid/PipeNetwork.java +++ b/src/main/java/resonantinduction/core/prefab/fluid/PipeNetwork.java @@ -22,12 +22,6 @@ import calclavia.lib.utility.FluidUtility; */ public class PipeNetwork extends FluidNetwork { - public HashMap> sideMap = new HashMap>(); - public HashMap connectionMap = new HashMap(); - public int maxFlowRate = 0; - public int currentPressure = 0; - public int currentFlowRate = 0; - @Override public void update() { @@ -41,62 +35,6 @@ public class PipeNetwork extends FluidNetwork } } - /** - * Old pipe distribution code. - */ - @Deprecated - public void oldDistribution() - { - /* - * Slight delay to allow visual effect to take place before draining the pipe's internal - * tank - */ - FluidStack stack = this.getTank().getFluid().copy(); - int count = this.sideMap.size(); - - Iterator>> it = new HashMap>(sideMap).entrySet().iterator(); - - while (it.hasNext()) - { - Entry> entry = it.next(); - int sideCount = entry.getValue().size(); - - for (ForgeDirection dir : entry.getValue()) - { - int volPer = (stack.amount / count); - int volPerSide = (volPer / sideCount); - IFluidHandler handler = entry.getKey(); - - /* - * Don't input to tanks from the sides where the pipe is extraction mode. This - * prevents feed-back loops. - */ - if (connectionMap.get(handler).canFlow()) - { - stack.amount -= handler.fill(dir, FluidUtility.getStack(stack, Math.min(volPerSide, this.maxFlowRate)), true); - } - - if (sideCount > 1) - --sideCount; - if (volPer <= 0) - break; - } - - if (count > 1) - count--; - - if (stack == null || stack.amount <= 0) - { - stack = null; - break; - } - } - - getTank().setFluid(stack); - // TODO check for change before rebuilding - reconstructTankInfo(); - } - /** * Calculate pressure in this pipe. */ @@ -243,36 +181,6 @@ public class PipeNetwork extends FluidNetwork return canUpdate(); } - @Override - public void reconstruct() - { - this.sideMap.clear(); - this.maxFlowRate = Integer.MAX_VALUE; - super.reconstruct(); - } - - @Override - public void reconstructConnector(IFluidConnector connector) - { - super.reconstructConnector(connector); - - for (int i = 0; i < 6; i++) - { - if (connector.getConnections()[i] instanceof IFluidHandler && !(connector.getConnections()[i] instanceof IFluidPipe)) - { - EnumSet set = this.sideMap.get(connector.getConnections()[i]); - if (set == null) - { - set = EnumSet.noneOf(ForgeDirection.class); - } - - set.add(ForgeDirection.getOrientation(i).getOpposite()); - sideMap.put((IFluidHandler) connector.getConnections()[i], set); - connectionMap.put((IFluidHandler) connector.getConnections()[i], connector); - } - } - } - @Override public FluidStack drain(IFluidConnector source, ForgeDirection from, FluidStack resource, boolean doDrain) {