Custom Inventory Types

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

  1. Hello everyone, I am wondering how someone could go about setting up inventories with custom types. For example, when you check the type of an inventory you do:
    Code (Text):
    if (inventory.getType() == InventoryType.CHEST) {
         //do stuff
    }

    I am wondering if I can setup custom inventories that seem the exact same normal inventories except their type is different. For example if I wanted to check a custom inventory I could do:
    Code (Text):
    if (inventory.getType() == InventoryType.SHOP) {
         //do stuff
    }
    I am fairly sure this is possible because I have read forums about people doing it I'm just not sure how they did it exactly. I'm sure they had to make a custom inventory class that extends one of Bukkit's/Spigot's inventory classes and then save/load the inventories on server restarts. I'm just not sure where to start exactly. If you have any ideas, I would be glad to hear them :)
     
  2. I think this is what you are looking for.
     
  3. I'm not at a computer where I can test, I saw the createInventory where you can enter the type, I am wondering how I can make my own InventoryType.

    For example if I did:
    Code (Text):
    Bukkit.createInventory(null, InventoryType.SELECTOR);
    It would create an inventory for me with pre-defined modifiers like the size or the name. I don't really need it to have pre-defined modifiers, just the unique type.
     
  4. I'm afraid that's not fully possible. Two possible ways of achieving this would be:

    • Overriding the InvetoryType enum, and creating your own
    • Creating your open method with custom modifiers to open an inventory
    The method overall is generally used to grab the type of container opened, not a specific kind/type of custom inventory.
     
  5. You can't override an enum...
    I agree with using a method though.

    @Hunky524 I would suggest just creating a short method that returns an inventory like the one you want. For example you could do something like this:
    Code (Text):
    public Inventory createSmallInventory(String title){
        Inventory inv = Bukkit.createInventory(null, 9, title);
        return inv;
    }
     

  6. I know how to create inventories. I am trying to find a way that I can easily identify custom inventories without having to check the name or title of the inventory. Making a custom inventory type seemed like the easiest option if it was possible (I didn't think it was because you can't extend/modify ENUMs), but I thought maybe there was some kind of hack you could do that would allow it. Maybe making custom inventory types is not the way to go.

    I was reading online about someone you made his own inventory types, but he uses his own InventoryClick Event with a custom inventory object that contains the custom type along with the inventory and many other variables. He posted the source code for people to see, if you want to take a look here is the link.



    If it's not possible then that's fine. Maybe there are better options to identify custom inventories without using their names.
     
  7. well do this thing

    Code (Java):
    public Inventory createGUI(GUIType type)  {
       if(type.SHOP) {
               // create inventory
       } else if(type.COOKIE) {
               // do another
          }
     return null;
    }

    enum GUIType type {

    SHOP,
    COOKIE
    }
     
    #7 MadMaxCookie, May 26, 2016
    Last edited: May 27, 2016
  8. Well you could always decompile the Spigot jar and add more inventory types. (Like what CivCraft did) But any plugin that used your inventory types would only work with that server jar.
     
    • Funny Funny x 1
  9. I was actually thinking about that, but decided against it because it feels like cheating and when spigot updates anything that I changed will be gone.
     
  10. That's not exactly what I was thinking. I was thinking of a way to easily identify a custom inventory once it has already been made.
     
  11. Choco

    Moderator

    Have a custom inventory wrapper class. This is the best way you'll accomplish this.

    Code (Java):
    public class CustomInventory{

        public enum CustomInventoryType{
            SHOP, SHOP2, SHOP3;
        }

        private final Inventory inventory;
        private final CustomInventoryType type;
        public CustomInventory(Inventory inventory, CustomInventoryType type){
            this.inventory = inventory;
            this.type = type;
        }

        public CustomInventory(int size, String name, CustomInventoryType type){
            this.inventory = Bukkit.createInventory(null, size, name);
            this.type = type;
        }

        // Getters for "inventory" and "type" fields
    }
     
    • Like Like x 1
  12. Yea, that's the method I decide to go with. Thanks for the help though :)
     
    • Like Like x 1