Split some common code from event handlers into new class OperationUtils.

This commit is contained in:
2025-05-25 00:47:42 +01:00
parent f938b99056
commit 308c74ef0d
3 changed files with 92 additions and 132 deletions

View File

@ -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 <T extends TagKey<?>> List<OperationItem> calculateOperationList(String id, List<T> tagList, List<OperationItem> operationItems) {
List<OperationItem> 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<OperationItem> 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;
}
}