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 ForgeConfigSpec.BooleanValue optimizationUseCache;
private static ForgeConfigSpec.ConfigValue<List<? extends String>> blockBreakGlobalOperationsRaw;
public static List<GlobalOperationItem> blockBreakGlobalOperationItems;
private static ForgeConfigSpec.ConfigValue<List<? extends String>> blockBreakOperationsRaw;
public static List<OperationItem> 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.")

View File

@ -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<OperationItem> getBlockBreakCacheEntry(String key) {
public static @Nullable List<OperationItem> getBlockBreakCacheEntry(String key) {
if (blockBreakCache.containsKey(key)) {
return new ArrayList<>(blockBreakCache.get(key));
}

View File

@ -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<OperationItem> 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<OperationItem> 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);