SPIGOT-5553, #1336: Add EntityKnockbackEvent

By: Jishuna <joshl5324@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-01-27 14:53:41 +11:00
parent 67ab2cd516
commit 7b15562116
5 changed files with 179 additions and 81 deletions

View File

@@ -1,6 +1,6 @@
--- a/net/minecraft/world/entity/EntityLiving.java
+++ b/net/minecraft/world/entity/EntityLiving.java
@@ -119,6 +119,30 @@
@@ -119,6 +119,31 @@
import net.minecraft.world.scores.ScoreboardTeam;
import org.slf4j.Logger;
@@ -21,6 +21,7 @@
+import org.bukkit.event.entity.ArrowBodyCountChangeEvent;
+import org.bukkit.event.entity.EntityDamageEvent;
+import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
+import org.bukkit.event.entity.EntityKnockbackEvent;
+import org.bukkit.event.entity.EntityPotionEffectEvent;
+import org.bukkit.event.entity.EntityRegainHealthEvent;
+import org.bukkit.event.entity.EntityResurrectEvent;
@@ -31,7 +32,7 @@
public abstract class EntityLiving extends Entity implements Attackable {
private static final Logger LOGGER = LogUtils.getLogger();
@@ -225,7 +249,21 @@
@@ -225,7 +250,21 @@
private float swimAmount;
private float swimAmountO;
protected BehaviorController<?> brain;
@@ -54,7 +55,7 @@
protected EntityLiving(EntityTypes<? extends EntityLiving> entitytypes, World world) {
super(entitytypes, world);
@@ -238,7 +276,9 @@
@@ -238,7 +277,9 @@
this.useItem = ItemStack.EMPTY;
this.lastClimbablePos = Optional.empty();
this.attributes = new AttributeMapBase(AttributeDefaults.getSupplier(entitytypes));
@@ -65,7 +66,7 @@
this.blocksBuilding = true;
this.rotA = (float) ((Math.random() + 1.0D) * 0.009999999776482582D);
this.reapplyPosition();
@@ -317,7 +357,13 @@
@@ -317,7 +358,13 @@
double d7 = Math.min((double) (0.2F + f / 15.0F), 2.5D);
int i = (int) (150.0D * d7);
@@ -80,7 +81,7 @@
}
super.checkFallDamage(d0, flag, iblockdata, blockposition);
@@ -672,13 +718,19 @@
@@ -672,13 +719,19 @@
}
public void onEquipItem(EnumItemSlot enumitemslot, ItemStack itemstack, ItemStack itemstack1) {
@@ -101,7 +102,7 @@
this.level().playSound((EntityHuman) null, this.getX(), this.getY(), this.getZ(), equipable.getEquipSound(), this.getSoundSource(), 1.0F, 1.0F);
}
@@ -752,6 +804,17 @@
@@ -752,6 +805,17 @@
}
}
@@ -119,7 +120,7 @@
if (nbttagcompound.contains("Health", 99)) {
this.setHealth(nbttagcompound.getFloat("Health"));
}
@@ -789,9 +852,32 @@
@@ -789,9 +853,32 @@
}
@@ -152,7 +153,7 @@
try {
while (iterator.hasNext()) {
MobEffectList mobeffectlist = (MobEffectList) iterator.next();
@@ -801,6 +887,12 @@
@@ -801,6 +888,12 @@
this.onEffectUpdated(mobeffect, true, (Entity) null);
})) {
if (!this.level().isClientSide) {
@@ -165,7 +166,7 @@
iterator.remove();
this.onEffectRemoved(mobeffect);
}
@@ -811,6 +903,17 @@
@@ -811,6 +904,17 @@
} catch (ConcurrentModificationException concurrentmodificationexception) {
;
}
@@ -183,7 +184,7 @@
if (this.effectsDirty) {
if (!this.level().isClientSide) {
@@ -937,7 +1040,13 @@
@@ -937,7 +1041,13 @@
this.entityData.set(EntityLiving.DATA_EFFECT_COLOR_ID, 0);
}
@@ -197,7 +198,7 @@
if (this.level().isClientSide) {
return false;
} else {
@@ -946,7 +1055,14 @@
@@ -946,7 +1056,14 @@
boolean flag;
for (flag = false; iterator.hasNext(); flag = true) {
@@ -213,7 +214,7 @@
iterator.remove();
}
@@ -975,19 +1091,49 @@
@@ -975,19 +1092,49 @@
return this.addEffect(mobeffect, (Entity) null);
}
@@ -264,7 +265,7 @@
flag = true;
}
@@ -1025,13 +1171,39 @@
@@ -1025,13 +1172,39 @@
return this.getMobType() == EnumMonsterType.UNDEAD;
}
@@ -305,7 +306,7 @@
if (mobeffect != null) {
this.onEffectRemoved(mobeffect);
@@ -1129,20 +1301,55 @@
@@ -1129,20 +1302,55 @@
}
@@ -362,7 +363,7 @@
this.entityData.set(EntityLiving.DATA_HEALTH_ID, MathHelper.clamp(f, 0.0F, this.getMaxHealth()));
}
@@ -1156,7 +1363,7 @@
@@ -1156,7 +1364,7 @@
return false;
} else if (this.level().isClientSide) {
return false;
@@ -371,7 +372,7 @@
return false;
} else if (damagesource.is(DamageTypeTags.IS_FIRE) && this.hasEffect(MobEffects.FIRE_RESISTANCE)) {
return false;
@@ -1167,10 +1374,11 @@
@@ -1167,10 +1375,11 @@
this.noActionTime = 0;
float f1 = f;
@@ -385,7 +386,7 @@
this.hurtCurrentlyUsedShield(f);
f2 = f;
f = 0.0F;
@@ -1194,23 +1402,33 @@
@@ -1194,23 +1403,33 @@
this.walkAnimation.setSpeed(1.5F);
boolean flag1 = true;
@@ -424,7 +425,25 @@
this.hurtHelmet(damagesource, f);
f *= 0.75F;
}
@@ -1328,19 +1546,32 @@
@@ -1269,7 +1488,7 @@
d0 = (Math.random() - Math.random()) * 0.01D;
}
- this.knockback(0.4000000059604645D, d0, d1);
+ this.knockback(0.4000000059604645D, d0, d1, entity1, entity1 == null ? EntityKnockbackEvent.KnockbackCause.DAMAGE : EntityKnockbackEvent.KnockbackCause.ENTITY_ATTACK); // CraftBukkit
if (!flag) {
this.indicateDamage(d0, d1);
}
@@ -1317,7 +1536,7 @@
}
protected void blockedByShield(EntityLiving entityliving) {
- entityliving.knockback(0.5D, entityliving.getX() - this.getX(), entityliving.getZ() - this.getZ());
+ entityliving.knockback(0.5D, entityliving.getX() - this.getX(), entityliving.getZ() - this.getZ(), null, EntityKnockbackEvent.KnockbackCause.SHIELD_BLOCK); // CraftBukkit
}
private boolean checkTotemDeathProtection(DamageSource damagesource) {
@@ -1328,19 +1547,32 @@
EnumHand[] aenumhand = EnumHand.values();
int i = aenumhand.length;
@@ -461,7 +480,7 @@
EntityPlayer entityplayer = (EntityPlayer) this;
entityplayer.awardStat(StatisticList.ITEM_USED.get(Items.TOTEM_OF_UNDYING));
@@ -1349,14 +1580,16 @@
@@ -1349,14 +1581,16 @@
}
this.setHealth(1.0F);
@@ -483,7 +502,7 @@
}
}
@@ -1463,14 +1696,22 @@
@@ -1463,14 +1697,22 @@
IBlockData iblockdata = Blocks.WITHER_ROSE.defaultBlockState();
if (this.level().getBlockState(blockposition).isAir() && iblockdata.canSurvive(this.level(), blockposition)) {
@@ -508,7 +527,7 @@
this.level().addFreshEntity(entityitem);
}
}
@@ -1490,21 +1731,40 @@
@@ -1490,21 +1732,40 @@
boolean flag = this.lastHurtByPlayerTime > 0;
@@ -538,7 +557,7 @@
+ return i;
+ } else {
+ return 0;
+ }
}
+ }
+ // CraftBukkit end
+
@@ -547,12 +566,41 @@
+ if (true && !(this instanceof net.minecraft.world.entity.boss.enderdragon.EntityEnderDragon)) { // CraftBukkit - SPIGOT-2420: Special case ender dragon will drop the xp over time
+ EntityExperienceOrb.award((WorldServer) this.level(), this.position(), this.expToDrop);
+ this.expToDrop = 0;
}
+ }
+ // CraftBukkit end
}
@@ -1596,6 +1856,28 @@
@@ -1533,13 +1794,25 @@
}
public void knockback(double d0, double d1, double d2) {
+ // CraftBukkit start - EntityKnockbackEvent
+ knockback(d0, d1, d2, null, EntityKnockbackEvent.KnockbackCause.UNKNOWN);
+ }
+
+ public void knockback(double d0, double d1, double d2, Entity attacker, EntityKnockbackEvent.KnockbackCause cause) {
d0 *= 1.0D - this.getAttributeValue(GenericAttributes.KNOCKBACK_RESISTANCE);
- if (d0 > 0.0D) {
- this.hasImpulse = true;
+ if (true || d0 > 0.0D) { // CraftBukkit - Call event even when force is 0
+ //this.hasImpulse = true; // CraftBukkit - Move down
Vec3D vec3d = this.getDeltaMovement();
Vec3D vec3d1 = (new Vec3D(d1, 0.0D, d2)).normalize().scale(d0);
- this.setDeltaMovement(vec3d.x / 2.0D - vec3d1.x, this.onGround() ? Math.min(0.4D, vec3d.y / 2.0D + d0) : vec3d.y, vec3d.z / 2.0D - vec3d1.z);
+ EntityKnockbackEvent event = CraftEventFactory.callEntityKnockbackEvent((org.bukkit.craftbukkit.entity.CraftLivingEntity) this.getBukkitEntity(), attacker, cause, d0, vec3d1, vec3d.x / 2.0D - vec3d1.x, this.onGround() ? Math.min(0.4D, vec3d.y / 2.0D + d0) : vec3d.y, vec3d.z / 2.0D - vec3d1.z);
+ if (event.isCancelled()) {
+ return;
+ }
+
+ this.hasImpulse = true;
+ this.setDeltaMovement(event.getFinalKnockback().getX(), event.getFinalKnockback().getY(), event.getFinalKnockback().getZ());
+ // CraftBukkit end
}
}
@@ -1596,6 +1869,28 @@
return itemstack.getEatingSound();
}
@@ -581,7 +629,7 @@
public Optional<BlockPosition> getLastClimbablePos() {
return this.lastClimbablePos;
}
@@ -1642,9 +1924,14 @@
@@ -1642,9 +1937,14 @@
int i = this.calculateFallDamage(f, f1);
if (i > 0) {
@@ -597,7 +645,7 @@
return true;
} else {
return flag;
@@ -1696,7 +1983,7 @@
@@ -1696,7 +1996,7 @@
protected float getDamageAfterArmorAbsorb(DamageSource damagesource, float f) {
if (!damagesource.is(DamageTypeTags.BYPASSES_ARMOR)) {
@@ -606,7 +654,7 @@
f = CombatMath.getDamageAfterAbsorb(f, (float) this.getArmorValue(), (float) this.getAttributeValue(GenericAttributes.ARMOR_TOUGHNESS));
}
@@ -1709,7 +1996,8 @@
@@ -1709,7 +2009,8 @@
} else {
int i;
@@ -616,7 +664,7 @@
i = (this.getEffect(MobEffects.DAMAGE_RESISTANCE).getAmplifier() + 1) * 5;
int j = 25 - i;
float f1 = f * (float) j;
@@ -1742,16 +2030,125 @@
@@ -1742,16 +2043,125 @@
}
}
@@ -654,7 +702,7 @@
+ };
+ float blockingModifier = blocking.apply((double) f).floatValue();
+ f += blockingModifier;
+
+ Function<Double, Double> armor = new Function<Double, Double>() {
+ @Override
+ public Double apply(Double f) {
@@ -743,14 +791,14 @@
+ absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION);
+ this.setAbsorptionAmount(Math.max(this.getAbsorptionAmount() - absorptionModifier, 0.0F));
+ float f2 = absorptionModifier;
+
+ if (f2 > 0.0F && f2 < 3.4028235E37F && this instanceof EntityHuman) {
+ ((EntityHuman) this).awardStat(StatisticList.DAMAGE_ABSORBED, Math.round(f2 * 10.0F));
+ }
if (f2 > 0.0F && f2 < 3.4028235E37F) {
Entity entity = damagesource.getEntity();
@@ -1762,13 +2159,47 @@
@@ -1762,13 +2172,47 @@
}
}
@@ -800,7 +848,7 @@
}
public CombatTracker getCombatTracker() {
@@ -1793,8 +2224,18 @@
@@ -1793,8 +2237,18 @@
}
public final void setArrowCount(int i) {
@@ -820,7 +868,7 @@
public final int getStingerCount() {
return (Integer) this.entityData.get(EntityLiving.DATA_STINGER_COUNT_ID);
@@ -2036,6 +2477,12 @@
@@ -2036,6 +2490,12 @@
public abstract ItemStack getItemBySlot(EnumItemSlot enumitemslot);
@@ -833,7 +881,7 @@
@Override
public abstract void setItemSlot(EnumItemSlot enumitemslot, ItemStack itemstack);
@@ -2270,6 +2717,7 @@
@@ -2270,6 +2730,7 @@
}
if (this.onGround() && !this.level().isClientSide) {
@@ -841,7 +889,7 @@
this.setSharedFlag(7, false);
}
} else {
@@ -2440,7 +2888,7 @@
@@ -2440,7 +2901,7 @@
}
}
@@ -850,7 +898,7 @@
if (this.tickCount % 20 == 0) {
this.getCombatTracker().recheckStatus();
}
@@ -2537,7 +2985,7 @@
@@ -2537,7 +2998,7 @@
this.refreshDirtyAttributes();
}
@@ -859,7 +907,7 @@
Map<EnumItemSlot, ItemStack> map = this.collectEquipmentChanges();
if (map != null) {
@@ -2839,6 +3287,7 @@
@@ -2839,6 +3300,7 @@
}
if (!this.level().isClientSide) {
@@ -867,7 +915,7 @@
this.setSharedFlag(7, flag);
}
@@ -3029,13 +3478,20 @@
@@ -3029,13 +3491,20 @@
@Override
public boolean isPickable() {
@@ -890,7 +938,7 @@
@Override
public float getYHeadRot() {
@@ -3231,7 +3687,26 @@
@@ -3231,7 +3700,26 @@
} else {
if (!this.useItem.isEmpty() && this.isUsingItem()) {
this.triggerItemUseEffects(this.useItem, 16);
@@ -918,7 +966,7 @@
if (itemstack != this.useItem) {
this.setItemInHand(enumhand, itemstack);
@@ -3309,6 +3784,12 @@
@@ -3309,6 +3797,12 @@
}
public boolean randomTeleport(double d0, double d1, double d2, boolean flag) {
@@ -931,7 +979,7 @@
double d3 = this.getX();
double d4 = this.getY();
double d5 = this.getZ();
@@ -3333,16 +3814,41 @@
@@ -3333,16 +3827,41 @@
}
if (flag2) {
@@ -976,7 +1024,7 @@
} else {
if (flag) {
world.broadcastEntityEvent(this, (byte) 46);
@@ -3354,7 +3860,7 @@
@@ -3354,7 +3873,7 @@
entitycreature.getNavigation().stop();
}
@@ -985,7 +1033,7 @@
}
}
@@ -3443,7 +3949,7 @@
@@ -3443,7 +3962,7 @@
}
public void stopSleeping() {
@@ -994,7 +1042,7 @@
World world = this.level();
java.util.Objects.requireNonNull(world);
@@ -3477,7 +3983,7 @@
@@ -3477,7 +3996,7 @@
@Nullable
public EnumDirection getBedOrientation() {
@@ -1003,7 +1051,7 @@
return blockposition != null ? BlockBed.getBedOrientation(this.level(), blockposition) : null;
}
@@ -3525,7 +4031,7 @@
@@ -3525,7 +4044,7 @@
Pair<MobEffect, Float> pair = (Pair) iterator.next();
if (!world.isClientSide && pair.getFirst() != null && world.random.nextFloat() < (Float) pair.getSecond()) {