From e4c444c6f83f6e3a485bfec348c7fcd26d8b2ed2 Mon Sep 17 00:00:00 2001 From: Micle Date: Mon, 26 May 2025 18:56:43 +0100 Subject: [PATCH 1/3] Using config option for saving to cache. --- .../micle/xptools/events/common/OnBlockBreakEventHandler.java | 4 +++- .../events/common/OnLivingExperienceDropEventHandler.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) 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 2dfd2a8..688e841 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java @@ -45,7 +45,9 @@ public class OnBlockBreakEventHandler { ); // Save operations to cache - OperationCache.addBlockBreakCacheEntry(block_id, operations); + if (Config.Server.optimizationUseCache.get()) { + OperationCache.addBlockBreakCacheEntry(block_id, operations); + } } // Add global operations before all others 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 91f5917..f0f2b70 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -45,7 +45,9 @@ public class OnLivingExperienceDropEventHandler { ); // Save operations to cache - OperationCache.addEntityKillCacheEntry(entity_id, operations); + if (Config.Server.optimizationUseCache.get()) { + OperationCache.addEntityKillCacheEntry(entity_id, operations); + } } // Add global operations before all others From 40f2a491b168e5d2793935ada8b6934c98c73f1e Mon Sep 17 00:00:00 2001 From: Micle Date: Mon, 26 May 2025 18:58:20 +0100 Subject: [PATCH 2/3] Unified cache implementation. Using references in events. --- .../java/dev/micle/xptools/config/Config.java | 4 +-- .../common/OnBlockBreakEventHandler.java | 7 ++-- .../OnLivingExperienceDropEventHandler.java | 7 ++-- .../xptools/operation/OperationCache.java | 36 ++++++++----------- 4 files changed, 26 insertions(+), 28 deletions(-) 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(); } } From 9ba56d55ee3f3b8b6be38b8f63cf5eb02ef280e4 Mon Sep 17 00:00:00 2001 From: Micle Date: Mon, 26 May 2025 19:15:38 +0100 Subject: [PATCH 3/3] Moved more similar code to utility method. --- .../common/OnBlockBreakEventHandler.java | 30 +++------ .../OnLivingExperienceDropEventHandler.java | 30 +++------ .../xptools/operation/OperationUtils.java | 61 ++++++++++++------- 3 files changed, 52 insertions(+), 69 deletions(-) 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 950ab56..8edae82 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java @@ -20,7 +20,6 @@ public class OnBlockBreakEventHandler { @SubscribeEvent public void OnBlockBreakEvent(BlockEvent.BreakEvent event) { Instant start = Instant.now(); - boolean usedCache = true; float xpToDrop = event.getExpToDrop(); // Get Block id @@ -30,27 +29,13 @@ public class OnBlockBreakEventHandler { block_id = block_rl.toString(); } - // Collect operations - List operations = null; - if (Config.Server.optimizationUseCache.get()) { - operations = cache.getEntry(block_id); - } - - if (operations == null) { - usedCache = false; - - // Calculate operations - operations = OperationUtils.calculateOperationList( - block_id, - event.getState().getTags().toList(), - Config.Server.blockBreakOperationItems - ); - - // Save operations to cache - if (Config.Server.optimizationUseCache.get()) { - cache.addEntry(block_id, operations); - } - } + // Get operations + List operations = OperationUtils.getOperationList( + block_id, + event.getState().getTags().toList(), + Config.Server.blockBreakOperationItems, + cache + ); // Add global operations before all others operations.addAll(0, Config.Server.blockBreakGlobalOperationItems); @@ -62,7 +47,6 @@ public class OnBlockBreakEventHandler { if (Config.Server.debugExtra.get()) { 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 08874d5..f7620d6 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -20,7 +20,6 @@ public class OnLivingExperienceDropEventHandler { @SubscribeEvent public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) { Instant start = Instant.now(); - boolean usedCache = true; float xpToDrop = event.getDroppedExperience(); // Get Entity id @@ -30,27 +29,13 @@ public class OnLivingExperienceDropEventHandler { entity_id = entity_rl.toString(); } - // Collect operations - List operations = null; - if (Config.Server.optimizationUseCache.get()) { - operations = cache.getEntry(entity_id); - } - - if (operations == null) { - usedCache = false; - - // Calculate operations - operations = OperationUtils.calculateOperationList( - entity_id, - event.getEntity().getType().getTags().toList(), - Config.Server.entityKillOperationItems - ); - - // Save operations to cache - if (Config.Server.optimizationUseCache.get()) { - cache.addEntry(entity_id, operations); - } - } + // Get operations + List operations = OperationUtils.getOperationList( + entity_id, + event.getEntity().getType().getTags().toList(), + Config.Server.entityKillOperationItems, + cache + ); // Add global operations before all others operations.addAll(0, Config.Server.entityKillGlobalOperationItems); @@ -62,7 +47,6 @@ public class OnLivingExperienceDropEventHandler { if (Config.Server.debugExtra.get()) { 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/OperationUtils.java b/src/main/java/dev/micle/xptools/operation/OperationUtils.java index f0ed28e..8ecfbed 100644 --- a/src/main/java/dev/micle/xptools/operation/OperationUtils.java +++ b/src/main/java/dev/micle/xptools/operation/OperationUtils.java @@ -1,5 +1,6 @@ package dev.micle.xptools.operation; +import dev.micle.xptools.config.Config; import net.minecraft.tags.TagKey; import java.util.ArrayList; @@ -8,36 +9,50 @@ import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class OperationUtils { - public static > List calculateOperationList(String id, List tagList, List operationItems) { - List operations = new ArrayList<>(); + public static > List getOperationList(String id, List tagList, List operationItems, OperationCache cache) { + List operations = null; - // Collect operations on relevant id - if (!id.isEmpty()) { - for (OperationItem operationItem : operationItems) { - if (!operationItem.isTag() && operationItem.getId().equals(id)) { - operations.add(operationItem); - } - } + // Get from cache if possible + if (Config.Server.optimizationUseCache.get()) { + operations = cache.getEntry(id); } - // Collect operations on relevant tag_id - for (T tagKey : tagList) { - String tag_id = tagKey.location().toString(); - for (OperationItem operationItem : operationItems) { - if (operationItem.isTag() && operationItem.getId().equals(tag_id)) { - operations.add(operationItem); + if (operations == null) { + operations = new ArrayList<>(); + + // Collect operations on relevant id + if (!id.isEmpty()) { + for (OperationItem operationItem : operationItems) { + if (!operationItem.isTag() && operationItem.getId().equals(id)) { + operations.add(operationItem); + } } } - } - // Sort operations based on priority - operations.sort(Comparator.comparingInt(OperationItem::getPriority)); + // Collect operations on relevant tag_id + for (T tagKey : tagList) { + String tag_id = tagKey.location().toString(); + for (OperationItem operationItem : operationItems) { + if (operationItem.isTag() && operationItem.getId().equals(tag_id)) { + operations.add(operationItem); + } + } + } - // Remove any operations after last operation - for (OperationItem operationItem : operations) { - if (operationItem.isLast()) { - operations = operations.subList(0, operations.indexOf(operationItem) + 1); - break; + // Sort operations based on priority + operations.sort(Comparator.comparingInt(OperationItem::getPriority)); + + // Remove any operations after last operation + for (OperationItem operationItem : operations) { + if (operationItem.isLast()) { + operations = operations.subList(0, operations.indexOf(operationItem) + 1); + break; + } + } + + // Save to cache if possible + if (Config.Server.optimizationUseCache.get()) { + cache.addEntry(id, operations); } }