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.config.Config;
|
||||||
import dev.micle.xptools.operation.OperationCache;
|
import dev.micle.xptools.operation.OperationCache;
|
||||||
import dev.micle.xptools.operation.OperationItem;
|
import dev.micle.xptools.operation.OperationItem;
|
||||||
|
import dev.micle.xptools.operation.OperationUtils;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
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.event.level.BlockEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.registries.ForgeRegistries;
|
import net.minecraftforge.registries.ForgeRegistries;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
|
||||||
|
|
||||||
public class OnBlockBreakEventHandler {
|
public class OnBlockBreakEventHandler {
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
@ -38,37 +34,12 @@ public class OnBlockBreakEventHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (operations == null) {
|
if (operations == null) {
|
||||||
operations = new ArrayList<>();
|
// Calculate operations
|
||||||
|
operations = OperationUtils.calculateOperationList(
|
||||||
// Collect operations on relevant block_id
|
block_id,
|
||||||
if (!block_id.isEmpty()) {
|
event.getState().getTags().toList(),
|
||||||
for (OperationItem operationItem : Config.Server.blockBreakOperationItems) {
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save operations to cache
|
// Save operations to cache
|
||||||
OperationCache.addBlockBreakCacheEntry(block_id, operations);
|
OperationCache.addBlockBreakCacheEntry(block_id, operations);
|
||||||
@ -78,36 +49,7 @@ public class OnBlockBreakEventHandler {
|
|||||||
operations.addAll(0, Config.Server.blockBreakGlobalOperationItems);
|
operations.addAll(0, Config.Server.blockBreakGlobalOperationItems);
|
||||||
|
|
||||||
// Apply operations to xp drops
|
// Apply operations to xp drops
|
||||||
for (OperationItem operation : operations) {
|
xpToDrop = OperationUtils.calculateNewXpAmount(xpToDrop, 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug logging
|
// Debug logging
|
||||||
if (Config.Server.debugExtra.get()) {
|
if (Config.Server.debugExtra.get()) {
|
||||||
|
@ -4,19 +4,15 @@ import dev.micle.xptools.XpTools;
|
|||||||
import dev.micle.xptools.config.Config;
|
import dev.micle.xptools.config.Config;
|
||||||
import dev.micle.xptools.operation.OperationCache;
|
import dev.micle.xptools.operation.OperationCache;
|
||||||
import dev.micle.xptools.operation.OperationItem;
|
import dev.micle.xptools.operation.OperationItem;
|
||||||
|
import dev.micle.xptools.operation.OperationUtils;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
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.event.entity.living.LivingExperienceDropEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.registries.ForgeRegistries;
|
import net.minecraftforge.registries.ForgeRegistries;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
|
||||||
|
|
||||||
public class OnLivingExperienceDropEventHandler {
|
public class OnLivingExperienceDropEventHandler {
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
@ -38,37 +34,12 @@ public class OnLivingExperienceDropEventHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (operations == null) {
|
if (operations == null) {
|
||||||
operations = new ArrayList<>();
|
// Calculate operations
|
||||||
|
operations = OperationUtils.calculateOperationList(
|
||||||
// Collect operations on relevant entity_id
|
entity_id,
|
||||||
if (!entity_id.isEmpty()) {
|
event.getEntity().getType().getTags().toList(),
|
||||||
for (OperationItem operationItem : Config.Server.entityKillOperationItems) {
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save operations to cache
|
// Save operations to cache
|
||||||
OperationCache.addEntityKillCacheEntry(entity_id, operations);
|
OperationCache.addEntityKillCacheEntry(entity_id, operations);
|
||||||
@ -78,36 +49,7 @@ public class OnLivingExperienceDropEventHandler {
|
|||||||
operations.addAll(0, Config.Server.entityKillGlobalOperationItems);
|
operations.addAll(0, Config.Server.entityKillGlobalOperationItems);
|
||||||
|
|
||||||
// Apply operations to xp drops
|
// Apply operations to xp drops
|
||||||
for (OperationItem operation : operations) {
|
xpToDrop = OperationUtils.calculateNewXpAmount(xpToDrop, 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug logging
|
// Debug logging
|
||||||
if (Config.Server.debugExtra.get()) {
|
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