Skip to content

Commit

Permalink
Fixed Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
osos2000-ui committed Dec 22, 2024
1 parent aaeaaa9 commit 9539594
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/zeroBzeroT/antiillegals/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void onPlayerItemDrop(@NotNull final PlayerDropItemEvent event) {
final Item itemDrop = event.getItemDrop();
final ItemStack itemStack = itemDrop.getItemStack();

RevertHelper.checkItemStack(itemStack, itemDrop.getLocation(), true);
RevertHelper.checkItemStack(itemStack, itemDrop.getLocation(), true, true); // Using cache
}

@EventHandler(ignoreCancelled = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private RevertHelper() {
*/
public static boolean revert(@Nullable final ItemStack itemStack, @Nullable final Location location,
final boolean checkRecursive, @NotNull final Predicate<ItemState> predicate) {
return predicate.test(checkItemStack(itemStack, location, checkRecursive));
return predicate.test(checkItemStack(itemStack, location, checkRecursive, true)); // Using cache
}

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ public static RevertionResult checkInventory(@NotNull final Inventory inventory,
boolean wasFixed = false;

for (final ItemStack itemStack : inventory.getContents()) {
switch (checkItemStack(itemStack, location, checkRecursive)) {
switch (checkItemStack(itemStack, location, checkRecursive, false)) {
case ILLEGAL -> removeItemStacks.add(itemStack);
case WAS_FIXED -> wasFixed = true;
case IS_BOOK -> bookItemStacks.add(itemStack);
Expand Down Expand Up @@ -287,22 +287,28 @@ public static void checkArmorContents(@NotNull final PlayerInventory playerInven
* @param itemStack the item to revert
* @param location the location where books / book shulkers should drop (if any)
* @param checkRecursive whether containers in item form should be checked for their content recursively
* @param useCache whether to use the cache for item state checks
* @return the state of the item that was checked
*/
@NotNull
public static ItemState checkItemStack(@Nullable final ItemStack itemStack, @Nullable final Location location,
final boolean checkRecursive) {
final boolean checkRecursive, final boolean useCache) {

if (itemStack == null || itemStack.getType() == Material.AIR || itemStack.getAmount() == 0)
return ItemState.EMPTY;

final int metaHash = CachedState.itemStackHashCode(itemStack);
final CachedState cachedRevertedItem = REVERTED_ITEM_CACHE.getIfPresent(metaHash);

CachedState cachedRevertedItem = useCache ? REVERTED_ITEM_CACHE.getIfPresent(metaHash) : null;


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

if (revertedState.wasModified() && useCache) {

REVERTED_ITEM_CACHE.put(metaHash, new CachedState(itemStack.clone(), revertedState));
}
return revertedState;
}
cachedRevertedItem.applyRevertedState(itemStack);
Expand Down

0 comments on commit 9539594

Please sign in to comment.