Player Interact Event Error Help

Discussion in 'Spigot Plugin Development' started by dyenxunit, May 23, 2015.

  1. Im running into an error with this code that i have setup for when a player is in the loop Test and if they click a online player they will send a message but when ever i try to click an entity like a villager npc it gives me the error below can someone please help?.

    Code (Text):
    @EventHandler(priority=EventPriority.HIGH)
          public void PlayerInteractEvent(PlayerInteractEntityEvent e) {
            Entity target = e.getRightClicked();
              Player targetPlayer = (Player)target;
              Player p = e.getPlayer();
              if(!Test.contains(targetPlayer.getName())){
                  //if(Bukkit.getPlayer(targetPlayer.getName()).isOnline()){
                  if(Bukkit.getServer().getOnlinePlayers().contains(targetPlayer.getName())){
              }
              }
              else if((target instanceof Player)) {
    player.sendMessage("Test");
    }

    Code (Text):
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:1305) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInUseEntity.a(SourceFile:52) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInUseEntity.a(SourceFile:11) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_65]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_65]
    Caused by: java.lang.ClassCastException: net.citizensnpcs.npc.entity.EndermiteController$EndermiteNPC cannot be cast to org.bukkit.entity.Player
        at me.dyenxunit.magnum.Main.Main.PlayerInteractEvent(Main.java:54) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_65]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        ... 15 more
     
  2. You have to check that e.getRightClicked() instanceof Player before you can cast it as a Player.
     
  3. what about the action?
    like:
    Code (Text):
    if(e.getAction() == Action.RIGHT_CLICK_BLOCK)
     
  4. I'm trying to make it send a message when u click the online player
     
  5. then its just
    Code (Text):
    Entity target = (Player) e.getRightClicked();
    (i think)
     
  6. Nope still doesnt work it still gives me this error

    Code (Text):
    [12:10:17 ERROR]: Could not pass event PlayerInteractEntityEvent to Test v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:1305) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInUseEntity.a(SourceFile:52) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInUseEntity.a(SourceFile:11) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_65]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_65]
    Caused by: java.lang.ClassCastException: net.citizensnpcs.npc.entity.EndermiteController$EndermiteNPC cannot be cast to org.bukkit.entity.Player
        at me.dyenxunit.magnum.Main.Main.PlayerInteractEvent(Main.java:53) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_65]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        ... 15 more
     
     
  7. @dyenxunit your custom entity is not a Player.