Package redempt.redlib.itemutils
Class ItemUtils
java.lang.Object
redempt.redlib.itemutils.ItemUtils
A utility class to easily modify items
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 itemstatic 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 itemstatic 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 itemstatic org.bukkit.inventory.ItemStack
addEnchant
(org.bukkit.inventory.ItemStack item, org.bukkit.enchantments.Enchantment enchant, int level) Add an enchantment to an ItemStackstatic org.bukkit.inventory.ItemStack
addItemFlags
(org.bukkit.inventory.ItemStack item, org.bukkit.inventory.ItemFlag... flags) Adds ItemFlags to the itemstatic org.bukkit.inventory.ItemStack
Adds multiple lines of lore to an ItemStackstatic org.bukkit.inventory.ItemStack
Add a line of lore to an ItemStackstatic <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 itemstatic 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 itemsstatic boolean
compare
(org.bukkit.inventory.ItemStack first, org.bukkit.inventory.ItemStack second, ItemTrait... traits) Compares the traits of two itemsstatic int
count
(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item) Counts the number of the given item in the given inventorystatic 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 inventorystatic int
count
(org.bukkit.inventory.Inventory inv, org.bukkit.Material type) Counts the number of items of the given type in the given inventorystatic int
countAndRemove
(org.bukkit.inventory.Inventory inv, org.bukkit.inventory.ItemStack item) Remove all matching items, returning the number that were removedstatic 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 removedstatic 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 removedstatic int
countAndRemove
(org.bukkit.inventory.Inventory inv, org.bukkit.Material type) Remove all items of a specified type, returning the number that were removedstatic 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 removedstatic org.bukkit.inventory.ItemStack
damage
(org.bukkit.inventory.ItemStack item, int amount) Damages an itemstatic org.bukkit.inventory.ItemStack
fromString
(String json) Constructs an ItemStack from a previously-serialized JSON stringstatic 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 roomstatic 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 roomstatic 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 roomstatic boolean
isEmpty
(org.bukkit.inventory.ItemStack item) Checks whether an item is empty, meaning it is either null or airstatic int
minimumChestSize
(int items) Calculates the minimum chest size (next highest multiple of 9) required to fit the given number of item stacksstatic 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 inventorystatic 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 inventorystatic 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 inventorystatic 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 ItemStackstatic 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 ItemStackstatic org.bukkit.inventory.ItemStack
Renames an ItemStack, functionally identical tosetName(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 itemstatic org.bukkit.inventory.ItemStack
Set a single line of lore for an ItemStackstatic org.bukkit.inventory.ItemStack
Set multiple lines of lore for an ItemStackstatic org.bukkit.inventory.ItemStack
Set multiple lines of lore for an ItemStackstatic org.bukkit.inventory.ItemStack
Renames an ItemStackstatic org.bukkit.inventory.ItemStack
setUnbreakable
(org.bukkit.inventory.ItemStack item) Sets an item to be unbreakablestatic org.bukkit.inventory.ItemStack
skull
(org.bukkit.OfflinePlayer owner) Creates a player skullstatic String
toString
(org.bukkit.inventory.ItemStack item) Converts an ItemStack to a JSON string
-
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 tosetName(ItemStack, String)
but kept for legacy reasons. This has no effect on items without meta such as Air- Parameters:
item
- The ItemStack to be renamedname
- 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 renamedname
- 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 loreline
- 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 lorelore
- 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 loreline
- 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 lorelines
- 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 fromline
- 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 fromindex
- 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 lorelore
- 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 enchantedenchant
- The Enchantment to add to the ItemStacklevel
- 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 addedattribute
- The Attribute to be addedmodifier
- 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 addedattribute
- The Attribute to be addedamount
- The amount to modify it byoperation
- 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 toflags
- 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 forcustomModelData
- 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 typeZ
- The retrieved object type- Parameters:
item
- The item to add persistent data tokey
- The key to add the data undertype
- The type of the datadata
- 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 addedattribute
- The Attribute to be addedamount
- The amount to modify it byoperation
- The operation by which the value will be modifiedslot
- 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 damageamount
- 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 initem
- The item to countcomparison
- 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 initem
- 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 intype
- 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 fromitem
- The item to be removedamount
- The amount of items to removecomparison
- 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 fromitem
- The item to be removedamount
- 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 fromtype
- The item type to be removedamount
- 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 fromitem
- The item to count and removemax
- The maximum number of items to removecomparison
- 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 fromitem
- The item to count and removemax
- 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 fromtype
- The item type to count and removemax
- 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 fromitem
- 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 fromtype
- 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 toitems
- 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 toitem
- The item to be given to the playeramount
- 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 totype
- The item type to be given to the playeramount
- 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 ItemStacksecond
- The second ItemStacktraits
- 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 ItemStacksecond
- 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
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
Constructs an ItemStack from a previously-serialized JSON string- Parameters:
json
- The JSON string created usingtoString(ItemStack)
- Returns:
- The deserialized ItemStack
-