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$1.execute( ~[server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.plugin.RegisteredListener.callEvent( ~[server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.plugin.SimplePluginManager.fireEvent( [server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.plugin.SimplePluginManager.callEvent( [server.jar:git-Spigot-596221b-9a1fc1e]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerDeathEvent( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityPlayer.die( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityHuman.damageEntity( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityPlayer.damageEntity( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.Explosion.a( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.createExplosion( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.WorldServer.createExplosion( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityTNTPrimed.explode( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.EntityTNTPrimed.B_( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.entityJoinedWorld( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.h( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.World.tickEntities( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.WorldServer.tickEntities( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D( [server.jar:git-Spigot-596221b-9a1fc1e]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C( [server.jar:git-Spigot-596221b-9a1fc1e]
    at [server.jar:git-Spigot-596221b-9a1fc1e]
    at [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
    at me.luseres.perzioscraft.Main.onKill( ~[?:?]
    at sun.reflect.GeneratedMethodAccessor309.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_151]
    at java.lang.reflect.Method.invoke( ~[?:1.8.0_151]
    at$1.execute( ~[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


    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