Skip to content

Commit

Permalink
Re-struct item cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Rothes committed Jul 29, 2024
1 parent bb61293 commit ff552f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
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.Material;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.neosearch.stringsearcher.Emit;

Expand All @@ -23,13 +22,12 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiPredicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand All @@ -40,17 +38,17 @@ public class ReplacerManager {
private char papiHead;
private char papiTail;
private final List<ReplacerConfig> replacerConfigList = new ArrayList<>();
private final EnumMap<Material, HashMap<ItemMeta, ItemMetaCache>> replacedItemCache = new EnumMap<>(Material.class);
private final ConcurrentHashMap<ItemStack, HandledItemCache> cacheTable = new ConcurrentHashMap<>();
private PsrTask cleanTask;

public static class ItemMetaCache {
public static class HandledItemCache {

private final ReadWriteNBT nbtItem;
private long lastAccessTime;
private boolean blocked;
private int[] placeholderIndexes;

public ItemMetaCache(ReadWriteNBT nbtItem, long lastAccessTime, boolean blocked, int[] placeholderIndexes) {
public HandledItemCache(ReadWriteNBT nbtItem, long lastAccessTime, boolean blocked, int[] placeholderIndexes) {
this.nbtItem = nbtItem;
this.lastAccessTime = lastAccessTime;
this.blocked = blocked;
Expand Down Expand Up @@ -123,18 +121,15 @@ public void registerTask() {
long currentTime = System.currentTimeMillis();
int purged = 0;

List<ItemMeta> needToRemove = new ArrayList<>();
for (Map.Entry<Material, HashMap<ItemMeta, ItemMetaCache>> outEntry : replacedItemCache.entrySet()) {
List<ItemStack> needToRemove = new ArrayList<>();
for (Map.Entry<ItemStack, HandledItemCache> entry : cacheTable.entrySet()) {
needToRemove.clear();
HashMap<ItemMeta, ItemMetaCache> map = outEntry.getValue();
for (Map.Entry<ItemMeta, ItemMetaCache> entry : map.entrySet()) {
if ((currentTime - entry.getValue().lastAccessTime) > cleanAccessInterval) {
needToRemove.add(entry.getKey());
}
if ((currentTime - entry.getValue().lastAccessTime) > cleanAccessInterval) {
needToRemove.add(entry.getKey());
}
if (!needToRemove.isEmpty()) {
for (ItemMeta itemMeta : needToRemove) {
map.remove(itemMeta);
for (ItemStack itemStack : needToRemove) {
cacheTable.remove(itemStack);
purged++;
}
}
Expand Down Expand Up @@ -206,18 +201,16 @@ public void saveReplacerConfigs() {
}

@Nullable
public ItemMetaCache getReplacedItemCache(ItemMeta itemMeta, Material material) {
HashMap<ItemMeta, ItemMetaCache> map = replacedItemCache.get(material);
return map == null ? null : map.get(itemMeta);
public HandledItemCache getReplacedItemCache(ItemStack original) {
return cacheTable.get(original);
}

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

ItemMetaCache itemMetaCache = new ItemMetaCache(nbtItem, System.currentTimeMillis(), blocked, papiIndexes);
replacedItemCache.computeIfAbsent(type, key -> new HashMap<>()).put(original, itemMetaCache);
return itemMetaCache;
HandledItemCache handledItemCache = new HandledItemCache(nbtItem, System.currentTimeMillis(), blocked, papiIndexes);
cacheTable.put(original, handledItemCache);
return handledItemCache;
}

public List<ReplacerConfig> getAcceptedReplacers(@Nonnull PsrUser user, @Nonnull BiPredicate<ReplacerConfig, PsrUser> filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTList
import io.github.rothes.protocolstringreplacer.ProtocolStringReplacer
import io.github.rothes.protocolstringreplacer.plugin
import io.github.rothes.protocolstringreplacer.replacer.ReplacerManager.ItemMetaCache
import io.github.rothes.protocolstringreplacer.replacer.ReplacerManager.HandledItemCache
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
Expand All @@ -17,7 +17,7 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
AbstractContainer<ItemStack>(itemStack, root) {

val isFromCache: Boolean
lateinit var metaCache: ItemMetaCache
lateinit var metaCache: HandledItemCache
private set
private var nbt: ReadWriteNBT
private val original: ItemMeta = content.itemMeta
Expand All @@ -26,15 +26,15 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
if (useCache) {
val replacerManager = ProtocolStringReplacer.getInstance().replacerManager

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

0 comments on commit ff552f9

Please sign in to comment.