Compare commits
13 Commits
1.20.1/v1.
...
1.20.1/v1.
Author | SHA1 | Date | |
---|---|---|---|
f7203ab897 | |||
9b38f5601b | |||
c690ccdc99 | |||
aca3f0202a | |||
47b56e4b6d | |||
eed36cbcee | |||
b26f628b58 | |||
64c6da0b2e | |||
ac459e9c58 | |||
11ccd940da | |||
ef99967059 | |||
eba233a983 | |||
dbe3f19790 |
@ -9,7 +9,7 @@ group = mod_group_id
|
|||||||
version = mod_version
|
version = mod_version
|
||||||
|
|
||||||
base {
|
base {
|
||||||
archivesName = mod_id
|
archivesName = "${mod_id}-${minecraft_version}"
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
@ -4,7 +4,7 @@ org.gradle.daemon=false
|
|||||||
mod_id=xp_tools
|
mod_id=xp_tools
|
||||||
mod_name=XP Tools
|
mod_name=XP Tools
|
||||||
mod_license=All Rights Reserved
|
mod_license=All Rights Reserved
|
||||||
mod_version=1.0.0
|
mod_version=1.0.2
|
||||||
mod_group_id=dev.micle
|
mod_group_id=dev.micle
|
||||||
mod_authors=Micle
|
mod_authors=Micle
|
||||||
mod_description=Minecraft Forge mod for tweaking various things about XP in Minecraft. Primarily targeted at modpack developers.
|
mod_description=Minecraft Forge mod for tweaking various things about XP in Minecraft. Primarily targeted at modpack developers.
|
||||||
|
@ -13,6 +13,7 @@ import net.minecraftforge.fml.event.config.ModConfigEvent;
|
|||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Mod.EventBusSubscriber(modid = XpTools.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
|
@Mod.EventBusSubscriber(modid = XpTools.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||||
public final class Config {
|
public final class Config {
|
||||||
@ -104,13 +105,13 @@ public final class Config {
|
|||||||
.comment("Global operations are run before any unique operations.")
|
.comment("Global operations are run before any unique operations.")
|
||||||
.comment("Examples:")
|
.comment("Examples:")
|
||||||
.comment("'set,0,0,0' - Sets the xp of all blocks to 0.")
|
.comment("'set,0,0,0' - Sets the xp of all blocks to 0.")
|
||||||
.define("blockBreakGlobalOperations", new ArrayList<>());
|
.defineList("blockBreakGlobalOperations", List.of(), Server::isValidGlobalOperationEntry);
|
||||||
blockBreakOperationsRaw = builder
|
blockBreakOperationsRaw = builder
|
||||||
.comment("List of unique operations. Format: '[block_id/tag_id],[operation],[min],[max],[priority],[is_last]'")
|
.comment("List of unique operations. Format: '[block_id/tag_id],[operation],[min],[max],[priority],[is_last]'")
|
||||||
.comment("Examples:")
|
.comment("Examples:")
|
||||||
.comment("'minecraft:dirt,set,2,2,0,true' - Sets the xp drop of the dirt block to 2, takes highest priority and stops any additional operations.")
|
.comment("'minecraft:dirt,set,2,2,0,true' - Sets the xp drop of the dirt block to 2, takes highest priority and stops any additional operations.")
|
||||||
.comment("'#forge:ores,multiply,1,2,1,false' - Multiplies xp drop of all blocks tagged forge:ores by 1-2, allows additional operations.")
|
.comment("'#forge:ores,multiply,1,2,1,false' - Multiplies xp drop of all blocks tagged forge:ores by 1-2, allows additional operations.")
|
||||||
.define("blockBreakOperations", new ArrayList<>());
|
.defineList("blockBreakOperations", List.of(), Server::isValidOperationEntry);
|
||||||
builder.pop();
|
builder.pop();
|
||||||
|
|
||||||
builder.comment("Settings for entity killing").push("entity_killing");
|
builder.comment("Settings for entity killing").push("entity_killing");
|
||||||
@ -120,20 +121,20 @@ public final class Config {
|
|||||||
.comment("Global operations are run before any unique operations.")
|
.comment("Global operations are run before any unique operations.")
|
||||||
.comment("Examples:")
|
.comment("Examples:")
|
||||||
.comment("'set,0,0,0' - Sets the xp of all entities to 0.")
|
.comment("'set,0,0,0' - Sets the xp of all entities to 0.")
|
||||||
.define("entityKillGlobalOperations", new ArrayList<>());
|
.defineList("entityKillGlobalOperations", List.of(), Server::isValidGlobalOperationEntry);
|
||||||
entityKillOperationsRaw = builder
|
entityKillOperationsRaw = builder
|
||||||
.comment("List of unique operations. Format: '[entity_id/tag_id],[operation],[min],[max],[priority],[is_last]'")
|
.comment("List of unique operations. Format: '[entity_id/tag_id],[operation],[min],[max],[priority],[is_last]'")
|
||||||
.comment("Examples:")
|
.comment("Examples:")
|
||||||
.comment("'minecraft:creeper,set,2,2,0,true' - Sets the xp drop for killing creepers to 2, takes highest priority and stops any additional operations.")
|
.comment("'minecraft:creeper,set,2,2,0,true' - Sets the xp drop for killing creepers to 2, takes highest priority and stops any additional operations.")
|
||||||
.comment("'#some_mod:some_tag,multiply,1,2,1,false' - Multiplies the xp drop for killing all entities tagged some_mod:some_tag by 1-2, allows additional operations.")
|
.comment("'#some_mod:some_tag,multiply,1,2,1,false' - Multiplies the xp drop for killing all entities tagged some_mod:some_tag by 1-2, allows additional operations.")
|
||||||
.define("entityKillOperations", new ArrayList<>());
|
.defineList("entityKillOperations", List.of(), Server::isValidOperationEntry);
|
||||||
builder.pop();
|
builder.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void onConfigReload() {
|
private static void onConfigReload() {
|
||||||
// Clear cache
|
// Clear cache
|
||||||
OperationCache.clearBlockBreakCache();
|
OperationCache.blockBreakCache.clear();
|
||||||
OperationCache.clearEntityKillCache();
|
OperationCache.entityKillCache.clear();
|
||||||
|
|
||||||
// Parse all block break global operations
|
// Parse all block break global operations
|
||||||
blockBreakGlobalOperationItems = new ArrayList<>();
|
blockBreakGlobalOperationItems = new ArrayList<>();
|
||||||
@ -161,5 +162,23 @@ public final class Config {
|
|||||||
entityKillOperationItems.add(OperationItem.fromConfig(s));
|
entityKillOperationItems.add(OperationItem.fromConfig(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isValidGlobalOperationEntry(Object entry) {
|
||||||
|
return entry instanceof String && ((String) entry).matches(
|
||||||
|
String.format(
|
||||||
|
"(?i)^(%s),(\\d*\\.?\\d+),(\\d*\\.?\\d+),(\\d+)$",
|
||||||
|
Arrays.stream(OperationType.values()).map(OperationType::toString).collect(Collectors.joining("|"))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isValidOperationEntry(Object entry) {
|
||||||
|
return entry instanceof String && ((String) entry).matches(
|
||||||
|
String.format(
|
||||||
|
"(?i)^(\\#?\\w+:[\\w\\/]+),(%s),(\\d*\\.?\\d+),(\\d*\\.?\\d+),(\\d+),(TRUE|FALSE)$",
|
||||||
|
Arrays.stream(OperationType.values()).map(OperationType::toString).collect(Collectors.joining("|"))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,11 @@ import java.time.Instant;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OnBlockBreakEventHandler {
|
public class OnBlockBreakEventHandler {
|
||||||
|
private final OperationCache cache = OperationCache.blockBreakCache;
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
|
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
boolean usedCache = true;
|
|
||||||
float xpToDrop = event.getExpToDrop();
|
float xpToDrop = event.getExpToDrop();
|
||||||
|
|
||||||
// Get Block id
|
// Get Block id
|
||||||
@ -28,25 +29,13 @@ public class OnBlockBreakEventHandler {
|
|||||||
block_id = block_rl.toString();
|
block_id = block_rl.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect operations
|
// Get operations
|
||||||
List<OperationItem> operations = null;
|
List<OperationItem> operations = OperationUtils.getOperationList(
|
||||||
if (Config.Server.optimizationUseCache.get()) {
|
block_id,
|
||||||
operations = OperationCache.getBlockBreakCacheEntry(block_id);
|
event.getState().getTags().toList(),
|
||||||
}
|
Config.Server.blockBreakOperationItems,
|
||||||
|
cache
|
||||||
if (operations == null) {
|
);
|
||||||
usedCache = false;
|
|
||||||
|
|
||||||
// Calculate operations
|
|
||||||
operations = OperationUtils.calculateOperationList(
|
|
||||||
block_id,
|
|
||||||
event.getState().getTags().toList(),
|
|
||||||
Config.Server.blockBreakOperationItems
|
|
||||||
);
|
|
||||||
|
|
||||||
// Save operations to cache
|
|
||||||
OperationCache.addBlockBreakCacheEntry(block_id, operations);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add global operations before all others
|
// Add global operations before all others
|
||||||
operations.addAll(0, Config.Server.blockBreakGlobalOperationItems);
|
operations.addAll(0, Config.Server.blockBreakGlobalOperationItems);
|
||||||
@ -58,7 +47,7 @@ public class OnBlockBreakEventHandler {
|
|||||||
if (Config.Server.debugExtra.get()) {
|
if (Config.Server.debugExtra.get()) {
|
||||||
XpTools.LOGGER.debug("Completed block break event:");
|
XpTools.LOGGER.debug("Completed block break event:");
|
||||||
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
||||||
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
|
XpTools.LOGGER.debug("\tCache entries: {}", cache.size());
|
||||||
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
|
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
|
||||||
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);
|
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,11 @@ import java.time.Instant;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OnLivingExperienceDropEventHandler {
|
public class OnLivingExperienceDropEventHandler {
|
||||||
|
private final OperationCache cache = OperationCache.entityKillCache;
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
|
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
boolean usedCache = true;
|
|
||||||
float xpToDrop = event.getDroppedExperience();
|
float xpToDrop = event.getDroppedExperience();
|
||||||
|
|
||||||
// Get Entity id
|
// Get Entity id
|
||||||
@ -28,25 +29,13 @@ public class OnLivingExperienceDropEventHandler {
|
|||||||
entity_id = entity_rl.toString();
|
entity_id = entity_rl.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect operations
|
// Get operations
|
||||||
List<OperationItem> operations = null;
|
List<OperationItem> operations = OperationUtils.getOperationList(
|
||||||
if (Config.Server.optimizationUseCache.get()) {
|
entity_id,
|
||||||
operations = OperationCache.getEntityKillCacheEntry(entity_id);
|
event.getEntity().getType().getTags().toList(),
|
||||||
}
|
Config.Server.entityKillOperationItems,
|
||||||
|
cache
|
||||||
if (operations == null) {
|
);
|
||||||
usedCache = false;
|
|
||||||
|
|
||||||
// Calculate operations
|
|
||||||
operations = OperationUtils.calculateOperationList(
|
|
||||||
entity_id,
|
|
||||||
event.getEntity().getType().getTags().toList(),
|
|
||||||
Config.Server.entityKillOperationItems
|
|
||||||
);
|
|
||||||
|
|
||||||
// Save operations to cache
|
|
||||||
OperationCache.addEntityKillCacheEntry(entity_id, operations);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add global operations before all others
|
// Add global operations before all others
|
||||||
operations.addAll(0, Config.Server.entityKillGlobalOperationItems);
|
operations.addAll(0, Config.Server.entityKillGlobalOperationItems);
|
||||||
@ -58,7 +47,7 @@ public class OnLivingExperienceDropEventHandler {
|
|||||||
if (Config.Server.debugExtra.get()) {
|
if (Config.Server.debugExtra.get()) {
|
||||||
XpTools.LOGGER.debug("Completed entity kill event:");
|
XpTools.LOGGER.debug("Completed entity kill event:");
|
||||||
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
||||||
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
|
XpTools.LOGGER.debug("\tCache entries: {}", cache.size());
|
||||||
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
|
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
|
||||||
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);
|
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);
|
||||||
}
|
}
|
||||||
|
@ -6,36 +6,28 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OperationCache {
|
public class OperationCache {
|
||||||
private static HashMap<String, List<OperationItem>> blockBreakCache;
|
public static OperationCache blockBreakCache = new OperationCache();
|
||||||
private static HashMap<String, List<OperationItem>> entityKillCache;
|
public static OperationCache entityKillCache = new OperationCache();
|
||||||
|
private HashMap<String, List<OperationItem>> cache = new HashMap<>();
|
||||||
|
|
||||||
public static void clearBlockBreakCache() {
|
private OperationCache() {}
|
||||||
blockBreakCache = new HashMap<>();
|
|
||||||
|
public void clear() {
|
||||||
|
cache = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearEntityKillCache() {
|
public @Nullable List<OperationItem> getEntry(String key) {
|
||||||
entityKillCache = new HashMap<>();
|
if (cache.containsKey(key)) {
|
||||||
}
|
return new ArrayList<>(cache.get(key));
|
||||||
|
|
||||||
public static @Nullable List<OperationItem> getBlockBreakCacheEntry(String key) {
|
|
||||||
if (blockBreakCache.containsKey(key)) {
|
|
||||||
return new ArrayList<>(blockBreakCache.get(key));
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @Nullable List<OperationItem> getEntityKillCacheEntry(String key) {
|
public void addEntry(String key, List<OperationItem> value) {
|
||||||
if (entityKillCache.containsKey(key)) {
|
cache.putIfAbsent(key, new ArrayList<>(value));
|
||||||
return new ArrayList<>(entityKillCache.get(key));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addBlockBreakCacheEntry(String key, List<OperationItem> value) {
|
public int size() {
|
||||||
blockBreakCache.putIfAbsent(key, new ArrayList<>(value));
|
return cache.size();
|
||||||
}
|
|
||||||
|
|
||||||
public static void addEntityKillCacheEntry(String key, List<OperationItem> value) {
|
|
||||||
entityKillCache.putIfAbsent(key, new ArrayList<>(value));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.micle.xptools.operation;
|
package dev.micle.xptools.operation;
|
||||||
|
|
||||||
|
import dev.micle.xptools.config.Config;
|
||||||
import net.minecraft.tags.TagKey;
|
import net.minecraft.tags.TagKey;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -8,36 +9,50 @@ import java.util.List;
|
|||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class OperationUtils {
|
public class OperationUtils {
|
||||||
public static <T extends TagKey<?>> List<OperationItem> calculateOperationList(String id, List<T> tagList, List<OperationItem> operationItems) {
|
public static <T extends TagKey<?>> List<OperationItem> getOperationList(String id, List<T> tagList, List<OperationItem> operationItems, OperationCache cache) {
|
||||||
List<OperationItem> operations = new ArrayList<>();
|
List<OperationItem> operations = null;
|
||||||
|
|
||||||
// Collect operations on relevant id
|
// Get from cache if possible
|
||||||
if (!id.isEmpty()) {
|
if (Config.Server.optimizationUseCache.get()) {
|
||||||
for (OperationItem operationItem : operationItems) {
|
operations = cache.getEntry(id);
|
||||||
if (!operationItem.isTag() && operationItem.getId().equals(id)) {
|
|
||||||
operations.add(operationItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect operations on relevant tag_id
|
if (operations == null) {
|
||||||
for (T tagKey : tagList) {
|
operations = new ArrayList<>();
|
||||||
String tag_id = tagKey.location().toString();
|
|
||||||
for (OperationItem operationItem : operationItems) {
|
// Collect operations on relevant id
|
||||||
if (operationItem.isTag() && operationItem.getId().equals(tag_id)) {
|
if (!id.isEmpty()) {
|
||||||
operations.add(operationItem);
|
for (OperationItem operationItem : operationItems) {
|
||||||
|
if (!operationItem.isTag() && operationItem.getId().equals(id)) {
|
||||||
|
operations.add(operationItem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Sort operations based on priority
|
// Collect operations on relevant tag_id
|
||||||
operations.sort(Comparator.comparingInt(OperationItem::getPriority));
|
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
|
// Sort operations based on priority
|
||||||
for (OperationItem operationItem : operations) {
|
operations.sort(Comparator.comparingInt(OperationItem::getPriority));
|
||||||
if (operationItem.isLast()) {
|
|
||||||
operations = operations.subList(0, operations.indexOf(operationItem) + 1);
|
// Remove any operations after last operation
|
||||||
break;
|
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