Paper/patches/api/0116-ItemStack-API-addition...

205 lines
6.3 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 22 Jun 2018 22:59:18 -0400
Subject: [PATCH] ItemStack API additions for quantity/flags/lore
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
Updated Upstream (Bukkit/CraftBukkit) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 69fa4695 Add some missing deprecation annotations f850da2e Update Maven plugins/versions 8d8400db Use regular compiler seeing as ECJ doesn't support Java 21 JRE c29e1688 Revert "BUILDTOOLS-676: Downgrade Maven compiler version" 07bce714 SPIGOT-7355: More field renames and fixes 6a8ea764 Fix bad merge in penultimate commit 50a7920c Fix imports in previous commit 83640dd1 PR-995: Add required feature to MinecraftExperimental for easy lookups fc1f96cf BUILDTOOLS-676: Downgrade Maven compiler version CraftBukkit Changes: 90f1059ba Fix item placement 661afb43c SPIGOT-7633: Clearer error message for missing particle data 807b465b3 SPIGOT-7634: Armadillo updates infrequently 590cf09a8 Fix unit tests always seeing Mojang server as unavailable 7c7ac5eb2 SPIGOT-7636: Fix clearing ItemMeta 4a72905cf SPIGOT-7635: Fix Player#transfer and cookie methods ebb50e136 Fix incorrect Vault implementation b33fed8b7 Update Maven plugins/versions 6f00f0608 SPIGOT-7632: Control middle clicking chest does not copy contents db821f405 Use regular compiler seeing as ECJ doesn't support Java 21 JRE 8a2976737 Revert "BUILDTOOLS-676: Downgrade Maven compiler version" 0297f87bb SPIGOT-7355: More field renames and fixes 2d03bdf6a SPIGOT-7629: Fix loading banner patterns e77951fac Fix equality of deserialized display names c66f3e4fd SPIGOT-7631: Fix deserialisation of BlockStateMeta 9c2c7be8d SPIGOT-7630: Fix crash saving unticked leashed entities 8c1e7c841 PR-1384: Disable certain PlayerProfile tests, if Mojang's services or internet are not available ced93d572 SPIGOT-7626: sendSignChange() has no effect c77362cae SPIGOT-7625: ItemStack with lore cannot be serialized in 1.20.5 ff2004387 SPIGOT-7620: Fix server crash when hoppers transfer items to double chests 8b4abeb03 BUILDTOOLS-676: Downgrade Maven compiler version
2024-04-25 23:21:18 +02:00
index 7afb1b4fde29d09ad2289956531daded56b02147..c1e64e09d792da7c7918e401b306f8945a0ff32d 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java
2022-06-07 19:20:30 +02:00
@@ -3,6 +3,7 @@ package org.bukkit.inventory;
import com.google.common.base.Preconditions;
2021-06-11 14:02:28 +02:00
import com.google.common.collect.ImmutableMap;
import java.util.LinkedHashMap;
+import java.util.List; // Paper
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Updated Upstream (Bukkit/CraftBukkit) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 69fa4695 Add some missing deprecation annotations f850da2e Update Maven plugins/versions 8d8400db Use regular compiler seeing as ECJ doesn't support Java 21 JRE c29e1688 Revert "BUILDTOOLS-676: Downgrade Maven compiler version" 07bce714 SPIGOT-7355: More field renames and fixes 6a8ea764 Fix bad merge in penultimate commit 50a7920c Fix imports in previous commit 83640dd1 PR-995: Add required feature to MinecraftExperimental for easy lookups fc1f96cf BUILDTOOLS-676: Downgrade Maven compiler version CraftBukkit Changes: 90f1059ba Fix item placement 661afb43c SPIGOT-7633: Clearer error message for missing particle data 807b465b3 SPIGOT-7634: Armadillo updates infrequently 590cf09a8 Fix unit tests always seeing Mojang server as unavailable 7c7ac5eb2 SPIGOT-7636: Fix clearing ItemMeta 4a72905cf SPIGOT-7635: Fix Player#transfer and cookie methods ebb50e136 Fix incorrect Vault implementation b33fed8b7 Update Maven plugins/versions 6f00f0608 SPIGOT-7632: Control middle clicking chest does not copy contents db821f405 Use regular compiler seeing as ECJ doesn't support Java 21 JRE 8a2976737 Revert "BUILDTOOLS-676: Downgrade Maven compiler version" 0297f87bb SPIGOT-7355: More field renames and fixes 2d03bdf6a SPIGOT-7629: Fix loading banner patterns e77951fac Fix equality of deserialized display names c66f3e4fd SPIGOT-7631: Fix deserialisation of BlockStateMeta 9c2c7be8d SPIGOT-7630: Fix crash saving unticked leashed entities 8c1e7c841 PR-1384: Disable certain PlayerProfile tests, if Mojang's services or internet are not available ced93d572 SPIGOT-7626: sendSignChange() has no effect c77362cae SPIGOT-7625: ItemStack with lore cannot be serialized in 1.20.5 ff2004387 SPIGOT-7620: Fix server crash when hoppers transfer items to double chests 8b4abeb03 BUILDTOOLS-676: Downgrade Maven compiler version
2024-04-25 23:21:18 +02:00
@@ -666,5 +667,185 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
2021-06-11 14:02:28 +02:00
// Requires access to NMS
return ensureServerConversions().getMaxItemUseDuration();
}
+
+ /**
+ * Clones the itemstack and returns it a single quantity.
+ * @return The new itemstack with 1 quantity
+ */
+ @NotNull
+ public ItemStack asOne() {
+ return asQuantity(1);
+ }
+
+ /**
+ * Clones the itemstack and returns it as the specified quantity
+ * @param qty The quantity of the cloned item
+ * @return The new itemstack with specified quantity
+ */
+ @NotNull
+ public ItemStack asQuantity(int qty) {
+ ItemStack clone = clone();
+ clone.setAmount(qty);
+ return clone;
+ }
+
+ /**
+ * Adds 1 to this itemstack. Will not go over the items max stack size.
+ * @return The same item (not a clone)
+ */
+ @NotNull
+ public ItemStack add() {
+ return add(1);
+ }
+
+ /**
+ * Adds quantity to this itemstack. Will not go over the items max stack size.
+ *
+ * @param qty The amount to add
+ * @return The same item (not a clone)
+ */
+ @NotNull
+ public ItemStack add(int qty) {
+ setAmount(Math.min(getMaxStackSize(), getAmount() + qty));
+ return this;
+ }
+
+ /**
+ * Subtracts 1 to this itemstack. Going to 0 or less will invalidate the item.
+ * @return The same item (not a clone)
+ */
+ @NotNull
+ public ItemStack subtract() {
+ return subtract(1);
+ }
+
+ /**
+ * Subtracts quantity to this itemstack. Going to 0 or less will invalidate the item.
+ *
+ * @param qty The amount to add
+ * @return The same item (not a clone)
+ */
+ @NotNull
+ public ItemStack subtract(int qty) {
+ setAmount(Math.max(0, getAmount() - qty));
+ return this;
+ }
+
+ /**
+ * If the item has lore, returns it, else it will return null
+ * @return The lore, or null
+ * @deprecated in favor of {@link #lore()}
+ */
+ @Deprecated
+ public @Nullable List<String> getLore() {
+ if (!hasItemMeta()) {
+ return null;
+ }
+ ItemMeta itemMeta = getItemMeta();
+ if (!itemMeta.hasLore()) {
+ return null;
+ }
+ return itemMeta.getLore();
+ }
+
+ /**
+ * If the item has lore, returns it, else it will return null
+ * @return The lore, or null
+ */
+ public @Nullable List<net.kyori.adventure.text.Component> lore() {
+ if (!this.hasItemMeta()) {
+ return null;
+ }
+ final ItemMeta itemMeta = getItemMeta();
+ if (!itemMeta.hasLore()) {
+ return null;
+ }
+ return itemMeta.lore();
+ }
+
+ /**
+ * Sets the lore for this item.
+ * Removes lore when given null.
+ *
+ * @param lore the lore that will be set
+ * @deprecated in favour of {@link #lore(List)}
+ */
+ @Deprecated
+ public void setLore(@Nullable List<String> lore) {
+ ItemMeta itemMeta = getItemMeta();
+ if (itemMeta == null) {
+ throw new IllegalStateException("Cannot set lore on " + getType());
+ }
+ itemMeta.setLore(lore);
+ setItemMeta(itemMeta);
+ }
+
+ /**
+ * Sets the lore for this item.
+ * Removes lore when given null.
+ *
+ * @param lore the lore that will be set
+ */
+ public void lore(@Nullable List<? extends net.kyori.adventure.text.Component> lore) {
2021-06-11 14:02:28 +02:00
+ ItemMeta itemMeta = getItemMeta();
+ if (itemMeta == null) {
+ throw new IllegalStateException("Cannot set lore on " + getType());
+ }
+ itemMeta.lore(lore);
+ this.setItemMeta(itemMeta);
+ }
+
+ /**
+ * Set itemflags which should be ignored when rendering a ItemStack in the Client. This Method does silently ignore double set itemFlags.
+ *
+ * @param itemFlags The hideflags which shouldn't be rendered
+ */
+ public void addItemFlags(@NotNull ItemFlag... itemFlags) {
+ ItemMeta itemMeta = getItemMeta();
+ if (itemMeta == null) {
+ throw new IllegalStateException("Cannot add flags on " + getType());
+ }
+ itemMeta.addItemFlags(itemFlags);
+ setItemMeta(itemMeta);
+ }
+
+ /**
+ * Remove specific set of itemFlags. This tells the Client it should render it again. This Method does silently ignore double removed itemFlags.
+ *
+ * @param itemFlags Hideflags which should be removed
+ */
+ public void removeItemFlags(@NotNull ItemFlag... itemFlags) {
+ ItemMeta itemMeta = getItemMeta();
+ if (itemMeta == null) {
+ throw new IllegalStateException("Cannot remove flags on " + getType());
+ }
+ itemMeta.removeItemFlags(itemFlags);
+ setItemMeta(itemMeta);
+ }
+
+ /**
+ * Get current set itemFlags. The collection returned is unmodifiable.
+ *
+ * @return A set of all itemFlags set
+ */
+ @NotNull
2022-06-07 19:20:30 +02:00
+ public java.util.Set<ItemFlag> getItemFlags() {
2021-06-11 14:02:28 +02:00
+ ItemMeta itemMeta = getItemMeta();
+ if (itemMeta == null) {
+ return java.util.Collections.emptySet();
+ }
+ return itemMeta.getItemFlags();
+ }
+
+ /**
+ * Check if the specified flag is present on this item.
+ *
+ * @param flag the flag to check
+ * @return if it is present
+ */
+ public boolean hasItemFlag(@NotNull ItemFlag flag) {
+ ItemMeta itemMeta = getItemMeta();
+ return itemMeta != null && itemMeta.hasItemFlag(flag);
+ }
// Paper end
}