Class ItemUtils

java.lang.Object
redempt.redlib.itemutils.ItemUtils

public class ItemUtils extends Object
A utility class to easily modify items
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.bukkit.inventory.ItemStack
    addAttribute(org.bukkit.inventory.ItemStack item, org.bukkit.attribute.Attribute attribute, double amount, org.bukkit.attribute.AttributeModifier.Operation operation)
    Add an attribute to the item
    static org.bukkit.inventory.ItemStack
    addAttribute(org.bukkit.inventory.ItemStack item, org.bukkit.attribute.Attribute attribute, double amount, org.bukkit.attribute.AttributeModifier.Operation operation, org.bukkit.inventory.EquipmentSlot slot)
    Add an attribute to the item
    static org.bukkit.inventory.ItemStack
    addAttribute(org.bukkit.inventory.ItemStack item, org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier modifier)
    Add an attribute to the item
    static org.bukkit.inventory.ItemStack
    addEnchant(org.bukkit.inventory.ItemStack item, org.bukkit.enchantments.Enchantment enchant, int level)
    Add an enchantment to an ItemStack
    static org.bukkit.inventory.ItemStack
    addItemFlags(org.bukkit.inventory.ItemStack item, org.bukkit.inventory.ItemFlag... flags)
    Adds ItemFlags to the item
    static org.bukkit.inventory.ItemStack
    addLore(org.bukkit.inventory.ItemStack item, Iterable<String> lines)
    Adds multiple lines of lore to an ItemStack
    static org.bukkit.inventory.ItemStack
    addLore(org.bukkit.inventory.ItemStack item, String line)
    Add a line of lore to an ItemStack
    static <T, Z> org.bukkit.inventory.ItemStack
    addPersistentTag(org.bukkit.inventory.ItemStack item, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T,Z> type, Z data)
    Adds persistent data to the item
    static org.bukkit.inventory.Inventory
    cloneInventory(org.bukkit.inventory.Inventory inv)
    Creates a mock inventory clone of the given inventory.
    static boolean
    compare(org.bukkit.inventory.ItemStack first, org.bukkit.inventory.ItemStack second)
    Compares the type, name, and lore of two items
    static boolean
    compare(org.bukkit.inventory.ItemStack first, org.bukkit.inventory.ItemStack second, ItemTrait... traits)
    Compares the traits of two items
    static int
    count(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item)
    Counts the number of the given item in the given inventory
    static int
    count(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, BiPredicate<org.bukkit.inventory.ItemStack,org.bukkit.inventory.ItemStack> comparison)
    Counts the number of the given item in the given inventory
    static int
    count(org.bukkit.inventory.Inventory inv, org.bukkit.Material type)
    Counts the number of items of the given type in the given inventory
    static int
    countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item)
    Remove all matching items, returning the number that were removed
    static int
    countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int max)
    Remove all matching items up to a maximum, returning the number that were removed
    static int
    countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int max, BiPredicate<org.bukkit.inventory.ItemStack,org.bukkit.inventory.ItemStack> comparison)
    Remove all matching items up to a maximum, returning the number that were removed
    static int
    countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.Material type)
    Remove all items of a specified type, returning the number that were removed
    static int
    countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.Material type, int max)
    Remove all matching items up to a maximum, returning the number that were removed
    static org.bukkit.inventory.ItemStack
    damage(org.bukkit.inventory.ItemStack item, int amount)
    Damages an item
    static org.bukkit.inventory.ItemStack
    Constructs an ItemStack from a previously-serialized JSON string
    static void
    give(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack... items)
    Give the player the specified items, dropping them on the ground if there is not enough room
    static void
    give(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack item, int amount)
    Gives the player the specified amount of the specified item, dropping them on the ground if there is not enough room
    static void
    give(org.bukkit.entity.Player player, org.bukkit.Material type, int amount)
    Gives the player the specified amount of the specified item type, dropping them on the ground if there is not enough room
    static boolean
    isEmpty(org.bukkit.inventory.ItemStack item)
    Checks whether an item is empty, meaning it is either null or air
    static int
    minimumChestSize(int items)
    Calculates the minimum chest size (next highest multiple of 9) required to fit the given number of item stacks
    static boolean
    remove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int amount)
    Removes the specified amount of the given item from the given inventory
    static boolean
    remove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int amount, BiPredicate<org.bukkit.inventory.ItemStack,org.bukkit.inventory.ItemStack> comparison)
    Removes the specified amount of the given item from the given inventory
    static boolean
    remove(org.bukkit.inventory.Inventory inv, org.bukkit.Material type, int amount)
    Removes the specified amount of the given item type from the given inventory
    static org.bukkit.inventory.ItemStack
    removeLoreLine(org.bukkit.inventory.ItemStack item, int index)
    Removes a specific index-line of lore from an ItemStack if present in an ItemStack
    static org.bukkit.inventory.ItemStack
    removeLoreLine(org.bukkit.inventory.ItemStack item, String line)
    Remove a specific line of lore from an ItemStack if present in an ItemStack
    static org.bukkit.inventory.ItemStack
    rename(org.bukkit.inventory.ItemStack item, String name)
    Renames an ItemStack, functionally identical to setName(ItemStack, String) but kept for legacy reasons.
    static org.bukkit.inventory.ItemStack
    setCustomModelData(org.bukkit.inventory.ItemStack item, int customModelData)
    Sets the custom model data of the item
    static org.bukkit.inventory.ItemStack
    setLore(org.bukkit.inventory.ItemStack item, String line)
    Set a single line of lore for an ItemStack
    static org.bukkit.inventory.ItemStack
    setLore(org.bukkit.inventory.ItemStack item, String... lore)
    Set multiple lines of lore for an ItemStack
    static org.bukkit.inventory.ItemStack
    setLore(org.bukkit.inventory.ItemStack item, List<String> lore)
    Set multiple lines of lore for an ItemStack
    static org.bukkit.inventory.ItemStack
    setName(org.bukkit.inventory.ItemStack item, String name)
    Renames an ItemStack
    static org.bukkit.inventory.ItemStack
    setUnbreakable(org.bukkit.inventory.ItemStack item)
    Sets an item to be unbreakable
    static org.bukkit.inventory.ItemStack
    skull(org.bukkit.OfflinePlayer owner)
    Creates a player skull
    static String
    toString(org.bukkit.inventory.ItemStack item)
    Converts an ItemStack to a JSON string

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ItemUtils

      public ItemUtils()
  • Method Details

    • skull

      public static org.bukkit.inventory.ItemStack skull(org.bukkit.OfflinePlayer owner)
      Creates a player skull
      Parameters:
      owner - The owning player
      Returns:
      The skull for the player
    • rename

      public static org.bukkit.inventory.ItemStack rename(org.bukkit.inventory.ItemStack item, String name)
      Renames an ItemStack, functionally identical to setName(ItemStack, String) but kept for legacy reasons. This has no effect on items without meta such as Air
      Parameters:
      item - The ItemStack to be renamed
      name - The name to give the ItemStack
      Returns:
      The renamed ItemStack
    • setName

      public static org.bukkit.inventory.ItemStack setName(org.bukkit.inventory.ItemStack item, String name)
      Renames an ItemStack
      Parameters:
      item - The ItemStack to be renamed
      name - The name to give the ItemStack
      Returns:
      The renamed ItemStack
    • setLore

      public static org.bukkit.inventory.ItemStack setLore(org.bukkit.inventory.ItemStack item, String line)
      Set a single line of lore for an ItemStack
      Parameters:
      item - The ItemStack to be given lore
      line - The line of lore to be given
      Returns:
      The modified ItemStack
    • setLore

      public static org.bukkit.inventory.ItemStack setLore(org.bukkit.inventory.ItemStack item, List<String> lore)
      Set multiple lines of lore for an ItemStack
      Parameters:
      item - The ItemStack to be given lore
      lore - The lines of lore to be given
      Returns:
      The modified ItemStack
    • addLore

      public static org.bukkit.inventory.ItemStack addLore(org.bukkit.inventory.ItemStack item, String line)
      Add a line of lore to an ItemStack
      Parameters:
      item - The ItemStack to be given lore
      line - The line of lore to add
      Returns:
      The modified ItemStack
    • addLore

      public static org.bukkit.inventory.ItemStack addLore(org.bukkit.inventory.ItemStack item, Iterable<String> lines)
      Adds multiple lines of lore to an ItemStack
      Parameters:
      item - The ItemStack to be given lore
      lines - The lines or lore to add
      Returns:
      The modified ItemStack
    • removeLoreLine

      public static org.bukkit.inventory.ItemStack removeLoreLine(org.bukkit.inventory.ItemStack item, String line)
      Remove a specific line of lore from an ItemStack if present in an ItemStack
      Parameters:
      item - The ItemStack to remove lore from
      line - The line of lore to remove
      Returns:
      The modified ItemStack
    • removeLoreLine

      public static org.bukkit.inventory.ItemStack removeLoreLine(org.bukkit.inventory.ItemStack item, int index)
      Removes a specific index-line of lore from an ItemStack if present in an ItemStack
      Parameters:
      item - The ItemStack to remove lore from
      index - The index of the line of lore to remove
      Returns:
      The modified ItemStack
    • setLore

      public static org.bukkit.inventory.ItemStack setLore(org.bukkit.inventory.ItemStack item, String... lore)
      Set multiple lines of lore for an ItemStack
      Parameters:
      item - The ItemStack to be given lore
      lore - The lines of lore to be given
      Returns:
      The modified ItemStack
    • setUnbreakable

      public static org.bukkit.inventory.ItemStack setUnbreakable(org.bukkit.inventory.ItemStack item)
      Sets an item to be unbreakable
      Parameters:
      item - The item to make unbreakable
      Returns:
      The unbreakable item
    • addEnchant

      public static org.bukkit.inventory.ItemStack addEnchant(org.bukkit.inventory.ItemStack item, org.bukkit.enchantments.Enchantment enchant, int level)
      Add an enchantment to an ItemStack
      Parameters:
      item - The ItemStack to be enchanted
      enchant - The Enchantment to add to the ItemStack
      level - The level of the Enchantment
      Returns:
      The enchanted ItemStack
    • addAttribute

      public static org.bukkit.inventory.ItemStack addAttribute(org.bukkit.inventory.ItemStack item, org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier modifier)
      Add an attribute to the item
      Parameters:
      item - The item to have an attribute added
      attribute - The Attribute to be added
      modifier - The AttributeModifier to be added
      Returns:
      The modified ItemStack
    • addAttribute

      public static org.bukkit.inventory.ItemStack addAttribute(org.bukkit.inventory.ItemStack item, org.bukkit.attribute.Attribute attribute, double amount, org.bukkit.attribute.AttributeModifier.Operation operation)
      Add an attribute to the item
      Parameters:
      item - The item to have an attribute added
      attribute - The Attribute to be added
      amount - The amount to modify it by
      operation - The operation by which the value will be modified
      Returns:
      The modified item
    • addItemFlags

      public static org.bukkit.inventory.ItemStack addItemFlags(org.bukkit.inventory.ItemStack item, org.bukkit.inventory.ItemFlag... flags)
      Adds ItemFlags to the item
      Parameters:
      item - The item to add ItemFlags to
      flags - The ItemFlags to add
      Returns:
      The modified item
    • setCustomModelData

      public static org.bukkit.inventory.ItemStack setCustomModelData(org.bukkit.inventory.ItemStack item, int customModelData)
      Sets the custom model data of the item
      Parameters:
      item - The item to set the custom model data for
      customModelData - The custom model data to set
      Returns:
      The modified item
    • addPersistentTag

      public static <T, Z> org.bukkit.inventory.ItemStack addPersistentTag(org.bukkit.inventory.ItemStack item, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T,Z> type, Z data)
      Adds persistent data to the item
      Type Parameters:
      T - The primary object type
      Z - The retrieved object type
      Parameters:
      item - The item to add persistent data to
      key - The key to add the data under
      type - The type of the data
      data - The data to store
      Returns:
      The modified item
    • addAttribute

      public static org.bukkit.inventory.ItemStack addAttribute(org.bukkit.inventory.ItemStack item, org.bukkit.attribute.Attribute attribute, double amount, org.bukkit.attribute.AttributeModifier.Operation operation, org.bukkit.inventory.EquipmentSlot slot)
      Add an attribute to the item
      Parameters:
      item - The item to have an attribute added
      attribute - The Attribute to be added
      amount - The amount to modify it by
      operation - The operation by which the value will be modified
      slot - The slot this attribute will be effective in
      Returns:
      The modified item
    • damage

      public static org.bukkit.inventory.ItemStack damage(org.bukkit.inventory.ItemStack item, int amount)
      Damages an item
      Parameters:
      item - The item to damage
      amount - How much damage to apply
      Returns:
      The damaged item
      Throws:
      IllegalArgumentException - if the item is not damageable
    • count

      public static int count(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, BiPredicate<org.bukkit.inventory.ItemStack,org.bukkit.inventory.ItemStack> comparison)
      Counts the number of the given item in the given inventory
      Parameters:
      inv - The inventory to count the items in
      item - The item to count
      comparison - A filter to compare items for counting
      Returns:
      The number of items found
    • count

      public static int count(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item)
      Counts the number of the given item in the given inventory
      Parameters:
      inv - The inventory to count the items in
      item - The item to count
      Returns:
      The number of items found
    • count

      public static int count(org.bukkit.inventory.Inventory inv, org.bukkit.Material type)
      Counts the number of items of the given type in the given inventory
      Parameters:
      inv - The inventory to count the items in
      type - The type of item to count
      Returns:
      The number of items found
    • remove

      public static boolean remove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int amount, BiPredicate<org.bukkit.inventory.ItemStack,org.bukkit.inventory.ItemStack> comparison)
      Removes the specified amount of the given item from the given inventory
      Parameters:
      inv - The inventory to remove the items from
      item - The item to be removed
      amount - The amount of items to remove
      comparison - A filter to compare items for removal
      Returns:
      Whether the amount specified could be removed. False if it removed less than specified.
    • remove

      public static boolean remove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int amount)
      Removes the specified amount of the given item from the given inventory
      Parameters:
      inv - The inventory to remove the items from
      item - The item to be removed
      amount - The amount of items to remove
      Returns:
      Whether the amount specified could be removed. False if it removed less than specified.
    • remove

      public static boolean remove(org.bukkit.inventory.Inventory inv, org.bukkit.Material type, int amount)
      Removes the specified amount of the given item type from the given inventory
      Parameters:
      inv - The inventory to remove the items from
      type - The item type to be removed
      amount - The amount of items to remove
      Returns:
      Whether the amount specified could be removed. False if it removed less than specified.
    • countAndRemove

      public static int countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int max, BiPredicate<org.bukkit.inventory.ItemStack,org.bukkit.inventory.ItemStack> comparison)
      Remove all matching items up to a maximum, returning the number that were removed
      Parameters:
      inv - The inventory to count and remove items from
      item - The item to count and remove
      max - The maximum number of items to remove
      comparison - A filter to compare items for counting and removal
      Returns:
      How many items were removed
    • countAndRemove

      public static int countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item, int max)
      Remove all matching items up to a maximum, returning the number that were removed
      Parameters:
      inv - The inventory to count and remove items from
      item - The item to count and remove
      max - The maximum number of items to remove
      Returns:
      How many items were removed
    • countAndRemove

      public static int countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.Material type, int max)
      Remove all matching items up to a maximum, returning the number that were removed
      Parameters:
      inv - The inventory to count and remove items from
      type - The item type to count and remove
      max - The maximum number of items to remove
      Returns:
      How many items were removed
    • countAndRemove

      public static int countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item)
      Remove all matching items, returning the number that were removed
      Parameters:
      inv - The inventory to count and remove items from
      item - The item to count and remove
      Returns:
      How many items were removed
    • countAndRemove

      public static int countAndRemove(org.bukkit.inventory.Inventory inv, org.bukkit.Material type)
      Remove all items of a specified type, returning the number that were removed
      Parameters:
      inv - The inventory to count and remove items from
      type - The item type to count and remove
      Returns:
      How many items were removed
    • give

      public static void give(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack... items)
      Give the player the specified items, dropping them on the ground if there is not enough room
      Parameters:
      player - The player to give the items to
      items - The items to be given
    • give

      public static void give(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack item, int amount)
      Gives the player the specified amount of the specified item, dropping them on the ground if there is not enough room
      Parameters:
      player - The player to give the items to
      item - The item to be given to the player
      amount - The amount the player should be given
    • give

      public static void give(org.bukkit.entity.Player player, org.bukkit.Material type, int amount)
      Gives the player the specified amount of the specified item type, dropping them on the ground if there is not enough room
      Parameters:
      player - The player to give the items to
      type - The item type to be given to the player
      amount - The amount the player should be given
    • compare

      public static boolean compare(org.bukkit.inventory.ItemStack first, org.bukkit.inventory.ItemStack second, ItemTrait... traits)
      Compares the traits of two items
      Parameters:
      first - The first ItemStack
      second - The second ItemStack
      traits - The ItemTraits to compare
      Returns:
      Whether the two items are identical in terms of the traits provided. Returns true if both items are null, and false if only one is null.
    • compare

      public static boolean compare(org.bukkit.inventory.ItemStack first, org.bukkit.inventory.ItemStack second)
      Compares the type, name, and lore of two items
      Parameters:
      first - The first ItemStack
      second - The second ItemStack
      Returns:
      Whether the two items are identical in terms of type, name, and lore. Returns true if both items are null, and false if only one is null.
    • cloneInventory

      public static org.bukkit.inventory.Inventory cloneInventory(org.bukkit.inventory.Inventory inv)
      Creates a mock inventory clone of the given inventory. Do not try to open this inventory for players, it will throw an error.
      Parameters:
      inv - The inventory to clone
      Returns:
      A mock clone inventory
    • minimumChestSize

      public static int minimumChestSize(int items)
      Calculates the minimum chest size (next highest multiple of 9) required to fit the given number of item stacks
      Parameters:
      items - The number of item stacks
      Returns:
      The minimum chest size to accommodate the items
    • isEmpty

      public static boolean isEmpty(org.bukkit.inventory.ItemStack item)
      Checks whether an item is empty, meaning it is either null or air
      Parameters:
      item - The item to check
      Returns:
      Whether the item is empty
    • toString

      public static String toString(org.bukkit.inventory.ItemStack item)
      Converts an ItemStack to a JSON string
      Parameters:
      item - The ItemStack to convert to a string
      Returns:
      A JSON string representing the given item
    • fromString

      public static org.bukkit.inventory.ItemStack fromString(String json)
      Constructs an ItemStack from a previously-serialized JSON string
      Parameters:
      json - The JSON string created using toString(ItemStack)
      Returns:
      The deserialized ItemStack