From a1c7e317c4f93dc06f87c57fe3e1c1ad81f44ac5 Mon Sep 17 00:00:00 2001 From: Micle Date: Sun, 25 May 2025 00:47:42 +0100 Subject: [PATCH] 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; + } +}