From 0a828367ee0ee6f1f13f8815df7a80e177fdb572 Mon Sep 17 00:00:00 2001 From: Micle Date: Sat, 24 May 2025 23:34:52 +0100 Subject: [PATCH 1/9] Created event handler. --- .../common/OnLivingExperienceDropEventHandler.java | 9 +++++++++ src/main/java/dev/micle/xptools/proxy/Proxy.java | 2 ++ 2 files changed, 11 insertions(+) create mode 100644 src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java diff --git a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java new file mode 100644 index 0000000..6cc7a23 --- /dev/null +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -0,0 +1,9 @@ +package dev.micle.xptools.events.common; + +import net.minecraftforge.event.entity.living.LivingExperienceDropEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +public class OnLivingExperienceDropEventHandler { + @SubscribeEvent + public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {} +} diff --git a/src/main/java/dev/micle/xptools/proxy/Proxy.java b/src/main/java/dev/micle/xptools/proxy/Proxy.java index eefac49..58ea058 100644 --- a/src/main/java/dev/micle/xptools/proxy/Proxy.java +++ b/src/main/java/dev/micle/xptools/proxy/Proxy.java @@ -3,6 +3,7 @@ package dev.micle.xptools.proxy; import dev.micle.xptools.XpTools; import dev.micle.xptools.config.Config; import dev.micle.xptools.events.common.OnBlockBreakEventHandler; +import dev.micle.xptools.events.common.OnLivingExperienceDropEventHandler; import net.minecraft.client.Minecraft; import net.minecraft.server.MinecraftServer; import net.minecraft.world.entity.player.Player; @@ -34,6 +35,7 @@ public class Proxy implements IProxy { // Register event handlers MinecraftForge.EVENT_BUS.register(new OnBlockBreakEventHandler()); + MinecraftForge.EVENT_BUS.register(new OnLivingExperienceDropEventHandler()); } private static void setup(FMLCommonSetupEvent event) {} From e4a1a08cf5fa897c0f4ce8216db8ee4b2e6e9566 Mon Sep 17 00:00:00 2001 From: Micle Date: Sat, 24 May 2025 23:51:25 +0100 Subject: [PATCH 2/9] Added config options for operations. --- .../java/dev/micle/xptools/config/Config.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index 687ee33..1320aa4 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -76,6 +76,11 @@ public final class Config { private static ForgeConfigSpec.ConfigValue> blockBreakOperationsRaw; public static List blockBreakOperationItems; + private static ForgeConfigSpec.ConfigValue> entityKillGlobalOperationsRaw; + public static List entityKillGlobalOperationItems; + private static ForgeConfigSpec.ConfigValue> entityKillOperationsRaw; + public static List entityKillOperationItems; + Server(ForgeConfigSpec.Builder builder) { builder.comment("Settings for debugging").push("debug"); debugExtra = builder @@ -105,6 +110,21 @@ public final class Config { .comment("'#forge:ores,multiply,1,2,1,false' - Multiplies xp drop of all blocks tagged forge:ores by 1-2, allows additional operations.") .define("blockBreakOperations", new ArrayList<>()); builder.pop(); + + builder.comment("Settings for entity killing").push("entity_killing"); + builder.comment("Available operations: " + Arrays.toString(OperationType.values())); + entityKillGlobalOperationsRaw = builder + .comment("List of global operations. Format: '[operation],[min],[max],[priority]'") + .comment("Global operations are run before any unique operations.") + .comment("Examples:") + .comment("'set,0,0,0' - Sets the xp of all blocks to 0.") + .define("entityKillGlobalOperations", new ArrayList<>()); + entityKillOperationsRaw = builder + .comment("List of unique operations. Format: '[entity_id],[operation],[min],[max],[priority],[is_last]'") + .comment("Examples:") + .comment("'minecraft:creeper,set,2,2,0,true' - Sets the xp drop for killing creepers to 2, takes highest priority and stops any additional operations.") + .define("entityKillOperations", new ArrayList<>()); + builder.pop(); } private static void onConfigReload() { @@ -123,6 +143,19 @@ public final class Config { for (String s : blockBreakOperationsRaw.get()) { blockBreakOperationItems.add(OperationItem.fromConfig(s)); } + + // Parse all entity kill global operations + entityKillGlobalOperationItems = new ArrayList<>(); + for (String s : entityKillGlobalOperationsRaw.get()) { + entityKillGlobalOperationItems.add(GlobalOperationItem.fromConfig(s)); + } + entityKillGlobalOperationItems.sort(Comparator.comparingInt(OperationItem::getPriority)); + + // Parse all entity kill unique operations + entityKillOperationItems = new ArrayList<>(); + for (String s : entityKillOperationsRaw.get()) { + entityKillOperationItems.add(OperationItem.fromConfig(s)); + } } } } From d0e1eb1a2fb66c1df4bfb4119f9c762cf5c261f9 Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 00:10:14 +0100 Subject: [PATCH 3/9] Updated config comment to mention tags. --- src/main/java/dev/micle/xptools/config/Config.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index 1320aa4..9151360 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -120,9 +120,10 @@ public final class Config { .comment("'set,0,0,0' - Sets the xp of all blocks to 0.") .define("entityKillGlobalOperations", new ArrayList<>()); entityKillOperationsRaw = builder - .comment("List of unique operations. Format: '[entity_id],[operation],[min],[max],[priority],[is_last]'") + .comment("List of unique operations. Format: '[entity_id/tag_id],[operation],[min],[max],[priority],[is_last]'") .comment("Examples:") .comment("'minecraft:creeper,set,2,2,0,true' - Sets the xp drop for killing creepers to 2, takes highest priority and stops any additional operations.") + .comment("'#some_mod:some_tag,multiply,1,2,1,false' - Multiplies the xp drop for killing all entities tagged some_mod:some_tag by 1-2, allows additional operations.") .define("entityKillOperations", new ArrayList<>()); builder.pop(); } From 008960ba57fb1fc5a02464a14b2d2764cfb2e978 Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 00:13:13 +0100 Subject: [PATCH 4/9] Added entity kill operation cache. --- .../java/dev/micle/xptools/config/Config.java | 1 + .../micle/xptools/operation/OperationCache.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index 9151360..9a5d2da 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -131,6 +131,7 @@ public final class Config { private static void onConfigReload() { // Clear cache OperationCache.clearBlockBreakCache(); + OperationCache.clearEntityKillCache(); // Parse all block break global operations blockBreakGlobalOperationItems = new ArrayList<>(); diff --git a/src/main/java/dev/micle/xptools/operation/OperationCache.java b/src/main/java/dev/micle/xptools/operation/OperationCache.java index 446d716..8a35668 100644 --- a/src/main/java/dev/micle/xptools/operation/OperationCache.java +++ b/src/main/java/dev/micle/xptools/operation/OperationCache.java @@ -7,11 +7,16 @@ import java.util.List; public class OperationCache { private static HashMap> blockBreakCache; + private static HashMap> entityKillCache; public static void clearBlockBreakCache() { blockBreakCache = 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)); @@ -19,7 +24,18 @@ public class OperationCache { return null; } + public static @Nullable List getEntityKillCacheEntry(String key) { + if (entityKillCache.containsKey(key)) { + return new ArrayList<>(entityKillCache.get(key)); + } + return null; + } + 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)); + } } From aedc41b4ec0f618f9b17fb1d60feaee9143c1d9e Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 00:23:44 +0100 Subject: [PATCH 5/9] Fixed comment. --- src/main/java/dev/micle/xptools/config/Config.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index 9a5d2da..6f02c2e 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -117,7 +117,7 @@ public final class Config { .comment("List of global operations. Format: '[operation],[min],[max],[priority]'") .comment("Global operations are run before any unique operations.") .comment("Examples:") - .comment("'set,0,0,0' - Sets the xp of all blocks to 0.") + .comment("'set,0,0,0' - Sets the xp of all entities to 0.") .define("entityKillGlobalOperations", new ArrayList<>()); entityKillOperationsRaw = builder .comment("List of unique operations. Format: '[entity_id/tag_id],[operation],[min],[max],[priority],[is_last]'") From a99a36496ac48e239f5591a342e13dbac45a90f5 Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 00:24:13 +0100 Subject: [PATCH 6/9] Implemented logic for entity killing. --- .../OnLivingExperienceDropEventHandler.java | 116 +++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) 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 6cc7a23..f9d4a51 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -1,9 +1,123 @@ package dev.micle.xptools.events.common; +import dev.micle.xptools.XpTools; +import dev.micle.xptools.config.Config; +import dev.micle.xptools.operation.OperationCache; +import dev.micle.xptools.operation.OperationItem; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.world.entity.EntityType; import net.minecraftforge.event.entity.living.LivingExperienceDropEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.registries.ForgeRegistries; + +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; public class OnLivingExperienceDropEventHandler { @SubscribeEvent - public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {} + public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) { + Instant start = Instant.now(); + float xpToDrop = event.getDroppedExperience(); + + // Get Entity id + ResourceLocation entity_rl = ForgeRegistries.ENTITY_TYPES.getKey(event.getEntity().getType()); + String entity_id = ""; + if (entity_rl != null) { + entity_id = entity_rl.toString(); + } + + // Collect operations + List operations = null; + if (Config.Server.optimizationUseCache.get()) { + operations = OperationCache.getEntityKillCacheEntry(entity_id); + } + + if (operations == null) { + operations = new ArrayList<>(); + + // Collect operations on relevant entity_id + if (!entity_id.isEmpty()) { + for (OperationItem operationItem : Config.Server.entityKillOperationItems) { + if (!operationItem.isTag() && operationItem.getId().equals(entity_id)) { + operations.add(operationItem); + } + } + } + + // Collect operations on relevant tag_id + for (TagKey> tagKey : event.getEntity().getType().getTags().toList()) { + String tag_id = tagKey.location().toString(); + for (OperationItem operationItem : Config.Server.entityKillOperationItems) { + if (operationItem.isTag() && operationItem.getId().equals(tag_id)) { + operations.add(operationItem); + } + } + } + + // 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 operations to cache + OperationCache.addEntityKillCacheEntry(entity_id, operations); + } + + // Add global operations before all others + operations.addAll(0, Config.Server.entityKillGlobalOperationItems); + + // Apply operations to xp drops + for (OperationItem operation : operations) { + // Calculate operation value + float opValue = (operation.getMin() == operation.getMax()) ? + operation.getMin() : + ThreadLocalRandom.current().nextFloat(operation.getMin(), operation.getMax()); + + // Apply operation + switch (operation.getType()) { + case SET: + xpToDrop = opValue; + break; + case ADD: + xpToDrop += opValue; + break; + case SUBTRACT: + xpToDrop -= opValue; + break; + case MULTIPLY: + xpToDrop *= opValue; + break; + case DIVIDE: + xpToDrop /= opValue; + break; + } + + // Stop if this is the last operation + if (operation.isLast()) { + break; + } + } + + // Debug logging + if (Config.Server.debugExtra.get()) { + XpTools.LOGGER.debug("Completed entity kill event:"); + XpTools.LOGGER.debug("\tOperations: {}", operations); + XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); + XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop); + } + + // Apply xp drop + event.setDroppedExperience((int)xpToDrop); + } } From a1c7e317c4f93dc06f87c57fe3e1c1ad81f44ac5 Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 00:47:42 +0100 Subject: [PATCH 7/9] Split some common code from event handlers into new class OperationUtils. --- .../common/OnBlockBreakEventHandler.java | 74 ++---------------- .../OnLivingExperienceDropEventHandler.java | 74 ++---------------- .../xptools/operation/OperationUtils.java | 76 +++++++++++++++++++ 3 files changed, 92 insertions(+), 132 deletions(-) create mode 100644 src/main/java/dev/micle/xptools/operation/OperationUtils.java 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 cce9704..e329085 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java @@ -4,19 +4,15 @@ import dev.micle.xptools.XpTools; import dev.micle.xptools.config.Config; import dev.micle.xptools.operation.OperationCache; import dev.micle.xptools.operation.OperationItem; +import dev.micle.xptools.operation.OperationUtils; import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.TagKey; -import net.minecraft.world.level.block.Block; import net.minecraftforge.event.level.BlockEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.registries.ForgeRegistries; import java.time.Duration; import java.time.Instant; -import java.util.ArrayList; -import java.util.Comparator; import java.util.List; -import java.util.concurrent.ThreadLocalRandom; public class OnBlockBreakEventHandler { @SubscribeEvent @@ -38,37 +34,12 @@ public class OnBlockBreakEventHandler { } if (operations == null) { - operations = new ArrayList<>(); - - // Collect operations on relevant block_id - if (!block_id.isEmpty()) { - for (OperationItem operationItem : Config.Server.blockBreakOperationItems) { - if (!operationItem.isTag() && operationItem.getId().equals(block_id)) { - operations.add(operationItem); - } - } - } - - // Collect operations on relevant tag_id - for (TagKey tagKey : event.getState().getTags().toList()) { - String tag_id = tagKey.location().toString(); - for (OperationItem operationItem : Config.Server.blockBreakOperationItems) { - if (operationItem.isTag() && operationItem.getId().equals(tag_id)) { - operations.add(operationItem); - } - } - } - - // 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; - } - } + // Calculate operations + operations = OperationUtils.calculateOperationList( + block_id, + event.getState().getTags().toList(), + Config.Server.blockBreakOperationItems + ); // Save operations to cache OperationCache.addBlockBreakCacheEntry(block_id, operations); @@ -78,36 +49,7 @@ public class OnBlockBreakEventHandler { operations.addAll(0, Config.Server.blockBreakGlobalOperationItems); // Apply operations to xp drops - for (OperationItem operation : operations) { - // Calculate operation value - float opValue = (operation.getMin() == operation.getMax()) ? - operation.getMin() : - ThreadLocalRandom.current().nextFloat(operation.getMin(), operation.getMax()); - - // Apply operation - switch (operation.getType()) { - case SET: - xpToDrop = opValue; - break; - case ADD: - xpToDrop += opValue; - break; - case SUBTRACT: - xpToDrop -= opValue; - break; - case MULTIPLY: - xpToDrop *= opValue; - break; - case DIVIDE: - xpToDrop /= opValue; - break; - } - - // Stop if this is the last operation - if (operation.isLast()) { - break; - } - } + xpToDrop = OperationUtils.calculateNewXpAmount(xpToDrop, operations); // Debug logging if (Config.Server.debugExtra.get()) { 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 f9d4a51..765ae80 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -4,19 +4,15 @@ import dev.micle.xptools.XpTools; import dev.micle.xptools.config.Config; import dev.micle.xptools.operation.OperationCache; import dev.micle.xptools.operation.OperationItem; +import dev.micle.xptools.operation.OperationUtils; import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.TagKey; -import net.minecraft.world.entity.EntityType; import net.minecraftforge.event.entity.living.LivingExperienceDropEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.registries.ForgeRegistries; import java.time.Duration; import java.time.Instant; -import java.util.ArrayList; -import java.util.Comparator; import java.util.List; -import java.util.concurrent.ThreadLocalRandom; public class OnLivingExperienceDropEventHandler { @SubscribeEvent @@ -38,37 +34,12 @@ public class OnLivingExperienceDropEventHandler { } if (operations == null) { - operations = new ArrayList<>(); - - // Collect operations on relevant entity_id - if (!entity_id.isEmpty()) { - for (OperationItem operationItem : Config.Server.entityKillOperationItems) { - if (!operationItem.isTag() && operationItem.getId().equals(entity_id)) { - operations.add(operationItem); - } - } - } - - // Collect operations on relevant tag_id - for (TagKey> tagKey : event.getEntity().getType().getTags().toList()) { - String tag_id = tagKey.location().toString(); - for (OperationItem operationItem : Config.Server.entityKillOperationItems) { - if (operationItem.isTag() && operationItem.getId().equals(tag_id)) { - operations.add(operationItem); - } - } - } - - // 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; - } - } + // Calculate operations + operations = OperationUtils.calculateOperationList( + entity_id, + event.getEntity().getType().getTags().toList(), + Config.Server.entityKillOperationItems + ); // Save operations to cache OperationCache.addEntityKillCacheEntry(entity_id, operations); @@ -78,36 +49,7 @@ public class OnLivingExperienceDropEventHandler { operations.addAll(0, Config.Server.entityKillGlobalOperationItems); // Apply operations to xp drops - for (OperationItem operation : operations) { - // Calculate operation value - float opValue = (operation.getMin() == operation.getMax()) ? - operation.getMin() : - ThreadLocalRandom.current().nextFloat(operation.getMin(), operation.getMax()); - - // Apply operation - switch (operation.getType()) { - case SET: - xpToDrop = opValue; - break; - case ADD: - xpToDrop += opValue; - break; - case SUBTRACT: - xpToDrop -= opValue; - break; - case MULTIPLY: - xpToDrop *= opValue; - break; - case DIVIDE: - xpToDrop /= opValue; - break; - } - - // Stop if this is the last operation - if (operation.isLast()) { - break; - } - } + xpToDrop = OperationUtils.calculateNewXpAmount(xpToDrop, operations); // Debug logging if (Config.Server.debugExtra.get()) { diff --git a/src/main/java/dev/micle/xptools/operation/OperationUtils.java b/src/main/java/dev/micle/xptools/operation/OperationUtils.java new file mode 100644 index 0000000..f0ed28e --- /dev/null +++ b/src/main/java/dev/micle/xptools/operation/OperationUtils.java @@ -0,0 +1,76 @@ +package dev.micle.xptools.operation; + +import net.minecraft.tags.TagKey; + +import java.util.ArrayList; +import java.util.Comparator; +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<>(); + + // Collect operations on relevant id + if (!id.isEmpty()) { + for (OperationItem operationItem : operationItems) { + if (!operationItem.isTag() && operationItem.getId().equals(id)) { + operations.add(operationItem); + } + } + } + + // 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); + } + } + } + + // 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; + } + } + + return operations; + } + + public static float calculateNewXpAmount(float xp, List operations) { + for (OperationItem operation : operations) { + // Calculate operation value + float opValue = (operation.getMin() == operation.getMax()) ? + operation.getMin() : + ThreadLocalRandom.current().nextFloat(operation.getMin(), operation.getMax()); + + // Apply operation + switch (operation.getType()) { + case SET: + xp = opValue; + break; + case ADD: + xp += opValue; + break; + case SUBTRACT: + xp -= opValue; + break; + case MULTIPLY: + xp *= opValue; + break; + case DIVIDE: + xp /= opValue; + break; + } + } + + return xp; + } +} From fd0845c9464782c0f82b765ad44653c67419734f Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 01:07:20 +0100 Subject: [PATCH 8/9] Added extra debugging info. --- .../micle/xptools/events/common/OnBlockBreakEventHandler.java | 4 ++++ .../events/common/OnLivingExperienceDropEventHandler.java | 4 ++++ 2 files changed, 8 insertions(+) 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 e329085..2dfd2a8 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnBlockBreakEventHandler.java @@ -18,6 +18,7 @@ public class OnBlockBreakEventHandler { @SubscribeEvent public void OnBlockBreakEvent(BlockEvent.BreakEvent event) { Instant start = Instant.now(); + boolean usedCache = true; float xpToDrop = event.getExpToDrop(); // Get Block id @@ -34,6 +35,8 @@ public class OnBlockBreakEventHandler { } if (operations == null) { + usedCache = false; + // Calculate operations operations = OperationUtils.calculateOperationList( block_id, @@ -55,6 +58,7 @@ 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("\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 765ae80..91f5917 100644 --- a/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java +++ b/src/main/java/dev/micle/xptools/events/common/OnLivingExperienceDropEventHandler.java @@ -18,6 +18,7 @@ public class OnLivingExperienceDropEventHandler { @SubscribeEvent public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) { Instant start = Instant.now(); + boolean usedCache = true; float xpToDrop = event.getDroppedExperience(); // Get Entity id @@ -34,6 +35,8 @@ public class OnLivingExperienceDropEventHandler { } if (operations == null) { + usedCache = false; + // Calculate operations operations = OperationUtils.calculateOperationList( entity_id, @@ -55,6 +58,7 @@ 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("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop); } From 06a26771eea270007efc24db90ab6bcd84df2d00 Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 01:13:26 +0100 Subject: [PATCH 9/9] Improved cache config option description. Disabled cache by default. --- src/main/java/dev/micle/xptools/config/Config.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/micle/xptools/config/Config.java b/src/main/java/dev/micle/xptools/config/Config.java index 6f02c2e..1f8b219 100644 --- a/src/main/java/dev/micle/xptools/config/Config.java +++ b/src/main/java/dev/micle/xptools/config/Config.java @@ -90,9 +90,11 @@ public final class Config { 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); + .comment("Allows saving lists of operations per unique id (block_id/entity_id etc.).") + .comment("This will speed up getting the order of operations after the initial calculation.") + .comment("The downside is that it uses more RAM of course (I don't know how much), while it shouldn't be a lot it might not be worth it if you don't have many operations.") + .comment("The cache is not persistent and gets cleared whenever the config gets reloaded.") + .define("optimizationUseCache", false); builder.pop(); builder.comment("Settings for block breaking").push("block_breaking");