Solved Error in Entity Death Event

KokushiboTheMoon

New member
Jun 24, 2022
3
1
1
Hi, when i test my code, a bug with the entity death event happens randomly. The bug only happens from kills made from non-player entities.

Here is the error:

Code:
Could not pass event EntityDeathEvent to LifeRetrieve vALPHA.1.1
java.lang.NullPointerException: null
    at java.util.Objects.requireNonNull(Objects.java:208) ~[?:?]
    at com.shayaan.liferetrive.PlayerKill.onPlayerKill(PlayerKill.java:23) ~[LifeRetrive.jar:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor2.execute(Unknown Source) ~[?:?]
    at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:75) ~[paper-api-1.19-R0.1-SNAPSHOT.jar:?]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[paper-api-1.19-R0.1-SNAPSHOT.jar:git-Paper-34]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-api-1.19-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:664) ~[paper-api-1.19-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.craftbukkit.v1_19_R1.event.CraftEventFactory.callEntityDeathEvent(CraftEventFactory.java:877) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.world.entity.LivingEntity.dropAllDeathLoot(LivingEntity.java:1748) ~[?:?]
    at net.minecraft.world.entity.LivingEntity.die(LivingEntity.java:1645) ~[?:?]
    at net.minecraft.world.entity.LivingEntity.hurt(LivingEntity.java:1475) ~[?:?]
    at net.minecraft.world.entity.ambient.Bat.hurt(Bat.java:223) ~[?:?]
    at net.minecraft.world.entity.Entity.baseTick(Entity.java:823) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.world.entity.LivingEntity.baseTick(LivingEntity.java:393) ~[?:?]
    at net.minecraft.world.entity.Mob.baseTick(Mob.java:329) ~[?:?]
    at net.minecraft.world.entity.Entity.tick(Entity.java:776) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.world.entity.LivingEntity.tick(LivingEntity.java:2909) ~[?:?]
    at net.minecraft.world.entity.Mob.tick(Mob.java:402) ~[?:?]
    at net.minecraft.world.entity.ambient.Bat.tick(Bat.java:116) ~[?:?]
    at net.minecraft.server.level.ServerLevel.tickNonPassenger(ServerLevel.java:1155) ~[?:?]
    at net.minecraft.world.level.Level.guardEntityTick(Level.java:898) ~[?:?]
    at net.minecraft.server.level.ServerLevel.lambda$tick$6(ServerLevel.java:692) ~[?:?]
    at net.minecraft.world.level.entity.EntityTickList.forEach(EntityTickList.java:42) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.server.level.ServerLevel.tick(ServerLevel.java:672) ~[?:?]
    at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:1551) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:446) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1414) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1187) ~[paper-1.19.jar:git-Paper-34]
    at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:302) ~[paper-1.19.jar:git-Paper-34]
    at java.lang.Thread.run(Thread.java:833) ~[?:?]

I'm pretty sure that the error occurs in this function:

