2020-04-02 21:07:06 +00:00
|
|
|
From 3a0213a42c48222049d43905151dae92adfd80a6 Mon Sep 17 00:00:00 2001
|
2018-06-23 03:03:46 +00:00
|
|
|
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
|
2020-04-02 21:07:06 +00:00
|
|
|
index 125b74dd8..4940ba6a9 100644
|
2018-06-23 03:03:46 +00:00
|
|
|
--- a/src/main/java/org/bukkit/inventory/ItemStack.java
|
|
|
|
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java
|
|
|
|
@@ -2,7 +2,9 @@ package org.bukkit.inventory;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import java.util.LinkedHashMap;
|
2019-04-23 04:47:07 +00:00
|
|
|
+import java.util.List; // Paper
|
2018-06-23 03:03:46 +00:00
|
|
|
import java.util.Map;
|
2019-04-23 04:47:07 +00:00
|
|
|
+import java.util.Set; // Paper
|
2018-06-23 03:03:46 +00:00
|
|
|
import org.apache.commons.lang.Validate;
|
|
|
|
import org.bukkit.Bukkit;
|
2019-04-23 04:47:07 +00:00
|
|
|
import org.bukkit.Material;
|
2020-01-22 03:06:28 +00:00
|
|
|
@@ -629,5 +631,140 @@ public class ItemStack implements Cloneable, ConfigurationSerializable {
|
2018-06-23 03:03:46 +00:00
|
|
|
// Requires access to NMS
|
|
|
|
return ensureServerConversions().getMaxItemUseDuration();
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Clones the itemstack and returns it a single quantity.
|
|
|
|
+ * @return The new itemstack with 1 quantity
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ 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
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ 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)
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ 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)
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ 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)
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ 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)
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ public ItemStack subtract(int qty) {
|
2018-06-30 08:45:17 +00:00
|
|
|
+ setAmount(Math.max(0, getAmount() - qty));
|
2018-06-23 03:03:46 +00:00
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * If the item has lore, returns it, else it will return null
|
|
|
|
+ * @return The lore, or null
|
|
|
|
+ */
|
|
|
|
+ @Nullable
|
|
|
|
+ public List<String> getLore() {
|
|
|
|
+ if (!hasItemMeta()) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ ItemMeta itemMeta = getItemMeta();
|
|
|
|
+ if (!itemMeta.hasLore()) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return itemMeta.getLore();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets the lore for this item.
|
|
|
|
+ * Removes lore when given null.
|
|
|
|
+ *
|
|
|
|
+ * @param lore the lore that will be set
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public void setLore(@Nullable List<String> lore) {
|
2018-06-23 03:03:46 +00:00
|
|
|
+ ItemMeta itemMeta = getItemMeta();
|
|
|
|
+ itemMeta.setLore(lore);
|
|
|
|
+ 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
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public void addItemFlags(@NotNull ItemFlag... itemFlags) {
|
2018-06-23 03:03:46 +00:00
|
|
|
+ ItemMeta itemMeta = getItemMeta();
|
|
|
|
+ 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
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public void removeItemFlags(@NotNull ItemFlag... itemFlags) {
|
2018-06-23 03:03:46 +00:00
|
|
|
+ ItemMeta itemMeta = getItemMeta();
|
|
|
|
+ itemMeta.removeItemFlags(itemFlags);
|
|
|
|
+ setItemMeta(itemMeta);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get current set itemFlags. The collection returned is unmodifiable.
|
|
|
|
+ *
|
|
|
|
+ * @return A set of all itemFlags set
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-06-23 03:03:46 +00:00
|
|
|
+ public Set<ItemFlag> getItemFlags() {
|
|
|
|
+ ItemMeta itemMeta = getItemMeta();
|
|
|
|
+ return itemMeta.getItemFlags();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Check if the specified flag is present on this item.
|
|
|
|
+ *
|
|
|
|
+ * @param flag the flag to check
|
|
|
|
+ * @return if it is present
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public boolean hasItemFlag(@NotNull ItemFlag flag) {
|
2018-06-23 03:03:46 +00:00
|
|
|
+ ItemMeta itemMeta = getItemMeta();
|
|
|
|
+ return itemMeta.hasItemFlag(flag);
|
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
}
|
|
|
|
--
|
2020-04-02 21:07:06 +00:00
|
|
|
2.25.1
|
2018-06-23 03:03:46 +00:00
|
|
|
|