From 3c807340a4631c7809f839de58b9d5bfb8bc1d89 Mon Sep 17 00:00:00 2001 From: MarioFinale Date: Tue, 2 Apr 2024 00:26:35 -0300 Subject: [PATCH] Fixes #22 Now we check the source of the explosion. Also, we don't try to get type of damager when damager is an instance of a player. --- README.md | 3 +++ .../mariofinale/VillagerSaver_Listener.java | 25 ++++++++++++++----- src/plugin.yml | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 73cf4df..f437280 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ It preserves trades, homes, job location and updates reputation. ## Changelog +### 1.4.1 +- Fixes [#22](https://github.com/MarioFinale/VillagerSaver/issues/22) (ClassCastException when a Villager is Killed by a Creeper Explosion). + ### 1.4.0 - Now it converts Villagers Killed by Drowned tridents or a [TNT explosion primed by a Trident](https://www.youtube.com/watch?v=qR_jv8wefAY). diff --git a/src/cl/mariofinale/VillagerSaver_Listener.java b/src/cl/mariofinale/VillagerSaver_Listener.java index 1d53d8a..25bf68d 100644 --- a/src/cl/mariofinale/VillagerSaver_Listener.java +++ b/src/cl/mariofinale/VillagerSaver_Listener.java @@ -1,9 +1,6 @@ package cl.mariofinale; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.entity.Villager; +import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -68,8 +65,21 @@ private Entity getKillerEntity(EntityDamageEvent damageCauseEvent, EntityDamageE // Set the damager as the entity attacking return ((EntityDamageByEntityEvent) damageCauseEvent).getDamager(); case ENTITY_EXPLOSION: - // Set the damager as the entity that primed the TNT - return ((TNTPrimed) ((EntityDamageByEntityEvent) damageCauseEvent).getDamager()).getSource(); + Entity damagerEntity = ((EntityDamageByEntityEvent) damageCauseEvent).getDamager(); + if (damagerEntity instanceof TNTPrimed){ + // Set the damager as the entity that primed the TNT + return ((TNTPrimed) damagerEntity).getSource(); + } + if (damagerEntity instanceof Creeper){ + // Get the damager as the entity that the Creeper was Targeting. + Entity creeperTarget = ((Creeper) damagerEntity).getTarget(); + //Check if the target entity is valid. + if(creeperTarget != null && creeperTarget.isValid()){ + // Set the damager as the entity that the Creeper was Targeting. + return creeperTarget; + } + //We will ignore other sources of explosions. + } default: return null; } @@ -94,6 +104,9 @@ private boolean isValidDamageCause(EntityDamageEvent.DamageCause cause) { * @return true if the entity is a zombie variant, false otherwise. */ private boolean isZombieVariant(Entity entity) { + //If instance of player, return false immediately. + if (entity instanceof Player) return false; + //Check if entity type matches any of the Zombie Types. return VillagerSaver_PluginVars.ZombieTypes.contains(entity.getType()); } diff --git a/src/plugin.yml b/src/plugin.yml index be330fb..210b086 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: VillagerSaver -version: 1.4.0 +version: 1.4.1 authors: [MarioFinale, Kasama, Kyle Hunady] main: cl.mariofinale.VillagerSaver api-version: '1.20'