Class XItemStack


  • public final class XItemStack
    extends Object
    XItemStack - YAML Item Serializer
    Using ConfigurationSection Example:
         ConfigurationSection section = plugin.getConfig().getConfigurationSection("staffs.dragon-staff");
         ItemStack item = XItemStack.deserialize(section);
     
    ItemStack: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html
    Version:
    5.2.0.1.1
    Author:
    Crypto Morin
    See Also:
    XMaterial, XPotion, SkullUtils, XEnchantment, ItemStack
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static List<org.bukkit.inventory.ItemStack> addItems​(org.bukkit.inventory.Inventory inventory, boolean split, Predicate<Integer> modifiableSlots, org.bukkit.inventory.ItemStack... items)
      Optimized version of Inventory.addItem(ItemStack...) https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
      static List<org.bukkit.inventory.ItemStack> addItems​(org.bukkit.inventory.Inventory inventory, boolean split, org.bukkit.inventory.ItemStack... items)  
      static org.bukkit.inventory.ItemStack deserialize​(org.bukkit.configuration.ConfigurationSection config)
      Deserialize an ItemStack from the config.
      static int firstEmpty​(org.bukkit.inventory.Inventory inventory, int beginIndex)  
      static int firstEmpty​(org.bukkit.inventory.Inventory inventory, int beginIndex, Predicate<Integer> modifiableSlots)
      Gets the first item slot in the inventory that is empty or matches the given item argument.
      static int firstPartial​(org.bukkit.inventory.Inventory inventory, org.bukkit.inventory.ItemStack item, int beginIndex)  
      static int firstPartial​(org.bukkit.inventory.Inventory inventory, org.bukkit.inventory.ItemStack item, int beginIndex, Predicate<Integer> modifiableSlots)
      Gets the item slot in the inventory that matches the given item argument.
      static int firstPartialOrEmpty​(org.bukkit.inventory.Inventory inventory, org.bukkit.inventory.ItemStack item, int beginIndex)
      Gets the first empty slot or partial item in the inventory from an index.
      static List<org.bukkit.inventory.ItemStack> giveOrDrop​(org.bukkit.entity.Player player, boolean split, org.bukkit.inventory.ItemStack... items)
      Adds a list of items to the player's inventory and drop the items that did not fit.
      static List<org.bukkit.inventory.ItemStack> giveOrDrop​(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack... items)
      Adds a list of items to the player's inventory and drop the items that did not fit.
      static org.bukkit.Color parseColor​(String str)
      Parses RGB color codes from a string.
      static void serialize​(org.bukkit.inventory.ItemStack item, org.bukkit.configuration.ConfigurationSection config)
      Writes an ItemStack object into a config.
      static List<org.bukkit.inventory.ItemStack> stack​(Collection<org.bukkit.inventory.ItemStack> items)  
      static List<org.bukkit.inventory.ItemStack> stack​(Collection<org.bukkit.inventory.ItemStack> items, 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.
    • Method Detail

      • serialize

        public static void serialize​(@Nonnull
                                     org.bukkit.inventory.ItemStack item,
                                     @Nonnull
                                     org.bukkit.configuration.ConfigurationSection config)
        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.
        Since:
        1.0.0
      • deserialize

        @Nullable
        public static org.bukkit.inventory.ItemStack deserialize​(@Nonnull
                                                                 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
      • parseColor

        @Nonnull
        public static org.bukkit.Color parseColor​(@Nullable
                                                  String str)
        Parses RGB color codes from a string. This only works for 1.13 and above.
        Parameters:
        str - the RGB string.
        Returns:
        a color based on the RGB.
        Since:
        1.1.0
      • giveOrDrop

        @Nonnull
        public static List<org.bukkit.inventory.ItemStack> giveOrDrop​(@Nonnull
                                                                      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.
        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

        @Nonnull
        public static List<org.bukkit.inventory.ItemStack> giveOrDrop​(@Nonnull
                                                                      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.
        Parameters:
        player - the player to give the items to.
        items - the items to give.
        split - same as addItems(Inventory, boolean, ItemStack...)
        Returns:
        the items that did not fit and were dropped.
        Since:
        2.0.1
      • addItems

        public static List<org.bukkit.inventory.ItemStack> addItems​(@Nonnull
                                                                    org.bukkit.inventory.Inventory inventory,
                                                                    boolean split,
                                                                    @Nonnull
                                                                    org.bukkit.inventory.ItemStack... items)
      • addItems

        @Nonnull
        public static List<org.bukkit.inventory.ItemStack> addItems​(@Nonnull
                                                                    org.bukkit.inventory.Inventory inventory,
                                                                    boolean split,
                                                                    @Nullable
                                                                    Predicate<Integer> modifiableSlots,
                                                                    @Nonnull
                                                                    org.bukkit.inventory.ItemStack... items)
        Optimized version of Inventory.addItem(ItemStack...) https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
        Parameters:
        inventory - the inventory to add the items to.
        split - if it should check for the inventory stack size Inventory.getMaxStackSize() or item's max stack size ItemStack.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

        public static int firstPartial​(@Nonnull
                                       org.bukkit.inventory.Inventory inventory,
                                       @Nullable
                                       org.bukkit.inventory.ItemStack item,
                                       int beginIndex)
      • firstPartial

        public static int firstPartial​(@Nonnull
                                       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. The matched item must be ItemStack.isSimilar(ItemStack) and has not reached its ItemStack.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

        public static List<org.bukkit.inventory.ItemStack> stack​(@Nonnull
                                                                 Collection<org.bukkit.inventory.ItemStack> items)
      • stack

        @Nonnull
        public static List<org.bukkit.inventory.ItemStack> stack​(@Nonnull
                                                                 Collection<org.bukkit.inventory.ItemStack> items,
                                                                 @Nonnull
                                                                 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

        public static int firstEmpty​(@Nonnull
                                     org.bukkit.inventory.Inventory inventory,
                                     int beginIndex)
      • firstEmpty

        public static int firstEmpty​(@Nonnull
                                     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. The matched item must be ItemStack.isSimilar(ItemStack) and has not reached its ItemStack.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

        public static int firstPartialOrEmpty​(@Nonnull
                                              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.
        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:
        firstEmpty(Inventory, int), firstPartial(Inventory, ItemStack, int)