Do you use an InventoryHolder when creating Inventories?

Discussion in 'Spigot Plugin Development' started by Pannkaka, Aug 15, 2017.

  1. Hello!

    I have noticed while looking at code here on Spigot that almost no one uses the InventoryHolder interface while creating inventories, they just set it to null and I don't really understand why.

    The most common way I have seen here to check if the clicked inventory actually is your created inventory is by comparing the names of the inventories and see if they match, but why just not just get the InventoryHolder and check if it matches your InventoryHolder?

    You can even store a reference/manager class to the inventory inside the InventoryHolder which you can easily get.

    Example:
    Code (Text):
    class FooHolder implements InventoryHolder {
        private GUI gui;

        FooHolder(GUI gui){
            this.gui = gui;
        }

        GUI getGUI(){
            return gui;
        }
    }

    Inventory inv = Bukkit.createInventory(new FooHolder(gui), 9, "Foo inventory");
    And then can you just compare the InventoryHolders like this:
    Code (Text):
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event){
        InventoryHolder holder = event.getInventory().getHolder();

        if(holder instanceof FooHolder){
            (FooHolder) holder).getGUI().bar();
        }
    }
    I think this is really clean and easy to keep track of an inventory and so on..
     
    • Winner Winner x 3
    • Useful Useful x 2
  2. It's way better then checking for the name indeed. I just compaired the two inventories directly but I'll might use this...
     
  3. This needs more attention, helped me on making my gui system better than ever. Thank you!
     
  4. Just updated my Plugin to use HashMap World Objects instead of ckmparing world names, ill add this to my Plugin effeciency update, thanks :)
     
  5. Thats interesting. I‘ve never seen somebody before using this. But I might use this for my inventory API. It‘s a better way to check this than the title or something like that.
     
  6. Haha,
    I always use this since I found it out.
    You can even just put custom data in the holder like the current page, what actions to run when a slot is clicked and waaayy more.
     
  7. You can be 100% sure that I'm not creating an Inventory if I'm not creating a private nested or inner class the class in which I create it (and to listen for its clicks too). I found this out when I was looking for the best way to distinguish Inventory objects and I'm pretty sure that a better way to accomplish that doesn't exist in the Bukkit API.
     
  8. #8 MasterDoctor, Oct 26, 2017
    Last edited: Aug 8, 2018
  9. I know this is old but still extremely useful. So therefore, bump!
     
    • Agree Agree x 1
  10. Sorry if this is a dumb question and I know this thread is old, but what is the GUI object you are refrencing here?
     
  11. Firestar311

    Supporter

    One he created himself
     
  12. ah thx
     
  13. not to identify a GUI for plugins

    Source

    There is no point in bumping this, this is a question, not a resource. People that are in search of help will easily find this thread with google search (assuming they have competent googling skills).

    *sigh*
     
  14. No need to be rude. Thanks for your opinions on the matter.