diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index fb683cc..fd0ca36 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -63,15 +63,23 @@ public final class Config { } public static class Server { + public static ForgeConfigSpec.BooleanValue optimizationUseCache; + private static ForgeConfigSpec.ConfigValue> blockBreakGlobalOperationsRaw; public static List blockBreakGlobalOperationItems; private static ForgeConfigSpec.ConfigValue> blockBreakOperationsRaw; public static List blockBreakOperationItems; Server(ForgeConfigSpec.Builder builder) { - builder.comment("Available operations: " + Arrays.toString(OperationType.values())); + builder.comment("Settings for optimizations").push("optimization"); + optimizationUseCache = builder + .comment("When enabled, the list of operations to perform per unique_id will be cached after the first calculation.") + .comment("Although this does increase performance at the cost of RAM, the overall performance hit of this mod is tiny anyway... but oh well") + .define("optimizationUseCache", true); + builder.pop(); builder.comment("Settings for block breaking").push("block_breaking"); + builder.comment("Available operations: " + Arrays.toString(OperationType.values())); blockBreakGlobalOperationsRaw = builder .comment("List of global operations. Format: '[operation],[min],[max],[priority]'") .comment("Global operations are run before any unique operations.") diff --git a/src/main/java/dev/micle/xptools/config/OperationCache.java b/src/main/java/dev/micle/xptools/config/OperationCache.java index 7469f0c..50058e8 100644 --- a/src/main/java/dev/micle/xptools/config/OperationCache.java +++ b/src/main/java/dev/micle/xptools/config/OperationCache.java @@ -1,5 +1,6 @@ package dev.micle.xptools.config; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -11,7 +12,7 @@ public class OperationCache { blockBreakCache = new HashMap<>(); } - public static List getBlockBreakCacheEntry(String key) { + public static @Nullable List getBlockBreakCacheEntry(String key) { if (blockBreakCache.containsKey(key)) { return new ArrayList<>(blockBreakCache.get(key)); } 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 e7e6d28..a9f7891 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java @@ -21,6 +21,7 @@ import java.util.concurrent.ThreadLocalRandom; public class OnBlockBreakEventHandler { @SubscribeEvent public void OnBlockBreakEvent(BlockEvent.BreakEvent event) { + Instant start = Instant.now(); float xpToDrop = event.getExpToDrop(); // Get Block id @@ -31,11 +32,12 @@ public class OnBlockBreakEventHandler { } // Collect operations - Instant start = Instant.now(); - List operations = OperationCache.getBlockBreakCacheEntry(block_id); - if (operations != null) { - XpTools.LOGGER.debug("Got operations from cache for {} [time: {}]", block_id, Duration.between(start, Instant.now()).toNanos()); - } else { + List operations = null; + if (Config.Server.optimizationUseCache.get()) { + operations = OperationCache.getBlockBreakCacheEntry(block_id); + } + + if (operations == null) { operations = new ArrayList<>(); // Collect operations on relevant block_id @@ -68,8 +70,6 @@ public class OnBlockBreakEventHandler { } } - XpTools.LOGGER.debug("Calculated operations for {} [time: {}]", block_id, Duration.between(start, Instant.now()).toNanos()); - // Save operations to cache OperationCache.addBlockBreakCacheEntry(block_id, operations); } @@ -108,8 +108,12 @@ public class OnBlockBreakEventHandler { break; } } - XpTools.LOGGER.debug(operations.toString()); - XpTools.LOGGER.debug(String.valueOf(xpToDrop)); + + // Debug logging + XpTools.LOGGER.debug("Completed block break event:"); + XpTools.LOGGER.debug("\tOperations: {}", operations); + XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); + XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop); // Apply xp drop event.setExpToDrop((int)xpToDrop);