Switch statement - InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by JacksonHogan, May 29, 2016.

  1. I'm using the InventoryClickEvent to check if they click an item in the gui. This is the code i', using...
    Code (Java):
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            Player player = (Player)e.getWhoClicked();
         
            switch(e.getCurrentItem().getType()){
            case ENCHANTED_BOOK:
             
                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("Enchants"))){
                    player.closeInventory();
                    Bukkit.broadcastMessage("Testing");
                    InventoryManager.openMainInventory(player);
                    break;
                }
             
                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("Random Enchant"))){
                    player.closeInventory();
                    Bukkit.broadcastMessage("Testing1");
                    RandomEnchant.giveRandomEnchant(player);
                    break;
                }
             
            case BED:
                player.closeInventory();
                Bukkit.broadcastMessage("Testing2");
                break;
         
            default:
                player.closeInventory();
                Bukkit.broadcastMessage("Testing3");
                break;
            }
        }
    What ever item they click in the gui, it aways sends the message: "Testing2".
     
    #1 JacksonHogan, May 29, 2016
    Last edited: May 29, 2016
  2. Try this

    case material: {
    //code
    }
     
  3. You need a break statement after the first switch. You only have it for the ifs
     
  4. case ENCHANTED_BOOK:
    ....
    break;