Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exception when removing air from slot #6836

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test "remove air from air slot":
set {_chest} to chest inventory with 3 rows
# throws exception if not fixed
remove 1 of (slot 0 of {_chest}) from (slot 0 of {_chest})