Inventory Menu Error

Discussion in 'Plugin Development' started by leonidas3535, Jun 18, 2015.

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

    leonidas3535

    mistake where I would be glad if you help your friends ?????

    *------------------------------------------------------------------------------------*

    MenuInv
    Code:
    package com.leonidas.bukkit.menuinv;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MenuInv extends JavaPlugin implements Listener{
      
        private Menu menu;
      
        public void onEnabled() {
            menu = new Menu(this);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
      
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
          
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            menu.show(e.getPlayer());
        }
      
    }
    
    Menu.java
    Code:
    package com.leonidas.bukkit.menuinv;
    
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    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.material.Wool;
    import org.bukkit.plugin.Plugin;
    
    public class Menu implements Listener{
      
        private Inventory inv;
        private ItemStack c, s, a;
      
        public Menu(Plugin p) {
          
            inv  = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
          
            c = createItem(DyeColor.GREEN, ChatColor.GREEN + "Creative");
            s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "Survival");
            a = createItem(DyeColor.RED, ChatColor.RED + "Adventure");
                  
            inv.setItem(2, c);
            inv.setItem(4, s);
            inv.setItem(6, a);
          
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
      
        private ItemStack createItem(DyeColor dc, String name) {
          
            ItemStack i =  new Wool(dc).toItemStack(1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Set your gamemode", "to " + name.toLowerCase() + " mode"));
            i.setItemMeta(im);
            return i;
        }
      
        public void show(Player p) {
          
            p.openInventory(inv);
          
        }
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
          
            if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
            if(e.getCurrentItem() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Createive")) {
                e.setCancelled(true);
                e.getWhoClicked().setGameMode(GameMode.CREATIVE);
                e.getWhoClicked().closeInventory();
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
                e.setCancelled(true);
                e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
                e.getWhoClicked().closeInventory();
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
                e.setCancelled(true);
                e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
                e.getWhoClicked().closeInventory();
            }
          
        }
      
    }
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    leonidas3535

    When I right-click menu does not open any block
     
  4. Offline

    xTrollxDudex

    You didn't register the Menu class
     
  5. Offline

    leonidas3535

    @xTrollxDudex
    How do I fix or how to do it
     
  6. Offline

    CXdur

    Code:
        private Menu menu = new Menu(this);
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(menu, this);
        }
      
         // Move this to the menu listener class
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
     
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            menu.show(e.getPlayer());
        }
    I also noticed that you were using onEnabled() rather than onEnable(). I haven't been doing this for a few months now so I'm not completely sure, but I believe that it's wrong. I don't see any benefit in changing it from onEnable to onEnabled, so I doubt that they'd change that. Make sure it's onEnable and onDisable!
     
  7. Offline

    leonidas3535

  8. Offline

    BrickBoy55

    @leonidas3535 This is sort of off topic, but theres better ways to make plugins then having the exact same code as this:
     
  9. Offline

    xTrollxDudex

    And you forgot to register the main class as well
     
  10. Offline

    87pen

    http://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.350132
    Creating inventories like this is much easier:
    Code:
    package me.kowagatte.menututorial;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.MaterialData;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MenuTutorial extends JavaPlugin implements Listener{
    
        @EventHandler
        public void onClockRightClick(PlayerInteractEvent e){
            Player p = (Player)e.getPlayer();
            //Getting action
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
               
                //Getting Item(Must include null check)
                if(e.getItem() != null && e.getItem().getItemMeta() != null && e.getItem().equals(new ItemStack(Material.WATCH))){
                    //Creating menu/Inventory
                    Inventory inv = Bukkit.createInventory(null, 9, "Main menu");
                   
                    //Creating an Item for the menu
                    ItemStack next = createItem("Next Page", "Scroll to the next page!", Material.GLASS, 1);
                   
                    //Setting position of item in inventory slots start at 0
                    inv.setItem(8, next);
               
                    //Opening inventory
                    p.openInventory(inv);
                }
            }
        }
       
          //creatItem method I used to create the menu item.
          public ItemStack createItem(String name, String lore, Material m, int amount){
                ItemStack i = new MaterialData(m).toItemStack(amount);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList(new String[] { lore }));
                i.setItemMeta(im);
                return i;
              }
       
          //getting if player clicked in an inventory
          @EventHandler
          public void onInventoryClick(InventoryClickEvent e){
              Player p = (Player)e.getWhoClicked();
              //Checking if the inventory is the menu
              if(e.getInventory().getName().equalsIgnoreCase("Main menu")){
                  //Checking if they clicked null in the inventory
                  if(e.getCurrentItem() != null){
                      //Checking if they clicked the item I created in the menu.
                      if(e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().getDisplayName().equals("Next Page")){
                              //Creating a second menu.
                            Inventory inv = Bukkit.createInventory(null, 9, "Second menu");
                             
                            //Cancelling event so they can't take out the item
                            e.setCancelled(true);
                            //closing the first menu.
                            p.closeInventory();
                            //Opening the second menu.
                            p.openInventory(inv);
                      }
                      return;
                  }
                  return;
              }
          }
          
     
  11. Offline

    CXdur

    Please be more specific. The only thing I see missing is onDisable() which as far as I know is no longer crucial.
     
  12. Offline

    xTrollxDudex

    @CXdur
    You need to register 2 listeners, the Menu class and the main class.
     
Thread Status:
Not open for further replies.

Share This Page