Moved more similar code to utility method.

This commit is contained in:
2025-05-26 19:15:38 +01:00
parent eba233a983
commit ef99967059
3 changed files with 52 additions and 69 deletions

View File

@ -20,7 +20,6 @@ public class OnBlockBreakEventHandler {
@SubscribeEvent @SubscribeEvent
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) { public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
Instant start = Instant.now(); Instant start = Instant.now();
boolean usedCache = true;
float xpToDrop = event.getExpToDrop(); float xpToDrop = event.getExpToDrop();
// Get Block id // Get Block id
@ -30,27 +29,13 @@ public class OnBlockBreakEventHandler {
block_id = block_rl.toString(); block_id = block_rl.toString();
} }
// Collect operations // Get operations
List<OperationItem> operations = null; List<OperationItem> operations = OperationUtils.getOperationList(
if (Config.Server.optimizationUseCache.get()) { block_id,
operations = cache.getEntry(block_id); event.getState().getTags().toList(),
} Config.Server.blockBreakOperationItems,
cache
if (operations == null) { );
usedCache = false;
// Calculate operations
operations = OperationUtils.calculateOperationList(
block_id,
event.getState().getTags().toList(),
Config.Server.blockBreakOperationItems
);
// Save operations to cache
if (Config.Server.optimizationUseCache.get()) {
cache.addEntry(block_id, operations);
}
}
// Add global operations before all others // Add global operations before all others
operations.addAll(0, Config.Server.blockBreakGlobalOperationItems); operations.addAll(0, Config.Server.blockBreakGlobalOperationItems);
@ -62,7 +47,6 @@ public class OnBlockBreakEventHandler {
if (Config.Server.debugExtra.get()) { if (Config.Server.debugExtra.get()) {
XpTools.LOGGER.debug("Completed block break event:"); XpTools.LOGGER.debug("Completed block break event:");
XpTools.LOGGER.debug("\tOperations: {}", operations); XpTools.LOGGER.debug("\tOperations: {}", operations);
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
XpTools.LOGGER.debug("\tCache entries: {}", cache.size()); XpTools.LOGGER.debug("\tCache entries: {}", cache.size());
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop); XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);

View File

@ -20,7 +20,6 @@ public class OnLivingExperienceDropEventHandler {
@SubscribeEvent @SubscribeEvent
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) { public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
Instant start = Instant.now(); Instant start = Instant.now();
boolean usedCache = true;
float xpToDrop = event.getDroppedExperience(); float xpToDrop = event.getDroppedExperience();
// Get Entity id // Get Entity id
@ -30,27 +29,13 @@ public class OnLivingExperienceDropEventHandler {
entity_id = entity_rl.toString(); entity_id = entity_rl.toString();
} }
// Collect operations // Get operations
List<OperationItem> operations = null; List<OperationItem> operations = OperationUtils.getOperationList(
if (Config.Server.optimizationUseCache.get()) { entity_id,
operations = cache.getEntry(entity_id); event.getEntity().getType().getTags().toList(),
} Config.Server.entityKillOperationItems,
cache
if (operations == null) { );
usedCache = false;
// Calculate operations
operations = OperationUtils.calculateOperationList(
entity_id,
event.getEntity().getType().getTags().toList(),
Config.Server.entityKillOperationItems
);
// Save operations to cache
if (Config.Server.optimizationUseCache.get()) {
cache.addEntry(entity_id, operations);
}
}
// Add global operations before all others // Add global operations before all others
operations.addAll(0, Config.Server.entityKillGlobalOperationItems); operations.addAll(0, Config.Server.entityKillGlobalOperationItems);
@ -62,7 +47,6 @@ public class OnLivingExperienceDropEventHandler {
if (Config.Server.debugExtra.get()) { if (Config.Server.debugExtra.get()) {
XpTools.LOGGER.debug("Completed entity kill event:"); XpTools.LOGGER.debug("Completed entity kill event:");
XpTools.LOGGER.debug("\tOperations: {}", operations); XpTools.LOGGER.debug("\tOperations: {}", operations);
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
XpTools.LOGGER.debug("\tCache entries: {}", cache.size()); XpTools.LOGGER.debug("\tCache entries: {}", cache.size());
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos()); XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop); XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);

View File

@ -1,5 +1,6 @@
package dev.micle.xptools.operation; package dev.micle.xptools.operation;
import dev.micle.xptools.config.Config;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import java.util.ArrayList; import java.util.ArrayList;
@ -8,36 +9,50 @@ import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class OperationUtils { public class OperationUtils {
public static <T extends TagKey<?>> List<OperationItem> calculateOperationList(String id, List<T> tagList, List<OperationItem> operationItems) { public static <T extends TagKey<?>> List<OperationItem> getOperationList(String id, List<T> tagList, List<OperationItem> operationItems, OperationCache cache) {
List<OperationItem> operations = new ArrayList<>(); List<OperationItem> operations = null;
// Collect operations on relevant id // Get from cache if possible
if (!id.isEmpty()) { if (Config.Server.optimizationUseCache.get()) {
for (OperationItem operationItem : operationItems) { operations = cache.getEntry(id);
if (!operationItem.isTag() && operationItem.getId().equals(id)) {
operations.add(operationItem);
}
}
} }
// Collect operations on relevant tag_id if (operations == null) {
for (T tagKey : tagList) { operations = new ArrayList<>();
String tag_id = tagKey.location().toString();
for (OperationItem operationItem : operationItems) { // Collect operations on relevant id
if (operationItem.isTag() && operationItem.getId().equals(tag_id)) { if (!id.isEmpty()) {
operations.add(operationItem); for (OperationItem operationItem : operationItems) {
if (!operationItem.isTag() && operationItem.getId().equals(id)) {
operations.add(operationItem);
}
} }
} }
}
// Sort operations based on priority // Collect operations on relevant tag_id
operations.sort(Comparator.comparingInt(OperationItem::getPriority)); 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);
}
}
}
// Remove any operations after last operation // Sort operations based on priority
for (OperationItem operationItem : operations) { operations.sort(Comparator.comparingInt(OperationItem::getPriority));
if (operationItem.isLast()) {
operations = operations.subList(0, operations.indexOf(operationItem) + 1); // Remove any operations after last operation
break; for (OperationItem operationItem : operations) {
if (operationItem.isLast()) {
operations = operations.subList(0, operations.indexOf(operationItem) + 1);
break;
}
}
// Save to cache if possible
if (Config.Server.optimizationUseCache.get()) {
cache.addEntry(id, operations);
} }
} }