Skip to content

Commit

Permalink
Fix death of player killed player using ender crystal
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Dec 22, 2024
1 parent 1b2baa2 commit 2b67849
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

public class EntityDamageByEntity implements Listener {

public static final Map<UUID, Entity> explosions = new HashMap<>();

@EventHandler
public void entityDamageByEntity(EntityDamageByEntityEvent e) {
// Get the damager of ender crystal
Util.loadCrystalDamager(e.getEntity(), e.getDamager());

if (e.getEntity() instanceof Player && Bukkit.getServer().getOnlinePlayers().contains((Player) e.getEntity())) {
Player p = (Player) e.getEntity();
Optional<PlayerManager> getPlayer = PlayerManager.getPlayer(p);
getPlayer.ifPresent(pm -> {
if (e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) {
if (e.getDamager() instanceof EnderCrystal && explosions.containsKey(e.getDamager().getUniqueId())) {
pm.setLastEntityDamager(explosions.get(e.getDamager().getUniqueId()));
Entity lastCrystalDamager = Util.crystalDeathData.get(e.getDamager().getUniqueId());

if (e.getDamager() instanceof EnderCrystal && lastCrystalDamager != null) {
pm.setLastEntityDamager(lastCrystalDamager);
pm.setLastExplosiveEntity(e.getDamager());
} else if (e.getDamager() instanceof TNTPrimed) { // For <= 1.20.2, because TNT explosion became BLOCK_EXPLOSION since 1.20.3
TNTPrimed tnt = (TNTPrimed) e.getDamager();
Expand Down Expand Up @@ -102,9 +102,11 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
Optional<EntityManager> getEntity = EntityManager.getEntity(e.getEntity().getUniqueId());
getEntity.ifPresent(em -> {
if (e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) {
if (e.getDamager() instanceof EnderCrystal && explosions.containsKey(e.getDamager())) {
if (explosions.get(e.getDamager().getUniqueId()) instanceof Player) {
Optional<PlayerManager> getPlayer = PlayerManager.getPlayer((Player) explosions.get(e.getDamager().getUniqueId()));
Entity lastCrystalDamager = Util.crystalDeathData.get(e.getDamager().getUniqueId());

if (e.getDamager() instanceof EnderCrystal && lastCrystalDamager != null) {
if (lastCrystalDamager instanceof Player) {
Optional<PlayerManager> getPlayer = PlayerManager.getPlayer((Player) lastCrystalDamager);
getPlayer.ifPresent(em::setLastPlayerDamager);
em.setLastExplosiveEntity(e.getDamager());
}
Expand Down Expand Up @@ -156,16 +158,5 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
}
}
}
if (e.getEntity() instanceof EnderCrystal) {
if (e.getDamager().getType().isAlive()) {
explosions.put(e.getEntity().getUniqueId(), e.getDamager());
} else if (e.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) e.getDamager();
if (projectile.getShooter() instanceof LivingEntity) {
explosions.put(e.getEntity().getUniqueId(), (LivingEntity) projectile.getShooter());
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.BoundingBox;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -41,6 +47,8 @@ public class Util {
.replacement(convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix")))
.build();

public static final Map<UUID, LivingEntity> crystalDeathData = new HashMap<>(); // <EndCrystal UUID, Causing Entity instance>

public static TextReplacementConfig replace(String matchLiteral, String replace) {
return TextReplacementConfig.builder()
.matchLiteral(matchLiteral)
Expand Down Expand Up @@ -140,6 +148,23 @@ public static void getExplosionNearbyEffected(Player p, Block b) {
Bukkit.getPluginManager().callEvent(explodeEvent);
}

public static void loadCrystalDamager(Entity entity, Entity damager) {
// Scenario 1
// Player clicked (damaged) crystal
// I didn't consider the scenario about entity clicked crystal, idk if it's needed?
if (entity instanceof EnderCrystal && damager instanceof Player) {
crystalDeathData.put(entity.getUniqueId(), (Player) damager);
}
// Scenario 2
// The crystal is triggered by a projectile when pass through (player A / LivingEntity -> projectile -> crystal -> Player A/B / Entity)
else if (entity instanceof EnderCrystal && damager instanceof Projectile) {
ProjectileSource shooter = ((Projectile) damager).getShooter();
if (shooter instanceof LivingEntity) {
crystalDeathData.put(entity.getUniqueId(), (LivingEntity) shooter);
}
}
}

public static List<World> getBroadcastWorlds(Entity e) {
List<World> broadcastWorlds = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.mrshawn.deathmessages.config.EntityDeathMessages;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.utils.EntityUtil;
import dev.mrshawn.deathmessages.utils.Util;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.AreaEffectCloud;
Expand All @@ -23,25 +24,25 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

public class EntityDamageByEntity implements Listener {

public static final Map<UUID, Entity> explosions = new HashMap<>();

@EventHandler
public void entityDamageByEntity(EntityDamageByEntityEvent e) {
// Get the damager of ender crystal
Util.loadCrystalDamager(e.getEntity(), e.getDamager());

if (e.getEntity() instanceof Player && Bukkit.getServer().getOnlinePlayers().contains((Player) e.getEntity())) {
Player p = (Player) e.getEntity();
Optional<PlayerManager> getPlayer = PlayerManager.getPlayer(p);
getPlayer.ifPresent(pm -> {
if (e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) {
if (e.getDamager() instanceof EnderCrystal && explosions.containsKey(e.getDamager().getUniqueId())) {
pm.setLastEntityDamager(explosions.get(e.getDamager().getUniqueId()));
Entity lastCrystalDamager = Util.crystalDeathData.get(e.getDamager().getUniqueId());

if (e.getDamager() instanceof EnderCrystal && lastCrystalDamager != null) {
pm.setLastEntityDamager(lastCrystalDamager);
pm.setLastExplosiveEntity(e.getDamager());
} else if (e.getDamager() instanceof TNTPrimed) { // For <= 1.20.2, because TNT explosion became BLOCK_EXPLOSION since 1.20.3
TNTPrimed tnt = (TNTPrimed) e.getDamager();
Expand Down Expand Up @@ -82,6 +83,9 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
}
});
} else if (!(e.getEntity() instanceof Player) && e.getDamager() instanceof Player) {
// Cleanup this part below
// listenedMobs should not use loop
// should use listenedMobs.contains(EntityUtil.getConfigNodeByEntity(e.getEntity()))
ConfigurationSection entityConfig = EntityDeathMessages.getInstance().getConfig().getConfigurationSection("Entities");

if (entityConfig == null) return;
Expand All @@ -101,9 +105,11 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
Optional<EntityManager> getEntity = EntityManager.getEntity(e.getEntity().getUniqueId());
getEntity.ifPresent(em -> {
if (e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) {
if (e.getDamager() instanceof EnderCrystal && explosions.containsKey(e.getDamager())) {
if (explosions.get(e.getDamager().getUniqueId()) instanceof Player) {
Optional<PlayerManager> getPlayer = PlayerManager.getPlayer((Player) explosions.get(e.getDamager().getUniqueId()));
Entity lastCrystalDamager = Util.crystalDeathData.get(e.getDamager().getUniqueId());

if (e.getDamager() instanceof EnderCrystal && lastCrystalDamager != null) {
if (lastCrystalDamager instanceof Player) {
Optional<PlayerManager> getPlayer = PlayerManager.getPlayer((Player) lastCrystalDamager);
getPlayer.ifPresent(em::setLastPlayerDamager);
em.setLastExplosiveEntity(e.getDamager());
}
Expand Down Expand Up @@ -155,16 +161,5 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
}
}
}
if (e.getEntity() instanceof EnderCrystal) {
if (e.getDamager().getType().isAlive()) {
explosions.put(e.getEntity().getUniqueId(), e.getDamager());
} else if (e.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) e.getDamager();
if (projectile.getShooter() instanceof LivingEntity) {
explosions.put(e.getEntity().getUniqueId(), (LivingEntity) projectile.getShooter());
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.BoundingBox;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -41,6 +47,8 @@ public class Util {
.replacement(convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix")))
.build();

public static final Map<UUID, LivingEntity> crystalDeathData = new HashMap<>(); // <EndCrystal UUID, Causing Entity instance>

public static TextReplacementConfig replace(String matchLiteral, String replace) {
return TextReplacementConfig.builder()
.matchLiteral(matchLiteral)
Expand Down Expand Up @@ -136,6 +144,23 @@ public static void getExplosionNearbyEffected(Player p, Block b) {
Bukkit.getPluginManager().callEvent(explodeEvent);
}

public static void loadCrystalDamager(Entity entity, Entity damager) {
// Scenario 1
// Player clicked (damaged) crystal
// I didn't consider the scenario about entity clicked crystal, idk if it's needed?
if (entity instanceof EnderCrystal && damager instanceof Player) {
crystalDeathData.put(entity.getUniqueId(), (Player) damager);
}
// Scenario 2
// The crystal is triggered by a projectile when pass through (player A / LivingEntity -> projectile -> crystal -> Player A/B / Entity)
else if (entity instanceof EnderCrystal && damager instanceof Projectile) {
ProjectileSource shooter = ((Projectile) damager).getShooter();
if (shooter instanceof LivingEntity) {
crystalDeathData.put(entity.getUniqueId(), (LivingEntity) shooter);
}
}
}

public static List<World> getBroadcastWorlds(Entity e) {
List<World> broadcastWorlds = new ArrayList<>();

Expand Down

0 comments on commit 2b67849

Please sign in to comment.