Skip to content

Commit

Permalink
Fix pkill() on latest 1.20.4 builds
Browse files Browse the repository at this point in the history
This does now cause a damage event that is cancellable. It also changes the damage cause from CUSTOM to KILL, but this is appropriate and in line with other changes, as KILL didn't previously exist.
  • Loading branch information
PseudoKnight committed Feb 12, 2024
1 parent cda45a1 commit f293439
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.laytonsmith.abstraction.bukkit.entities;

import com.laytonsmith.PureUtilities.Common.ReflectionUtils;
import com.laytonsmith.abstraction.MCAttributeModifier;
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCEntityEquipment;
Expand All @@ -26,11 +27,14 @@
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.Block;
import org.bukkit.damage.DamageSource;
import org.bukkit.damage.DamageType;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.BlockIterator;
Expand Down Expand Up @@ -346,8 +350,16 @@ public void setTarget(MCLivingEntity target, Target t) {

@Override
public void kill() {
le.setLastDamageCause(new EntityDamageEvent(le, EntityDamageEvent.DamageCause.CUSTOM, le.getHealth()));
le.setHealth(0D);
try {
le.damage(le.getHealth(), DamageSource.builder(DamageType.GENERIC_KILL).build());
} catch (NoClassDefFoundError | NoSuchMethodError ex) {
// probably before 1.20.4
EntityDamageEvent event = ReflectionUtils.newInstance(EntityDamageEvent.class,
new Class[]{Entity.class, DamageCause.class, double.class},
new Object[]{le, EntityDamageEvent.DamageCause.CUSTOM, le.getHealth()});
le.setLastDamageCause(event);
le.setHealth(0);
}
}

@Override
Expand Down

0 comments on commit f293439

Please sign in to comment.