Getting default xp drop amount, applying all operations on it and setting the event xp drops.

This commit is contained in:
2025-05-24 19:59:24 +01:00
parent f3e9854aea
commit 3ee1bfa9e6

View File

@ -13,10 +13,12 @@ import net.minecraftforge.registries.ForgeRegistries;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class OnBlockBreakEventHandler {
@SubscribeEvent
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
float xpToDrop = (Config.Server.blockBreakDefaultNoXp.get()) ? 0 : event.getExpToDrop();
List<OperationItem> operations = new ArrayList<>();
// Collect operations on relevant block_id
@ -43,6 +45,42 @@ public class OnBlockBreakEventHandler {
// Sort operations based on priority
operations.sort(Comparator.comparingInt(OperationItem::getPriority));
// 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()) {
operations = operations.subList(0, operations.indexOf(operation) + 1);
break;
}
}
XpTools.LOGGER.debug(operations.toString());
XpTools.LOGGER.debug(String.valueOf(xpToDrop));
// Apply xp drop
event.setExpToDrop((int)xpToDrop);
}
}