Java:
@EventHandler
    public void onPlayerKill(EntityDeathEvent e) {

        if (Objects.requireNonNull(e.getEntity().getKiller()).getType() == EntityType.PLAYER) {

        Player p = e.getEntity().getKiller();

        int oldKills = YAMLfile.getKills(p);

        if (e.getEntity().getType() == EntityType.WITHER || e.getEntity().getType() == EntityType.ENDER_DRAGON) {
            YAMLfile.setKills(p, YAMLfile.getKills(p) + 5);
        } else {
            YAMLfile.setKills(p, YAMLfile.getKills(p) + 1);
        }

        int multiple5 =  Math.floorDiv(oldKills, 5) * 5;

        if (YAMLfile.getKills(p) - 5 >= multiple5) {
            YAMLfile.setHP(p, YAMLfile.getHP(p) + 10.0);
            Component mainTitle = Component.text("Level Up!", NamedTextColor.DARK_RED);
            Component subTitle = Component.text(multiple5).color(NamedTextColor.BLACK).append(Component.text(" -> " + YAMLfile.getHP(p)));

            Title title = Title.title(mainTitle, subTitle);

            Objects.requireNonNull(p.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(YAMLfile.getHP(p));

            p.showTitle(title);

        }

        int multiple10 = Math.floorDiv(oldKills, 10) * 10;

        if (YAMLfile.getHP(p) - 10 >= multiple10) {
            if (YAMLfile.getBloodSwordLevel(p) == 0) {
                ItemGiver giver = new ItemGiver(1);
                YAMLfile.setBloodSwordLevel(p, 1);
                p.getInventory().addItem(giver.getSword());

            } else {
                ItemGiver giver = new ItemGiver(YAMLfile.getBloodSwordLevel(p) + 1);
                YAMLfile.setBloodSwordLevel(p, YAMLfile.getBloodSwordLevel(p) + 1);
                p.getInventory().addItem(giver.getSword());
            }
        }

        Bukkit.getServer().sendMessage(
                Component.text(ChatColor.DARK_RED + p.getName() + " killed someone. They are a murderer with " + ChatColor.BLACK + YAMLfile.getKills(p) + ChatColor.DARK_RED + " kill(s). Watch out, cuz " + p.getName() + " gonna getcha!")
        );

        }

Here is the whole class where the error happens.

Java:
package com.shayaan.liferetrive;

import com.shayaan.liferetrive.bloodsword.ItemGiver;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.title.Title;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;

import java.util.Objects;

public class PlayerKill implements Listener {

    @EventHandler
    public void onPlayerKill(EntityDeathEvent e) {

        if (Objects.requireNonNull(e.getEntity().getKiller()).getType() == EntityType.PLAYER) {

        Player p = e.getEntity().getKiller();

        int oldKills = YAMLfile.getKills(p);

        if (e.getEntity().getType() == EntityType.WITHER || e.getEntity().getType() == EntityType.ENDER_DRAGON) {
            YAMLfile.setKills(p, YAMLfile.getKills(p) + 5);
        } else {
            YAMLfile.setKills(p, YAMLfile.getKills(p) + 1);
        }

        int multiple5 =  Math.floorDiv(oldKills, 5) * 5;

        if (YAMLfile.getKills(p) - 5 >= multiple5) {
            YAMLfile.setHP(p, YAMLfile.getHP(p) + 10.0);
            Component mainTitle = Component.text("Level Up!", NamedTextColor.DARK_RED);
            Component subTitle = Component.text(multiple5).color(NamedTextColor.BLACK).append(Component.text(" -> " + YAMLfile.getHP(p)));

            Title title = Title.title(mainTitle, subTitle);

            Objects.requireNonNull(p.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(YAMLfile.getHP(p));

            p.showTitle(title);

        }

        int multiple10 = Math.floorDiv(oldKills, 10) * 10;

        if (YAMLfile.getHP(p) - 10 >= multiple10) {
            if (YAMLfile.getBloodSwordLevel(p) == 0) {
                ItemGiver giver = new ItemGiver(1);
                YAMLfile.setBloodSwordLevel(p, 1);
                p.getInventory().addItem(giver.getSword());

            } else {
                ItemGiver giver = new ItemGiver(YAMLfile.getBloodSwordLevel(p) + 1);
                YAMLfile.setBloodSwordLevel(p, YAMLfile.getBloodSwordLevel(p) + 1);
                p.getInventory().addItem(giver.getSword());
            }
        }

        Bukkit.getServer().sendMessage(
                Component.text(ChatColor.DARK_RED + p.getName() + " killed someone. They are a murderer with " + ChatColor.BLACK + YAMLfile.getKills(p) + ChatColor.DARK_RED + " kill(s). Watch out, cuz " + p.getName() + " gonna getcha!")
        );

        }


    }

}


Here is my main class.

Java:
package com.shayaan.liferetrive;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public final class Main extends JavaPlugin {



    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(new PlayerJoins(), this);
        Bukkit.getPluginManager().registerEvents(new PlayerKill(), this);

    }

    @Override
    public void onDisable() {

    }
}


Anything wrong with it? I've tried adding things like preventing non player kills from procedding, but it doesn't work.
 

electronicboy

Administrator
Staff member
Dec 11, 2021
118
8
23
18
getKiller() returns null when there is no player associated with the kill, you need to add a null check, not wrap the return in a requireNotNull