Skip to content

Commit 9539594

Browse files
committed
Fixed Cache
1 parent aaeaaa9 commit 9539594

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/main/java/org/zeroBzeroT/antiillegals/Events.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void onPlayerItemDrop(@NotNull final PlayerDropItemEvent event) {
7878
final Item itemDrop = event.getItemDrop();
7979
final ItemStack itemStack = itemDrop.getItemStack();
8080

81-
RevertHelper.checkItemStack(itemStack, itemDrop.getLocation(), true);
81+
RevertHelper.checkItemStack(itemStack, itemDrop.getLocation(), true, true); // Using cache
8282
}
8383

8484
@EventHandler(ignoreCancelled = true)

src/main/java/org/zeroBzeroT/antiillegals/helpers/RevertHelper.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private RevertHelper() {
5353
*/
5454
public static boolean revert(@Nullable final ItemStack itemStack, @Nullable final Location location,
5555
final boolean checkRecursive, @NotNull final Predicate<ItemState> predicate) {
56-
return predicate.test(checkItemStack(itemStack, location, checkRecursive));
56+
return predicate.test(checkItemStack(itemStack, location, checkRecursive, true)); // Using cache
5757
}
5858

5959
/**
@@ -195,7 +195,7 @@ public static RevertionResult checkInventory(@NotNull final Inventory inventory,
195195
boolean wasFixed = false;
196196

197197
for (final ItemStack itemStack : inventory.getContents()) {
198-
switch (checkItemStack(itemStack, location, checkRecursive)) {
198+
switch (checkItemStack(itemStack, location, checkRecursive, false)) {
199199
case ILLEGAL -> removeItemStacks.add(itemStack);
200200
case WAS_FIXED -> wasFixed = true;
201201
case IS_BOOK -> bookItemStacks.add(itemStack);
@@ -287,22 +287,28 @@ public static void checkArmorContents(@NotNull final PlayerInventory playerInven
287287
* @param itemStack the item to revert
288288
* @param location the location where books / book shulkers should drop (if any)
289289
* @param checkRecursive whether containers in item form should be checked for their content recursively
290+
* @param useCache whether to use the cache for item state checks
290291
* @return the state of the item that was checked
291292
*/
292293
@NotNull
293294
public static ItemState checkItemStack(@Nullable final ItemStack itemStack, @Nullable final Location location,
294-
final boolean checkRecursive) {
295+
final boolean checkRecursive, final boolean useCache) {
296+
295297
if (itemStack == null || itemStack.getType() == Material.AIR || itemStack.getAmount() == 0)
296298
return ItemState.EMPTY;
297299

298300
final int metaHash = CachedState.itemStackHashCode(itemStack);
299-
final CachedState cachedRevertedItem = REVERTED_ITEM_CACHE.getIfPresent(metaHash);
301+
302+
CachedState cachedRevertedItem = useCache ? REVERTED_ITEM_CACHE.getIfPresent(metaHash) : null;
303+
300304

301305
if (cachedRevertedItem == null) {
302306
final ItemState revertedState = checkItemStackUncached(itemStack, location, checkRecursive);
303-
if (revertedState.wasModified())
304-
REVERTED_ITEM_CACHE.put(metaHash, new CachedState(itemStack.clone(), revertedState));
305307

308+
if (revertedState.wasModified() && useCache) {
309+
310+
REVERTED_ITEM_CACHE.put(metaHash, new CachedState(itemStack.clone(), revertedState));
311+
}
306312
return revertedState;
307313
}
308314
cachedRevertedItem.applyRevertedState(itemStack);

0 commit comments

Comments
 (0)