-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ItemStack#isEmpty and related methods (#9664)
* Add new patches * Change from an EMPTY static var to a static method since ItemStack is mutable * Properly set nullability of return value * Move annotation changes to different patch * Send the Kotlin code back to where it came from * rebased --------- Co-authored-by: Jake Potrebic <[email protected]>
- Loading branch information
1 parent
83cfeb1
commit deb92c2
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
patches/api/0440-Allow-proper-checking-of-empty-item-stacks.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Aeltumn <[email protected]> | ||
Date: Mon, 28 Aug 2023 13:41:09 +0200 | ||
Subject: [PATCH] Allow proper checking of empty item stacks | ||
|
||
This adds a method to check if an item stack is empty or not. This mirrors vanilla's implementation of the same method. | ||
|
||
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java | ||
index d15a74c38576c49df61cfab02c70fc5d8c0dd5f7..0af73cc04edb93b9772136d4d808f657ea40e733 100644 | ||
--- a/src/main/java/org/bukkit/inventory/ItemStack.java | ||
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java | ||
@@ -985,5 +985,24 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat | ||
public @NotNull ItemStack damage(int amount, @NotNull org.bukkit.entity.LivingEntity livingEntity) { | ||
return livingEntity.damageItemStack(this, amount); | ||
} | ||
+ | ||
+ /** | ||
+ * Returns an empty item stack, consists of an air material and a stack size of 0. | ||
+ * | ||
+ * Any item stack with a material of air or a stack size of 0 is seen | ||
+ * as being empty by {@link ItemStack#isEmpty}. | ||
+ */ | ||
+ @NotNull | ||
+ public static ItemStack empty() { | ||
+ return new ItemStack(); | ||
+ } | ||
+ | ||
+ /** | ||
+ * Returns whether this item stack is empty and contains no item. This means | ||
+ * it is either air or the stack has a size of 0. | ||
+ */ | ||
+ public boolean isEmpty() { | ||
+ return type.isAir() || amount <= 0; | ||
+ } | ||
// Paper end | ||
} |
31 changes: 31 additions & 0 deletions
31
patches/server/1033-Allow-proper-checking-of-empty-item-stacks.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Aeltumn <[email protected]> | ||
Date: Mon, 28 Aug 2023 13:44:09 +0200 | ||
Subject: [PATCH] Allow proper checking of empty item stacks | ||
|
||
|
||
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java | ||
index 0e5abd2a8694b24d4077a602a544e9c2b4c31822..6556d7ab09826bb5a99f11385eddc26b67e44d68 100644 | ||
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java | ||
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java | ||
@@ -33,12 +33,19 @@ public final class CraftItemStack extends ItemStack { | ||
} | ||
// Paper end - MC Utils | ||
|
||
+ // Paper start - override isEmpty to use vanilla's impl | ||
+ @Override | ||
+ public boolean isEmpty() { | ||
+ return handle == null || handle.isEmpty(); | ||
+ } | ||
+ // Paper end | ||
+ | ||
public static net.minecraft.world.item.ItemStack asNMSCopy(ItemStack original) { | ||
if (original instanceof CraftItemStack) { | ||
CraftItemStack stack = (CraftItemStack) original; | ||
return stack.handle == null ? net.minecraft.world.item.ItemStack.EMPTY : stack.handle.copy(); | ||
} | ||
- if (original == null || original.getType() == Material.AIR) { | ||
+ if (original == null || original.isEmpty()) { // Paper - use isEmpty | ||
return net.minecraft.world.item.ItemStack.EMPTY; | ||
} | ||
|