7 Commits

7 changed files with 76 additions and 91 deletions

View File

@ -9,7 +9,7 @@ group = mod_group_id
version = mod_version
base {
archivesName = mod_id
archivesName = "${mod_id}-${minecraft_version}"
}
java {

View File

@ -4,7 +4,7 @@ org.gradle.daemon=false
mod_id=xp_tools
mod_name=XP Tools
mod_license=All Rights Reserved
mod_version=1.0.0
mod_version=1.0.1
mod_group_id=dev.micle
mod_authors=Micle
mod_description=Minecraft Forge mod for tweaking various things about XP in Minecraft. Primarily targeted at modpack developers.

View File

@ -132,8 +132,8 @@ public final class Config {
private static void onConfigReload() {
// Clear cache
OperationCache.clearBlockBreakCache();
OperationCache.clearEntityKillCache();
OperationCache.blockBreakCache.clear();
OperationCache.entityKillCache.clear();
// Parse all block break global operations
blockBreakGlobalOperationItems = new ArrayList<>();

View File

@ -15,10 +15,11 @@ import java.time.Instant;
import java.util.List;
public class OnBlockBreakEventHandler {
private final OperationCache cache = OperationCache.blockBreakCache;
@SubscribeEvent
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
Instant start = Instant.now();
boolean usedCache = true;
float xpToDrop = event.getExpToDrop();
// Get Block id
@ -28,25 +29,13 @@ public class OnBlockBreakEventHandler {
block_id = block_rl.toString();
}
// Collect operations
List<OperationItem> operations = null;
if (Config.Server.optimizationUseCache.get()) {
operations = OperationCache.getBlockBreakCacheEntry(block_id);
}
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);
}
// Get operations
List<OperationItem> operations = OperationUtils.getOperationList(
block_id,
event.getState().getTags().toList(),
Config.Server.blockBreakOperationItems,
cache
);
// Add global operations before all others
operations.addAll(0, Config.Server.blockBreakGlobalOperationItems);
@ -58,7 +47,7 @@ public class OnBlockBreakEventHandler {
if (Config.Server.debugExtra.get()) {
XpTools.LOGGER.debug("Completed block break event:");
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("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);
}

View File

@ -15,10 +15,11 @@ import java.time.Instant;
import java.util.List;
public class OnLivingExperienceDropEventHandler {
private final OperationCache cache = OperationCache.entityKillCache;
@SubscribeEvent
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
Instant start = Instant.now();
boolean usedCache = true;
float xpToDrop = event.getDroppedExperience();
// Get Entity id
@ -28,25 +29,13 @@ public class OnLivingExperienceDropEventHandler {
entity_id = entity_rl.toString();
}
// Collect operations
List<OperationItem> operations = null;
if (Config.Server.optimizationUseCache.get()) {
operations = OperationCache.getEntityKillCacheEntry(entity_id);
}
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);
}
// Get operations
List<OperationItem> operations = OperationUtils.getOperationList(
entity_id,
event.getEntity().getType().getTags().toList(),
Config.Server.entityKillOperationItems,
cache
);
// Add global operations before all others
operations.addAll(0, Config.Server.entityKillGlobalOperationItems);
@ -58,7 +47,7 @@ public class OnLivingExperienceDropEventHandler {
if (Config.Server.debugExtra.get()) {
XpTools.LOGGER.debug("Completed entity kill event:");
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("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);
}

View File

@ -6,36 +6,28 @@ import java.util.HashMap;
import java.util.List;
public class OperationCache {
private static HashMap<String, List<OperationItem>> blockBreakCache;
private static HashMap<String, List<OperationItem>> entityKillCache;
public static OperationCache blockBreakCache = new OperationCache();
public static OperationCache entityKillCache = new OperationCache();
private HashMap<String, List<OperationItem>> cache = new HashMap<>();
public static void clearBlockBreakCache() {
blockBreakCache = new HashMap<>();
private OperationCache() {}
public void clear() {
cache = new HashMap<>();
}
public static void clearEntityKillCache() {
entityKillCache = new HashMap<>();
}
public static @Nullable List<OperationItem> getBlockBreakCacheEntry(String key) {
if (blockBreakCache.containsKey(key)) {
return new ArrayList<>(blockBreakCache.get(key));
public @Nullable List<OperationItem> getEntry(String key) {
if (cache.containsKey(key)) {
return new ArrayList<>(cache.get(key));
}
return null;
}
public static @Nullable List<OperationItem> getEntityKillCacheEntry(String key) {
if (entityKillCache.containsKey(key)) {
return new ArrayList<>(entityKillCache.get(key));
}
return null;
public void addEntry(String key, List<OperationItem> value) {
cache.putIfAbsent(key, new ArrayList<>(value));
}
public static void addBlockBreakCacheEntry(String key, List<OperationItem> value) {
blockBreakCache.putIfAbsent(key, new ArrayList<>(value));
}
public static void addEntityKillCacheEntry(String key, List<OperationItem> value) {
entityKillCache.putIfAbsent(key, new ArrayList<>(value));
public int size() {
return cache.size();
}
}

View File

@ -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);
}
}