From 390dd7ceeaae5fd1e5fdc36c8166b2e2da8aa3cf Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Sun, 30 Jun 2024 21:44:42 +0200 Subject: [PATCH 1/2] fix exception when removing air from slot --- .../ch/njol/skript/bukkitutil/ItemUtils.java | 35 +++++++++++++------ .../regressions/6830-remove air from slot.sk | 5 +++ 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/test/skript/tests/regressions/6830-remove air from slot.sk diff --git a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java index 31bf99c2d56..216be44b918 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java @@ -42,9 +42,17 @@ public class ItemUtils { * @return Damage. */ public static int getDamage(ItemStack itemStack) { - ItemMeta meta = itemStack.getItemMeta(); - if (meta instanceof Damageable) - return ((Damageable) meta).getDamage(); + return getDamage(itemStack.getItemMeta()); + } + + /** + * Gets damage/durability of an itemmeta, or 0 if it does not have damage. + * @param itemMeta ItemMeta. + * @return Damage. + */ + public static int getDamage(ItemMeta itemMeta) { + if (itemMeta instanceof Damageable) + return ((Damageable) itemMeta).getDamage(); return 0; // Non damageable item } @@ -143,15 +151,22 @@ public static Material asItem(Material type) { /** * Tests whether two item stacks are of the same type, i.e. it ignores the amounts. * - * @param is1 - * @param is2 + * @param itemStack1 + * @param itemStack2 * @return Whether the item stacks are of the same type */ - public static boolean itemStacksEqual(final @Nullable ItemStack is1, final @Nullable ItemStack is2) { - if (is1 == null || is2 == null) - return is1 == is2; - return is1.getType() == is2.getType() && ItemUtils.getDamage(is1) == ItemUtils.getDamage(is2) - && is1.getItemMeta().equals(is2.getItemMeta()); + public static boolean itemStacksEqual(@Nullable ItemStack itemStack1, @Nullable ItemStack itemStack2) { + if (itemStack1 == null || itemStack2 == null) + return itemStack1 == itemStack2; + if (itemStack1.getType() != itemStack2.getType()) + return false; + + ItemMeta itemMeta1 = itemStack1.getItemMeta(); + ItemMeta itemMeta2 = itemStack2.getItemMeta(); + if (itemMeta1 == null || itemMeta2 == null) + return itemMeta1 == itemMeta2; + + return itemStack1.getItemMeta().equals(itemStack2.getItemMeta()); } // Only 1.15 and versions after have Material#isAir method diff --git a/src/test/skript/tests/regressions/6830-remove air from slot.sk b/src/test/skript/tests/regressions/6830-remove air from slot.sk new file mode 100644 index 00000000000..c10a48451b0 --- /dev/null +++ b/src/test/skript/tests/regressions/6830-remove air from slot.sk @@ -0,0 +1,5 @@ +test "remove air from air slot": + set {a} to chest inventory with 3 rows + # throws exception if not fixed + remove 1 of (slot 0 of {a}) from (slot 0 of {a}) + delete {a} \ No newline at end of file From 6c08c0c337ba32690bf4fc9ac66af78df0379e8c Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Sun, 30 Jun 2024 21:47:46 +0200 Subject: [PATCH 2/2] Update 6830-remove air from slot.sk --- .../skript/tests/regressions/6830-remove air from slot.sk | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/skript/tests/regressions/6830-remove air from slot.sk b/src/test/skript/tests/regressions/6830-remove air from slot.sk index c10a48451b0..13eaa29b1d3 100644 --- a/src/test/skript/tests/regressions/6830-remove air from slot.sk +++ b/src/test/skript/tests/regressions/6830-remove air from slot.sk @@ -1,5 +1,4 @@ test "remove air from air slot": - set {a} to chest inventory with 3 rows + set {_chest} to chest inventory with 3 rows # throws exception if not fixed - remove 1 of (slot 0 of {a}) from (slot 0 of {a}) - delete {a} \ No newline at end of file + remove 1 of (slot 0 of {_chest}) from (slot 0 of {_chest})