Files
xp_tools/src/main/java/dev/micle/xptools/operation/OperationUtils.java

92 lines
3.0 KiB
Java

package dev.micle.xptools.operation;
import dev.micle.xptools.config.Config;
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> getOperationList(String id, List<T> tagList, List<OperationItem> operationItems, OperationCache cache) {
List<OperationItem> operations = null;
// Get from cache if possible
if (Config.Server.optimizationUseCache.get()) {
operations = cache.getEntry(id);
}
if (operations == null) {
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;
}
}
// Save to cache if possible
if (Config.Server.optimizationUseCache.get()) {
cache.addEntry(id, operations);
}
}
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;
}
}