From 285fc6314c0bc6ba0b561540def504c20716103b Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Sun, 27 Feb 2022 23:04:29 -0300 Subject: [PATCH 1/5] small optimization to crafting tree --- .../java/appeng/crafting/CraftingJob.java | 11 ++-- .../appeng/crafting/CraftingTreeNode.java | 2 +- .../appeng/crafting/CraftingTreeProcess.java | 57 +++++++------------ .../appeng/crafting/MECraftingInventory.java | 17 ++++-- .../appeng/me/cache/CraftingGridCache.java | 16 ------ .../java/appeng/util/item/AEItemStack.java | 7 ++- 6 files changed, 43 insertions(+), 67 deletions(-) diff --git a/src/main/java/appeng/crafting/CraftingJob.java b/src/main/java/appeng/crafting/CraftingJob.java index e3f2a84f7..75c0bc4ad 100644 --- a/src/main/java/appeng/crafting/CraftingJob.java +++ b/src/main/java/appeng/crafting/CraftingJob.java @@ -50,7 +50,7 @@ import appeng.hooks.TickHandler; public class CraftingJob implements Runnable, ICraftingJob { - private static final String LOG_CRAFTING_JOB = "CraftingJob (%s) issued by %s requesting [%s] using %s bytes took %s ms"; + private static final String LOG_CRAFTING_JOB = "CraftingJob (%s) issued by %s requesting [%s] using %s bytes took %s us"; private static final String LOG_MACHINE_SOURCE_DETAILS = "Machine[object=%s, %s]"; private final MECraftingInventory original; @@ -86,8 +86,7 @@ public class CraftingJob implements Runnable, ICraftingJob this.callback = callback; final ICraftingGrid cc = grid.getCache( ICraftingGrid.class ); final IStorageGrid sg = grid.getCache( IStorageGrid.class ); - this.original = new MECraftingInventory( sg - .getInventory( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ), actionSrc, false, false, false ); + this.original = new MECraftingInventory( sg.getInventory( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ), actionSrc, false, false, false ); this.setTree( this.getCraftingTree( cc, what ) ); this.availableCheck = null; @@ -263,7 +262,7 @@ public class CraftingJob implements Runnable, ICraftingJob this.availableCheck = null; - synchronized( this.monitor ) + synchronized ( this.monitor ) { this.running = false; this.done = true; @@ -311,7 +310,7 @@ public class CraftingJob implements Runnable, ICraftingJob /** * @return true if this needs more simulation */ - public boolean simulateFor(final int milli) + public boolean simulateFor( final int milli ) { this.time = milli; @@ -353,7 +352,7 @@ public class CraftingJob implements Runnable, ICraftingJob if( AELog.isCraftingLogEnabled() ) { final String itemToOutput = this.output.toString(); - final long elapsedTime = timer.elapsed( TimeUnit.MILLISECONDS ); + final long elapsedTime = timer.elapsed( TimeUnit.MICROSECONDS ); final String actionSource; if( this.actionSrc.player().isPresent() ) diff --git a/src/main/java/appeng/crafting/CraftingTreeNode.java b/src/main/java/appeng/crafting/CraftingTreeNode.java index e980d46b5..3f5038515 100644 --- a/src/main/java/appeng/crafting/CraftingTreeNode.java +++ b/src/main/java/appeng/crafting/CraftingTreeNode.java @@ -177,7 +177,7 @@ public class CraftingTreeNode for( IAEItemStack fuzz : itemList ) { - if( this.parent.details.isValidItemForSlot( this.getSlot(), fuzz.copy().setStackSize( 1 ).createItemStack(), this.world ) ) + if( this.parent.details.isValidItemForSlot( this.getSlot(), fuzz.copy().getCachedItemStack( 1 ), this.world ) ) { fuzz = fuzz.copy(); fuzz.setStackSize( l ); diff --git a/src/main/java/appeng/crafting/CraftingTreeProcess.java b/src/main/java/appeng/crafting/CraftingTreeProcess.java index c6ff5cf1c..98f8dbc99 100644 --- a/src/main/java/appeng/crafting/CraftingTreeProcess.java +++ b/src/main/java/appeng/crafting/CraftingTreeProcess.java @@ -19,10 +19,12 @@ package appeng.crafting; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import it.unimi.dsi.fastutil.objects.Object2LongArrayMap; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.world.World; @@ -40,6 +42,7 @@ import appeng.api.storage.data.IItemList; import appeng.container.ContainerNull; import appeng.me.cluster.implementations.CraftingCPUCluster; import appeng.util.Platform; +import org.apache.commons.lang3.tuple.Pair; public class CraftingTreeProcess @@ -48,7 +51,7 @@ public class CraftingTreeProcess private final CraftingTreeNode parent; final ICraftingPatternDetails details; private final CraftingJob job; - private final Map nodes = new HashMap<>(); + private final Object2LongArrayMap nodes = new Object2LongArrayMap<>(); private final int depth; boolean possible = true; private World world; @@ -70,32 +73,15 @@ public class CraftingTreeProcess { final IAEItemStack[] list = details.getInputs(); - final InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 ); - final IAEItemStack[] is = details.getInputs(); - for( int x = 0; x < ic.getSizeInventory(); x++ ) - { - ic.setInventorySlotContents( x, is[x] == null ? ItemStack.EMPTY : is[x].createItemStack() ); - } - - for( int x = 0; x < ic.getSizeInventory(); x++ ) - { - final ItemStack g = ic.getStackInSlot( x ); - if( !g.isEmpty() && g.getCount() > 1 ) - { - this.fullSimulation = true; - } - } - for( final IAEItemStack part : details.getCondensedInputs() ) { - final ItemStack g = part.createItemStack(); - boolean isAnInput = false; for( final IAEItemStack a : details.getCondensedOutputs() ) { - if( !g.isEmpty() && a != null && a.equals( g ) ) + if( a != null && a.equals( part ) ) { isAnInput = true; + break; } } @@ -104,15 +90,13 @@ public class CraftingTreeProcess this.limitQty = true; } - if( g.getItem().hasContainerItem( g ) ) + if( part.getItem().hasContainerItem( part.getDefinition() ) ) { this.limitQty = this.containerItems = true; } } - final boolean complicated = false; - - if( this.containerItems || complicated ) + if( this.containerItems ) { for( int x = 0; x < list.length; x++ ) { @@ -145,14 +129,13 @@ public class CraftingTreeProcess { for( final IAEItemStack part : details.getCondensedInputs() ) { - final ItemStack g = part.createItemStack(); - boolean isAnInput = false; for( final IAEItemStack a : details.getCondensedOutputs() ) { - if( !g.isEmpty() && a != null && a.equals( g ) ) + if( a != null && a.equals( part ) ) { isAnInput = true; + break; } } @@ -190,7 +173,7 @@ public class CraftingTreeProcess { final InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 ); - for( final Entry entry : this.nodes.entrySet() ) + for( final Entry entry : this.nodes.object2LongEntrySet() ) { final IAEItemStack item = entry.getKey().getStack( entry.getValue() ); final IAEItemStack stack = entry.getKey().request( inv, item.getStackSize(), src ); @@ -214,7 +197,7 @@ public class CraftingTreeProcess else { // request and remove inputs... - for( final Entry entry : this.nodes.entrySet() ) + for( final Entry entry : this.nodes.object2LongEntrySet() ) { final IAEItemStack item = entry.getKey().getStack( entry.getValue() ); final IAEItemStack stack = entry.getKey().request( inv, item.getStackSize() * i, src ); @@ -247,9 +230,9 @@ public class CraftingTreeProcess void dive( final CraftingJob job ) { job.addTask( this.getAmountCrafted( this.parent.getStack( 1 ) ), this.crafts, this.details, this.depth ); - for( final CraftingTreeNode pro : this.nodes.keySet() ) + for( final Entry entry : this.nodes.object2LongEntrySet() ) { - pro.dive( job ); + entry.getKey().dive( job ); } job.addBytes( this.crafts * 8 + this.bytes ); @@ -286,9 +269,9 @@ public class CraftingTreeProcess this.crafts = 0; this.bytes = 0; - for( final CraftingTreeNode pro : this.nodes.keySet() ) + for( final Entry entry : this.nodes.object2LongEntrySet() ) { - pro.setSimulate(); + entry.getKey().setSimulate(); } } @@ -296,9 +279,9 @@ public class CraftingTreeProcess { craftingCPUCluster.addCrafting( this.details, this.crafts ); - for( final CraftingTreeNode pro : this.nodes.keySet() ) + for( final Entry entry : this.nodes.object2LongEntrySet() ) { - pro.setJob( storage, craftingCPUCluster, src ); + entry.getKey().setJob( storage, craftingCPUCluster, src ); } } @@ -311,9 +294,9 @@ public class CraftingTreeProcess plan.addRequestable( i ); } - for( final CraftingTreeNode pro : this.nodes.keySet() ) + for( final Entry entry : this.nodes.object2LongEntrySet() ) { - pro.getPlan( plan ); + entry.getKey().getPlan( plan ); } } } diff --git a/src/main/java/appeng/crafting/MECraftingInventory.java b/src/main/java/appeng/crafting/MECraftingInventory.java index 4fda335a6..842e668f6 100644 --- a/src/main/java/appeng/crafting/MECraftingInventory.java +++ b/src/main/java/appeng/crafting/MECraftingInventory.java @@ -97,8 +97,7 @@ public class MECraftingInventory implements IMEInventory this.injectedCache = null; } - this.localCache = this.target - .getAvailableItems( new ItemListIgnoreCrafting<>( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() ) ); + this.localCache = this.target.getAvailableItems( new ItemListIgnoreCrafting<>( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() ) ); this.par = parent; } @@ -140,7 +139,14 @@ public class MECraftingInventory implements IMEInventory this.localCache = new ItemListIgnoreCrafting<>( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() ); for( final IAEItemStack is : target.getStorageList() ) { - this.localCache.add( target.extractItems( is, Actionable.SIMULATE, src ) ); + if( src.player().isPresent() ) + { + this.localCache.add( target.extractItems( is, Actionable.SIMULATE, src ) ); + } + else + { + this.localCache.add( is ); + } } this.par = null; @@ -321,7 +327,10 @@ public class MECraftingInventory implements IMEInventory } } failed = true; - if( !src.player().isPresent() ) break; + if( !src.player().isPresent() ) + { + break; + } } } } diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index 240d0851a..b02f97001 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -544,22 +544,6 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper if( res == null ) { - if( details != null && details.isCraftable() ) - { - for( final IAEItemStack ais : this.craftableItems.keySet() ) - { - if( ais.getItem() == whatToCraft.getItem() && ( !ais.getItem().getHasSubtypes() || ais.getItemDamage() == whatToCraft.getItemDamage() ) ) - { - // TODO: check if OK - // TODO: this is slightly hacky, but fine as long as we only deal with itemstacks - if( details.isValidItemForSlot( slotIndex, ais.asItemStackRepresentation(), world ) ) - { - return this.craftableItems.get( ais ); - } - } - } - } - return ImmutableSet.of(); } diff --git a/src/main/java/appeng/util/item/AEItemStack.java b/src/main/java/appeng/util/item/AEItemStack.java index ce09a306d..d18d95785 100644 --- a/src/main/java/appeng/util/item/AEItemStack.java +++ b/src/main/java/appeng/util/item/AEItemStack.java @@ -57,7 +57,7 @@ public final class AEItemStack extends AEStack implements IAEItemS private String displayName; @SideOnly( Side.CLIENT ) private List tooltip; - private WeakReference cachedItemStack; + private ItemStack cachedItemStack; private AEItemStack( final AEItemStack is ) { @@ -66,6 +66,7 @@ public final class AEItemStack extends AEStack implements IAEItemS this.setCountRequestable( is.getCountRequestable() ); this.sharedStack = is.sharedStack; this.oreReference = is.oreReference; + this.cachedItemStack = is.cachedItemStack; } private AEItemStack( final AESharedItemStack is, long size ) @@ -282,7 +283,7 @@ public final class AEItemStack extends AEStack implements IAEItemS ItemStack currentCached = null; if( this.cachedItemStack != null ) { - currentCached = this.cachedItemStack.get(); + currentCached = this.cachedItemStack; } ItemStack itemStack; @@ -304,7 +305,7 @@ public final class AEItemStack extends AEStack implements IAEItemS @Override public void setCachedItemStack( ItemStack itemStack ) { - this.cachedItemStack = new WeakReference<>( itemStack ); + this.cachedItemStack = itemStack; } @Override From 6b29288de6c741cc743732c7f0a9f15286751e00 Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Mon, 28 Feb 2022 00:13:07 -0300 Subject: [PATCH 2/5] remove crafting penalties --- .../java/appeng/helpers/DualityInterface.java | 18 +-------- .../parts/automation/PartExportBus.java | 37 ++----------------- 2 files changed, 4 insertions(+), 51 deletions(-) diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index f0824e6b6..224d463a8 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -108,8 +108,6 @@ import static gregtech.api.block.machines.BlockMachine.getMetaTileEntity; public class DualityInterface implements IGridTickable, IStorageMonitorable, IInventoryDestination, IAEAppEngInventory, IConfigManagerHost, ICraftingProvider, IUpgradeableHost { - private final int[] failedCraftTriesSlot = {0, 0, 0, 0, 0, 0, 0, 0, 0}; - public static final int NUMBER_OF_STORAGE_SLOTS = 9; public static final int NUMBER_OF_CONFIG_SLOTS = 9; public static final int NUMBER_OF_PATTERN_SLOTS = 36; @@ -970,21 +968,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn { if( this.getInstalledUpgrades( Upgrades.CRAFTING ) > 0 && itemStack != null ) { - if( this.failedCraftTriesSlot[x] <= 0 ) - { - boolean crafted = this.craftingTracker.handleCrafting( x, itemStack.getStackSize(), itemStack, d, this.iHost.getTileEntity().getWorld(), this.gridProxy.getGrid(), this.gridProxy.getCrafting(), this.mySource ); - - if( crafted ) - { - this.failedCraftTriesSlot[x] = 0; - } - else - { - this.failedCraftTriesSlot[x] += 2; - } - return crafted; - } - this.failedCraftTriesSlot[x] -= 1; + return this.craftingTracker.handleCrafting( x, itemStack.getStackSize(), itemStack, d, this.iHost.getTileEntity().getWorld(), this.gridProxy.getGrid(), this.gridProxy.getCrafting(), this.mySource ); } } catch( final GridAccessException e ) diff --git a/src/main/java/appeng/parts/automation/PartExportBus.java b/src/main/java/appeng/parts/automation/PartExportBus.java index c53b054bb..d987b9109 100644 --- a/src/main/java/appeng/parts/automation/PartExportBus.java +++ b/src/main/java/appeng/parts/automation/PartExportBus.java @@ -19,7 +19,6 @@ package appeng.parts.automation; -import javax.annotation.Nullable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -72,8 +71,6 @@ import appeng.util.item.AEItemStack; public class PartExportBus extends PartSharedItemBus implements ICraftingRequester { - private final int[] failedCraftTriesSlot = {0, 0, 0, 0, 0, 0, 0, 0, 0}; - public static final ResourceLocation MODEL_BASE = new ResourceLocation( AppEng.MOD_ID, "part/export_bus_base" ); @PartModels @@ -156,22 +153,8 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest if( this.craftOnly() ) { - if( this.failedCraftTriesSlot[x] <= 0 ) - { + this.didSomething = this.craftingTracker.handleCrafting( slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(), this.getProxy().getGrid(), cg, this.mySrc ) || this.didSomething; - this.didSomething = this.craftingTracker.handleCrafting( slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(), this.getProxy().getGrid(), cg, this.mySrc ) || this.didSomething; - - if( this.didSomething ) - { - this.failedCraftTriesSlot[x] = 0; - } - else - { - this.failedCraftTriesSlot[x] += 2; - } - } - this.failedCraftTriesSlot[x] -= 1; - continue; } final long before = this.itemToSend; @@ -197,21 +180,7 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest if( this.itemToSend == before && this.isCraftingEnabled() ) { - if( this.failedCraftTriesSlot[x] <= 0 ) - { - - this.didSomething = this.craftingTracker.handleCrafting( slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(), this.getProxy().getGrid(), cg, this.mySrc ) || this.didSomething; - - if( this.didSomething ) - { - this.failedCraftTriesSlot[x] = 0; - } - else - { - this.failedCraftTriesSlot[x] += 2; - } - } - this.failedCraftTriesSlot[x] -= 1; + this.didSomething = this.craftingTracker.handleCrafting( slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(), this.getProxy().getGrid(), cg, this.mySrc ) || this.didSomething; } } @@ -310,7 +279,7 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest items.setCachedItemStack( remaining ); } } - + if( remaining == inputStack ) { return items; From a0f3ef9d86e07ea16bd6475f878c227b387b483c Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Mon, 28 Feb 2022 21:25:13 -0300 Subject: [PATCH 3/5] cut crafting tree earlier if ingredients are available --- .../java/appeng/crafting/CraftingJob.java | 12 ++++++++ .../appeng/crafting/CraftingTreeNode.java | 29 +++++++++++++++++++ .../appeng/crafting/CraftingTreeProcess.java | 25 ++++++++++++++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/main/java/appeng/crafting/CraftingJob.java b/src/main/java/appeng/crafting/CraftingJob.java index 75c0bc4ad..2f0d450d4 100644 --- a/src/main/java/appeng/crafting/CraftingJob.java +++ b/src/main/java/appeng/crafting/CraftingJob.java @@ -57,6 +57,8 @@ public class CraftingJob implements Runnable, ICraftingJob private final World world; private final IItemList crafting = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); private final IItemList missing = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); + private final IItemList usedWhileBuilding = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); + private final HashMap opsAndMultiplier = new HashMap<>(); private final Object monitor = new Object(); private final Stopwatch watch = Stopwatch.createUnstarted(); @@ -102,6 +104,16 @@ public class CraftingJob implements Runnable, ICraftingJob this.availableCheck.injectItems( o, Actionable.MODULATE, this.actionSrc ); } + public IItemList getUsedWhileBuilding() + { + return usedWhileBuilding; + } + + public IAEItemStack getOriginal( IAEItemStack request ) + { + return original.getItemList().findPrecise( request ); + } + IAEItemStack checkUse( final IAEItemStack available ) { return this.availableCheck.extractItems( available, Actionable.MODULATE, this.actionSrc ); diff --git a/src/main/java/appeng/crafting/CraftingTreeNode.java b/src/main/java/appeng/crafting/CraftingTreeNode.java index 3f5038515..f8857923e 100644 --- a/src/main/java/appeng/crafting/CraftingTreeNode.java +++ b/src/main/java/appeng/crafting/CraftingTreeNode.java @@ -88,6 +88,35 @@ public class CraftingTreeNode } } + public CraftingTreeNode( final ICraftingGrid cc, final CraftingJob job, final IAEItemStack wat, final CraftingTreeProcess par, final int slot, final int depth, boolean available ) + { + this.what = wat; + this.parent = par; + this.slot = slot; + this.world = job.getWorld(); + this.job = job; + this.sim = false; + + this.canEmit = cc.canEmitFor( this.what ); + + if( this.canEmit ) + { + return; // if you can emit for something, you can't make it with patterns. + } + + if( !available ) + { + for( final ICraftingPatternDetails details : cc.getCraftingFor( this.what, this.parent == null ? null : this.parent.details, slot, this.world ) )// in + // order. + { + if( this.parent == null || this.parent.notRecursive( details ) ) + { + this.nodes.add( new CraftingTreeProcess( cc, job, details, this, depth + 1 ) ); + } + } + } + } + boolean notRecursive( final ICraftingPatternDetails details ) { IAEItemStack[] o = details.getCondensedOutputs(); diff --git a/src/main/java/appeng/crafting/CraftingTreeProcess.java b/src/main/java/appeng/crafting/CraftingTreeProcess.java index 98f8dbc99..1b72eb3e8 100644 --- a/src/main/java/appeng/crafting/CraftingTreeProcess.java +++ b/src/main/java/appeng/crafting/CraftingTreeProcess.java @@ -93,6 +93,7 @@ public class CraftingTreeProcess if( part.getItem().hasContainerItem( part.getDefinition() ) ) { this.limitQty = this.containerItems = true; + break; } } @@ -103,7 +104,16 @@ public class CraftingTreeProcess final IAEItemStack part = list[x]; if( part != null ) { - this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() ); + job.getUsedWhileBuilding().addStorage( part ); + IAEItemStack used = job.getUsedWhileBuilding().findPrecise( part ); + if( job.getOriginal( part ) != null && used.getStackSize() <= job.getOriginal( part ).getStackSize() ) + { + this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1, true ), part.getStackSize() ); + } + else + { + this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() ); + } } } } @@ -117,8 +127,17 @@ public class CraftingTreeProcess final IAEItemStack comparePart = list[x]; if( part != null && part.equals( comparePart ) ) { - // use the first slot... - this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() ); + job.getUsedWhileBuilding().addStorage( part ); + IAEItemStack used = job.getUsedWhileBuilding().findPrecise( part ); + if( job.getOriginal( part ) != null && used.getStackSize() <= job.getOriginal( part ).getStackSize() ) + { + this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1, true ), part.getStackSize() ); + } + else + { + // use the first slot... + this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() ); + } break; } } From 4192d721a03e7d7a113247c87600c87372a08845 Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Mon, 28 Feb 2022 21:25:33 -0300 Subject: [PATCH 4/5] only fire simulation if a player requested it --- .../java/appeng/crafting/CraftingJob.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/appeng/crafting/CraftingJob.java b/src/main/java/appeng/crafting/CraftingJob.java index 2f0d450d4..b4a4d6cf6 100644 --- a/src/main/java/appeng/crafting/CraftingJob.java +++ b/src/main/java/appeng/crafting/CraftingJob.java @@ -175,23 +175,26 @@ public class CraftingJob implements Runnable, ICraftingJob try { - final Stopwatch timer = Stopwatch.createStarted(); - final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true ); - craftingInventory.ignore( this.output ); - - this.availableCheck = new MECraftingInventory( this.original, false, false, false ); - - this.getTree().setSimulate(); - this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc ); - this.getTree().dive( this ); - - for( final String s : this.opsAndMultiplier.keySet() ) + if( actionSrc.player().isPresent() ) { - final TwoIntegers ti = this.opsAndMultiplier.get( s ); - AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) ); - } + final Stopwatch timer = Stopwatch.createStarted(); + final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true ); + craftingInventory.ignore( this.output ); - this.logCraftingJob( "simulate", timer ); + this.availableCheck = new MECraftingInventory( this.original, false, false, false ); + + this.getTree().setSimulate(); + this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc ); + this.getTree().dive( this ); + + for( final String s : this.opsAndMultiplier.keySet() ) + { + final TwoIntegers ti = this.opsAndMultiplier.get( s ); + AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) ); + } + + this.logCraftingJob( "simulate", timer ); + } } catch( final CraftBranchFailure e1 ) { From f98f6ef8529d9265a034810e1d17002dbb16dc2d Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Mon, 28 Feb 2022 22:57:35 -0300 Subject: [PATCH 5/5] misc cleaning --- .../ContainerCraftConfirm.java | 1 + .../java/appeng/crafting/CraftingJob.java | 29 ++++++---- .../appeng/crafting/CraftingTreeNode.java | 57 ++++++++++--------- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/main/java/appeng/container/implementations/ContainerCraftConfirm.java b/src/main/java/appeng/container/implementations/ContainerCraftConfirm.java index ec0dd4f15..4593fdde8 100644 --- a/src/main/java/appeng/container/implementations/ContainerCraftConfirm.java +++ b/src/main/java/appeng/container/implementations/ContainerCraftConfirm.java @@ -150,6 +150,7 @@ public class ContainerCraftConfirm extends AEBaseContainer if( ccr.getCpu() == c ) { found = true; + break; } } diff --git a/src/main/java/appeng/crafting/CraftingJob.java b/src/main/java/appeng/crafting/CraftingJob.java index b4a4d6cf6..4c2b0d854 100644 --- a/src/main/java/appeng/crafting/CraftingJob.java +++ b/src/main/java/appeng/crafting/CraftingJob.java @@ -61,7 +61,8 @@ public class CraftingJob implements Runnable, ICraftingJob private final HashMap opsAndMultiplier = new HashMap<>(); private final Object monitor = new Object(); - private final Stopwatch watch = Stopwatch.createUnstarted(); + private final Stopwatch tickSpreadingWatch = Stopwatch.createUnstarted(); + private final Stopwatch craftingTreeWatch = Stopwatch.createUnstarted(); private CraftingTreeNode tree; private final IAEItemStack output; private boolean simulate = false; @@ -150,7 +151,7 @@ public class CraftingJob implements Runnable, ICraftingJob TickHandler.INSTANCE.registerCraftingSimulation( this.world, this ); this.handlePausing(); - final Stopwatch timer = Stopwatch.createStarted(); + craftingTreeWatch.start(); final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true ); craftingInventory.ignore( this.output ); @@ -165,9 +166,8 @@ public class CraftingJob implements Runnable, ICraftingJob AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) ); } - this.logCraftingJob( "real", timer ); - // if ( mode == Actionable.MODULATE ) - // craftingInventory.moveItemsToStorage( storage ); + craftingTreeWatch.stop(); + this.logCraftingJob( "real", craftingTreeWatch ); } catch( final CraftBranchFailure e ) { @@ -177,7 +177,7 @@ public class CraftingJob implements Runnable, ICraftingJob { if( actionSrc.player().isPresent() ) { - final Stopwatch timer = Stopwatch.createStarted(); + craftingTreeWatch.reset().start(); final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true ); craftingInventory.ignore( this.output ); @@ -193,7 +193,13 @@ public class CraftingJob implements Runnable, ICraftingJob AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) ); } - this.logCraftingJob( "simulate", timer ); + craftingTreeWatch.stop(); + this.logCraftingJob( "simulate", craftingTreeWatch ); + } + else + { + craftingTreeWatch.stop(); + this.logCraftingJob( "real", craftingTreeWatch ); } } catch( final CraftBranchFailure e1 ) @@ -240,10 +246,11 @@ public class CraftingJob implements Runnable, ICraftingJob this.incTime = 0; synchronized ( this.monitor ) { - if( this.watch.elapsed( TimeUnit.MICROSECONDS ) > this.time ) + if( this.tickSpreadingWatch.elapsed( TimeUnit.MICROSECONDS ) > this.time ) { this.running = false; - this.watch.stop(); + this.craftingTreeWatch.stop(); + this.tickSpreadingWatch.stop(); this.monitor.notify(); } @@ -337,8 +344,8 @@ public class CraftingJob implements Runnable, ICraftingJob } if( !this.actionSrc.player().isPresent() ) { - this.watch.reset(); - this.watch.start(); + this.tickSpreadingWatch.reset(); + this.tickSpreadingWatch.start(); this.monitor.notify(); } this.running = true; diff --git a/src/main/java/appeng/crafting/CraftingTreeNode.java b/src/main/java/appeng/crafting/CraftingTreeNode.java index f8857923e..adbdf36f8 100644 --- a/src/main/java/appeng/crafting/CraftingTreeNode.java +++ b/src/main/java/appeng/crafting/CraftingTreeNode.java @@ -153,31 +153,8 @@ public class CraftingTreeNode final List thingsUsed = new ArrayList<>(); this.what.setStackSize( l ); - IAEItemStack available = inv.extractItems( this.what, Actionable.MODULATE, src ); - if( available != null ) - { - if( !this.exhausted ) - { - final IAEItemStack is = this.job.checkUse( available ); - - if( is != null ) - { - thingsUsed.add( is.copy() ); - this.used.add( is ); - } - } - - this.bytes += available.getStackSize(); - l -= available.getStackSize(); - - if( l == 0 ) - { - return available; - } - } - - else if( this.getSlot() >= 0 && this.parent != null && this.parent.details.isCraftable() ) + if( this.getSlot() >= 0 && this.parent != null && this.parent.details.isCraftable() ) { final Collection itemList; final IItemList inventoryList = inv.getItemList(); @@ -211,7 +188,7 @@ public class CraftingTreeNode fuzz = fuzz.copy(); fuzz.setStackSize( l ); - available = inv.extractItems( fuzz, Actionable.MODULATE, src ); + final IAEItemStack available = inv.extractItems( fuzz, Actionable.MODULATE, src ); if( available != null ) { @@ -237,6 +214,32 @@ public class CraftingTreeNode } } } + else + { + final IAEItemStack available = inv.extractItems( this.what, Actionable.MODULATE, src ); + + if( available != null ) + { + if( !this.exhausted ) + { + final IAEItemStack is = this.job.checkUse( available ); + + if( is != null ) + { + thingsUsed.add( is.copy() ); + this.used.add( is ); + } + } + + this.bytes += available.getStackSize(); + l -= available.getStackSize(); + + if( l == 0 ) + { + return available; + } + } + } if( this.canEmit ) { @@ -263,7 +266,7 @@ public class CraftingTreeNode madeWhat.setStackSize( l ); - available = inv.extractItems( madeWhat, Actionable.MODULATE, src ); + final IAEItemStack available = inv.extractItems( madeWhat, Actionable.MODULATE, src ); if( available != null ) { @@ -293,7 +296,7 @@ public class CraftingTreeNode pro.request( subInv, 1, src ); this.what.setStackSize( l ); - available = subInv.extractItems( this.what, Actionable.MODULATE, src ); + final IAEItemStack available = subInv.extractItems( this.what, Actionable.MODULATE, src ); if( available != null ) {