Package com.cryptomorin.xseries
Class XItemStack
java.lang.Object
com.cryptomorin.xseries.XItemStack
XItemStack - YAML ItemStack Serializer
Using ConfigurationSection Example:
Using ConfigurationSection Example:
ConfigurationSection section = plugin.getConfig().getConfigurationSection("staffs.dragon-staff");
ItemStack item = XItemStack.deserialize(section);
What's the point of this class when MemorySection.getItemStack(String) exists?
That method works based on YAML tags which makes the config hideous and doesn't have syntax sugars for certain
configurations to make the config cleaner and concise. Also, certain values will have unreadable formats.
- Version:
- 8.0.0
- Author:
- Crypto Morin
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic final classstatic final class -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull List<org.bukkit.inventory.ItemStack> addItems(@NotNull org.bukkit.inventory.Inventory inventory, boolean split, @Nullable Predicate<Integer> modifiableSlots, @NotNull org.bukkit.inventory.ItemStack... items) Optimized version ofInventory.addItem(ItemStack...)CraftInventorystatic List<org.bukkit.inventory.ItemStack> addItems(@NotNull org.bukkit.inventory.Inventory inventory, boolean split, @NotNull org.bukkit.inventory.ItemStack... items) static @NotNull org.bukkit.inventory.ItemStackdeserialize(@NotNull Map<String, Object> serializedItem) Deserialize an ItemStack from aMap.static @NotNull org.bukkit.inventory.ItemStackdeserialize(@NotNull Map<String, Object> serializedItem, @NotNull Function<String, String> translator) Deserialize an ItemStack from aMap.static @NotNull org.bukkit.inventory.ItemStackdeserialize(@NotNull org.bukkit.configuration.ConfigurationSection config) Deserialize an ItemStack from the config.static @NotNull org.bukkit.inventory.ItemStackdeserialize(@NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull Function<String, String> translator) static @NotNull org.bukkit.inventory.ItemStackdeserialize(@NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull Function<String, String> translator, @Nullable Consumer<Exception> restart) Deserialize an ItemStack from the config.static @NotNull org.bukkit.inventory.ItemStackedit(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull Function<String, String> translator, @Nullable Consumer<Exception> restart) Deserialize an ItemStack from the config.static @org.jetbrains.annotations.Range(from=-1L, to=2147483647L) intfirstEmpty(@NotNull org.bukkit.inventory.Inventory inventory, int beginIndex) static @org.jetbrains.annotations.Range(from=-1L, to=2147483647L) intfirstEmpty(@NotNull org.bukkit.inventory.Inventory inventory, int beginIndex, @Nullable Predicate<Integer> modifiableSlots) Gets the first item slot in the inventory that is empty or matches the given item argument.static @org.jetbrains.annotations.NotNull,@org.jetbrains.annotations.Range(from=-1L, to=2147483647L) intfirstPartial(@NotNull org.bukkit.inventory.Inventory inventory, @Nullable org.bukkit.inventory.ItemStack item, int beginIndex) static @org.jetbrains.annotations.NotNull,@org.jetbrains.annotations.Range(from=-1L, to=2147483647L) intfirstPartial(@NotNull org.bukkit.inventory.Inventory inventory, @Nullable org.bukkit.inventory.ItemStack item, int beginIndex, @Nullable Predicate<Integer> modifiableSlots) Gets the item slot in the inventory that matches the given item argument.static @org.jetbrains.annotations.Range(from=-1L, to=2147483647L) intfirstPartialOrEmpty(@NotNull org.bukkit.inventory.Inventory inventory, @Nullable org.bukkit.inventory.ItemStack item, int beginIndex) Gets the first empty slot or partial item in the inventory from an index.static org.bukkit.inventory.ItemStack[]getStorageContents(org.bukkit.inventory.Inventory inventory) Cross-version compatible version ofInventory.getStorageContents().static @NotNull List<org.bukkit.inventory.ItemStack> giveOrDrop(@NotNull org.bukkit.entity.Player player, boolean split, @Nullable org.bukkit.inventory.ItemStack... items) Adds a list of items to the player's inventory and drop the items that did not fit.static @NotNull List<org.bukkit.inventory.ItemStack> giveOrDrop(@NotNull org.bukkit.entity.Player player, @Nullable org.bukkit.inventory.ItemStack... items) Adds a list of items to the player's inventory and drop the items that did not fit.static booleanisEmpty(@Nullable org.bukkit.inventory.ItemStack item) Checks if this item isnullorMaterial.AIR.static booleannotEmpty(@Nullable org.bukkit.inventory.ItemStack item) static @NotNull org.bukkit.ColorparseColor(@Nullable String str) Parses RGB color codes from a string.serialize(@NotNull org.bukkit.inventory.ItemStack item) Writes an ItemStack properties into aMap.static voidserialize(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.configuration.ConfigurationSection config) static voidserialize(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull Function<String, String> translator) Writes an ItemStack object into a config.static @NotNull List<org.bukkit.inventory.ItemStack> stack(@NotNull Collection<org.bukkit.inventory.ItemStack> items) static @NotNull List<org.bukkit.inventory.ItemStack> stack(@NotNull Collection<org.bukkit.inventory.ItemStack> items, @NotNull BiPredicate<org.bukkit.inventory.ItemStack, org.bukkit.inventory.ItemStack> similarity) Stacks up the items in the given item collection that are pass the similarity check.
-
Field Details
-
SUPPORTS_CUSTOM_MODEL_DATA
public static final boolean SUPPORTS_CUSTOM_MODEL_DATA
-
-
Method Details
-
serialize
public static void serialize(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.configuration.ConfigurationSection config) - Since:
- 1.0.0
- See Also:
-
serialize
public static void serialize(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull @NotNull Function<String, String> translator) Writes an ItemStack object into a config. The config file will not save after the object is written.- Parameters:
item- the ItemStack to serialize.config- the config section to write this item to.translator- the function applied to item name and each lore lines.- Since:
- 7.4.0
-
serialize
Writes an ItemStack properties into aMap.- Parameters:
item- the ItemStack to serialize.- Returns:
- a Map containing the serialized ItemStack properties.
-
deserialize
@NotNull public static @NotNull org.bukkit.inventory.ItemStack deserialize(@NotNull @NotNull org.bukkit.configuration.ConfigurationSection config) Deserialize an ItemStack from the config.- Parameters:
config- the config section to deserialize the ItemStack object from.- Returns:
- a deserialized ItemStack.
- Since:
- 1.0.0
-
deserialize
@NotNull public static @NotNull org.bukkit.inventory.ItemStack deserialize(@NotNull @NotNull Map<String, Object> serializedItem) Deserialize an ItemStack from aMap.- Parameters:
serializedItem- the map holding the item configurations to deserialize the ItemStack object from.- Returns:
- a deserialized ItemStack.
-
deserialize
-
deserialize
@NotNull public static @NotNull org.bukkit.inventory.ItemStack deserialize(@NotNull @NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull @NotNull Function<String, String> translator, @Nullable @Nullable Consumer<Exception> restart) Deserialize an ItemStack from the config.- Parameters:
config- the config section to deserialize the ItemStack object from.- Returns:
- an edited ItemStack.
- Since:
- 7.2.0
-
deserialize
@NotNull public static @NotNull org.bukkit.inventory.ItemStack deserialize(@NotNull @NotNull Map<String, Object> serializedItem, @NotNull @NotNull Function<String, String> translator) Deserialize an ItemStack from aMap.- Parameters:
serializedItem- the map holding the item configurations to deserialize the ItemStack object from.translator- the translator to use for translating the item's name.- Returns:
- a deserialized ItemStack.
-
edit
@NotNull public static @NotNull org.bukkit.inventory.ItemStack edit(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.configuration.ConfigurationSection config, @NotNull @NotNull Function<String, String> translator, @Nullable @Nullable Consumer<Exception> restart) Deserialize an ItemStack from the config.- Parameters:
config- the config section to deserialize the ItemStack object from.translator- the function applied to item name and each lore line.restart- the function called when an error occurs while deserializing one of the properties.- Returns:
- an edited ItemStack.
- Since:
- 1.0.0
-
parseColor
Parses RGB color codes from a string. This only works for 1.13 and above. Accepts the following formats: "r, g, b" "#RRGGBB" decimal number representing"r << 16 | g << 8 | b"(format "0xRRGGBB" is converted to decimal by SnakeYAML and handled as such)- Parameters:
str- the RGB string.- Returns:
- a color based on the RGB.
- Since:
- 1.1.0
-
giveOrDrop
@NotNull @Contract(mutates="param1") public static @NotNull List<org.bukkit.inventory.ItemStack> giveOrDrop(@NotNull @NotNull org.bukkit.entity.Player player, @Nullable @Nullable org.bukkit.inventory.ItemStack... items) Adds a list of items to the player's inventory and drop the items that did not fit.- Parameters:
player- the player to give the items to.items- the items to give.- Returns:
- the items that did not fit and were dropped.
- Since:
- 2.0.1
-
giveOrDrop
@NotNull @Contract(mutates="param1") public static @NotNull List<org.bukkit.inventory.ItemStack> giveOrDrop(@NotNull @NotNull org.bukkit.entity.Player player, boolean split, @Nullable @Nullable org.bukkit.inventory.ItemStack... items) Adds a list of items to the player's inventory and drop the items that did not fit.- Parameters:
player- the player to give the items to.split- same asaddItems(Inventory, boolean, ItemStack...)items- the items to give.- Returns:
- the items that did not fit and were dropped.
- Since:
- 2.0.1
-
addItems
@Contract(mutates="param1") public static List<org.bukkit.inventory.ItemStack> addItems(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, boolean split, @NotNull @NotNull org.bukkit.inventory.ItemStack... items) -
addItems
@NotNull @Contract(mutates="param1") public static @NotNull List<org.bukkit.inventory.ItemStack> addItems(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, boolean split, @Nullable @Nullable Predicate<Integer> modifiableSlots, @NotNull @NotNull org.bukkit.inventory.ItemStack... items) Optimized version ofInventory.addItem(ItemStack...)CraftInventory- Parameters:
inventory- the inventory to add the items to.split- false if it should check for the inventory stack sizeInventory.getMaxStackSize()or true for item's max stack sizeItemStack.getMaxStackSize()when putting items. This is useful when you're adding stacked tools such as swords that you'd like to split them to other slots.modifiableSlots- the slots that are allowed to be used for adding the items, otherwise null to allow all slots.items- the items to add.- Returns:
- items that didn't fit in the inventory.
- Since:
- 4.0.0
-
firstPartial
@NotNull @Contract(pure=true) public static @org.jetbrains.annotations.NotNull,@org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int firstPartial(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, @Nullable @Nullable org.bukkit.inventory.ItemStack item, int beginIndex) -
firstPartial
@NotNull @Contract(pure=true) public static @org.jetbrains.annotations.NotNull,@org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int firstPartial(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, @Nullable @Nullable org.bukkit.inventory.ItemStack item, int beginIndex, @Nullable @Nullable Predicate<Integer> modifiableSlots) Gets the item slot in the inventory that matches the given item argument. The matched item must beItemStack.isSimilar(ItemStack)and has not reached itsItemStack.getMaxStackSize()for the inventory.- Parameters:
inventory- the inventory to match the item from.item- the item to match.beginIndex- the index which to start the search from in the inventory.modifiableSlots- the slots that can be used to share items.- Returns:
- the first matched item slot, otherwise -1
- Throws:
IndexOutOfBoundsException- if the beginning index is less than 0 or greater than the inventory storage size.- Since:
- 4.0.0
-
stack
@NotNull @Contract(pure=true) public static @NotNull List<org.bukkit.inventory.ItemStack> stack(@NotNull @NotNull Collection<org.bukkit.inventory.ItemStack> items) -
stack
@NotNull @Contract(pure=true) public static @NotNull List<org.bukkit.inventory.ItemStack> stack(@NotNull @NotNull Collection<org.bukkit.inventory.ItemStack> items, @NotNull @NotNull BiPredicate<org.bukkit.inventory.ItemStack, org.bukkit.inventory.ItemStack> similarity) Stacks up the items in the given item collection that are pass the similarity check. This means that if you have a collection that consists of separate items with the same material, you can reduce them using the following:List<ItemStack> items = Arrays.asList(XMaterial.STONE.parseItem(), XMaterial.STONE.parseItem(), XMaterial.AIR.parseItem()); items = XItemStack.stack(items, (first, second) -> first.getType == second.getType()); // items -> [STONE x2, AIR x1]- Parameters:
items- the items to stack.- Returns:
- stacked up items.
- Since:
- 4.0.0
-
firstEmpty
@Contract(pure=true) public static @org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int firstEmpty(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, int beginIndex) -
firstEmpty
@Contract(pure=true) public static @org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int firstEmpty(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, int beginIndex, @Nullable @Nullable Predicate<Integer> modifiableSlots) Gets the first item slot in the inventory that is empty or matches the given item argument. The matched item must beItemStack.isSimilar(ItemStack)and has not reached itsItemStack.getMaxStackSize()for the inventory.- Parameters:
inventory- the inventory to search from.beginIndex- the item slot to start the search from in the inventory.modifiableSlots- the slots that can be used.- Returns:
- first empty item slot, otherwise -1
- Throws:
IndexOutOfBoundsException- if the beginning index is less than 0 or greater than the inventory storage size.- Since:
- 4.0.0
-
firstPartialOrEmpty
@Contract(pure=true) public static @org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int firstPartialOrEmpty(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, @Nullable @Nullable org.bukkit.inventory.ItemStack item, int beginIndex) Gets the first empty slot or partial item in the inventory from an index.- Parameters:
inventory- the inventory to search from.beginIndex- the item slot to start the search from in the inventory.- Returns:
- first empty or partial item slot, otherwise -1
- Throws:
IndexOutOfBoundsException- if the beginning index is less than 0 or greater than the inventory storage size.- Since:
- 4.2.0
- See Also:
-
getStorageContents
@Contract(pure=true) public static org.bukkit.inventory.ItemStack[] getStorageContents(org.bukkit.inventory.Inventory inventory) Cross-version compatible version ofInventory.getStorageContents(). -
notEmpty
@Contract(pure=true) public static boolean notEmpty(@Nullable @Nullable org.bukkit.inventory.ItemStack item) - Since:
- 7.5.2
- See Also:
-
isEmpty
@Contract(pure=true) public static boolean isEmpty(@Nullable @Nullable org.bukkit.inventory.ItemStack item) Checks if this item isnullorMaterial.AIR. The latter can only happen in the following situations:PlayerInventory.getItemInMainHand()PlayerInventory.getItemInOffHand()
- Since:
- 7.5.2
- See Also:
-