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/player/EntityHuman.java
+++ b/net/minecraft/world/entity/player/EntityHuman.java
@@ -112,6 +112,20 @@
@@ -112,6 +112,21 @@
import net.minecraft.world.scores.ScoreboardTeam;
import org.slf4j.Logger;
@@ -14,6 +14,7 @@
+import org.bukkit.event.entity.CreatureSpawnEvent;
+import org.bukkit.event.entity.EntityCombustByEntityEvent;
+import org.bukkit.event.entity.EntityExhaustionEvent;
+import org.bukkit.event.entity.EntityKnockbackEvent;
+import org.bukkit.event.player.PlayerDropItemEvent;
+import org.bukkit.event.player.PlayerVelocityEvent;
+// CraftBukkit end
@@ -21,7 +22,7 @@
public abstract class EntityHuman extends EntityLiving {
private static final Logger LOGGER = LogUtils.getLogger();
@@ -127,7 +141,8 @@
@@ -127,7 +142,8 @@
public static final float SWIMMING_BB_HEIGHT = 0.6F;
public static final float DEFAULT_EYE_HEIGHT = 1.62F;
public static final EntitySize STANDING_DIMENSIONS = EntitySize.scalable(0.6F, 1.8F);
@@ -31,7 +32,7 @@
private static final DataWatcherObject<Float> DATA_PLAYER_ABSORPTION_ID = DataWatcher.defineId(EntityHuman.class, DataWatcherRegistry.FLOAT);
private static final DataWatcherObject<Integer> DATA_SCORE_ID = DataWatcher.defineId(EntityHuman.class, DataWatcherRegistry.INT);
protected static final DataWatcherObject<Byte> DATA_PLAYER_MODE_CUSTOMISATION = DataWatcher.defineId(EntityHuman.class, DataWatcherRegistry.BYTE);
@@ -136,10 +151,10 @@
@@ -136,10 +152,10 @@
protected static final DataWatcherObject<NBTTagCompound> DATA_SHOULDER_RIGHT = DataWatcher.defineId(EntityHuman.class, DataWatcherRegistry.COMPOUND_TAG);
private long timeEntitySatOnShoulder;
private final PlayerInventory inventory = new PlayerInventory(this);
@@ -44,7 +45,7 @@
protected int jumpTriggerTime;
public float oBob;
public float bob;
@@ -168,6 +183,16 @@
@@ -168,6 +184,16 @@
public EntityFishingHook fishing;
protected float hurtDir;
@@ -61,7 +62,7 @@
public EntityHuman(World world, BlockPosition blockposition, float f, GameProfile gameprofile) {
super(EntityTypes.PLAYER, world);
this.lastItemInMainHand = ItemStack.EMPTY;
@@ -312,7 +337,7 @@
@@ -312,7 +338,7 @@
ItemStack itemstack = this.getItemBySlot(EnumItemSlot.HEAD);
if (itemstack.is(Items.TURTLE_HELMET) && !this.isEyeInFluid(TagsFluid.WATER)) {
@@ -70,7 +71,7 @@
}
}
@@ -482,8 +507,14 @@
@@ -482,8 +508,14 @@
public void rideTick() {
if (!this.level().isClientSide && this.wantsToStopRiding() && this.isPassenger()) {
this.stopRiding();
@@ -87,7 +88,7 @@
super.rideTick();
this.oBob = this.bob;
this.bob = 0.0F;
@@ -505,7 +536,8 @@
@@ -505,7 +537,8 @@
if (this.level().getDifficulty() == EnumDifficulty.PEACEFUL && this.level().getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION)) {
if (this.getHealth() < this.getMaxHealth() && this.tickCount % 20 == 0) {
@@ -97,7 +98,7 @@
}
if (this.foodData.needsFood() && this.tickCount % 10 == 0) {
@@ -665,6 +697,13 @@
@@ -665,6 +698,13 @@
@Nullable
public EntityItem drop(ItemStack itemstack, boolean flag, boolean flag1) {
@@ -111,7 +112,7 @@
if (itemstack.isEmpty()) {
return null;
} else {
@@ -699,6 +738,33 @@
@@ -699,6 +739,33 @@
entityitem.setDeltaMovement((double) (-f3 * f2 * 0.3F) + Math.cos((double) f5) * (double) f6, (double) (-f1 * 0.3F + 0.1F + (this.random.nextFloat() - this.random.nextFloat()) * 0.1F), (double) (f4 * f2 * 0.3F) + Math.sin((double) f5) * (double) f6);
}
@@ -145,7 +146,7 @@
return entityitem;
}
}
@@ -789,7 +855,7 @@
@@ -789,7 +856,7 @@
}
if (nbttagcompound.contains("LastDeathLocation", 10)) {
@@ -154,7 +155,7 @@
Logger logger = EntityHuman.LOGGER;
Objects.requireNonNull(logger);
@@ -822,7 +888,7 @@
@@ -822,7 +889,7 @@
}
this.getLastDeathLocation().flatMap((globalpos) -> {
@@ -163,7 +164,7 @@
Logger logger = EntityHuman.LOGGER;
Objects.requireNonNull(logger);
@@ -849,12 +915,12 @@
@@ -849,12 +916,12 @@
return false;
} else {
if (!this.level().isClientSide) {
@@ -178,7 +179,7 @@
}
if (this.level().getDifficulty() == EnumDifficulty.EASY) {
@@ -866,7 +932,13 @@
@@ -866,7 +933,13 @@
}
}
@@ -193,7 +194,7 @@
}
}
}
@@ -886,10 +958,29 @@
@@ -886,10 +959,29 @@
}
public boolean canHarmPlayer(EntityHuman entityhuman) {
@@ -226,7 +227,7 @@
}
@Override
@@ -931,8 +1022,13 @@
@@ -931,8 +1023,13 @@
}
}
@@ -241,7 +242,7 @@
if (!this.isInvulnerableTo(damagesource)) {
f = this.getDamageAfterArmorAbsorb(damagesource, f);
f = this.getDamageAfterMagicAbsorb(damagesource, f);
@@ -947,7 +1043,7 @@
@@ -947,7 +1044,7 @@
}
if (f != 0.0F) {
@@ -250,7 +251,7 @@
this.getCombatTracker().recordDamage(damagesource, f);
this.setHealth(this.getHealth() - f);
if (f < 3.4028235E37F) {
@@ -957,6 +1053,7 @@
@@ -957,6 +1054,7 @@
this.gameEvent(GameEvent.ENTITY_DAMAGE);
}
}
@@ -258,7 +259,7 @@
}
@Override
@@ -1121,7 +1218,7 @@
@@ -1121,7 +1219,7 @@
f *= 0.2F + f2 * f2 * 0.8F;
f1 *= f2;
@@ -267,7 +268,7 @@
if (f > 0.0F || f1 > 0.0F) {
boolean flag = f2 > 0.9F;
boolean flag1 = false;
@@ -1160,8 +1257,15 @@
@@ -1160,8 +1258,15 @@
if (entity instanceof EntityLiving) {
f3 = ((EntityLiving) entity).getHealth();
if (j > 0 && !entity.isOnFire()) {
@@ -285,20 +286,30 @@
}
}
@@ -1189,8 +1293,11 @@
@@ -1171,7 +1276,7 @@
if (flag5) {
if (i > 0) {
if (entity instanceof EntityLiving) {
- ((EntityLiving) entity).knockback((double) ((float) i * 0.5F), (double) MathHelper.sin(this.getYRot() * 0.017453292F), (double) (-MathHelper.cos(this.getYRot() * 0.017453292F)));
+ ((EntityLiving) entity).knockback((double) ((float) i * 0.5F), (double) MathHelper.sin(this.getYRot() * 0.017453292F), (double) (-MathHelper.cos(this.getYRot() * 0.017453292F)), this, EntityKnockbackEvent.KnockbackCause.ENTITY_ATTACK); // CraftBukkit
} else {
entity.push((double) (-MathHelper.sin(this.getYRot() * 0.017453292F) * (float) i * 0.5F), 0.1D, (double) (MathHelper.cos(this.getYRot() * 0.017453292F) * (float) i * 0.5F));
}
@@ -1189,8 +1294,11 @@
EntityLiving entityliving = (EntityLiving) iterator.next();
if (entityliving != this && entityliving != entity && !this.isAlliedTo((Entity) entityliving) && (!(entityliving instanceof EntityArmorStand) || !((EntityArmorStand) entityliving).isMarker()) && this.distanceToSqr((Entity) entityliving) < 9.0D) {
- entityliving.knockback(0.4000000059604645D, (double) MathHelper.sin(this.getYRot() * 0.017453292F), (double) (-MathHelper.cos(this.getYRot() * 0.017453292F)));
- entityliving.hurt(this.damageSources().playerAttack(this), f4);
+ // CraftBukkit start - Only apply knockback if the damage hits
+ if (entityliving.hurt(this.damageSources().playerAttack(this).sweep(), f4)) {
entityliving.knockback(0.4000000059604645D, (double) MathHelper.sin(this.getYRot() * 0.017453292F), (double) (-MathHelper.cos(this.getYRot() * 0.017453292F)));
- entityliving.hurt(this.damageSources().playerAttack(this), f4);
+ entityliving.knockback(0.4000000059604645D, (double) MathHelper.sin(this.getYRot() * 0.017453292F), (double) (-MathHelper.cos(this.getYRot() * 0.017453292F)), this, EntityKnockbackEvent.KnockbackCause.SWEEP_ATTACK); // CraftBukkit
+ }
+ // CraftBukkit end
}
}
@@ -1199,9 +1306,26 @@
@@ -1199,9 +1307,26 @@
}
if (entity instanceof EntityPlayer && entity.hurtMarked) {
@@ -325,7 +336,7 @@
}
if (flag2) {
@@ -1246,7 +1370,14 @@
@@ -1246,7 +1371,14 @@
this.awardStat(StatisticList.DAMAGE_DEALT, Math.round(f5 * 10.0F));
if (j > 0) {
@@ -341,7 +352,7 @@
}
if (this.level() instanceof WorldServer && f5 > 2.0F) {
@@ -1256,12 +1387,17 @@
@@ -1256,12 +1388,17 @@
}
}
@@ -360,7 +371,7 @@
}
}
@@ -1338,6 +1474,12 @@
@@ -1338,6 +1475,12 @@
}
public Either<EntityHuman.EnumBedResult, Unit> startSleepInBed(BlockPosition blockposition) {
@@ -373,7 +384,7 @@
this.startSleeping(blockposition);
this.sleepCounter = 0;
return Either.right(Unit.INSTANCE);
@@ -1425,9 +1567,9 @@
@@ -1425,9 +1568,9 @@
super.jumpFromGround();
this.awardStat(StatisticList.JUMP);
if (this.isSprinting()) {
@@ -385,7 +396,7 @@
}
}
@@ -1454,7 +1596,11 @@
@@ -1454,7 +1597,11 @@
this.setDeltaMovement(vec3d2.x, d0 * 0.6D, vec3d2.z);
this.resetFallDistance();
@@ -398,7 +409,7 @@
} else {
super.travel(vec3d);
}
@@ -1507,12 +1653,24 @@
@@ -1507,12 +1654,24 @@
}
public void startFallFlying() {
@@ -424,7 +435,7 @@
}
@Override
@@ -1626,10 +1784,21 @@
@@ -1626,10 +1785,21 @@
return this.experienceLevel >= 30 ? 112 + (this.experienceLevel - 30) * 9 : (this.experienceLevel >= 15 ? 37 + (this.experienceLevel - 15) * 5 : 7 + this.experienceLevel * 2);
}
@@ -447,7 +458,7 @@
}
}
@@ -1715,13 +1884,20 @@
@@ -1715,13 +1885,20 @@
@Override
public void setItemSlot(EnumItemSlot enumitemslot, ItemStack itemstack) {
@@ -471,7 +482,7 @@
}
}
@@ -1760,26 +1936,31 @@
@@ -1760,26 +1937,31 @@
protected void removeEntitiesOnShoulder() {
if (this.timeEntitySatOnShoulder + 20L < this.level().getGameTime()) {