Util Better way of dealing with inventory GUIs

Discussion in 'Resources' started by Tabuu_, Feb 4, 2018.

Thread Status:
Not open for further replies.
  1. Online

    Tabuu_

    Hi Bukkit forum user,

    I had to deal with a lot of inventory GUIs in the past week. My problem with current methodes is that most of them check the title or have complicated onInventoryClick events that check for specific itemstacks.

    I came up with a solution and would like to share it with you.
    [​IMG]
    First of all I created a interface called "IGUI". This interface will extend the Bukkit object "InventoryHolder". All GUIs created by the plugin will implement this interface.

    IGUI Code (open)

    Code:
    public interface IGUI extends InventoryHolder{
        public void onGUIClick(Player whoClicked, int slot, ItemStack clickedItem);
    }
    


    After that I created an event listener that listens for the Bukkit event "InventoryClickEvent" and checks if the "InventoryHolder" of this "Inventory" object is an IGUI object. If so then it will call the "onGUIClick" methode of that IGUI object. Don't forget to register this event in your main class.

    GUI EventHandler Code (open)

    Code:
    public class GUIEH implements Listener{
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if(e.getInventory().getHolder() instanceof IGUI) {
                e.setCancelled(true);
                IGUI gui = (IGUI) e.getInventory().getHolder();
                gui.onGUIClick((Player)e.getWhoClicked(), e.getRawSlot(), e.getCurrentItem());
            }  
        }
    }
    


    Finally you can create a GUI. Create a new class and let it implement the "IGUI" interface. Now you just add the two unimplemented methodes ("getInventory" & "onGUIClick"). In the "getInventory" methode of this new class you should return an inventory, this inventory will be your GUI. In the "onGUIClick" methode you can handle the player interaction with the GUI. When you create the inventory the owner argument should be the new class you've created (so in code you would just write "this").

    Example GUI Class (open)

    Code:
    public class ExampleGUI implements IGUI{
    
        @Override
        public Inventory getInventory() {
            Inventory GUI = Bukkit.createInventory(this, 9, "Example GUI");
            GUI.setItem(4, new ItemStack(Material.BARRIER));
            return GUI;
        }
    
        @Override
        public void onGUIClick(Player whoClicked, int slot, ItemStack clickedItem) {
    
            if(clickedItem == null || clickedItem.getType().equals(Material.AIR))
                return;
    
            if(slot == 1) {
                whoClicked.closeInventory();
            }
        }
    }


    Say you wanted to have a GUI that has pages and base the content of the GUI on the page number. This would be no problem.

    Example Page GUI Class (open)

    Code:
    public class ExamplePageGUI implements IGUI {
    
        private int _currentPage = 0;
    
        @Override
        public Inventory getInventory() {
    
            Inventory GUI = Bukkit.createInventory(this, 54, "Current page: " + _currentPage + 1);
    
            if(_currentPage == 0)
                GUI.setItem(4, new ItemStack(Material.FEATHER));
            else if(_currentPage == 1)
                GUI.setItem(4, new ItemStack(Material.BARRIER));
    
            return GUI;
        }
    
        @Override
        public void onGUIClick(Player whoClicked, int slot, ItemStack clickedItem) {
            if(clickedItem == null || clickedItem.getType().equals(Material.AIR))
                return;
    
            if(_currentPage == 0 && slot == 1) {
                whoClicked.openInventory(this.setPage(this.getPage() + 1).getInventory());
            }
            else if(_currentPage == 1 && slot == 1) {
                whoClicked.closeInventory();
            }
        }
    
        public ExamplePageGUI setPage(int page) {
            if(page < 0)
                _currentPage = 0;
            else if(page > 1)
                _currentPage = 1;
            else
                _currentPage = page;
            return this;
        }
    
        public int getPage() {
            return _currentPage;
        }
    
    }


    Now all you need to do when you want to open a GUI is this:
    Code:
    ExamplePageGUI gui = new ExamplePageGUI();
    player.openInventory(gui.getInventory());
    [​IMG]
    Thank you for taking the time to read this post.
    If you like my creation consider a donation

    Spigot Post | Bukkit Post
     
    MrGriefer_ and Zombie_Striker like this.
Thread Status:
Not open for further replies.

Share This Page