From 6cbc5aa953e9c713101a6cb1051a7bb818fb406c Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Fri, 20 Jun 2014 02:12:39 -0500 Subject: [PATCH] Recursive crafting works. MA's can accept pushes. Interfaces no alter pushing behavior depending on ICraftingMachine settings. Fixed bug where crafting would record items that don't exist as available for the initial pull. --- crafting/CraftBranchFailure.java | 3 +- crafting/CraftingJob.java | 9 +++- crafting/CraftingTreeNode.java | 6 ++- helpers/DualityInterface.java | 42 ++++++++++--------- .../implementations/CraftingCPUCluster.java | 29 +++++++++++-- tile/crafting/TileMolecularAssembler.java | 3 +- 6 files changed, 64 insertions(+), 28 deletions(-) diff --git a/crafting/CraftBranchFailure.java b/crafting/CraftBranchFailure.java index 04c4f535..a183d136 100644 --- a/crafting/CraftBranchFailure.java +++ b/crafting/CraftBranchFailure.java @@ -1,6 +1,7 @@ package appeng.crafting; import appeng.api.storage.data.IAEItemStack; +import appeng.util.Platform; public class CraftBranchFailure extends Exception { @@ -10,7 +11,7 @@ public class CraftBranchFailure extends Exception IAEItemStack missing; public CraftBranchFailure(IAEItemStack what, long howMany) { - super( "this should have been caught!" ); + super( "Failed: " + Platform.getItemDisplayName( what ) + " x " + howMany ); missing = what.copy(); missing.setStackSize( howMany ); } diff --git a/crafting/CraftingJob.java b/crafting/CraftingJob.java index 19ec4c4e..1b3323a9 100644 --- a/crafting/CraftingJob.java +++ b/crafting/CraftingJob.java @@ -31,6 +31,7 @@ public class CraftingJob implements Runnable boolean simulate = false; final MECraftingInventory original; + final MECraftingInventory availableCheck; public CraftingTreeNode tree; private BaseActionSource actionSrc; @@ -45,6 +46,12 @@ public class CraftingJob implements Runnable storage = AEApi.instance().storage().createItemList(); prophecies = new HashSet(); original = null; + availableCheck = null; + } + + public IAEItemStack checkUse(IAEItemStack available) + { + return availableCheck.extractItems( available, Actionable.MODULATE, this.actionSrc ); } public CraftingJob(ICraftingHost host, IAEItemStack what, Actionable mode) { @@ -57,7 +64,7 @@ public class CraftingJob implements Runnable CraftingCache cc = host.getGrid().getCache( CraftingCache.class ); IStorageGrid sg = host.getGrid().getCache( IStorageGrid.class ); original = new MECraftingInventory( sg.getItemInventory(), false, false, false ); - + availableCheck = new MECraftingInventory( sg.getItemInventory(), false, false, false ); tree = getCraftingTree( cc, what ); } diff --git a/crafting/CraftingTreeNode.java b/crafting/CraftingTreeNode.java index 022354c7..b01b61e8 100644 --- a/crafting/CraftingTreeNode.java +++ b/crafting/CraftingTreeNode.java @@ -32,6 +32,7 @@ public class CraftingTreeNode boolean cannotUse = false; long missing = 0; + CraftingJob job; IItemList used = AEApi.instance().storage().createItemList(); boolean exhausted = false; @@ -42,6 +43,7 @@ public class CraftingTreeNode parent = par; this.slot = slot; this.world = job.jobHost.getWorld(); + this.job = job; sim = false; for (ICraftingPatternDetails details : cc.getCraftingFor( what ))// in order. @@ -90,7 +92,7 @@ public class CraftingTreeNode if ( available != null ) { if ( !exhausted ) - used.add( available ); + used.add( job.checkUse( available ) ); l -= available.getStackSize(); if ( l == 0 ) @@ -106,7 +108,7 @@ public class CraftingTreeNode if ( available != null ) { if ( !exhausted ) - used.add( available ); + used.add( job.checkUse( available ) ); l -= available.getStackSize(); if ( l == 0 ) diff --git a/helpers/DualityInterface.java b/helpers/DualityInterface.java index 58da22de..d08f4ba8 100644 --- a/helpers/DualityInterface.java +++ b/helpers/DualityInterface.java @@ -706,29 +706,31 @@ public class DualityInterface implements IGridTickable, ISegmentedInventory, ISt TileEntity te = w.getTileEntity( tile.xCoord + s.offsetX, tile.yCoord + s.offsetY, tile.zCoord + s.offsetZ ); if ( te instanceof ICraftingMachine ) { - if ( ((ICraftingMachine) te).pushPattern( patternDetails, table, s.getOpposite() ) ) - return true; - } - else - { - InventoryAdaptor ad = InventoryAdaptor.getAdaptor( te, s.getOpposite() ); - if ( ad != null ) + ICraftingMachine cm = (ICraftingMachine) te; + if ( cm.acceptsPlans() ) { - possibleDirections.remove( s ); - - for (int x = 0; x < table.getSizeInventory(); x++) - { - ItemStack is = table.getStackInSlot( x ); - if ( is != null ) - { - addToSendList( ad.addItems( is ) ); - } - } - - pushItemsOut( possibleDirections ); - return true; + if ( cm.pushPattern( patternDetails, table, s.getOpposite() ) ) + return true; + continue; } } + + InventoryAdaptor ad = InventoryAdaptor.getAdaptor( te, s.getOpposite() ); + if ( ad != null ) + { + + for (int x = 0; x < table.getSizeInventory(); x++) + { + ItemStack is = table.getStackInSlot( x ); + if ( is != null ) + { + addToSendList( ad.addItems( is ) ); + } + } + + pushItemsOut( possibleDirections ); + return true; + } } return false; diff --git a/me/cluster/implementations/CraftingCPUCluster.java b/me/cluster/implementations/CraftingCPUCluster.java index 086aa4d0..bcb67000 100644 --- a/me/cluster/implementations/CraftingCPUCluster.java +++ b/me/cluster/implementations/CraftingCPUCluster.java @@ -136,7 +136,7 @@ public class CraftingCPUCluster implements IAECluster public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src) { - if ( input instanceof IAEItemStack ) + if ( input instanceof IAEItemStack && type == Actionable.MODULATE ) { IAEItemStack what = (IAEItemStack) input; IAEItemStack is = waitingFor.findPrecise( what ); @@ -149,21 +149,29 @@ public class CraftingCPUCluster implements IAECluster { is.decStackSize( input.getStackSize() ); + AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() + " remaining : " + + (is.getStackSize() + getRemainingTasks()) + " total left : waiting: " + (waiting ? "yes" : "no") ); + if ( finalOutput.equals( input ) ) return input; // ignore it. - return inventory.injectItems( what, Actionable.MODULATE, src ); + // 2000 + return inventory.injectItems( what, type, src ); } IAEItemStack insert = what.copy(); insert.setStackSize( is.getStackSize() ); what.decStackSize( is.getStackSize() ); + + AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() + " remaining : " + (is.getStackSize() + getRemainingTasks()) + + " total left : waiting: " + (waiting ? "yes" : "no") ); + is.setStackSize( 0 ); if ( finalOutput.equals( input ) ) return input; // ignore it. - inventory.injectItems( insert, Actionable.MODULATE, src ); + inventory.injectItems( insert, type, src ); return what; } @@ -172,6 +180,14 @@ public class CraftingCPUCluster implements IAECluster return input; } + private int getRemainingTasks() + { + int o = 0; + for (Entry tp : tasks.entrySet()) + o += tp.getValue().value * tp.getKey().getCondencedOutputs()[0].getStackSize(); + return o; + } + private boolean canCraft(IAEItemStack[] condencedInputs) { for (IAEItemStack is : condencedInputs) @@ -185,6 +201,7 @@ public class CraftingCPUCluster implements IAECluster public void updateCraftingLogic(IGrid grid, CraftingCache cc) { + waiting = false; if ( waiting || tasks.isEmpty() ) // nothing to do here... return; @@ -203,6 +220,9 @@ public class CraftingCPUCluster implements IAECluster for (ICraftingMedium m : cc.getMediums( e.getKey() )) { + if ( e.getValue().value <= 0 ) + continue; + if ( !m.isBusy() ) { if ( ic == null ) @@ -220,6 +240,8 @@ public class CraftingCPUCluster implements IAECluster { for (IAEItemStack fuzz : inventory.getItemList().findFuzzy( input[x], FuzzyMode.IGNORE_ALL )) { + fuzz = fuzz.copy(); + fuzz.setStackSize( input[x].getStackSize() ); IAEItemStack ais = inventory.extractItems( fuzz, Actionable.MODULATE, null ); ItemStack is = ais == null ? null : ais.getItemStack(); @@ -310,6 +332,7 @@ public class CraftingCPUCluster implements IAECluster try { + waitingFor.resetStatus(); job.tree.setJob( ci, this, src ); ci.commit( src ); finalOutput = job.getOutput(); diff --git a/tile/crafting/TileMolecularAssembler.java b/tile/crafting/TileMolecularAssembler.java index 01b8ecb2..00aecdf3 100644 --- a/tile/crafting/TileMolecularAssembler.java +++ b/tile/crafting/TileMolecularAssembler.java @@ -15,6 +15,7 @@ import appeng.api.config.RedstoneMode; import appeng.api.config.Settings; import appeng.api.config.Upgrades; import appeng.api.implementations.IUpgradeableHost; +import appeng.api.implementations.tiles.ICraftingMachine; import appeng.api.networking.IGridNode; import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.networking.ticking.IGridTickable; @@ -39,7 +40,7 @@ import appeng.util.InventoryAdaptor; import appeng.util.Platform; public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEngInventory, ISidedInventory, IUpgradeableHost, IConfigManagerHost, - IGridTickable + IGridTickable, ICraftingMachine { static final int[] sides = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };