Solved How to make GUI items locked

Discussion in 'Spigot Plugin Development' started by Stellrow, Jul 2, 2018.

  1. This should help. There are more than a few errors that you should look into.
    Code (Java):

        @EventHandler
       public void InvenClick(InventoryClickEvent event) {
           Player player = (Player) event.getWhoClicked();

           Inventory inv = event.getInventory();
           // Inventory open = event.getClickedInventory();
           ItemStack item = event.getCurrentItem();

           /*
            * I don't think this can be null
            *
            * if(open == null) { return; }
            *
            *
            */


           if (!ChatColor.stripColor(inv.getName()).equalsIgnoreCase("Test"))  return;
           

           if (item == null)
               return; // null check

           if (event.isCancelled())
               event.setCancelled(true); // Assuming you want to lock all items.

           /*
            * You have to null check first, if you don't, you might get a NPE
            */

           if (item.getType() == Material.STAINED_GLASS_PANE) { // this is unnecessary since you want to lock all items. You should consider removing this.
               event.setCancelled(true);
           }
           /*
            * You can't get the itemmeta without knowing if it has one or not. So you check
            * if it does.
            */

           if (!item.hasItemMeta())
               return; // ItemMeta check
           ItemMeta meta = item.getItemMeta(); // this is now safe to use.

           if (!meta.hasDisplayName())
               return; // displayname checlk
           String name = meta.getDisplayName(); // now safe to use.

           if (ChatColor.stripColor(name).equalsIgnoreCase("Health"))
               player.setHealth(20);

       }
     

    Most importantly: Register your listener class on your onEnable.
    Code (Java):
    Bukkit.getPluginManager().registerEvents(this, new EventsClass());
     
    #2 darklazerog, Jul 2, 2018
    Last edited: Jul 2, 2018