From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: William Blake Galbreath Date: Sun, 15 Mar 2020 20:52:12 -0500 Subject: [PATCH] ItemStack convenience methods diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java index 615eb24ffdd8f6d55ccd4f21760b809c1098bc68..fbbe9e52ed3437d2407e042f45b0a18abdd48fe1 100644 --- a/src/main/java/org/bukkit/Material.java +++ b/src/main/java/org/bukkit/Material.java @@ -5799,4 +5799,40 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla public BlockType asBlockType() { return blockType.get(); } + + // Purpur start + public boolean isArmor() { + switch (this) { + // + case LEATHER_BOOTS: + case LEATHER_CHESTPLATE: + case LEATHER_HELMET: + case LEATHER_LEGGINGS: + case CHAINMAIL_BOOTS: + case CHAINMAIL_CHESTPLATE: + case CHAINMAIL_HELMET: + case CHAINMAIL_LEGGINGS: + case IRON_BOOTS: + case IRON_CHESTPLATE: + case IRON_HELMET: + case IRON_LEGGINGS: + case GOLDEN_BOOTS: + case GOLDEN_CHESTPLATE: + case GOLDEN_HELMET: + case GOLDEN_LEGGINGS: + case DIAMOND_BOOTS: + case DIAMOND_CHESTPLATE: + case DIAMOND_HELMET: + case DIAMOND_LEGGINGS: + case NETHERITE_BOOTS: + case NETHERITE_CHESTPLATE: + case NETHERITE_HELMET: + case NETHERITE_LEGGINGS: + case TURTLE_HELMET: + return true; + default: + return false; + } + } + // Purpur end } diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java index b59222b8c262545d100a9fd28b3bf1d2a4cf4eb0..4414381ac942c040480d53b03565b160eb85c444 100644 --- a/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/src/main/java/org/bukkit/inventory/ItemStack.java @@ -19,6 +19,13 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.material.MaterialData; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +// Purpur start +import com.google.common.collect.Multimap; +import java.util.Collection; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.block.data.BlockData; +// Purpur end /** * Represents a stack of items. @@ -1137,4 +1144,482 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat return Bukkit.getUnsafe().computeTooltipLines(this, tooltipContext, player); } // Paper end - expose itemstack tooltip lines + + // Purpur start + /** + * Gets the display name that is set. + *

