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):
        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
    at$1.execute( ~[spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PlayerInteractManager.a( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.PlayerConnection.a( [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$ [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at java.util.concurrent.Executors$ [?:1.8.0_121]
    at [?: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( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.DedicatedServer.D( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at net.minecraft.server.v1_9_R2.MinecraftServer.C( [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at [spigot.jar:git-Spigot-c6871e2-0cd0397]
    at [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
    at me.hackersdontwin.airborne.mainclass.onPlayerInteract( ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_121]
    at java.lang.reflect.Method.invoke( ~[?:1.8.0_121]
    at$1.execute( ~[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:
  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.