Custom ItemStacks with ENUMs

Discussion in 'Spigot Plugin Development' started by Hunky524, May 25, 2016.

  1. I have worked a little bit with ENUMs in some of my past plugins, I mostly used them in economy plugins to make custom currency types and so on. However, what I want to do is create an ENUM called 'Buttons' and store ItemStacks to values like "Close", "Add", "Remove", etc. I know how to link ItemStacks to ENUM data, but I need to rename the ItemStack and modify it slightly based on what its purpose is.

    For example if I used the code: 'Buttons.Cancel', it would return a barrier block item that has the name cancel.
    If I used the code: 'Buttons.Add10', it would return an ItemStack with 10 green glass blocks that are named "Add Ten".


    This is my ENUM class in case you wanted to see it:
    Code (Text):
    public enum Buttons {
        Close(new ItemStack(Material.BARRIER, 1));

        private final ItemStack item;
        Buttons(ItemStack value) {
            this.item = value;
        }

        public ItemStack getButton() {
            return item;
        }
    }
     
  2. Why use an enumerator for this? Why not just store the ItemStacks in an ordinary class, and use getter setters to access them? In this case, you would be able to just return a new ItemStack in the getter with however many items you'd like I. The constructor. I don't really see any reason to use an enum for something like this.
     
  3. Make a method within the enum with arguments such as material, name, lore etc and use that method in your enum data.

    E.g.

    Code (Text):
    public enum Button {
        CANCEL(getItemStack("cancel", Material.Wool));

    }
     
  4. I know I can do it that way with a constructor and I was setting that up in the meantime in case the enum method is not possible, but I find the enum method much cleaner and easier to add/remove "buttons"(Mostly personal preference). I know I can do it with just a regular class, but I want to know if it can be done with an ENUM.
     
  5. That would require the method to create the ItemStack to be static, it's not the end of the world, but if there is another way without using static methods, it would be better.
     
  6. What about putting all those items as static attributes into a simple class? You could also access them by using Buttons.<Name>...

    (Using an enum there is just the wrong solution for your problem)
     
    • Agree Agree x 1
  7. If I do what @Thrusmyster said while making the method private and static, it seems like it's the best option for what I need. It lets me make the ItemStacks how I want them and the method can only be accessed within the ENUM.

    I'll leave the thread un-solved and unlocked for now, but if no one else has other ideas then I'll just lock and mark the thread as solved. Thanks for the help :)
     
  8. What's wrong with it being static? You do also know that storing the item stacks in enum data will mean any changes you might make to the returned item stack will change the item stack assigned to that enum value. So I would create a method in that enum that returns a copy of the item stack.
     
  9. Why do you want an enum so bad? What's wrong with a class?
     
  10. I know, that's what I wanted. My goal was to create an ENUM that linked modified ItemStacks to an ENUM value, but you can't apply changes to the ItemStack constructor upon creation like this:
    Code (Text):
    ItemStack(Material.GRASS, 1).setDisplayName("New Grass");
    However, with your suggestion, it allows me to store the modified ItemStacks to the ENUM value.
     
  11. Just preference, I'm not saying using it is better. My question was if it can be done or not.

    Doing it in an ENUM means I only have to edit one line of code whenever I want to add or remove a button rather than edit multiple lines.
     
  12. Here is the more completed ENUM class now for anyone with a similar problem:
    Code (Text):
    public enum Buttons {
        CLOSE(makeItemStack(Material.BARRIER, ChatColor.RED + "" + ChatColor.BOLD + "Close", 1)),
        REMOVE_ALL(makeItemStack(Material.STAINED_GLASS_PANE, ChatColor.RED + "Remove All But One", 64, (short) 14)),
        REMOVE_TEN(makeItemStack(Material.STAINED_GLASS_PANE, ChatColor.RED + "Remove Ten", 10, (short) 14)),
        REMOVE_ONE(makeItemStack(Material.STAINED_GLASS_PANE, ChatColor.RED + "Remove One", 1, (short) 14)),
        ADD_ALL(makeItemStack(Material.STAINED_GLASS_PANE, ChatColor.GREEN + "Add 64", 64, (short) 5)),
        ADD_TEN(makeItemStack(Material.STAINED_GLASS_PANE, ChatColor.GREEN + "Add 10", 10, (short) 5)),
        ADD_ONE(makeItemStack(Material.STAINED_GLASS_PANE, ChatColor.GREEN + "Add 1", 1, (short) 5));

        private final ItemStack item;
        Buttons(ItemStack value) {
            this.item = value;
        }

        public ItemStack getButton() {
            return item;
        }

        private static ItemStack makeItemStack(Material mat, String name, int amount, short data) {
            ItemStack rItem = new ItemStack(mat, amount, data);
           
            ItemMeta meta = rItem.getItemMeta();
            meta.setDisplayName(name);
            rItem.setItemMeta(meta);

            return rItem;
        }

        private static ItemStack makeItemStack(Material mat, String name, int amount) {
            ItemStack rItem = new ItemStack(mat, amount);

           
            ItemMeta meta = rItem.getItemMeta();
            meta.setDisplayName(name);
            rItem.setItemMeta(meta);

            return rItem;
        }
    }
     
    #12 Hunky524, May 25, 2016
    Last edited: May 26, 2016
    • Useful Useful x 2
    • Like Like x 1