From c3b1dea7c5d10781d7d34e94abf12dbfdf6459df Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Wed, 31 Mar 2021 11:57:46 -0300 Subject: [PATCH] better level emitting --- .../appeng/me/cache/TickManagerCache.java | 2 +- .../parts/automation/PartLevelEmitter.java | 110 +++++++++++------- 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/main/java/appeng/me/cache/TickManagerCache.java b/src/main/java/appeng/me/cache/TickManagerCache.java index 65420bed1..10dac7c49 100644 --- a/src/main/java/appeng/me/cache/TickManagerCache.java +++ b/src/main/java/appeng/me/cache/TickManagerCache.java @@ -130,7 +130,6 @@ public class TickManagerCache implements ITickManager this.addToQueue( tt ); } } - PartLevelEmitter.wipeCaches(); } catch( final Throwable t ) { @@ -139,6 +138,7 @@ public class TickManagerCache implements ITickManager tt.addEntityCrashInfo( crashreportcategory ); throw new ReportedException( crashreport ); } + PartLevelEmitter.wipeCache(); } private void addToQueue( final TickTracker tt ) diff --git a/src/main/java/appeng/parts/automation/PartLevelEmitter.java b/src/main/java/appeng/parts/automation/PartLevelEmitter.java index 549320589..ab4899152 100644 --- a/src/main/java/appeng/parts/automation/PartLevelEmitter.java +++ b/src/main/java/appeng/parts/automation/PartLevelEmitter.java @@ -19,17 +19,14 @@ package appeng.parts.automation; -import javax.annotation.Nonnull; - -import java.util.Collection; -import java.util.HashMap; import java.util.Random; -import appeng.api.networking.IGridNode; -import appeng.api.networking.ticking.*; import appeng.me.Grid; import appeng.me.GridNode; import appeng.util.item.AEItemStack; +import it.unimi.dsi.fastutil.objects.Object2LongMap; +import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; @@ -126,9 +123,9 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH private double centerX; private double centerY; private double centerZ; - static HashMap gridItemCount = new HashMap<>(); - static HashMap> fuzzyAEItemStackCount = new HashMap<>(); - static HashMap> preciseAEItemStackCount = new HashMap<>(); + static Object2LongMap gridItemCount = new Object2LongOpenHashMap<>(); + static Object2ObjectOpenHashMap> fuzzyAEItemStackCount = new Object2ObjectOpenHashMap<>(); + static Object2ObjectOpenHashMap> preciseAEItemStackCount = new Object2ObjectOpenHashMap<>(); @Reflected public PartLevelEmitter( final ItemStack is ) @@ -326,6 +323,11 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH } private void updateReportingValue( final IMEMonitor monitor ) + { + updateReportingValue( monitor,null ); + } + + private void updateReportingValue( final IMEMonitor monitor, Iterable change ) { final IAEItemStack myStack = this.config.getAEStackInSlot( 0 ); @@ -333,55 +335,77 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH if( node != null ) { + final Grid g = node.getInternalGrid(); if( myStack == null ) { - if( !gridItemCount.containsKey( g ) ) + if( change != null ) { - this.lastReportedValue = 0; - for( final IAEItemStack st : monitor.getStorageList() ) + if( gridItemCount.containsKey( g ) ) { - this.lastReportedValue += st.getStackSize(); + change.forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() ); } - gridItemCount.put( g,lastReportedValue ); - } else { this.lastReportedValue = gridItemCount.get( g ); } + } + gridItemCount.computeIfAbsent( g, ( aLong ) -> { + this.lastReportedValue = 0; + monitor.getStorageList().forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() ); + return ( lastReportedValue ); + } ); + this.lastReportedValue = gridItemCount.get( g ); } + else if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 ) { - if( fuzzyAEItemStackCount.isEmpty() || !fuzzyAEItemStackCount.containsKey( g ) || - ( fuzzyAEItemStackCount.containsKey( g ) && !fuzzyAEItemStackCount.get( g ).containsKey( (AEItemStack) myStack ) ) ) + final FuzzyMode fzMode = (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE ); + if( change != null ) { - this.lastReportedValue = 0; - final FuzzyMode fzMode = (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE ); - final Collection fuzzyList = monitor.getStorageList().findFuzzy( myStack, fzMode ); - for( final IAEItemStack st : fuzzyList ) + if( fuzzyAEItemStackCount.containsKey( g ) ) { - this.lastReportedValue += st.getStackSize(); + change.forEach( iaeItemStack -> { + if( iaeItemStack.sameOre( myStack ) ) lastReportedValue += iaeItemStack.getStackSize(); + } ); } - fuzzyAEItemStackCount.put( g, new HashMap<>( 1 )); - fuzzyAEItemStackCount.get( g ).put( (AEItemStack) myStack.copy(),lastReportedValue ); - } else { - this.lastReportedValue = fuzzyAEItemStackCount.get( g ).get( (AEItemStack) myStack ); + } + + fuzzyAEItemStackCount.computeIfAbsent( g, ( grid -> new Object2LongOpenHashMap<>() ) ); + fuzzyAEItemStackCount.get( g ).computeIfAbsent( (AEItemStack) myStack, ( aLong -> { + this.lastReportedValue = 0; + monitor.getStorageList().findFuzzy( myStack, fzMode ).forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() ); + return ( lastReportedValue ); + } ) ); + + this.lastReportedValue = fuzzyAEItemStackCount.get( g ).get( myStack ); + if( this.lastReportedValue == 0 ) + { + fuzzyAEItemStackCount.get( g ).remove( myStack ); + fuzzyAEItemStackCount.trim(); } } else { - if( preciseAEItemStackCount.isEmpty() || !preciseAEItemStackCount.containsKey( g ) || - ( preciseAEItemStackCount.containsKey( g ) && !preciseAEItemStackCount.get( g ).containsKey( (AEItemStack) myStack ) ) ) + if( change != null ) { - final IAEItemStack r = monitor.getStorageList().findPrecise( myStack ); - if( r == null ) + if( preciseAEItemStackCount.containsKey( g ) ) { - this.lastReportedValue = 0; + change.forEach( iaeItemStack -> { + if( iaeItemStack.isSameType( myStack ) ) lastReportedValue += iaeItemStack.getStackSize(); + } ); } - else - { - this.lastReportedValue = r.getStackSize(); - } - preciseAEItemStackCount.put( g, new HashMap<>( 1 )); - preciseAEItemStackCount.get( g ).put( (AEItemStack) myStack.copy(),lastReportedValue ); - } else { this.lastReportedValue = preciseAEItemStackCount.get( g ).get( (AEItemStack) myStack ); } + } + preciseAEItemStackCount.computeIfAbsent( g, ( grid -> new Object2LongOpenHashMap<>() ) ); + preciseAEItemStackCount.get( g ).computeIfAbsent( (AEItemStack) myStack, ( aLong -> { + this.lastReportedValue = 0; + IAEItemStack precise = monitor.getStorageList().findPrecise( myStack ); + if( precise != null ) lastReportedValue = precise.getStackSize(); + return ( lastReportedValue ); + } ) ); + this.lastReportedValue = preciseAEItemStackCount.get( g ).get( myStack ); + if( this.lastReportedValue == 0 ) + { + preciseAEItemStackCount.get( g ).remove( myStack ); + preciseAEItemStackCount.trim(); + } } } this.updateState(); @@ -435,7 +459,7 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH @Override public void postChange( final IBaseMonitor monitor, final Iterable change, final IActionSource actionSource ) { - this.updateReportingValue( (IMEMonitor) monitor ); + this.updateReportingValue( (IMEMonitor) monitor, change ); } @Override @@ -612,9 +636,9 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH } } - static public void wipeCaches(){ - gridItemCount = new HashMap<>(); - fuzzyAEItemStackCount = new HashMap<>(); - preciseAEItemStackCount = new HashMap<>(); + public static void wipeCache(){ + fuzzyAEItemStackCount.clear(); + preciseAEItemStackCount.clear(); + gridItemCount.clear(); } }