Solved Switch statement issues

Discussion in 'Spigot Plugin Development' started by zDoeProgram, Apr 18, 2017.

  1. Hello, I have 3 tnt blocks in the same inv, and im trying to figure out how to check which tnt the player clicked. this is my code:

    Code (Text):
    switch(e.getCurrentItem().getType()){
            case TNT:
               
                ItemStack TNT = new ItemStack(Material.TNT);
               
                if(TNT.getItemMeta().getDisplayName() == "§c1 §fHour factions tnt immunity!"){
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("1_HOUR_ACTIVATED")));
                    p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                    p.closeInventory();
                    break;
                   
                } else if (TNT.getItemMeta().getDisplayName() == "§c12 §fHour factions tnt immunity"){
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("12_HOUR_ACTIVATED")));
                    p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                    p.closeInventory();
                    break;
                   
                } else if (TNT.getItemMeta().getDisplayName() == "§c24 §fHour factions tnt immunity"){
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("24_HOUR_ACTIVATED")));
                    p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                    p.closeInventory();
                    break;
                   
                }
                break;
                default:
                    p.closeInventory();
                    break;
            }
     
  2. electronicboy

    IRC Staff

    You can't compare strings with ==, use .equals for comparing the contents of objects
     
  3. omg, I brain farted so much there... thanks u :D

    Now I get an error:
    Code (Text):
    [03:15:37 ERROR]: Could not pass event InventoryClickEvent to FluctisImmune v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1588) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:31) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:9) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
            at me.BeastCraft3.FluctisImmune.listeners.GUI.onInventoryClick(GUI.java:57) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
            ... 14 more
    the line that gives the error:
    Code (Text):
    if(TNT.getItemMeta().getDisplayName().equals("§c1 §fHour factions tnt immunity!")){
    entire event:
    Code (Text):
        @EventHandler(priority=EventPriority.MONITOR)
        public void onInventoryClick(InventoryClickEvent e){
           
            if(e.getClickedInventory() == null){
                e.setCancelled(true);
            }
           
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(INVname))
                return;
            Player p = (Player) e.getWhoClicked();
            e.setCancelled(true);
           
            if(e.getCurrentItem()==null || e.getCurrentItem().getType()==Material.AIR || !e.getCurrentItem().hasItemMeta()){
                 p.closeInventory();
                 return;
            }
           
            switch(e.getCurrentItem().getType()){
            case TNT:
               
                ItemStack TNT = new ItemStack(Material.TNT);
               
                if(TNT.getItemMeta().getDisplayName().equals("§c1 §fHour factions tnt immunity!")){
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("1_HOUR_ACTIVATED")));
                    p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                    p.closeInventory();
                    break;
                   
                } else if (TNT.getItemMeta().getDisplayName().equals("§c12 §fHour factions tnt immunity")){
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("12_HOUR_ACTIVATED")));
                    p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                    p.closeInventory();
                    break;
                   
                } else if (TNT.getItemMeta().getDisplayName().equals("§c24 §fHour factions tnt immunity")){
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("24_HOUR_ACTIVATED")));
                    p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                    p.closeInventory();
                    break;
                   
                }
                break;
                default:
                    p.closeInventory();
                    break;
            }
           
           
        }
     
    #3 zDoeProgram, Apr 18, 2017
    Last edited: Apr 18, 2017
  4. 1. You aren't checking if the itemstack has metadata (with .hasItemMeta())
    2. You are trying to compare if a NEW itemstack's name is equal to the one you want. Instead, you should be testing the one they clicked on ;)
     
    • Like Like x 1
  5. Mas

    Mas

    This, and remember to check if e.getCurrentItem() is null before calling getType()
     
    • Agree Agree x 1
  6. Also, if your switch has one case, you might as well just check for the type :p (and get rid of the AIR check, since an item can't be TNT and AIR at the same time)
     
  7. This is my code now, sorry for late respond. Was away from some time.

    Code (Text):
    @EventHandler(priority=EventPriority.MONITOR)
        public void onInventoryClick(InventoryClickEvent e){
           
            if(e.getClickedInventory() == null){
                e.setCancelled(true);
            }
           
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(INVname))
                return;
            Player p = (Player) e.getWhoClicked();
            e.setCancelled(true);
           
            if(e.getCurrentItem()==null || e.getCurrentItem().getType()==Material.AIR || !e.getCurrentItem().hasItemMeta()){
                 p.closeInventory();
                 return;
            }
            //e.getCurrentItem().getData().toItemStack().getItemMeta().getDisplayName();
            Main.getPlugin().getLogger().info("Debug 1");
            switch (e.getCurrentItem().getType().getData().getName()){
            case "§c1 §fHour factions tnt immunity!":
                Main.getPlugin().getLogger().info("Debug 1");
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("1_HOUR_ACTIVATED")));
                p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                p.closeInventory();
                break;
            case "§c12 §fHour factions tnt immunity":
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("12_HOUR_ACTIVATED")));
                p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                p.closeInventory();
                break;
            case "§c24 §fHour factions tnt immunity":
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("24_HOUR_ACTIVATED")));
                p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_TWINKLE, 3f, 3f);
                p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                p.closeInventory();
                break;
            default:
                p.closeInventory();
                break;
           
           
            }
    It only prints out: "debug message 1"
    I dont know how to check if the clicedItem's displayname is equal to my cases xP any help?
     
  8. Don't hardcode color codes. Also, it checks for the name of the item. You just need to make a case that has your item name.
     
  9. In the switch you are checking for the real name of the block and you should get the name from itemmeta
     
  10. How do I get the ItemMeta from the clicked block though? thats what im wondering xP

    EDIT:
    I'm a bit retarded. I forgot the gettItemMeta function. Solved
     
  11. getCurrentItem.getItemMeta