diff --git a/src/main/java/appeng/crafting/CraftingJob.java b/src/main/java/appeng/crafting/CraftingJob.java index c60b43b92..2c9712772 100644 --- a/src/main/java/appeng/crafting/CraftingJob.java +++ b/src/main/java/appeng/crafting/CraftingJob.java @@ -129,7 +129,7 @@ public class CraftingJob implements Runnable, ICraftingJob public void reserve( CraftingTreeNode node, IAEItemStack stack ) { - checkUse( stack ); + this.checkUse( stack ); reserved.put( node, stack ); } @@ -156,6 +156,8 @@ public class CraftingJob implements Runnable, ICraftingJob final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true ); this.availableCheck = new MECraftingInventory( this.original, false, false, false ); + this.reserved.values().forEach( availableCheck::reserve ); + craftingTreeWatch.start(); this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc ); craftingTreeWatch.stop(); @@ -185,9 +187,11 @@ public class CraftingJob implements Runnable, ICraftingJob if( actionSrc.player().isPresent() ) { final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true ); - this.getTree().setSimulate(); + this.availableCheck = new MECraftingInventory( this.original, false, false, false ); + this.reserved.values().forEach( availableCheck::reserve ); + craftingTreeWatch.reset().start(); this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc ); craftingTreeWatch.stop(); diff --git a/src/main/java/appeng/crafting/CraftingTreeNode.java b/src/main/java/appeng/crafting/CraftingTreeNode.java index 48c6ac2fa..2cc3313cb 100644 --- a/src/main/java/appeng/crafting/CraftingTreeNode.java +++ b/src/main/java/appeng/crafting/CraftingTreeNode.java @@ -56,6 +56,7 @@ public class CraftingTreeNode private final ArrayList nodes = new ArrayList<>(); private final boolean canEmit; private CraftingTreeNode loopHead; + private int times; private int bytes = 0; private long missing = 0; private long howManyEmitted = 0; @@ -88,14 +89,6 @@ public class CraftingTreeNode } } - for( IAEItemStack i : details.getCondensedInputs() ) - { - if( i.equals( job.getOutput() ) ) - { - job.getNeededForLoop().add( i.copy().setStackSize( i.getStackSize() * times ) ); - } - } - if( this.parent == null ) { this.nodes.add( new CraftingTreeProcess( cc, job, details, times, this ) ); @@ -115,7 +108,7 @@ public class CraftingTreeNode void reserveForNode() { - if( loopHead == this ) + if( this.equals( loopHead ) ) { return; } @@ -152,6 +145,14 @@ public class CraftingTreeNode this.what.setStackSize( l ); + if( parent != null ) + { + if( this.what.equals( job.getOutput() ) ) + { + job.getNeededForLoop().add( this.what.copy() ); + } + } + if( this.getSlot() >= 0 && this.parent != null && this.parent.details.isCraftable() ) { Collection itemList = new ArrayList<>(); @@ -203,7 +204,16 @@ public class CraftingTreeNode fuzz = fuzz.copy(); fuzz.setStackSize( l ); - final IAEItemStack available = inv.extractItems( fuzz, Actionable.MODULATE, src ); + final IAEItemStack available; + + if( reserved != null ) + { + available = reserved; + } + else + { + available = inv.extractItems( fuzz, Actionable.MODULATE, src ); + } if( available != null ) { @@ -216,6 +226,11 @@ public class CraftingTreeNode thingsUsed.add( is.copy() ); this.used.add( is ); } + else if( reserved != null ) + { + thingsUsed.add( reserved.copy() ); + this.used.add( reserved ); + } } this.bytes += available.getStackSize(); @@ -231,28 +246,37 @@ public class CraftingTreeNode } else { - final IAEItemStack available = inv.extractItems( this.what, Actionable.MODULATE, src ); + final IAEItemStack available; + + if( parent == null && this.what.equals( job.getOutput() ) ) + { + available = null; + } + else if( reserved != null ) + { + available = reserved; + } + else + { + available = inv.extractItems( this.what, Actionable.MODULATE, src ); + } if( available != null ) { if( !this.exhausted ) { - IAEItemStack is; - - if( reserved != null ) - { - is = reserved; - } - else - { - is = this.job.checkUse( available ); - } + final IAEItemStack is = this.job.checkUse( available ); if( is != null ) { thingsUsed.add( is.copy() ); this.used.add( is ); } + else if( reserved != null ) + { + thingsUsed.add( reserved.copy() ); + this.used.add( reserved ); + } } this.bytes += available.getStackSize(); diff --git a/src/main/java/appeng/crafting/MECraftingInventory.java b/src/main/java/appeng/crafting/MECraftingInventory.java index c61fb5d91..21e315de0 100644 --- a/src/main/java/appeng/crafting/MECraftingInventory.java +++ b/src/main/java/appeng/crafting/MECraftingInventory.java @@ -349,6 +349,24 @@ public class MECraftingInventory implements IMEInventory return true; } + void ignore( final IAEItemStack what ) + { + final IAEItemStack list = this.localCache.findPrecise( what ); + if( list != null ) + { + list.setStackSize( 0 ); + } + } + + void reserve( final IAEItemStack what ) + { + final IAEItemStack list = this.localCache.findPrecise( what ); + if( list != null ) + { + list.decStackSize( what.getStackSize() ); + } + } + private void addMissing( final IAEItemStack extra ) { this.missingCache.add( extra ); diff --git a/src/main/java/appeng/util/item/AEItemStack.java b/src/main/java/appeng/util/item/AEItemStack.java index 87417bf5f..41312db3b 100644 --- a/src/main/java/appeng/util/item/AEItemStack.java +++ b/src/main/java/appeng/util/item/AEItemStack.java @@ -283,16 +283,15 @@ public class AEItemStack extends AEStack implements IAEItemStack { if( this.cachedItemStack != null ) { - ItemStack currentCached = this.cachedItemStack; - this.cachedItemStack = null; - currentCached.setCount( Ints.saturatedCast( stackSize ) ); - return currentCached; + if( Platform.itemComparisons().isSameItem( this.getDefinition(), this.cachedItemStack ) ) + { + ItemStack currentCached = this.cachedItemStack; + this.cachedItemStack = null; + currentCached.setCount( Ints.saturatedCast( stackSize ) ); + return currentCached; + } } - - ItemStack itemStack = this.createItemStack(); - itemStack.setCount( Ints.saturatedCast( stackSize ) ); - - return itemStack; + return ItemHandlerHelper.copyStackWithSize( this.getDefinition(), Ints.saturatedCast( stackSize ) ); } @Override