Getting an error with player interact

Discussion in 'Spigot Plugin Development' started by HackersDontWin, Apr 15, 2017.

  1. Hello! So currently I'm making a minigame but I've gotten stuck when making the ability for players to chose a team. I give the players 2 types of wool (Blue wool and red wool) where they can right click to join a team. The code I've written below does work but if you're holding anything that's not one of the 2 types of wool (including my fist) and then right click, it gives an error in console.

    Code (Text):
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
           
            Player p = e.getPlayer();
            ItemStack i = p.getInventory().getItemInMainHand();
            ItemMeta im = i.getItemMeta();
           
           
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (im.getDisplayName().equals(ChatColor.DARK_BLUE + "Team Blue!")) {
                    p.performCommand("join blue");
                }
                if (im.getDisplayName().equals(ChatColor.DARK_RED + "Team Red!")) {
                    p.performCommand("join red");
                }
               
            }
        }
    Could not pass event PlayerInteractEvent to Airborne v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PlayerInteractManager.a(PlayerInteractManager.java:483) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:887) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PacketPlayInUseItem.a(SourceFile:55) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PacketPlayInUseItem.a(SourceFile:11) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]
    at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
    at me.hackersdontwin.airborne.mainclass.onPlayerInteract(mainclass.java:217) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-c6871e2-0cd0397]
    ... 17 more
     
  2. Check if the item is null
    If so, return

    Code (Text):
    if(i == null) return;
     
  3. Add some null checks in there. Line 217 in your code is where your error is.
     
  4. I've tried that but Eclipse tells me that it's dead code + I still get the error in the console
     
  5. Check if the itememeta is null or ItemMeta#getDisplayName() is null as well.
     
  6. Also tried that and I tried it again and still got the error
     
  7. The item stack type shouldn't have anything to do with this. The item, itememta, or itemmeta display name must be null on 217. Also the Wool class extends MetaData, you can't check if it's an instance of an itemstack.

    Could you add line numbers to the code snippet.
     
  8. No idea how to add line numbers (I don't use forums very often :p) so I'll just post a screenshot:
    44.png
     
  9. On 208, before the item meta is defined, check if the item is null. After the item meta is defined, check if(im == null || im.getDisplayName() == null)
     
  10. I tried that, and that works, but then the wool that's supposed to work doesn't
     
  11. you should check what item he is clicking on, if not check if item equals to null, check if display name equals to null, check if get item meta equals to null, check if has item meta and equals to null.