PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by PCPSells, May 31, 2016.

  1. Okay, so whenever a player interacts with anything... ANYTHING.. I get this error
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1200) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
    at listeners.RightClickToken.onPlayerInteract(RightClickToken.java:20) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor256.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]

    So here's what's at line 20
    Code (Text):
        if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("item"))) {
    and here's my code (removed a few things so that you can't take my work ;P)
    Code (Text):
    package listeners;
    import me.PCPSells.MaineT;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    public class RightClickToken implements Listener {
      MaineT plugin;
      public RightClickToken(MaineT pl) {
        this.plugin = pl; }
    @EventHandler
      public void onPlayerInteract(PlayerInteractEvent e) {
        if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("item"))) {
        if (e.getItem().getItemMeta().getDisplayName().equals(plugin.getConfig().getString("itemname").replace("&", "§"))) {
          /// Actions here ;)
          return;
        }} else {
            return;
            }}}
    Any help?
     
  2. The string you are getting from your config must be null.
     
  3. So what happens when the player doesn't have an item in their hand?
     
  4. Nope, the code it self works fine. It's just that anything ELSE such as right clicking a random block/breaking any block causes the error.
     
  5. Well interacting with any other block/item causes the error... no idea why.
     
  6. EMM that is not all right, since I also this error happens to me sometimes and I have all good :p... PS: With the spigot.1.9 library does not let you use event.getHand, but the earlier as spigot.1.8, spigot.1.7, etc if and also in the most new as spigot.1.9.4 :'v
     
  7. Just check if event.hasItem() before you try to get the item.
     
  8. omg..

    Code (Java):

      public void onPlayerInteract(PlayerInteractEvent e) {
        if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("item"))) {
        if (e.getItem().getItemMeta().getDisplayName().equals(plugin.getConfig().getString("itemname").replace("&", "§"))) {
    Lets start on getType.
    1. No item? No problem! Have a free NullPointerException ;)
    2. "item" in config can be null? No problem! Another NPE on the house!

    Meta.
    1. No item? No problem! Have a free NPE ;)
    2. No meta? No problem! Have a free NPE ;)
    3. No displayname? No problem! Have a free NPE ;)
    4. equals?! What? equalsIgnoreCase*
    5. "itemname" in config can be null? No problem! Have another NPE ;)
    6. Replace '&' with §? Soo.. "Mac & Cheese" is not allowed? :(
    ChatColor#translateAlternateColors
    Code (Java):
    package listeners;
    import me.PCPSells.MaineT;
    did you mean.. package me.pcpsells.listeners;
    me.PCPSells.MaineT? Did you mean.. me.pcpsells.MaineT?

    http://www.javatpoint.com/java-naming-conventions

    Nobody would take that..
     
    • Agree Agree x 2
    • Funny Funny x 1
  9. To fix the error in the first place the following:
    Code (Text):
    if (event.hasItem()) {
    return;
    }
    and after
    Code (Text):
    if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("item"))) {
                if (e.getItem().getItemMeta().getDisplayName().equals(plugin.getConfig().getString("itemname").replace("&", "§"))) {
                    /// Actions here ;)
                    return;
                } else {
                    event.setCanlled(false)  return }
            } else {
                event.setCanlled(false)
                return;
            }