diff --git a/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java b/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java index 8908217..be5c290 100644 --- a/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java +++ b/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java @@ -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 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 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(); @@ -102,9 +102,11 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) { Optional 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 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 getPlayer = PlayerManager.getPlayer((Player) lastCrystalDamager); getPlayer.ifPresent(em::setLastPlayerDamager); em.setLastExplosiveEntity(e.getDamager()); } @@ -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()); - } - } - - } } } diff --git a/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/Util.java b/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/Util.java index 6fef016..59559bc 100644 --- a/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/Util.java +++ b/DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/Util.java @@ -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; @@ -41,6 +47,8 @@ public class Util { .replacement(convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"))) .build(); + public static final Map crystalDeathData = new HashMap<>(); // + public static TextReplacementConfig replace(String matchLiteral, String replace) { return TextReplacementConfig.builder() .matchLiteral(matchLiteral) @@ -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 getBroadcastWorlds(Entity e) { List broadcastWorlds = new ArrayList<>(); diff --git a/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java b/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java index c425624..6669459 100644 --- a/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java +++ b/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/listeners/EntityDamageByEntity.java @@ -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; @@ -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 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 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(); @@ -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; @@ -101,9 +105,11 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) { Optional 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 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 getPlayer = PlayerManager.getPlayer((Player) lastCrystalDamager); getPlayer.ifPresent(em::setLastPlayerDamager); em.setLastExplosiveEntity(e.getDamager()); } @@ -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()); - } - } - - } } } diff --git a/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/Util.java b/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/Util.java index 91a8e06..80445d7 100644 --- a/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/Util.java +++ b/DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/Util.java @@ -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; @@ -41,6 +47,8 @@ public class Util { .replacement(convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"))) .build(); + public static final Map crystalDeathData = new HashMap<>(); // + public static TextReplacementConfig replace(String matchLiteral, String replace) { return TextReplacementConfig.builder() .matchLiteral(matchLiteral) @@ -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 getBroadcastWorlds(Entity e) { List broadcastWorlds = new ArrayList<>();