1313import io .github .rothes .protocolstringreplacer .replacer .containers .Container ;
1414import io .github .rothes .protocolstringreplacer .replacer .containers .Replaceable ;
1515import org .apache .commons .lang .Validate ;
16- import org .bukkit .Material ;
17- import org .bukkit .inventory .meta .ItemMeta ;
16+ import org .bukkit .inventory .ItemStack ;
1817import org .jetbrains .annotations .NotNull ;
1918import org .neosearch .stringsearcher .Emit ;
2019
2322import java .io .File ;
2423import java .util .ArrayList ;
2524import java .util .Collections ;
26- import java .util .EnumMap ;
2725import java .util .HashMap ;
28- import java .util .Iterator ;
2926import java .util .LinkedList ;
3027import java .util .List ;
3128import java .util .Map ;
3229import java .util .Set ;
30+ import java .util .concurrent .ConcurrentHashMap ;
3331import java .util .function .BiPredicate ;
3432import java .util .regex .Pattern ;
3533import 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 ) {
0 commit comments