diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index 1f8b219..75d9dc3 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -132,8 +132,8 @@ public final class Config { private static void onConfigReload() { // Clear cache - OperationCache.clearBlockBreakCache(); - OperationCache.clearEntityKillCache(); + OperationCache.blockBreakCache.clear(); + OperationCache.entityKillCache.clear(); // Parse all block break global operations blockBreakGlobalOperationItems = new ArrayList<>(); diff --git a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java index 688e841..950ab56 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java @@ -15,6 +15,8 @@ import java.time.Instant; import java.util.List; public class OnBlockBreakEventHandler { + private final OperationCache cache = OperationCache.blockBreakCache; + @SubscribeEvent public void OnBlockBreakEvent(BlockEvent.BreakEvent event) { Instant start = Instant.now(); @@ -31,7 +33,7 @@ public class OnBlockBreakEventHandler { // Collect operations List operations = null; if (Config.Server.optimizationUseCache.get()) { - operations = OperationCache.getBlockBreakCacheEntry(block_id); + operations = cache.getEntry(block_id); } if (operations == null) { @@ -46,7 +48,7 @@ public class OnBlockBreakEventHandler { // Save operations to cache if (Config.Server.optimizationUseCache.get()) { - OperationCache.addBlockBreakCacheEntry(block_id, operations); + cache.addEntry(block_id, operations); } } @@ -61,6 +63,7 @@ public class OnBlockBreakEventHandler { XpTools.LOGGER.debug("Completed block break event:"); XpTools.LOGGER.debug("\tOperations: {}", operations); XpTools.LOGGER.debug("\tUsed cache: {}", usedCache); + XpTools.LOGGER.debug("\tCache entries: {}", cache.size()); XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop); } diff --git a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java index f0f2b70..08874d5 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -15,6 +15,8 @@ import java.time.Instant; import java.util.List; public class OnLivingExperienceDropEventHandler { + private final OperationCache cache = OperationCache.entityKillCache; + @SubscribeEvent public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) { Instant start = Instant.now(); @@ -31,7 +33,7 @@ public class OnLivingExperienceDropEventHandler { // Collect operations List operations = null; if (Config.Server.optimizationUseCache.get()) { - operations = OperationCache.getEntityKillCacheEntry(entity_id); + operations = cache.getEntry(entity_id); } if (operations == null) { @@ -46,7 +48,7 @@ public class OnLivingExperienceDropEventHandler { // Save operations to cache if (Config.Server.optimizationUseCache.get()) { - OperationCache.addEntityKillCacheEntry(entity_id, operations); + cache.addEntry(entity_id, operations); } } @@ -61,6 +63,7 @@ public class OnLivingExperienceDropEventHandler { XpTools.LOGGER.debug("Completed entity kill event:"); XpTools.LOGGER.debug("\tOperations: {}", operations); XpTools.LOGGER.debug("\tUsed cache: {}", usedCache); + XpTools.LOGGER.debug("\tCache entries: {}", cache.size()); XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop); } diff --git a/src/main/java/dev/micle/xptools/operation/OperationCache.java b/src/main/java/dev/micle/xptools/operation/OperationCache.java index 8a35668..63c701f 100644 --- a/src/main/java/dev/micle/xptools/operation/OperationCache.java +++ b/src/main/java/dev/micle/xptools/operation/OperationCache.java @@ -6,36 +6,28 @@ import java.util.HashMap; import java.util.List; public class OperationCache { - private static HashMap> blockBreakCache; - private static HashMap> entityKillCache; + public static OperationCache blockBreakCache = new OperationCache(); + public static OperationCache entityKillCache = new OperationCache(); + private HashMap> cache = new HashMap<>(); - public static void clearBlockBreakCache() { - blockBreakCache = new HashMap<>(); + private OperationCache() {} + + public void clear() { + cache = new HashMap<>(); } - public static void clearEntityKillCache() { - entityKillCache = new HashMap<>(); - } - - public static @Nullable List getBlockBreakCacheEntry(String key) { - if (blockBreakCache.containsKey(key)) { - return new ArrayList<>(blockBreakCache.get(key)); + public @Nullable List getEntry(String key) { + if (cache.containsKey(key)) { + return new ArrayList<>(cache.get(key)); } return null; } - public static @Nullable List getEntityKillCacheEntry(String key) { - if (entityKillCache.containsKey(key)) { - return new ArrayList<>(entityKillCache.get(key)); - } - return null; + public void addEntry(String key, List value) { + cache.putIfAbsent(key, new ArrayList<>(value)); } - public static void addBlockBreakCacheEntry(String key, List value) { - blockBreakCache.putIfAbsent(key, new ArrayList<>(value)); - } - - public static void addEntityKillCacheEntry(String key, List value) { - entityKillCache.putIfAbsent(key, new ArrayList<>(value)); + public int size() { + return cache.size(); } }