Solved Check if player killed him self.

Discussion in 'Spigot Plugin Development' started by PerziosCraft, Jun 24, 2018.

  1. Hey,
    I am creating an bow game where you have to shoot explosives but you can kill your self with the arrows and I don't want to give money to the players that kill them self :p.
    My code:

    Code (Text):
    @EventHandler(priority = EventPriority.MONITOR)
        public void onKill(PlayerDeathEvent event) {
            Entity killer = event.getEntity().getKiller();
            Entity killed = event.getEntity();
                    if (killed instanceof Player) {
                if (killer.getName().equals(killed.getName())) {
                    //Dont give coins but remove coins
                } else {
                    //Give coins
                )
                  }
        }
    But when the kill their self an error shows up

    Code (Text):
    [20:22:38] [Server thread/ERROR]: Could not pass event PlayerDeathEvent to PerziosCraft v1.0.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:412) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityPlayer.die(EntityPlayer.java:423) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity(EntityLiving.java:951) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityHuman.damageEntity(EntityHuman.java:759) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityPlayer.damageEntity(EntityPlayer.java:577) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.Explosion.a(Explosion.java:133) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.createExplosion(World.java:1885) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.WorldServer.createExplosion(WorldServer.java:1224) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityTNTPrimed.explode(EntityTNTPrimed.java:94) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityTNTPrimed.B_(EntityTNTPrimed.java:74) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.entityJoinedWorld(World.java:1640) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.h(World.java:1610) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.tickEntities(World.java:1436) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.WorldServer.tickEntities(WorldServer.java:665) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:814) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [server.jar:git-Spigot-596221b-9a1fc1e]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
    at me.luseres.perzioscraft.Main.onKill(Main.java:380) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor309.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_151]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_151]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[server.jar:git-Spigot-596221b-9a1fc1e]
    ... 22 more
    Its giving me an nullpointer at line 380:
    Code (Text):
    if (killer.getName().equals(killed.getName())) {
    But how can a player get an nullpointer if it checks for an name because it check if the killer is an player
     
  2. So you are sure that killer is not null?

    It would kinda make sense that there is no killer, if a players kills him self.
     
  3. Code (Text):
    if (killed instanceof Player) {
    @MartenM it checks if the killer is an player
    And players have usernames

    EDIT: Oh wow I see now it checks for killed not killer
     
  4. PlayerDeathEvent gets fired only whenever a player dies. That's unnecessary.


    OP, try and debug the killer's name and killed's name. Also add some null checks on the killer so as to avoid NPE

    Also:

    Code (Java):
    @EventHandler(priority = EventPriority.MONITOR)
        public void onKill(PlayerDeathEvent event) {
    //        Entity killer = event.getEntity().getKiller();
    //        Entity killed = event.getEntity();
            Player killer = event.getEntity().getKiller();
            Player killed = event.getEntity();
    /*
    Add null checks for the killer and whether the killer is an instance of player.
    */

    //                if (killed instanceof Player) { The victim will always be a player with this event.
                if (killer.getName().equals(killed.getName())) {
                    //Dont give coins but remove coins
                } else {
                    //Give coins
                )
    //              }
        }
     
  5. Thank you that was the problem. Also I removed checking if killed is an player.
    I had to check if the killer was a player