1.8.8 PlayerInteractEvent Error when hand is empty

Discussion in 'Spigot Plugin Development' started by WhiteShiro, Oct 5, 2020.

  1. I get an error in console (even though it doesnt affect the code) whenever the Player Interact event is fired. I believe this is so as I look for the item in hand name, hence there is an error. I ve tried my best to fix it but I cant no matter what.
    error
    Code (Text):
    [17:22:06] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Survival v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
        at com.Survival.Main.openMenu(Main.java:190) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        ... 17 more
    My code
    Code (Text):

        @SuppressWarnings("deprecation")
        @EventHandler
        public void openMenu(PlayerInteractEvent event) {
            if(!(event.getItem().equals(null))) {
            if(event.getPlayer().getItemInHand().getItemMeta().getDisplayName() == "Menu") {
            openMenu(event.getPlayer());
            }
            }
        }
     
  2. Man the errors you have in just those lines you have given are killing me.
    1. You don't compare strings using == you use .equals(), this is basic stuff.
    2. You don't check if the item is null using .equals() rather you use == (I don't know if this really matters but this is how I do it).
    3. You don't check if the item has a displayname (and itemmeta) beforehand which can lead to errors.
    4. Why are you checking for event.getItem() to equal null but then AFTER that you get the item in their hand?.. Why don't you just use event.getItem() for both areas?..

    I feel like you've gotta learn java more before doing this, honestly if 3 lines I can already give you 4 errors here then there's something wrong.
     
    #2 Swiftlicious, Oct 5, 2020
    Last edited: Oct 5, 2020
  3. To add onto SwiftLicious, by putting in event.getItem() in a try block, you can catch NullPointerException and return if it throws (which it will if the hand is empty).
     
  4. Seriously don't do this and just use a null check..
     
    • Agree Agree x 1
  5. I know it seems strange, but I don't know if I'm crazy. I eventually gave up on doing a nullcheck on event.getItem() because it was driving me up the wall. Here's just a quick recreation of the code I'd written.

    Code (Text):

          ItemStack compass = event.getItem();
            if (compass != null) {
                if (compass.getType() == Material.getMaterial("COMPASS")) {
     
    The third line would then throw a NullPointerException when the player clicked with an empty hand. Tried everything under the sun, it somehow was even getting past Objects.RequireNonNull, and still throwing on the third line, rather than the first. Eventually I just settled for the try-catch block. I know it's incredibly dumb, but it's the only way I found that worked.
     
  6. Personally I haven't ever experienced this issue you claim to have though. Don't know why you're doing Material.getMaterial("COMPASS") though and not just Material.COMPASS, the COMPASS name has never changed from 1.8 (maybe even lower too) all the way to 1.16. You should be aware since you say the null error is from the third line that means the error is coming from your Material.getMaterial("COMPASS") which can be null. getType() can't be null. And since compass isn't null since it's making it past that null check it's legit only the getMaterial("COMPASS") that can be null. Don't know why it matters if it's an empty hand or not that changes that but yeah.
     
    • Agree Agree x 1
  7. Quick note, part of my testing did include just having the compass.getType() by itself and getting the compass material (although I do think originally I did it through Material.COMPASS), it would throw at just compass.getType if the hand was empty. I can't explain it. It's one of the oddest things I've ever encountered, so I thought I'd mention the workaround that worked for me, incase it was a similar issue.