Clickevent won't work

Discussion in 'Spigot Plugin Development' started by Brandooon, Apr 14, 2017.

  1. Hey,

    I'm relatively new to bukkit/ java coding. I was trying to make an Admin Menu GUI, which connects to another GUI located in another class. However in the first GUI everything is working perfectly. But in the second one, the GUI starts, but the item clicking event doesn't work.

    Could you please help me? Whilst clicking/ opening, no errors in console are found.


    AdminMenu class:
    Code (Text):


    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.brandon.server.lobbyparty.CoinFrenzy;


    public class AdminMenu implements Listener{

        CoinFrenzy CoinFrenzy = new CoinFrenzy();
       
       
        public void openAdminMenu(Player player) {
            Inventory inv = Bukkit.createInventory(null, 36, ChatColor.BLUE + "Admin Menu");
           
            ItemStack chatmanagement = new ItemStack(Material.STAINED_GLASS_PANE);
            ItemMeta chatmanagementMeta = chatmanagement.getItemMeta();
           
            ItemStack lobbyparty = new ItemStack(Material.STAINED_GLASS_PANE);
            ItemMeta lobbypartyMeta = lobbyparty.getItemMeta();
           
            ItemStack globalmute = new ItemStack(Material.NAME_TAG);
            ItemMeta globalmuteMeta = globalmute.getItemMeta();
           
            ItemStack clearchat = new ItemStack(Material.NAME_TAG);
            ItemMeta clearchatMeta = clearchat.getItemMeta();
           
            ItemStack coinfrenzy = new ItemStack(Material.NAME_TAG);
            ItemMeta coinfrenzyMeta = coinfrenzy.getItemMeta();
       
            chatmanagementMeta.setDisplayName(ChatColor.BLUE + "Chat Management ->");
            chatmanagement.setItemMeta(chatmanagementMeta);
           
            globalmuteMeta.setDisplayName(ChatColor.RED + "Global Mute");
            globalmute.setItemMeta(globalmuteMeta);
           
            clearchatMeta.setDisplayName(ChatColor.RED + "Clear Chat");
            clearchat.setItemMeta(clearchatMeta);
           
            lobbypartyMeta.setDisplayName(ChatColor.BLUE + "Lobby Party ->");
            lobbyparty.setItemMeta(lobbypartyMeta);
           
            coinfrenzyMeta.setDisplayName(ChatColor.RED + "Token Frenzy");
            coinfrenzy.setItemMeta(coinfrenzyMeta);
           
            inv.setItem(0, chatmanagement);
            inv.setItem(2, globalmute);
            inv.setItem(3, clearchat);
            inv.setItem(9, lobbyparty);
            inv.setItem(11, coinfrenzy);
            player.openInventory(inv);
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
    if(e.getInventory().getTitle().equals(ChatColor.BLUE + "Admin Menu")){
               
                if(e.getSlot() == 2){
                    Bukkit.getServer().dispatchCommand(e.getWhoClicked(), "globalmute");
                    e.getWhoClicked().closeInventory();
                }
               
                if(e.getSlot() == 3){
                    Bukkit.getServer().dispatchCommand(e.getWhoClicked(), "chatclear");
                    e.getWhoClicked().closeInventory();
            }
                if(e.getSlot() == 11){
                    Player p = (Player)e.getWhoClicked();
                    CoinFrenzy.openCoinFrenzy(p);
                }
                else{
                return;
            }
        }
       
       
        }
        @EventHandler
        public void OnPlayerJoin(PlayerJoinEvent event){
            if(event.getPlayer().hasPermission("server.admin")){
                ItemStack menu = new ItemStack(Material.REDSTONE_TORCH_ON);
                ItemMeta menuMeta = menu.getItemMeta();
                menuMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Admin Menu");
                menuMeta.setLore(Arrays.asList(ChatColor.RED + "Admin managing tool"));
                menu.setItemMeta(menuMeta);
                event.getPlayer().getInventory().setItem(8, menu);
            }
       
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Action a = event.getAction();
            ItemStack is = event.getItem();
           
            if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
                return;
           
            if(is.getType() == Material.REDSTONE_TORCH_ON)
                openAdminMenu(event.getPlayer());
        }
    }
     
    CoinFrenzy class:
    Code (Text):
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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;

    public class CoinFrenzy implements Listener {
       
        @EventHandler
        public void openCoinFrenzy(Player player) {
               
                ItemStack ten = new ItemStack(Material.TRIPWIRE_HOOK);
                ItemMeta tenmeta = ten.getItemMeta();
                tenmeta.setDisplayName(ChatColor.RED + "Ten Tokens");
                ten.setItemMeta(tenmeta);
               
                Inventory inv = Bukkit.createInventory(null, 27, ChatColor.BLUE + "Token Frenzy");
               
                inv.setItem(10, ten);
               
                player.openInventory(inv);
            }
       
        @EventHandler
        public void onClick(InventoryClickEvent e){
            if (e.getInventory().getTitle().equals(ChatColor.BLUE + "Token Frenzy")){
               
                if (e.getSlot() == 10){
                    e.getWhoClicked().sendMessage("Test");
                }
        }else{
            return;
        }

    }
    }
     
     
  2. Is the second listener registered? Also, why do you have an @EventHandler above your openCoinFrenzy method?
     
  3. Sorry for the late answer, i was on holiday. I removed the Eventhandler and that didn't do the job. I registered the event in the main class with:

    Code (Text):
    getServer().getPluginManager().registerEvents(new CoinFrenzy(), this);
     
  4. Try comparing the inventorys itself, not the names.

    And check for the itemnames, not the slots.

    Or try debugging what you clicked and what you expected to do
     
  5. I created a command to open the menu. When i opened it succesfully with a command, the clickevent functions as normal. However if i open it from the adminmenu, it won't work.

    Thanks for the help guys