Resource Get all items form any version

Discussion in 'Spigot Plugin Development' started by Nemo_64, Aug 15, 2019.

  1. Before anything, if you think this can be improved or you find an error, say it.

    Probably this has been made before, but I couln't find it, so I made it my own.

    Here I'll show you how to make an ArrayList of Material with all the materials in Materials.values() that can be items of any version.

    Steps:
    1-
    Run this code it and wait until its done
    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class items {

        public void generateList(JavaPlugin plugin) {
            Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
                @Override
                public void run() {
                    Bukkit.getConsoleSender().sendMessage("public ArrayList<Material> getItems() {");
                    Bukkit.getConsoleSender().sendMessage("    ArrayList<Material> items = new ArrayList<>();");
                    Inventory inv = Bukkit.createInventory(null, 27);
                    Material[] materials = Material.values();
                    for (int j = 0; j < materials.length; j++) {
                        inv.setItem(0, new ItemStack(materials[j]));
                        ItemStack item = inv.getItem(0);
                        if (item == null || item.getType() == Material.AIR)
                            continue;
                        Bukkit.broadcastMessage("    list.add(Material." + String.valueOf(materials[j]) + ");");
                        try {
                            Thread.sleep(60);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Bukkit.getConsoleSender().sendMessage("    return list;");
                    Bukkit.getConsoleSender().sendMessage("}");
                }
            });
        }
    }
    And you will see in the console something like this (this is just a part of all):
    Code (Text):
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WATER_BUCKET);

    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WET_SPONGE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHEAT);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHEAT_SEEDS);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_BANNER);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_BED);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_CARPET);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_CONCRETE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_CONCRETE_POWDER);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_DYE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_GLAZED_TERRACOTTA);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_SHULKER_BOX);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_STAINED_GLASS);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_STAINED_GLASS_PANE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_TERRACOTTA);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_TULIP);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WHITE_WOOL);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WITCH_SPAWN_EGG);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WITHER_ROSE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WITHER_SKELETON_SKULL);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WITHER_SKELETON_SPAWN_EGG);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WOLF_SPAWN_EGG);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WOODEN_AXE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WOODEN_HOE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WOODEN_PICKAXE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WOODEN_SHOVEL);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WOODEN_SWORD);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WRITABLE_BOOK);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.WRITTEN_BOOK);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_BANNER);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_BED);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_CARPET);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_CONCRETE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_CONCRETE_POWDER);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_DYE);
    [00:55:31] [Craft Scheduler Thread - 9/INFO]: list.add(Material.YELLOW_GLAZED_TERRACOTTA);


    2-
    Now you have to copy all of that and put it in a text editor like notepad++

    3-
    Use the replace all option to get rid of all the unwanted things like "[00:55:31] [Craft Scheduler Thread - 9/INFO]: "

    4-
    Copy the remaining code and use it.

    Is that easy.


    The tutorial ends here, but if you want to know how it works, keep reading.
    Firts we create an inventory and start putting on it every item in the Material.values(), then we check if that item is null or air, if it is we don't do anything, if not we print the line of code to add that material to the ArrayList. We need a little bit of delay between materials to not skip some of them.



    Take a look at my plugin: https://www.spigotmc.org/resources/easyshops2-0.69922/
     
    • Optimistic Optimistic x 1
  2. SteelPhoenix

    Junior Mod

    What is the point of this? And can't you just do the following:
    List<Material> list = Arrays.asList(Material.values());
     
  3. This will also have thinks like ACACIA_WALL_SIGN or PLAYER_WALL_HEAD. I just wanted to get rid of those
     
  4. SteelPhoenix

    Junior Mod

    So filter those out...
    List<Material> list = Arrays.stream(Material.values()).filter(mat -> {/*Some filter*/}).collect(Collectors.toList());
     
    • Agree Agree x 1
  5. I don't really know how that works
     
  6. You should then learn how that works so as not to waste time doing overcomplicated and time consuming things like this resource.
     
  7. Could you give me a link to a page to learn how it works