Gui system

Discussion in 'Plugin Development' started by MaTaMoR_, Apr 23, 2015.

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

    MaTaMoR_

    Hi, i made a little GUI system but i was wondering if it is efficient, so i hope you guys could check it out:

    Code:
    Show Spoiler

    Gui class:
    Show Spoiler

    Code:
    public class Gui {
    
    private static final Map<String, Gui> Guis = new HashMap<>();
    public static Map<String, Gui> getGuis() {
        return Guis;
    }
    
    private final String name;
    private String displayName;
    private int Size;
    private final Map<Integer, ItemCommand> Actions;
    private Inventory inventory;
    
    public Gui(String name, Integer Size) {
        this.name = name;
        this.displayName = name;
        this.Size = Size;
        this.Actions = new HashMap<>();
        this.inventory = null;
    
        Guis.put(name, this);
    }
    
    public String getName() {
        return this.name;
    }
    
    public String getDisplayName() {
        return this.displayName;
    }
    
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    
    public int getSize() {
        return this.Size;
    }
    
    public void setSize(int Size) {
        this.Size = Size;
    }
    
    public Map<Integer, ItemCommand> getActions() {
        return this.Actions;
    }
    
    public void setAction(int Position, ItemCommand command) {
        this.Actions.put(Position, command);
    }
    
    public void clearActions() {
        this.Actions.clear();
    }
    
    public Inventory getInventory() {
        return this.inventory;
    }
    
    public void build() {
        this.inventory = Bukkit.createInventory(null, Size, displayName);
    
        for(Entry<Integer, ItemCommand> entry : this.Actions.entrySet()) {
            int Position = entry.getKey();
            ItemCommand command = entry.getValue();
    
            this.inventory.setItem(Position, command.getItemStack());
        }
    }
    }
    

    GuiEngine class:
    Show Spoiler

    Code:
    public class GuiEngine implements Listener {
    
    private static final GuiEngine guiEngine = new GuiEngine();
    public static GuiEngine getInstance() {
        return guiEngine;
    }
    
    private Gui guiMatcher(String name) {
        for(Gui gui : Gui.getGuis().values()) {
            if(gui.getDisplayName().equals(name)) {
                return gui;
            }
        }
        return null;
    }
    
    @EventHandler
    public void noShift(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
       
        Inventory i = event.getInventory(); 
        if(i == null || i.getTitle() == null) {
            return;
        }
       
        ItemStack stack = event.getCurrentItem();
        if(stack == null || stack.getType().equals(Material.AIR)) {
            return;
        }
       
        Gui gui = guiMatcher(i.getTitle());
        if(gui == null) {
            return;
        }
       
        if(!event.isShiftClick()) {
            return;
        }
       
        event.setCancelled(true);
    }
    
    @EventHandler
    public void onClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
       
        Inventory ci = event.getClickedInventory(); 
        if(ci == null || ci.getTitle() == null) {
            return;
        }
       
        Gui gui = guiMatcher(ci.getTitle());
       
        if(gui == null) {
            return;
        }
       
        event.setCancelled(true);
       
        ItemStack stack = event.getCurrentItem();
       
        if(stack == null || stack.getType().equals(Material.AIR) || !stack.hasItemMeta()) {
            return;
        }
       
        ItemMeta im = stack.getItemMeta();
       
        if(!im.hasDisplayName()) {
            return;
        }
       
        int cs = event.getRawSlot();
       
        ItemCommand command = gui.getActions().get(cs);
       
        if(command == null) {
            return;
        }
       
        String cmd = command.getCommand();
       
        if(cmd == null) {
            return;
        }
       
        player.performCommand(cmd);
        player.closeInventory();
    }
    
    @EventHandler
    public void onDrag(InventoryDragEvent event) {
        Inventory inventory = event.getInventory();
    
        if(inventory == null || inventory.getTitle() == null) {
            return;
        }
    
        Gui gui = guiMatcher(inventory.getTitle());
    
        if(gui == null) {
            return;
        }
    
        event.setCancelled(true);
    }
    }
    

    GuiManager class:
    Show Spoiler

    Code:
    public class GuiManager {
    
    private static final GuiManager guiManager = new GuiManager();
    public static GuiManager getInstance() {
        return guiManager;
    }
    
    private ItemStack item(Material material, short data, String name) {
        ItemStack stack = new ItemStack(material, 0, data);
        ItemMeta im = stack.getItemMeta();
                 im.setDisplayName(Util.color(name));
        stack.setItemMeta(im);
        return stack;
    }
    public void setup() {
        Gui gui = new Gui("Minigames", 36);
            gui.setDisplayName(Util.color("&6&lIr a ..."));
    
        ItemCommand border = new ItemCommand(item(Material.STAINED_GLASS_PANE, (short) 2, "&7"), null);
        for(int i : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35)) {
            gui.setAction(i, border);
        }
    
        gui.setAction(11, new ItemCommand(item(Material.SKULL_ITEM, (short) 1, "&b&lFactions"), "warp factions"));
        gui.setAction(13, new ItemCommand(item(Material.BOW, (short) 0, "&a&lSkyWars"), "warp skywars"));
        gui.setAction(15, new ItemCommand(item(Material.COOKED_BEEF, (short) 0, "&e&lJuegos del Hambre"), "warp sg"));
        gui.setAction(19, new ItemCommand(item(Material.GRASS, (short) 0, "&6&lSurvival &cPróximamente..."), null));
        gui.setAction(21, new ItemCommand(item(Material.GOLDEN_APPLE, (short) 0, "&c&lFull PvP"), "warp pvp"));
        gui.setAction(23, new ItemCommand(item(Material.MOB_SPAWNER, (short) 0, "&9&lEscapa de la Bestia &cPróximamente..."), null));
        gui.setAction(25, new ItemCommand(item(Material.WHEAT, (short) 0, "&5&lTeam Skywars"), "warp teamsw"));
    
        gui.build();
    }
    }
    

    ItemCommand class:
    Show Spoiler

    Code:
    public class ItemCommand {
    
    private final ItemStack stack;
    private final String command;
    
    public ItemCommand(ItemStack stack, String command) {
        this.stack = stack;
        this.command = command;
    }
    
    public ItemStack getItemStack() {
        return this.stack;
    }
    public String getCommand() {
        return this.command;
    }
    }
    


    I setup all guis on enable
     
    Last edited: Apr 23, 2015
  2. Offline

    meguy26

    @MaTaMoR_
    Well, that is more or less how I do it, and your code is more efficient than mine, because I setup GUI's on the fly...
     
  3. Offline

    Protophite

    @MaTaMoR_ instead of setting all guis in the onEnable make a GuiHandler class and set the guis in there. In your onEnable create a new instance of GuiHandler. This makes your main class look a lot more clean.
     
  4. Offline

    mine-care

    From my point of view it is efficient.
     
  5. Offline

    MaTaMoR_

    xD thats what i do, look at the void "setup" on the class GuiManager, thats what i call on enable.
     
Thread Status:
Not open for further replies.

Share This Page