Added config option for using the cache.

This commit is contained in:
2025-05-24 22:44:07 +01:00
parent 20a56f83e0
commit 042ebf10dc
3 changed files with 24 additions and 11 deletions

View File

@ -63,15 +63,23 @@ public final class Config {
} }
public static class Server { public static class Server {
public static ForgeConfigSpec.BooleanValue optimizationUseCache;
private static ForgeConfigSpec.ConfigValue<List<? extends String>> blockBreakGlobalOperationsRaw; private static ForgeConfigSpec.ConfigValue<List<? extends String>> blockBreakGlobalOperationsRaw;
public static List<GlobalOperationItem> blockBreakGlobalOperationItems; public static List<GlobalOperationItem> blockBreakGlobalOperationItems;
private static ForgeConfigSpec.ConfigValue<List<? extends String>> blockBreakOperationsRaw; private static ForgeConfigSpec.ConfigValue<List<? extends String>> blockBreakOperationsRaw;
public static List<OperationItem> blockBreakOperationItems; public static List<OperationItem> blockBreakOperationItems;
Server(ForgeConfigSpec.Builder builder) { 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("Settings for block breaking").push("block_breaking");
builder.comment("Available operations: " + Arrays.toString(OperationType.values()));
blockBreakGlobalOperationsRaw = builder blockBreakGlobalOperationsRaw = builder
.comment("List of global operations. Format: '[operation],[min],[max],[priority]'") .comment("List of global operations. Format: '[operation],[min],[max],[priority]'")
.comment("Global operations are run before any unique operations.") .comment("Global operations are run before any unique operations.")

View File

@ -1,5 +1,6 @@
package dev.micle.xptools.config; package dev.micle.xptools.config;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -11,7 +12,7 @@ public class OperationCache {
blockBreakCache = new HashMap<>(); blockBreakCache = new HashMap<>();
} }
public static List<OperationItem> getBlockBreakCacheEntry(String key) { public static @Nullable List<OperationItem> getBlockBreakCacheEntry(String key) {
if (blockBreakCache.containsKey(key)) { if (blockBreakCache.containsKey(key)) {
return new ArrayList<>(blockBreakCache.get(key)); return new ArrayList<>(blockBreakCache.get(key));
} }

View File

@ -21,6 +21,7 @@ import java.util.concurrent.ThreadLocalRandom;
public class OnBlockBreakEventHandler { public class OnBlockBreakEventHandler {
@SubscribeEvent @SubscribeEvent
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) { public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
Instant start = Instant.now();
float xpToDrop = event.getExpToDrop(); float xpToDrop = event.getExpToDrop();
// Get Block id // Get Block id
@ -31,11 +32,12 @@ public class OnBlockBreakEventHandler {
} }
// Collect operations // Collect operations
Instant start = Instant.now(); List<OperationItem> operations = null;
List<OperationItem> operations = OperationCache.getBlockBreakCacheEntry(block_id); if (Config.Server.optimizationUseCache.get()) {
if (operations != null) { operations = OperationCache.getBlockBreakCacheEntry(block_id);
XpTools.LOGGER.debug("Got operations from cache for {} [time: {}]", block_id, Duration.between(start, Instant.now()).toNanos()); }
} else {
if (operations == null) {
operations = new ArrayList<>(); operations = new ArrayList<>();
// Collect operations on relevant block_id // 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 // Save operations to cache
OperationCache.addBlockBreakCacheEntry(block_id, operations); OperationCache.addBlockBreakCacheEntry(block_id, operations);
} }
@ -108,8 +108,12 @@ public class OnBlockBreakEventHandler {
break; 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 // Apply xp drop
event.setExpToDrop((int)xpToDrop); event.setExpToDrop((int)xpToDrop);