Skip to content

Commit ff552f9

Browse files
committed
Re-struct item cache
1 parent bb61293 commit ff552f9

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

bukkit/src/main/java/io/github/rothes/protocolstringreplacer/replacer/ReplacerManager.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import io.github.rothes.protocolstringreplacer.replacer.containers.Container;
1414
import io.github.rothes.protocolstringreplacer.replacer.containers.Replaceable;
1515
import org.apache.commons.lang.Validate;
16-
import org.bukkit.Material;
17-
import org.bukkit.inventory.meta.ItemMeta;
16+
import org.bukkit.inventory.ItemStack;
1817
import org.jetbrains.annotations.NotNull;
1918
import org.neosearch.stringsearcher.Emit;
2019

@@ -23,13 +22,12 @@
2322
import java.io.File;
2423
import java.util.ArrayList;
2524
import java.util.Collections;
26-
import java.util.EnumMap;
2725
import java.util.HashMap;
28-
import java.util.Iterator;
2926
import java.util.LinkedList;
3027
import java.util.List;
3128
import java.util.Map;
3229
import java.util.Set;
30+
import java.util.concurrent.ConcurrentHashMap;
3331
import java.util.function.BiPredicate;
3432
import java.util.regex.Pattern;
3533
import java.util.regex.PatternSyntaxException;
@@ -40,17 +38,17 @@ public class ReplacerManager {
4038
private char papiHead;
4139
private char papiTail;
4240
private final List<ReplacerConfig> replacerConfigList = new ArrayList<>();
43-
private final EnumMap<Material, HashMap<ItemMeta, ItemMetaCache>> replacedItemCache = new EnumMap<>(Material.class);
41+
private final ConcurrentHashMap<ItemStack, HandledItemCache> cacheTable = new ConcurrentHashMap<>();
4442
private PsrTask cleanTask;
4543

46-
public static class ItemMetaCache {
44+
public static class HandledItemCache {
4745

4846
private final ReadWriteNBT nbtItem;
4947
private long lastAccessTime;
5048
private boolean blocked;
5149
private int[] placeholderIndexes;
5250

53-
public ItemMetaCache(ReadWriteNBT nbtItem, long lastAccessTime, boolean blocked, int[] placeholderIndexes) {
51+
public HandledItemCache(ReadWriteNBT nbtItem, long lastAccessTime, boolean blocked, int[] placeholderIndexes) {
5452
this.nbtItem = nbtItem;
5553
this.lastAccessTime = lastAccessTime;
5654
this.blocked = blocked;
@@ -123,18 +121,15 @@ public void registerTask() {
123121
long currentTime = System.currentTimeMillis();
124122
int purged = 0;
125123

126-
List<ItemMeta> needToRemove = new ArrayList<>();
127-
for (Map.Entry<Material, HashMap<ItemMeta, ItemMetaCache>> outEntry : replacedItemCache.entrySet()) {
124+
List<ItemStack> needToRemove = new ArrayList<>();
125+
for (Map.Entry<ItemStack, HandledItemCache> entry : cacheTable.entrySet()) {
128126
needToRemove.clear();
129-
HashMap<ItemMeta, ItemMetaCache> map = outEntry.getValue();
130-
for (Map.Entry<ItemMeta, ItemMetaCache> entry : map.entrySet()) {
131-
if ((currentTime - entry.getValue().lastAccessTime) > cleanAccessInterval) {
132-
needToRemove.add(entry.getKey());
133-
}
127+
if ((currentTime - entry.getValue().lastAccessTime) > cleanAccessInterval) {
128+
needToRemove.add(entry.getKey());
134129
}
135130
if (!needToRemove.isEmpty()) {
136-
for (ItemMeta itemMeta : needToRemove) {
137-
map.remove(itemMeta);
131+
for (ItemStack itemStack : needToRemove) {
132+
cacheTable.remove(itemStack);
138133
purged++;
139134
}
140135
}
@@ -206,18 +201,16 @@ public void saveReplacerConfigs() {
206201
}
207202

208203
@Nullable
209-
public ItemMetaCache getReplacedItemCache(ItemMeta itemMeta, Material material) {
210-
HashMap<ItemMeta, ItemMetaCache> map = replacedItemCache.get(material);
211-
return map == null ? null : map.get(itemMeta);
204+
public HandledItemCache getReplacedItemCache(ItemStack original) {
205+
return cacheTable.get(original);
212206
}
213207

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

218-
ItemMetaCache itemMetaCache = new ItemMetaCache(nbtItem, System.currentTimeMillis(), blocked, papiIndexes);
219-
replacedItemCache.computeIfAbsent(type, key -> new HashMap<>()).put(original, itemMetaCache);
220-
return itemMetaCache;
211+
HandledItemCache handledItemCache = new HandledItemCache(nbtItem, System.currentTimeMillis(), blocked, papiIndexes);
212+
cacheTable.put(original, handledItemCache);
213+
return handledItemCache;
221214
}
222215

223216
public List<ReplacerConfig> getAcceptedReplacers(@Nonnull PsrUser user, @Nonnull BiPredicate<ReplacerConfig, PsrUser> filter) {

bukkit/src/main/kotlin/io/github/rothes/protocolstringreplacer/replacer/containers/ItemStackContainer.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList
88
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTList
99
import io.github.rothes.protocolstringreplacer.ProtocolStringReplacer
1010
import io.github.rothes.protocolstringreplacer.plugin
11-
import io.github.rothes.protocolstringreplacer.replacer.ReplacerManager.ItemMetaCache
11+
import io.github.rothes.protocolstringreplacer.replacer.ReplacerManager.HandledItemCache
1212
import org.bukkit.Material
1313
import org.bukkit.inventory.ItemStack
1414
import org.bukkit.inventory.meta.ItemMeta
@@ -17,7 +17,7 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
1717
AbstractContainer<ItemStack>(itemStack, root) {
1818

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

29-
val getCache = replacerManager.getReplacedItemCache(original, itemType)
29+
val getCache = replacerManager.getReplacedItemCache(content)
3030
if (getCache != null) {
3131
metaCache = getCache
3232
isFromCache = true
3333
nbt = metaCache.nbt
3434
} else {
3535
isFromCache = false
3636
nbt = NBT.itemStackToNBT(content)
37-
metaCache = replacerManager.addReplacedItemCache(original, nbt, itemType, false, IntArray(0))
37+
metaCache = replacerManager.addReplacedItemCache(content, nbt, false, IntArray(0))
3838
}
3939
} else {
4040
isFromCache = false

0 commit comments

Comments
 (0)