Moved more similar code to utility method.
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package dev.micle.xptools.operation;
|
||||
|
||||
import dev.micle.xptools.config.Config;
|
||||
import net.minecraft.tags.TagKey;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -8,36 +9,50 @@ 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<>();
|
||||
public static <T extends TagKey<?>> List<OperationItem> getOperationList(String id, List<T> tagList, List<OperationItem> operationItems, OperationCache cache) {
|
||||
List<OperationItem> operations = null;
|
||||
|
||||
// Collect operations on relevant id
|
||||
if (!id.isEmpty()) {
|
||||
for (OperationItem operationItem : operationItems) {
|
||||
if (!operationItem.isTag() && operationItem.getId().equals(id)) {
|
||||
operations.add(operationItem);
|
||||
}
|
||||
}
|
||||
// Get from cache if possible
|
||||
if (Config.Server.optimizationUseCache.get()) {
|
||||
operations = cache.getEntry(id);
|
||||
}
|
||||
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort operations based on priority
|
||||
operations.sort(Comparator.comparingInt(OperationItem::getPriority));
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove any operations after last operation
|
||||
for (OperationItem operationItem : operations) {
|
||||
if (operationItem.isLast()) {
|
||||
operations = operations.subList(0, operations.indexOf(operationItem) + 1);
|
||||
break;
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user