Added Nukkit PlayerDeathEvent listener

EntityDeathEvent is not called for player

Added listener for PlayerDeathEvent that performs the expected behavior

Affects issues:
- Fixed #1314
This commit is contained in:
Rsl1122 2020-02-07 10:25:53 +02:00
parent ae9ce49c43
commit f0c252fbde

View File

@ -26,6 +26,7 @@ import cn.nukkit.event.Listener;
import cn.nukkit.event.entity.EntityDamageByEntityEvent; import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent; import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.entity.EntityDeathEvent; import cn.nukkit.event.entity.EntityDeathEvent;
import cn.nukkit.event.player.PlayerDeathEvent;
import cn.nukkit.item.Item; import cn.nukkit.item.Item;
import com.djrapitops.plan.delivery.formatting.EntityNameFormatter; import com.djrapitops.plan.delivery.formatting.EntityNameFormatter;
import com.djrapitops.plan.delivery.formatting.ItemNameFormatter; import com.djrapitops.plan.delivery.formatting.ItemNameFormatter;
@ -41,7 +42,7 @@ import javax.inject.Inject;
import java.util.UUID; import java.util.UUID;
/** /**
* Event Listener for EntityDeathEvents. * Event Listener for detecting player and mob deaths.
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@ -59,16 +60,11 @@ public class DeathEventListener implements Listener {
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
} }
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onDeath(EntityDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
Entity dead = event.getEntity(); Player dead = event.getEntity();
SessionCache.getCachedSession(dead.getUniqueId()).ifPresent(Session::died);
if (dead instanceof Player) {
// Process Death
SessionCache.getCachedSession(((Player) dead).getUniqueId()).ifPresent(Session::died);
}
try { try {
EntityDamageEvent entityDamageEvent = dead.getLastDamageCause(); EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
@ -79,13 +75,33 @@ public class DeathEventListener implements Listener {
EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent; EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
Entity killerEntity = entityDamageByEntityEvent.getDamager(); Entity killerEntity = entityDamageByEntityEvent.getDamager();
UUID uuid = dead instanceof Player ? ((Player) dead).getUniqueId() : null; UUID uuid = dead.getUniqueId();
handleKill(time, uuid, killerEntity); handleKill(time, uuid, killerEntity);
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e); errorHandler.log(L.ERROR, this.getClass(), e);
} }
} }
@EventHandler(priority = EventPriority.MONITOR)
public void onMobDeath(EntityDeathEvent event) {
long time = System.currentTimeMillis();
Entity dead = event.getEntity();
try {
EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
if (!(entityDamageEvent instanceof EntityDamageByEntityEvent)) {
return;
}
EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
Entity killerEntity = entityDamageByEntityEvent.getDamager();
handleKill(time, /* Not a player */ null, killerEntity);
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);
}
}
private void handleKill(long time, UUID victimUUID, Entity killerEntity) { private void handleKill(long time, UUID victimUUID, Entity killerEntity) {
Runnable processor = null; Runnable processor = null;
if (killerEntity instanceof Player) { if (killerEntity instanceof Player) {