Split some common code from event handlers into new class OperationUtils.
This commit is contained in:
@ -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<Block> 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()) {
|
||||
|
@ -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<EntityType<?>> 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()) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user