Skip to content

Commit f293439

Browse files
committed
Fix pkill() on latest 1.20.4 builds
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.
1 parent cda45a1 commit f293439

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCLivingEntity.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.laytonsmith.abstraction.bukkit.entities;
22

3+
import com.laytonsmith.PureUtilities.Common.ReflectionUtils;
34
import com.laytonsmith.abstraction.MCAttributeModifier;
45
import com.laytonsmith.abstraction.MCEntity;
56
import com.laytonsmith.abstraction.MCEntityEquipment;
@@ -26,11 +27,14 @@
2627
import org.bukkit.attribute.AttributeInstance;
2728
import org.bukkit.attribute.AttributeModifier;
2829
import org.bukkit.block.Block;
30+
import org.bukkit.damage.DamageSource;
31+
import org.bukkit.damage.DamageType;
2932
import org.bukkit.entity.Entity;
3033
import org.bukkit.entity.LivingEntity;
3134
import org.bukkit.entity.Mob;
3235
import org.bukkit.entity.Player;
3336
import org.bukkit.event.entity.EntityDamageEvent;
37+
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
3438
import org.bukkit.potion.PotionEffect;
3539
import org.bukkit.potion.PotionEffectType;
3640
import org.bukkit.util.BlockIterator;
@@ -346,8 +350,16 @@ public void setTarget(MCLivingEntity target, Target t) {
346350

347351
@Override
348352
public void kill() {
349-
le.setLastDamageCause(new EntityDamageEvent(le, EntityDamageEvent.DamageCause.CUSTOM, le.getHealth()));
350-
le.setHealth(0D);
353+
try {
354+
le.damage(le.getHealth(), DamageSource.builder(DamageType.GENERIC_KILL).build());
355+
} catch (NoClassDefFoundError | NoSuchMethodError ex) {
356+
// probably before 1.20.4
357+
EntityDamageEvent event = ReflectionUtils.newInstance(EntityDamageEvent.class,
358+
new Class[]{Entity.class, DamageCause.class, double.class},
359+
new Object[]{le, EntityDamageEvent.DamageCause.CUSTOM, le.getHealth()});
360+
le.setLastDamageCause(event);
361+
le.setHealth(0);
362+
}
351363
}
352364

353365
@Override

0 commit comments

Comments
 (0)