From 070d13a095848341182ac5c9aff4c48b45aa4c9f Mon Sep 17 00:00:00 2001 From: DerPavlov Date: Sun, 17 May 2020 13:26:42 +0200 Subject: [PATCH] implemented fix for not working death messages --- .../at/pavlov/cannons/CreateExplosion.java | 35 ++++++++++++++----- .../cannons/listener/PlayerListener.java | 15 ++++---- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/at/pavlov/cannons/CreateExplosion.java b/src/main/java/at/pavlov/cannons/CreateExplosion.java index f42ae6aa..be096768 100644 --- a/src/main/java/at/pavlov/cannons/CreateExplosion.java +++ b/src/main/java/at/pavlov/cannons/CreateExplosion.java @@ -63,6 +63,8 @@ public class CreateExplosion { private final Cannons plugin; private final Config config; + private FlyingProjectile currentCannonball; + private final HashSet affectedEntities = new HashSet<>(); // the entity is used in 1 tick. There should be no garbage collector problem private final HashMap damageMap = new HashMap<>(); @@ -852,6 +854,7 @@ public void detonate(FlyingProjectile cannonball, org.bukkit.entity.Projectile p Bukkit.getServer().getPluginManager().callEvent(impactEvent); canceled = impactEvent.isCancelled(); + this.currentCannonball = cannonball; // if canceled then exit if (impactEvent.isCancelled()) { // event cancelled, make some effects - even if the area is protected by a @@ -912,14 +915,14 @@ private void fireEntityDeathEvent(FlyingProjectile cannonball) { // check which entities are affected by the event for (Entity entity : this.affectedEntities) { if (entity != null) { - // entity has died - if (entity.isDead() && entity instanceof LivingEntity) { - lEntities.add((LivingEntity) entity); - if (entity instanceof Player) { - this.killedPlayers.put(entity.getUniqueId(), new DeathCause(cannonball.getProjectile(), - cannonball.getCannonUID(), cannonball.getShooterUID())); - } - } + // entity has died + if (entity.isDead() && entity instanceof LivingEntity) { + lEntities.add((LivingEntity) entity); + if (entity instanceof Player) { + this.killedPlayers.put(entity.getUniqueId(), new DeathCause(cannonball.getProjectile(), + cannonball.getCannonUID(), cannonball.getShooterUID())); + } + } } } this.affectedEntities.clear(); @@ -932,6 +935,16 @@ private void fireEntityDeathEvent(FlyingProjectile cannonball) { } } + /** + * was this player affected by cannons, it might have been the death cause + * @param player player to check + * @return true if the player was affected by cannons + */ + public boolean wasAffectedByCannons(Player player){ + return this.affectedEntities.contains(player); + } + + /** * makes a sphere with additional explosions around the impact * @@ -1268,7 +1281,7 @@ public void sendExplosionToPlayers(Projectile projectile, Location loc, SoundHol * @return true if player was killed by a cannonball */ public boolean isKilledByCannons(UUID playerUID) { - return this.killedPlayers.containsKey(playerUID); + return this.killedPlayers.containsKey(playerUID); } /** @@ -1291,4 +1304,8 @@ public DeathCause getDeathCause(UUID playerUID) { public void removeKilledPlayer(UUID playerUID) { this.killedPlayers.remove(playerUID); } + + public FlyingProjectile getCurrentCannonball() { + return currentCannonball; + } } diff --git a/src/main/java/at/pavlov/cannons/listener/PlayerListener.java b/src/main/java/at/pavlov/cannons/listener/PlayerListener.java index 009be538..e28b5b88 100644 --- a/src/main/java/at/pavlov/cannons/listener/PlayerListener.java +++ b/src/main/java/at/pavlov/cannons/listener/PlayerListener.java @@ -2,6 +2,7 @@ import at.pavlov.cannons.Enum.InteractAction; import at.pavlov.cannons.container.DeathCause; +import at.pavlov.cannons.projectile.FlyingProjectile; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -61,15 +62,17 @@ public PlayerListener(Cannons plugin) public void PlayerDeath(PlayerDeathEvent event) { UUID killedUID = event.getEntity().getUniqueId(); - if (plugin.getExplosion().isKilledByCannons(killedUID)){ - DeathCause cause = plugin.getExplosion().getDeathCause(killedUID); + if (plugin.getExplosion().wasAffectedByCannons(event.getEntity())){ + //DeathCause cause = plugin.getExplosion().getDeathCause(killedUID); plugin.getExplosion().removeKilledPlayer(killedUID); Player shooter = null; - if (cause.getShooterUID() != null) - shooter = Bukkit.getPlayer(cause.getShooterUID()); - Cannon cannon = plugin.getCannon(cause.getCannonUID()); - String message = userMessages.getDeathMessage(killedUID, cause.getShooterUID(), cannon, cause.getProjectile()); +// if (cause.getShooterUID() != null) +// shooter = Bukkit.getPlayer(cause.getShooterUID()); +// Cannon cannon = plugin.getCannon(cause.getCannonUID()); + FlyingProjectile c = plugin.getExplosion().getCurrentCannonball(); + Cannon cannon = CannonManager.getCannon(c.getCannonUID()); + String message = userMessages.getDeathMessage(killedUID, c.getShooterUID(), cannon, c.getProjectile()); if (message != null && !message.equals(" ")) event.setDeathMessage(message); }