+ * Plugins should check that hasDisplayName() returns true + * before calling this method. + * + * @return the display name that is set + */ + @NotNull + public String getDisplayName() { + return this.craftDelegate.getDisplayName(); + } + + /** + * Sets the display name. + * + * @param name the name to set + */ + public void setDisplayName(@Nullable String name) { + this.craftDelegate.setDisplayName(name); + } + + /** + * Checks for existence of a display name. + * + * @return true if this has a display name + */ + public boolean hasDisplayName() { + return this.craftDelegate.hasDisplayName(); + } + + /** + * Gets the localized display name that is set. + *

+ * Plugins should check that hasLocalizedName() returns true + * before calling this method. + * + * @return the localized name that is set + */ + @NotNull + public String getLocalizedName() { + return this.craftDelegate.getLocalizedName(); + } + + /** + * Sets the localized name. + * + * @param name the name to set + */ + public void setLocalizedName(@Nullable String name) { + this.craftDelegate.setLocalizedName(name); + } + + /** + * Checks for existence of a localized name. + * + * @return true if this has a localized name + */ + public boolean hasLocalizedName() { + return this.craftDelegate.hasLocalizedName(); + } + + /** + * Checks for existence of lore. + * + * @return true if this has lore + */ + public boolean hasLore() { + return this.craftDelegate.hasLore(); + } + + /** + * Checks for existence of the specified enchantment. + * + * @param ench enchantment to check + * @return true if this enchantment exists for this meta + */ + public boolean hasEnchant(@NotNull Enchantment ench) { + return this.craftDelegate.hasEnchant(ench); + } + + /** + * Checks for the level of the specified enchantment. + * + * @param ench enchantment to check + * @return The level that the specified enchantment has, or 0 if none + */ + public int getEnchantLevel(@NotNull Enchantment ench) { + return this.craftDelegate.getEnchantLevel(ench); + } + + /** + * Returns a copy the enchantments in this ItemMeta.
+ * Returns an empty map if none. + * + * @return An immutable copy of the enchantments + */ + @NotNull + public Map getEnchants() { + return this.craftDelegate.getEnchants(); + } + + /** + * Adds the specified enchantment to this item meta. + * + * @param ench Enchantment to add + * @param level Level for the enchantment + * @param ignoreLevelRestriction this indicates the enchantment should be + * applied, ignoring the level limit + * @return true if the item meta changed as a result of this call, false + * otherwise + */ + public boolean addEnchant(@NotNull Enchantment ench, int level, boolean ignoreLevelRestriction) { + return this.craftDelegate.addEnchant(ench, level, ignoreLevelRestriction); + } + + /** + * Removes the specified enchantment from this item meta. + * + * @param ench Enchantment to remove + * @return true if the item meta changed as a result of this call, false + * otherwise + */ + public boolean removeEnchant(@NotNull Enchantment ench) { + return this.craftDelegate.removeEnchant(ench); + } + + /** + * Checks for the existence of any enchantments. + * + * @return true if an enchantment exists on this meta + */ + public boolean hasEnchants() { + return this.craftDelegate.hasEnchants(); + } + + /** + * Checks if the specified enchantment conflicts with any enchantments in + * this ItemMeta. + * + * @param ench enchantment to test + * @return true if the enchantment conflicts, false otherwise + */ + public boolean hasConflictingEnchant(@NotNull Enchantment ench) { + return this.craftDelegate.hasConflictingEnchant(ench); + } + + /** + * Sets the custom model data. + *

+ * CustomModelData is an integer that may be associated client side with a + * custom item model. + * + * @param data the data to set, or null to clear + */ + public void setCustomModelData(@Nullable Integer data) { + this.craftDelegate.setCustomModelData(data); + } + + /** + * Gets the custom model data that is set. + *

+ * CustomModelData is an integer that may be associated client side with a + * custom item model. + *

+ * Plugins should check that hasCustomModelData() returns true + * before calling this method. + * + * @return the localized name that is set + */ + public int getCustomModelData() { + return this.craftDelegate.getCustomModelData(); + } + + /** + * Checks for existence of custom model data. + *

+ * CustomModelData is an integer that may be associated client side with a + * custom item model. + * + * @return true if this has custom model data + */ + public boolean hasCustomModelData() { + return this.craftDelegate.hasCustomModelData(); + } + + /** + * Returns whether the item has block data currently attached to it. + * + * @return whether block data is already attached + */ + public boolean hasBlockData() { + return this.craftDelegate.hasBlockData(); + } + + /** + * Returns the currently attached block data for this item or creates a new + * one if one doesn't exist. + * + * The state is a copy, it must be set back (or to another item) with + * {@link #setBlockData(BlockData)} + * + * @param material the material we wish to get this data in the context of + * @return the attached data or new data + */ + @NotNull + public BlockData getBlockData(@NotNull Material material) { + return this.craftDelegate.getBlockData(material); + } + + /** + * Attaches a copy of the passed block data to the item. + * + * @param blockData the block data to attach to the block. + * @throws IllegalArgumentException if the blockData is null or invalid for + * this item. + */ + public void setBlockData(@NotNull BlockData blockData) { + this.craftDelegate.setBlockData(blockData); + } + + /** + * Gets the repair penalty + * + * @return the repair penalty + */ + public int getRepairCost() { + return this.craftDelegate.getRepairCost(); + } + + /** + * Sets the repair penalty + * + * @param cost repair penalty + */ + public void setRepairCost(int cost) { + this.craftDelegate.setRepairCost(cost); + } + + /** + * Checks to see if this has a repair penalty + * + * @return true if this has a repair penalty + */ + public boolean hasRepairCost() { + return this.craftDelegate.hasRepairCost(); + } + + /** + * Return if the unbreakable tag is true. An unbreakable item will not lose + * durability. + * + * @return true if the unbreakable tag is true + */ + public boolean isUnbreakable() { + return this.craftDelegate.isUnbreakable(); + } + + /** + * Sets the unbreakable tag. An unbreakable item will not lose durability. + * + * @param unbreakable true if set unbreakable + */ + public void setUnbreakable(boolean unbreakable) { + this.craftDelegate.setUnbreakable(unbreakable); + } + + /** + * Checks for the existence of any AttributeModifiers. + * + * @return true if any AttributeModifiers exist + */ + public boolean hasAttributeModifiers() { + return this.craftDelegate.hasAttributeModifiers(); + } + + /** + * Return an immutable copy of all Attributes and + * their modifiers in this ItemMeta.
+ * Returns null if none exist. + * + * @return an immutable {@link Multimap} of Attributes + * and their AttributeModifiers, or null if none exist + */ + @Nullable + public Multimap getAttributeModifiers() { + return this.craftDelegate.getAttributeModifiers(); + } + + /** + * Return an immutable copy of all {@link Attribute}s and their + * {@link AttributeModifier}s for a given {@link EquipmentSlot}.
+ * Any {@link AttributeModifier} that does have have a given + * {@link EquipmentSlot} will be returned. This is because + * AttributeModifiers without a slot are active in any slot.
+ * If there are no attributes set for the given slot, an empty map + * will be returned. + * + * @param slot the {@link EquipmentSlot} to check + * @return the immutable {@link Multimap} with the + * respective Attributes and modifiers, or an empty map + * if no attributes are set. + */ + @NotNull + public Multimap getAttributeModifiers(@Nullable EquipmentSlot slot) { + return this.craftDelegate.getAttributeModifiers(slot); + } + + /** + * Return an immutable copy of all {@link AttributeModifier}s + * for a given {@link Attribute} + * + * @param attribute the {@link Attribute} + * @return an immutable collection of {@link AttributeModifier}s + * or null if no AttributeModifiers exist for the Attribute. + * @throws NullPointerException if Attribute is null + */ + @Nullable + public Collection getAttributeModifiers(@NotNull Attribute attribute) { + return this.craftDelegate.getAttributeModifiers(attribute); + } + + /** + * Add an Attribute and it's Modifier. + * AttributeModifiers can now support {@link EquipmentSlot}s. + * If not set, the {@link AttributeModifier} will be active in ALL slots. + *
+ * Two {@link AttributeModifier}s that have the same {@link java.util.UUID} + * cannot exist on the same Attribute. + * + * @param attribute the {@link Attribute} to modify + * @param modifier the {@link AttributeModifier} specifying the modification + * @return true if the Attribute and AttributeModifier were + * successfully added + * @throws NullPointerException if Attribute is null + * @throws NullPointerException if AttributeModifier is null + * @throws IllegalArgumentException if AttributeModifier already exists + */ + public boolean addAttributeModifier(@NotNull Attribute attribute, @NotNull AttributeModifier modifier) { + return this.craftDelegate.addAttributeModifier(attribute, modifier); + } + + /** + * Set all {@link Attribute}s and their {@link AttributeModifier}s. + * To clear all currently set Attributes and AttributeModifiers use + * null or an empty Multimap. + * If not null nor empty, this will filter all entries that are not-null + * and add them to the ItemStack. + * + * @param attributeModifiers the new Multimap containing the Attributes + * and their AttributeModifiers + */ + public void setAttributeModifiers(@Nullable Multimap attributeModifiers) { + this.craftDelegate.setAttributeModifiers(attributeModifiers); + } + + /** + * Remove all {@link AttributeModifier}s associated with the given + * {@link Attribute}. + * This will return false if nothing was removed. + * + * @param attribute attribute to remove + * @return true if all modifiers were removed from a given + * Attribute. Returns false if no attributes were + * removed. + * @throws NullPointerException if Attribute is null + */ + public boolean removeAttributeModifier(@NotNull Attribute attribute) { + return this.craftDelegate.removeAttributeModifier(attribute); + } + + /** + * Remove all {@link Attribute}s and {@link AttributeModifier}s for a + * given {@link EquipmentSlot}.
+ * If the given {@link EquipmentSlot} is null, this will remove all + * {@link AttributeModifier}s that do not have an EquipmentSlot set. + * + * @param slot the {@link EquipmentSlot} to clear all Attributes and + * their modifiers for + * @return true if all modifiers were removed that match the given + * EquipmentSlot. + */ + public boolean removeAttributeModifier(@Nullable EquipmentSlot slot) { + return this.craftDelegate.removeAttributeModifier(slot); + } + + /** + * Remove a specific {@link Attribute} and {@link AttributeModifier}. + * AttributeModifiers are matched according to their {@link java.util.UUID}. + * + * @param attribute the {@link Attribute} to remove + * @param modifier the {@link AttributeModifier} to remove + * @return if any attribute modifiers were remove + * + * @throws NullPointerException if the Attribute is null + * @throws NullPointerException if the AttributeModifier is null + * + * @see AttributeModifier#getUniqueId() + */ + public boolean removeAttributeModifier(@NotNull Attribute attribute, @NotNull AttributeModifier modifier) { + return this.craftDelegate.removeAttributeModifier(attribute, modifier); + } + + /** + * Checks to see if this item has damage + * + * @return true if this has damage + */ + public boolean hasDamage() { + return this.craftDelegate.hasDamage(); + } + + /** + * Gets the damage + * + * @return the damage + */ + public int getDamage() { + return this.craftDelegate.getDamage(); + } + + /** + * Sets the damage + * + * @param damage item damage + */ + public void setDamage(int damage) { + this.craftDelegate.setDamage(damage); + } + + /** + * Repairs this item by 1 durability + */ + public void repair() { + repair(1); + } + + /** + * Damages this item by 1 durability + * + * @return True if damage broke the item + */ + public boolean damage() { + return damage(1); + } + + /** + * Repairs this item's durability by amount + * + * @param amount Amount of durability to repair + */ + public void repair(int amount) { + damage(-amount); + } + + /** + * Damages this item's durability by amount + * + * @param amount Amount of durability to damage + * @return True if damage broke the item + */ + public boolean damage(int amount) { + return damage(amount, false); + } + + /** + * Damages this item's durability by amount + * + * @param amount Amount of durability to damage + * @param ignoreUnbreaking Ignores unbreaking enchantment + * @return True if damage broke the item + */ + public boolean damage(int amount, boolean ignoreUnbreaking) { + return this.craftDelegate.damage(amount, ignoreUnbreaking); + } + // Purpur end }