diff --git a/pom.xml b/pom.xml index 0cd82ad..324f080 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.zeroBzeroT AntiIllegals - 1.0.61 + 1.0.62 jar ${project.artifactId} diff --git a/src/main/java/org/zeroBzeroT/antiillegals/result/CachedState.java b/src/main/java/org/zeroBzeroT/antiillegals/result/CachedState.java index 18c4e10..91558b4 100644 --- a/src/main/java/org/zeroBzeroT/antiillegals/result/CachedState.java +++ b/src/main/java/org/zeroBzeroT/antiillegals/result/CachedState.java @@ -1,5 +1,6 @@ package org.zeroBzeroT.antiillegals.result; +import com.google.gson.Gson; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; @@ -9,18 +10,38 @@ public record CachedState(@NotNull ItemStack revertedStack, @NotNull ItemState revertedState) { + /** + * hashes the item identity, not the object reference itself + * @param itemStack the itemstack to find the hashcode of + * @return the hashcode + */ public static int itemStackHashCode(@NotNull final ItemStack itemStack) { - final ItemMeta meta = itemStack.getItemMeta(); - - // hash the item identity, not the object reference itself return Objects.hash( itemStack.getType().ordinal(), itemStack.getDurability(), itemStack.getAmount(), - String.valueOf(meta) + nbtHashCode(itemStack) ); } + @NotNull + private static final Gson NBT_GSON = new Gson(); + + /** + * order-independent nbt hashcode implementation + * @param itemStack the itemstack of which the nbt json will be used + * @return the hashcode of the nbt, ignoring the order of keys/values to ensure proper identity + */ + public static int nbtHashCode(@NotNull final ItemStack itemStack) { + final ItemMeta nbt = itemStack.clone().getItemMeta(); + if (nbt == null) return 0; + + final String name = nbt.getDisplayName(); + nbt.setDisplayName(null); + + return Objects.hash(NBT_GSON.toJsonTree(nbt), name); + } + public void applyRevertedState(@NotNull final ItemStack cached) { if (revertedState == ItemState.CLEAN) return; // nothing to change