From 66bc243b4103c9693b8a7dcf0858d8e432822cc8 Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Sat, 19 Feb 2022 23:02:40 -0300 Subject: [PATCH] cache more itemstacks --- .../java/appeng/helpers/DualityInterface.java | 13 ++- .../appeng/helpers/MultiCraftingTracker.java | 86 +++++++++++-------- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index a22d54a4c..ac81f2f0f 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -893,12 +893,21 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn else if( itemStack.getStackSize() > 0 ) { // make sure strange things didn't happen... - ItemStack inputStack = itemStack.createItemStack(); - if( !adaptor.simulateAdd( inputStack ).isEmpty() ) + + ItemStack inputStack = itemStack.getCachedItemStack( itemStack.getStackSize() ); + + ItemStack remaining = adaptor.simulateAdd( inputStack ); + + if( !remaining.isEmpty() ) { + itemStack.setCachedItemStack( remaining ); changed = true; throw new GridAccessException(); } + else + { + itemStack.setCachedItemStack( inputStack ); + } IAEItemStack storedStack = this.gridProxy.getStorage().getInventory( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ).getStorageList().findPrecise( itemStack ); if( storedStack != null ) diff --git a/src/main/java/appeng/helpers/MultiCraftingTracker.java b/src/main/java/appeng/helpers/MultiCraftingTracker.java index 464ef0716..d19590b6e 100644 --- a/src/main/java/appeng/helpers/MultiCraftingTracker.java +++ b/src/main/java/appeng/helpers/MultiCraftingTracker.java @@ -24,6 +24,7 @@ import java.util.concurrent.Future; import com.google.common.collect.ImmutableSet; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; @@ -83,57 +84,70 @@ public class MultiCraftingTracker public boolean handleCrafting( final int x, final long itemToCraft, final IAEItemStack ais, final InventoryAdaptor d, final World w, final IGrid g, final ICraftingGrid cg, final IActionSource mySrc ) { - if( ais != null && d.simulateAdd( ais.createItemStack() ).isEmpty() ) + if( ais != null ) { - final Future craftingJob = this.getJob( x ); + ItemStack inputStack = ais.getCachedItemStack( ais.getStackSize() ); - if( this.getLink( x ) != null ) - { - return false; - } - else if( craftingJob != null ) - { + ItemStack remaining = d.simulateAdd( inputStack ); - try + if( remaining.isEmpty() ) + { + ais.setCachedItemStack( inputStack ); + + final Future craftingJob = this.getJob( x ); + + if( this.getLink( x ) != null ) { - ICraftingJob job = null; - if( craftingJob.isDone() ) + return false; + } + else if( craftingJob != null ) + { + + try { - job = craftingJob.get(); - } - - if( job != null ) - { - final ICraftingLink link = cg.submitJob( job, this.owner, null, false, mySrc ); - - this.setJob( x, null ); - - if( link != null ) + ICraftingJob job = null; + if( craftingJob.isDone() ) { - this.setLink( x, link ); + job = craftingJob.get(); + } - return true; + if( job != null ) + { + final ICraftingLink link = cg.submitJob( job, this.owner, null, false, mySrc ); + + this.setJob( x, null ); + + if( link != null ) + { + this.setLink( x, link ); + + return true; + } } } + catch( final InterruptedException e ) + { + // :P + } + catch( final ExecutionException e ) + { + // :P + } } - catch( final InterruptedException e ) + else { - // :P - } - catch( final ExecutionException e ) - { - // :P + if( this.getLink( x ) == null ) + { + final IAEItemStack aisC = ais.copy(); + aisC.setStackSize( itemToCraft ); + + this.setJob( x, cg.beginCraftingJob( w, g, mySrc, aisC, null ) ); + } } } else { - if( this.getLink( x ) == null ) - { - final IAEItemStack aisC = ais.copy(); - aisC.setStackSize( itemToCraft ); - - this.setJob( x, cg.beginCraftingJob( w, g, mySrc, aisC, null ) ); - } + ais.setCachedItemStack( remaining ); } } return false;