Util ItemTools - Simple inventory and item management, (de)serializing inventories and items & ItemFilter

Discussion in 'Resources' started by FisheyLP, Jul 9, 2015.

?

Do you find this useful?

  1. Yes :)

    4 vote(s)
    80.0%
  2. No :(

    1 vote(s)
    20.0%
  3. I don't know :| (if you don't know just ignore this or click on yes :D)

    0 vote(s)
    0.0%
Thread Status:
Not open for further replies.
  1. I got bored and thought because the default way of handling with inventory/item saving/reading, getting only ItemStacks with a specific displayName from an inventory and more is way to complicated and confusing. So I made this class:
    ItemTools class (open)
    Code:java
    1. import java.io.File;
    2. import java.util.ArrayList;
    3. import java.util.Collections;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.Plugin;
    12.  
    13. public class ItemTools {
    14.  
    15. /*
    16.   * ItemTools class by FisheyLP
    17.   */
    18.  
    19. public static void serializeItem(ItemStack item, FileConfiguration config, File file, String path) {
    20. config.set(path, item);
    21. try {
    22. config.save(file);
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. public static void serializeItem(ItemStack item, Plugin plugin, String path) {
    28. serializeItem(item, plugin.getConfig(), new File(plugin.getDataFolder(), "config.yml"), path);
    29. }
    30.  
    31. public static ItemStack deserializeItem(FileConfiguration config, String path) {
    32. return config.getItemStack(path, null);
    33. }
    34. public static ItemStack deserializeItem(Plugin plugin, String path) {
    35. return deserializeItem(plugin.getConfig(), path);
    36. }
    37.  
    38. public static void serializeInventory(Inventory inv, FileConfiguration config, File file, String path) {
    39. for (int i = 0; i < inv.getSize(); i++)
    40. serializeItem(inv.getContents(), config, file, path+"."+i);
    41. }
    42. public static void serializeInventory(Inventory inv, Plugin plugin, String path) {
    43. serializeInventory(inv, plugin.getConfig(), new File(plugin.getDataFolder(), "config.yml"), path);
    44. }
    45.  
    46. public static Inventory deserializeInventory(FileConfiguration config, String path) {
    47. Inventory inv = Bukkit.createInventory(null, config.getConfigurationSection(path).getKeys(false).size());
    48. for(String key : config.getConfigurationSection(path).getKeys(false))
    49. inv.setItem(Integer.parseInt(key), deserializeItem(config, path+"."+key));
    50. return inv;
    51. }
    52. public static Inventory deserializeInventory(Plugin plugin, String path) {
    53. return deserializeInventory(plugin.getConfig(), path);
    54. }
    55. public static ItemStack[] getItems(Inventory inv, Integer... index) {
    56. ItemStack[] items = new ItemStack[index.length];
    57. for (int i : index) items = inv.getItem(i);
    58. return items;
    59. }
    60. public static Integer[] getFilteredItemIndexes(Inventory inv, ItemFilter filter) {
    61. List<Integer> indexes = new ArrayList<Integer>();
    62. for (int i = 0; i < inv.getSize(); i++)
    63. if (filter.matches(inv.getContents())) indexes.add(i);
    64. return indexes.toArray(new Integer[indexes.size()]);
    65. }
    66. public static ItemStack[] getFilteredItems(Inventory inv, ItemFilter filter) {
    67. return getItems(inv, getFilteredItemIndexes(inv, filter));
    68. }
    69.  
    70. }
    71.  
    72. class ItemFilter {
    73.  
    74. private Material material = null;
    75. private int amount = -1;
    76. private short durability = -1;
    77. private byte data = -1;
    78. private String displayName = null;
    79. private boolean displayNameContains = false;
    80. private List<String> lore = null;
    81. private boolean loreContains = false;
    82.  
    83. public ItemFilter material(Material material) {
    84. this.material = material;
    85. return this;
    86. }
    87. public ItemFilter amount(int amount) {
    88. this.amount = amount;
    89. return this;
    90. }
    91. public ItemFilter durability(short durability) {
    92. this.durability = durability;
    93. return this;
    94. }
    95. public ItemFilter data(byte data) {
    96. this.data = data;
    97. return this;
    98. }
    99. public ItemFilter displayName(String displayName, boolean contains) {
    100. this.displayName = displayName;
    101. this.displayNameContains = contains;
    102. return this;
    103. }
    104. public ItemFilter lore(List<String> lore, boolean contains) {
    105. this.lore = lore;
    106. this.loreContains = contains;
    107. return this;
    108. }
    109.  
    110. @SuppressWarnings("deprecation")
    111. public boolean matches(ItemStack item) {
    112. boolean matches = true;
    113.  
    114. Material material = item.getType();
    115. int amount = item.getAmount();
    116. short durability = item.getDurability();
    117. byte data = item.getData().getData();
    118. String displayName = item.hasItemMeta() ? item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : null : null;
    119. List<String> lore = item.hasItemMeta() ? item.getItemMeta().hasLore() ? item.getItemMeta().getLore() : null : null;
    120.  
    121. if (this.material != null && this.material != material) matches = false;
    122. if (this.amount != -1 && this.amount != amount) matches = false;
    123. if (this.durability != -1 && this.durability != durability) matches = false;
    124. if (this.data != -1 && this.data != data) matches = false;
    125. if (this.lore != null) {
    126. if (!loreContains && !this.lore.equals(lore)) matches = false;
    127. if (!Collections.disjoint(this.lore, lore)) matches = false;
    128. }
    129. if (this.displayName != null) {
    130. if (!displayNameContains && !this.displayName.equals(displayName)) matches = false;
    131. if (!this.displayName.contains(displayName)) matches = false;
    132. }
    133. return matches;
    134. }
    135. }

    For example, you are making a SurvivalGames plugin and need to save the chests. It can be as simple as this:
    Code:java
    1. ItemTools.serializeInventory(chest.getBlockInventory(), instanceOfPlugin, "chest.1");

    And getting it:
    Code:java
    1. Inventory inv = ItemTools.deserializeInventory(instanceOfPlugin, "chest.1");

    I made it so you can do the same on custom config files! Another example with items:
    Code:java
    1. ItemTools.serializeItem(itemStack, config, file, "area-selecting-wand");

    And getting it:
    Code:java
    1. ItemStack item = ItemTools.deserializeItem(config, "area-selecting-wand");

    The next method is to get an ItemStack array just by providing 1 index or as many as you want:
    Code:java
    1. ItemStack[] items = ItemTools.getItems(inv, 0, 4, 9);

    And now, the most useful thing of it: The ItemFilter.
    It returns an ItemStack array of ALL items which match the filter (material, amount, durability, data, displayName or lore) you provided.
    Example: We want all items that have the material wool and contain the String "FisheyLP" in their displayName:
    Code:java
    1. ItemFilter filter = new ItemFilter().material(Material.WOOL).displayName("FisheyLP", true);
    2. ItemStack[] items = ItemTools.getFilteredItems(inv, filter);

    The boolean means that it can just contain the String and not only be equal to.
    The methods:
    • void serializeItem(ItemStack item, FileConfiguration config, File file, String path) - serialize an ItemStack to a custom config.
    • void serializeItem(ItemStack item, Plugin plugin, String path) - Serialize an ItemStack to .getConfig() of the plugin.
    • ItemStack deserializeItem(FileConfiguration config, String path) - Deserialize an ItemStack from a custom config.
    • ItemStack deserializeItem(Plugin plugin, String path) - Deserialize an ItemStack from .getConfig() of the plugin.
    • void serializeInventory(Inventory inv, FileConfiguration config, File file, String path) - Serialize an inventory to a custom config.
    • void serializeInventory(Inventory inv, Plugin plugin, String path) - Serialize an inventory to .getConfig() of the plugin.
    • Inventory deserializeInventory(FileConfiguration config, String path) - Deserialize an Inventory from a custom config.
    • Inventory deserializeInventory(Plugin plugin, String path) - Deserialize an Inventory from .getConfig() of the plugin.
    • ItemStack[] getItems(Inventory inv, Integer... index) - Get all items from an inventory at 1 ore more indexes.
    • Integer[] getFilteredItemIndexes(Inventory inv, ItemFilter filter) - Get all item indexes from an inventory with a custom ItemFilter.
    • ItemStack[] getFilteredItems(Inventory inv, ItemFilter filter) - Get all items from an inventory with a custom ItemFilter.
    To-Do list:
    • Add enchantment support to ItemFilter
    • make ItemFilter an abstract class to make custom algorithms for detecting if something is equal to something (easy example would be if item amount is more than 10 instead of the normal checking if the item amount is exactly 10)
    I hope you like it :D
     
    Last edited: Jul 9, 2015
    ChipDev likes this.
  2. Offline

    sethrem

    Good job mate.
     
  3. Offline

    mrCookieSlime

    @FisheyLP
    Those Wrapper methods for de/serializing ItemStacks are quite pointless though.
     
  4. Offline

    ChipDev

    Very useful!
    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page