Search code examples
javaminecraftbukkitinventory

Inventory ClickEvent not working


When I click the Item in the Inventory, it simply does nothing and I can drag it anywhere I want. Then I re-open the inventory and the Item is back. I want the item a click (The Notify item in this case) to toggle the notify boolean and close the Inventory. Please help. It may be a stupid solution but sometimes the simple stuff can escape me. Thanks.

This is the Inventory:

public class GUI extends JavaPlugin implements Listener
{

    private API api;

    AlphaCommand command = new AlphaCommand();

    public static Inventory inv = Bukkit.createInventory(null, 9, ChatColor.AQUA + "Alpha Config");

      public static boolean isNotifyEnabled()
      {
        return AlphaCommand.notify;
      }

      protected String getCheckName()
      {
        return this.getCheckName();
      }

    public static void openGUI(Player player, Inventory inv)
    {   
        ItemStack checks = new ItemStack(Material.DIAMOND_SWORD);
        ItemStack banPlayer = new ItemStack(Material.GOLD_AXE);
        ItemStack autoBan = new ItemStack(Material.DIAMOND_AXE);
        ItemStack notify = new ItemStack(Material.SLIME_BALL);

        ItemMeta checksMeta = checks.getItemMeta();
        ItemMeta autoBanMeta = autoBan.getItemMeta();
        ItemMeta notifyMeta = notify.getItemMeta();
        ItemMeta banPlayerMeta = banPlayer.getItemMeta();


        checksMeta.setDisplayName(ChatColor.BLUE + "Checks");
        autoBanMeta.setDisplayName(ChatColor.BLUE + "AutoBan");
        notifyMeta.setDisplayName(ChatColor.BLUE + "Notify");
        banPlayerMeta.setDisplayName(ChatColor.BLUE + "Ban Player");


        checks.setItemMeta(checksMeta);
        autoBan.setItemMeta(autoBanMeta);
        notify.setItemMeta(notifyMeta);
        banPlayer.setItemMeta(banPlayerMeta);


        inv.setItem(7, banPlayer);  
        inv.setItem(1, checks);
        inv.setItem(3, autoBan);
        inv.setItem(5, notify);

        player.openInventory(inv);

    }

    public static void open(Player player, Inventory inv)
    {
        if(player instanceof Player)
        {
            player.openInventory(inv);
        }

    }
    public static Inventory getGUI(){
        return inv;
        }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e)
    {
        Bukkit.getPluginManager().registerEvents(this, Bukkit.getPluginManager().getPlugins()[0]);
        String invName = ChatColor.stripColor(e.getClickedInventory().getName());

        Player player = (Player) e.getWhoClicked(); // The player that clicked the item
        ItemStack clicked = e.getCurrentItem(); // The item that was clicked
        @SuppressWarnings("unused")
        Inventory inventory = e.getInventory(); // The inventory that was clicked in

    if(invName.equalsIgnoreCase(inv.getName()))
    {

        if((clicked == null) || (clicked.getType() == Material.AIR) || (!clicked.hasItemMeta()))
        {
            player.closeInventory();
            return;
        }

        if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("AutoBan"))
        {
            Configuration config = this.api.getConfiguration();
            boolean autoBan = config.readBoolean("checks." + getCheckName() + ".AutoBan");

            if (config.readBoolean("checks." + getCheckName() + ".AutoBan") == true)
            {
                 autoBan = false;
                 e.setCancelled(true);
                 player.closeInventory();
                 return;
            }
            else if(autoBan == false)
            {
                autoBan = true;
                e.setCancelled(true);
                player.closeInventory();
                return;
            }
            else
            {
                e.setCancelled(true);
                player.closeInventory();
                return;
            }
        }

        if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("Notify"))
        {
            boolean notify = isNotifyEnabled();

            if(notify == true)
            {
                notify = false;
                e.setCancelled(true);
                player.closeInventory();
                return;
            }
            else if(notify == false)
            {
                notify = true;
                e.setCancelled(true);
                player.closeInventory();
                return;
            }
        }

        if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("Checks"))
        {
            player.sendMessage(ChatColor.RED + "This Feature is coming soon to Alpha.");
            e.setCancelled(true);
            player.closeInventory();
            return;
        }

        if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("Ban Player"))
        {
            player.sendMessage("This Feature is coming soon to Alpha V.4");
            e.setCancelled(true);
            player.closeInventory();
            return;

        }else
            {
            player.closeInventory();
            e.setCancelled(true);
            return;
            }

        }
    }

}

Solution

  • There seems to be multiple issues in the code that you have provided above that will cause the expected result to not be seen.

    #1

    Firstly you register the events for this class, inside the event itself, which means it'll never get registered unless the event get's called, which will never happen unless you register it... See the problem? So what you need to do is move the following line of code into your onEnable method.

    Bukkit.getPluginManager().registerEvents(this, Bukkit.getPluginManager().getPlugins()[0]);

    You need to make a few changes to this line of code. You need to replace this with a new instance of the listener object, new GUI(). It's also recommended that you register the listener with the plugin that your calling it from, but fortunately, if you put this in your onEnable, your onEnable method is inside your plugin class. This means that you can change your plugin to this. You new line of code will look like this.

    Bukkit.getPluginManager().registerEvents(new GUI(), this);

    Put that in your onEnable and that issue here should be resolved.

    #2

    The second issue is that when you're checking your item names and you're not stripping colour. This get's rid of an colours in the string. This is because your item names are ChatColor.BLUE + "AutoBan" however you're checking if it equals "AutoBan" which is different. All you need to do to fix this issue is exactly the same with what you did with the inventory name, and use ChatColor.stripColor(itemName) to get a string you can check against.

    For example:

    String itemName = ChatColor.stripColor(clicked.getItemMeta().getDisplayName()); if (itemName.equals("AutoBan")){

    Note: I also replaced equalsIgnoreCase with equals as the cases always matched, so was an unnecessary check.