Solved PlayerInteractEvent gives error when left clicking

Discussion in 'Spigot Plugin Development' started by MiniGutt, Mar 1, 2020.

  1. Hey! When I left click ingame with nothing in my hand, I get an error because of the PlayerInteractEvent. I am checking if the player has a special item in the hand, and if they have I want the event to cancel.

    This is the code:
    Code (Text):
        public void playerEvent(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Tomt Egg")) {
                event.setCancelled(true);
            }
        }


    Error message in console:
    Code (Text):
    [04:10:45] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Spillere-Core v2.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:541) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:528) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:442) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:409) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:405) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1696) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PacketPlayInArmAnimation.a(SourceFile:31) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PacketPlayInArmAnimation.a(SourceFile:9) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at java.lang.Thread.run(Thread.java:830) [?:?]
    Caused by: java.lang.NullPointerException
            at no.spillere.core.listeners.ItemListener.playerEvent(ItemListener.java:218) ~[?:?]
            at jdk.internal.reflect.GeneratedMethodAccessor72.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            ... 20 more
    If I have an item in my hand and left click nothing shows in the console, it just shows when left click with nothing in my hand. Does anyone of you see the reason why this happens?

    EDIT: I now see that if I have a empty hand and right clicks on a block I also get the error.
     
    #1 MiniGutt, Mar 1, 2020
    Last edited: Mar 1, 2020
  2. Check the player actually has an item in their hand first, also might want to check the item has a Display name before getting it.
     
  3. I tried that, but still shows an error.
    Code (Text):
        @EventHandler
        public void playerEvent(PlayerInteractEvent event) {
            Player player = event.getPlayer();
           
            if(player.getInventory().getItemInMainHand() != null) {
                //The line under this comment is where the console shows the error is happening
                if(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName() != null) {
                if(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Tomt Egg")) {
                    event.setCancelled(true);
                }
                }
            }
        }
     
  4. I think you need to check that the item isn't air as well.
     
  5. For further troubleshooting, I suggest adding a check to see if the item in main hand is air, and to see if the item has an item meta before checking for whether it has a display name.
     
  6. Not all items have meta data, you can't blindly check getDisplayName() even if you are checking if it is null, you have to use hasDisplayName() to see if it exists.
     
  7. I still cant seem to figure it out.

    Code (Text):
    @EventHandler
        public void playerEvent(PlayerInteractEvent event) {
            Player player = event.getPlayer();

            if(player.getInventory().getItemInMainHand() != null) {
                ItemStack item = new ItemStack(Material.AIR);
                if(player.getInventory().getItemInMainHand() != item) {
                    if(player.getInventory().getItemInMainHand().getItemMeta().hasDisplayName()) {
                        if(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Tomt Egg")) {
                            event.setCancelled(true);
                            player.sendMessage("hmm");
                        }
                    }
                }
            }
        }
    Its the hasDisplayName line that shows an error now.
     

  8. Surround the hasDisplayName if statement with a hasItemMeta if statement
     
    • Like Like x 1
  9. I don't know the exact terminology, but I'll try to explain anyway. The not equal to operator (!=) only compares values when used on primitive types. When it's used to compare objects, it only returns true if both objects have the same reference (aka they're the same object). If you want to compare the values of two objects you need to use the equals() method, although this doesn't work on all objects.

    Either way, in this case it would be better to check if the type of getItemInHand() is equal to air using this method: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html#getType--
     
    • Like Like x 1
  10. This solved my problem and it is not all working as intended, thanks! :)
     
  11. If I remember right it is called an "identity comparision"