Solved ItemMenus

Discussion in 'Plugin Development' started by Pink__Slime, May 15, 2013.

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

    Pink__Slime

    So I've looked at 2 different API's to see how I could do this but I could understand one because it was set out completely and utterly horrible.
    And the other was in another language.

    I am currently using a plugin for the item menus but I would really like to create my own but I can't seem to figure out how I would do so.
    All I think I will need to know is how to create the inventory (including the size), how to set the items there and how to tell what the item clicked was (I do have some ideas though).

    If someone could kindly explain these things to me, that would be great :)
     
  2. Offline

    AstramG

    I'm not sure if this is exactly correct I'm just doing it from memory but you can tinker around with it.
    Inventory inventory = Bukkit.createInventory("InventoryName", "size in intervals of 9", true);
     
  3. Offline

    JeroenV

    Pink__Slime

    Alright, here's a small example of a MenuListener class, which you use as the center point to create any menu:

    Code:
    public class MenuListener implements Listener{
     
        public Plugin plugin = MainMenu.plugin;
     
        public void setMenuOptions(Inventory inv,int slot,ItemStack it,String title,String... lore){
        ItemStack option = setItemNameAndLore(it,title,lore);
        inv.setItem(slot,option);
        }
     
        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;
        }
     
     
      [USER=90830436]EventHandler[/USER]
        void onClickEvent(InventoryClickEvent e) {
     
        if(e.getWhoClicked() instanceof Player){
        Player player = (Player)e.getWhoClicked();
        ItemStack it = e.getCurrentItem();
     
            if(it != null && it.getItemMeta() != null && it.getItemMeta().getDisplayName() != null){
     
       
                if(e.getInventory().getType() == InventoryType.CHEST){
     
                if(e.getInventory().getTitle().contains("(I)")){
                    closeInventory(player);
                    BM_Handler BH = new BM_Handler();
                    BH.onOptionClick(player,it.getItemMeta().getDisplayName());
                    e.setCancelled(true);
                    return;
                }
     
              }
     
        }
     
        public void closeInventory(final Player player){
                Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
       
                  [USER=90830440]Override[/USER]
                  public void run() {
             
                  player.closeInventory();
               
                  }
                  },1L);
        }
     
     
    }
    
    And here's the BM_Handler class:

    Code:
    public class BM_Handler{
     
        public Plugin plugin = MainMenu.plugin;
     
     
        public void openInvMenu(final Player player){
            Inventory inv = Bukkit.createInventory(player, 27, "menu_name");
            setOptions(inv,player);
            player.openInventory(inv);
        }
     
     
        public void onOptionClick(final Player player, String name){
     
            if(name.equalsIgnoreCase("back")){
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
           
                      [USER=90830440]Override[/USER]
                      public void run() {
                 
                          BM_Main BM = new BM_Main();
                          BM.openInvMenu(player);
                 
                      }
                          },1L);
                    return;
            }
     
        return;
        }
     
     
     
        public void setOptions(Inventory inv, Player player){
        ML.setMenuOptions(inv,0, new ItemStack(Material.NETHER_STAR, 1),"Back","Go to the previous menu.");
        }
     
    }
    
    I created this a while back for a plugin I was working on, it should all be pretty clear.

    Ps. When it says it means @
     
  4. Offline

    Pink__Slime

    JeroenV
    I'm having a few problems. Currently when clicking the 3 items I have in my menu, it does nothing. Because I'm using the code you provided, I thought I'd ask you before others.
    I'm not getting any errors so here are some classes:

    Main (Only gave what was needed):
    http://pastebin.com/04RaAeTc

    MenuListener:
    http://pastebin.com/7SU9cP3h

    BM_Handler:
    http://pastebin.com/rZHtJavn

    Methods:
    http://pastebin.com/adUAaGcy

    Ok I figured out that problem, forgot I added a colour to the menu name.
    I have a new problem now though however.
    When grabbing info and rules, it just doesn't want to work. I think it might be because they are written books with name and author changed but I'm not sure why it's not working. My play around with it a bit and report back.

    EDIT: I was setting the title of the book when I should have been setting the display name. I'm all good now :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page