Implemented cache for fetching operations.
This commit is contained in:
@ -8,10 +8,7 @@ import net.minecraftforge.fml.config.ModConfig;
|
|||||||
import net.minecraftforge.fml.event.config.ModConfigEvent;
|
import net.minecraftforge.fml.event.config.ModConfigEvent;
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@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 {
|
||||||
@ -91,6 +88,9 @@ public final class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void onConfigReload() {
|
private static void onConfigReload() {
|
||||||
|
// Clear cache
|
||||||
|
OperationCache.clearBlockBreakCache();
|
||||||
|
|
||||||
// Parse all block break global operations
|
// Parse all block break global operations
|
||||||
blockBreakGlobalOperationItems = new ArrayList<>();
|
blockBreakGlobalOperationItems = new ArrayList<>();
|
||||||
for (String s : blockBreakGlobalOperationsRaw.get()) {
|
for (String s : blockBreakGlobalOperationsRaw.get()) {
|
||||||
|
24
src/main/java/dev/micle/xptools/config/OperationCache.java
Normal file
24
src/main/java/dev/micle/xptools/config/OperationCache.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package dev.micle.xptools.config;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class OperationCache {
|
||||||
|
private static HashMap<String, List<OperationItem>> blockBreakCache;
|
||||||
|
|
||||||
|
public static void clearBlockBreakCache() {
|
||||||
|
blockBreakCache = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<OperationItem> getBlockBreakCacheEntry(String key) {
|
||||||
|
if (blockBreakCache.containsKey(key)) {
|
||||||
|
return new ArrayList<>(blockBreakCache.get(key));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addBlockBreakCacheEntry(String key, List<OperationItem> value) {
|
||||||
|
blockBreakCache.putIfAbsent(key, new ArrayList<>(value));
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package dev.micle.xptools.events.common;
|
|||||||
|
|
||||||
import dev.micle.xptools.XpTools;
|
import dev.micle.xptools.XpTools;
|
||||||
import dev.micle.xptools.config.Config;
|
import dev.micle.xptools.config.Config;
|
||||||
|
import dev.micle.xptools.config.OperationCache;
|
||||||
import dev.micle.xptools.config.OperationItem;
|
import dev.micle.xptools.config.OperationItem;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraft.tags.TagKey;
|
import net.minecraft.tags.TagKey;
|
||||||
@ -10,6 +11,8 @@ 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.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -19,12 +22,24 @@ public class OnBlockBreakEventHandler {
|
|||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
|
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
|
||||||
float xpToDrop = event.getExpToDrop();
|
float xpToDrop = event.getExpToDrop();
|
||||||
List<OperationItem> operations = new ArrayList<>();
|
|
||||||
|
// Get Block id
|
||||||
|
ResourceLocation block_rl = ForgeRegistries.BLOCKS.getKey(event.getState().getBlock());
|
||||||
|
String block_id = "";
|
||||||
|
if (block_rl != null) {
|
||||||
|
block_id = block_rl.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect operations
|
||||||
|
Instant start = Instant.now();
|
||||||
|
List<OperationItem> operations = OperationCache.getBlockBreakCacheEntry(block_id);
|
||||||
|
if (operations != null) {
|
||||||
|
XpTools.LOGGER.debug("Got operations from cache for {} [time: {}]", block_id, Duration.between(start, Instant.now()).toNanos());
|
||||||
|
} else {
|
||||||
|
operations = new ArrayList<>();
|
||||||
|
|
||||||
// Collect operations on relevant block_id
|
// Collect operations on relevant block_id
|
||||||
ResourceLocation block_rl = ForgeRegistries.BLOCKS.getKey(event.getState().getBlock());
|
if (!block_id.isEmpty()) {
|
||||||
if (block_rl != null) {
|
|
||||||
String block_id = block_rl.toString();
|
|
||||||
for (OperationItem operationItem : Config.Server.blockBreakOperationItems) {
|
for (OperationItem operationItem : Config.Server.blockBreakOperationItems) {
|
||||||
if (!operationItem.isTag() && operationItem.getId().equals(block_id)) {
|
if (!operationItem.isTag() && operationItem.getId().equals(block_id)) {
|
||||||
operations.add(operationItem);
|
operations.add(operationItem);
|
||||||
@ -45,6 +60,20 @@ public class OnBlockBreakEventHandler {
|
|||||||
// Sort operations based on priority
|
// Sort operations based on priority
|
||||||
operations.sort(Comparator.comparingInt(OperationItem::getPriority));
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
XpTools.LOGGER.debug("Calculated operations for {} [time: {}]", block_id, Duration.between(start, Instant.now()).toNanos());
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
@ -76,7 +105,6 @@ public class OnBlockBreakEventHandler {
|
|||||||
|
|
||||||
// Stop if this is the last operation
|
// Stop if this is the last operation
|
||||||
if (operation.isLast()) {
|
if (operation.isLast()) {
|
||||||
operations = operations.subList(0, operations.indexOf(operation) + 1);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user