Item Menu GUI?

Discussion in 'Plugin Development' started by Ad237, May 13, 2013.

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

    Ad237

    Ok so on another server the other day I noticed how they used click able inventory as custom GUIs. I want to implement this into my plugin to allow users to right click an item to open a GUI then they can pick the kit they want in a GUI. How would I go about doing this? Thanks. Sorry if I didn't explain well. I'm not sure how to explain it. Am I allowed to give you the IP of the other server so you can see it?
     
  2. Offline

    micrlink

    You need this class
    Code:
    import java.util.Arrays;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
     
    public class IconMenu implements Listener{
     
        private String name;
        private int size;
        private OptionClickEventHandler handler;
        private Plugin plugin;
        private String[] optionNames;
        private ItemStack[] optionIcons;
       
        public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin){
            this.name = name;
            this.size = size;
            this.handler = handler;
            this.plugin = plugin;
            this.optionNames = new String[size];
            this.optionIcons = new ItemStack[size];
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        public IconMenu setOption(int position, ItemStack icon, String name, String... info){
            optionNames[position] = name;
            optionIcons[position] = setItemNameAndLore(icon, name, info);
            return this;
        }
       
        public void open(Player player){
            Inventory inventory = Bukkit.createInventory(player, size, name);
            for(int i = 0; i<optionIcons.length;i++){
                if(optionIcons[i] !=null){
                    inventory.setItem(i, optionIcons[i]);
                }
            }
            player.openInventory(inventory);
        }
       
        public void destroy(){
            HandlerList.unregisterAll(this);
            handler = null;
            plugin = null;
            optionNames = null;
            optionIcons = null;
        }
       
        @EventHandler(priority = EventPriority.MONITOR)
        void onInventoryClick(InventoryClickEvent event){
            if(event.getInventory().getTitle().equals(name)){
                event.setCancelled(true);
                int slot = event.getRawSlot();
                if(slot>=0 && slot<size && optionNames[slot] !=null){
                    Plugin plugin = this.plugin;
                    OptionClickEvent e = new OptionClickEvent((Player) event.getWhoClicked(), slot, optionNames[slot]);
                    handler.onOptionClick(e);
                    if(e.willClose()){
                        final Player p = (Player) event.getWhoClicked();
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                            public void run(){
                                p.closeInventory();
                            }
                        }, 1);
                    }
                    if(e.willDestroy()){
                        destroy();
                    }
                }
            }
        }
        public interface OptionClickEventHandler {
            public void onOptionClick(OptionClickEvent event);
        }
       
        public class OptionClickEvent{
            private Player player;
            private int position;
            private String name;
            private boolean close;
            private boolean destroy;
           
            public OptionClickEvent(Player player, int position, String name){
                this.player = player;
                this.position = position;
                this.name = name;
                this.close = true;
                this.destroy = false;
            }
           
            public Player getPlayer(){
                return player;
            }
           
            public int getPositioin(){
                return position;
            }
           
            public String getName(){
                return name;
            }
           
            public boolean willClose(){
                return close;
            }
           
            public boolean willDestroy(){
                return destroy;
            }
           
            public void setWillClose(Boolean close){
                this.close = close;
            }
        }
       
        private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore){
            ItemMeta im = item.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList(lore));
            item.setItemMeta(im);
            return item;
        }
    }
    
    And then you do something like this
    Code:
    IconMenu hub = new IconMenu(ChatColor.DARK_GREEN+ "Server Hub"", 9, new IconMenu.OptionClickEventHandler() {
                    @Override
                    public void onOptionClick(IconMenu.OptionClickEvent event) {
                        Player p = event.getPlayer();
                        event.setWillClose(true);
                        if (event.getName().equalsIgnoreCase(
                                ChatColor.GOLD + "Rules")) {
                            rules(p);
                        }
                    }
                }, this).setOption(1, new ItemStack(Material.BOOK, 1), ChatColor.GOLD + "Rules", ChatColor.WHITE + "Please read them.")'
     
  3. Just code from the tutorial I gave?
     
  4. Offline

    Ivan

    micrlink Yeah I think you should credit nisovin for this :)
     
  5. Offline

    Ad237

    Are you sure the code works? I did everything he said (I think) and I got errors in the console. Reading that thread it seems others are also having problems.
     
  6. Offline

    LaxWasHere

    Show Spoiler

    [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page