Skip to content

Commit

Permalink
Use nbt string for faster cache system
Browse files Browse the repository at this point in the history
  • Loading branch information
Rothes committed Oct 5, 2024
1 parent 5573d6a commit c7d1bdb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.github.rothes.protocolstringreplacer.replacer.containers.Container;
import io.github.rothes.protocolstringreplacer.replacer.containers.Replaceable;
import org.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.neosearch.stringsearcher.Emit;

Expand All @@ -38,7 +37,7 @@ public class ReplacerManager {
private char papiHead;
private char papiTail;
private final List<ReplacerConfig> replacerConfigList = new ArrayList<>();
private final ConcurrentHashMap<ItemStack, HandledItemCache> cacheTable = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, HandledItemCache> cacheTable = new ConcurrentHashMap<>();
private PsrTask cleanTask;

public static class HandledItemCache {
Expand Down Expand Up @@ -121,15 +120,15 @@ public void registerTask() {
long currentTime = System.currentTimeMillis();
int purged = 0;

List<ItemStack> needToRemove = new ArrayList<>();
for (Map.Entry<ItemStack, HandledItemCache> entry : cacheTable.entrySet()) {
List<String> needToRemove = new ArrayList<>();
for (Map.Entry<String, HandledItemCache> entry : cacheTable.entrySet()) {
needToRemove.clear();
if ((currentTime - entry.getValue().lastAccessTime) > cleanAccessInterval) {
needToRemove.add(entry.getKey());
}
if (!needToRemove.isEmpty()) {
for (ItemStack itemStack : needToRemove) {
cacheTable.remove(itemStack);
for (String cacheKey : needToRemove) {
cacheTable.remove(cacheKey);
purged++;
}
}
Expand Down Expand Up @@ -201,11 +200,11 @@ public void saveReplacerConfigs() {
}

@Nullable
public HandledItemCache getReplacedItemCache(ItemStack original) {
public HandledItemCache getReplacedItemCache(String original) {
return cacheTable.get(original);
}

public HandledItemCache addReplacedItemCache(ItemStack original, @NotNull ReadWriteNBT nbtItem, boolean blocked, int[] papiIndexes) {
public HandledItemCache addReplacedItemCache(String original, @NotNull ReadWriteNBT nbtItem, boolean blocked, int[] papiIndexes) {
Validate.notNull(nbtItem, "Replaced NBTItem cannot be null");

HandledItemCache handledItemCache = new HandledItemCache(nbtItem, System.currentTimeMillis(), blocked, papiIndexes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
private val original: ItemMeta = content.itemMeta

init {
nbt = NBT.itemStackToNBT(content)
if (useCache) {
val replacerManager = ProtocolStringReplacer.getInstance().replacerManager

val getCache = replacerManager.getReplacedItemCache(content)
val nbtString = nbt.toString()
val getCache = replacerManager.getReplacedItemCache(nbtString)
if (getCache != null) {
metaCache = getCache
isFromCache = true
metaCache = getCache
nbt = metaCache.nbt
} else {
isFromCache = false
nbt = NBT.itemStackToNBT(content)
metaCache = replacerManager.addReplacedItemCache(content, nbt, false, IntArray(0))
metaCache = replacerManager.addReplacedItemCache(nbtString, nbt, false, IntArray(0))
}
} else {
isFromCache = false
nbt = NBT.itemStackToNBT(content)
}
}

Expand Down

0 comments on commit c7d1bdb

Please sign in to comment.