Skip to content

Commit

Permalink
Fixes #22
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarioFinale committed Apr 2, 2024
1 parent 2247d76 commit 3c80734
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
25 changes: 19 additions & 6 deletions src/cl/mariofinale/VillagerSaver_Listener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit 3c80734

Please sign in to comment.