From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: hpfxd Date: Tue, 2 Jan 2024 01:47:36 -0500 Subject: [PATCH] PandaSpigot: Configurable knockback Original license: GPLv3 Original project: https://github.com/hpfxd/PandaSpigot This patch allows velocity applied when a player is hit to be modified in the configuration. Reference: https://gist.github.com/YoungOG/e3265d98661957abece71594b70d6a01 diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index cadb6ebde38658aba251de1259db6449cefc9c9d..1cd8bbf9f0bd10811c4fbc28a6f33a7553cac4fc 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -1967,7 +1967,13 @@ public abstract class LivingEntity extends Entity implements Attackable { Vec3 vec3d = this.getDeltaMovement(); Vec3 vec3d1 = (new Vec3(x, 0.0D, z)).normalize().scale(strength); - this.setDeltaMovement(vec3d.x / 2.0D - vec3d1.x, this.onGround() ? Math.min(0.4D, vec3d.y / 2.0D + strength) : vec3d.y, vec3d.z / 2.0D - vec3d1.z); + // Leaf start - Configurable PandaSpigot knockback + this.setDeltaMovement( + vec3d.x / org.dreeam.leaf.LeafConfig.friction - vec3d1.x * org.dreeam.leaf.LeafConfig.horizontal, + this.onGround() ? Math.min(org.dreeam.leaf.LeafConfig.vertical, vec3d.y / org.dreeam.leaf.LeafConfig.friction + org.dreeam.leaf.LeafConfig.verticalLimit) : vec3d.y, + vec3d.z / org.dreeam.leaf.LeafConfig.friction - vec3d1.z * org.dreeam.leaf.LeafConfig.horizontal + ); + // Leaf end // Paper start - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent Vec3 currentMovement = this.getDeltaMovement(); org.bukkit.util.Vector delta = new org.bukkit.util.Vector(currentMovement.x - vec3d.x, currentMovement.y - vec3d.y, currentMovement.z - vec3d.z); diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java index 7264f6e05a6b1fee296ccde4f39e973c8a454b58..02ff878b2cc3b014d245e3e53565760a063adad1 100644 --- a/src/main/java/net/minecraft/world/entity/player/Player.java +++ b/src/main/java/net/minecraft/world/entity/player/Player.java @@ -1371,11 +1371,23 @@ public abstract class Player extends LivingEntity { if (flag5) { if (i > 0) { + // Leaf start - Configurable PandaSpigot knockback if (target instanceof LivingEntity) { - ((LivingEntity) target).knockback((double) ((float) i * 0.5F), (double) Mth.sin(this.getYRot() * 0.017453292F), (double) (-Mth.cos(this.getYRot() * 0.017453292F)), this); // Paper - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent + ((LivingEntity) target).knockback( + (float) i * 0.5F, + Mth.sin(this.getYRot() * 0.017453292F), + -Mth.cos(this.getYRot() * 0.017453292F), + this + ); // Paper - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent } else { - target.push((double) (-Mth.sin(this.getYRot() * 0.017453292F) * (float) i * 0.5F), 0.1D, (double) (Mth.cos(this.getYRot() * 0.017453292F) * (float) i * 0.5F), this); // Paper - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent + target.push( + -Mth.sin(this.getYRot() * 0.017453292F) * (float) i * org.dreeam.leaf.LeafConfig.extraHorizontal, + org.dreeam.leaf.LeafConfig.extraVertical, + Mth.cos(this.getYRot() * 0.017453292F) * (float) i * org.dreeam.leaf.LeafConfig.extraHorizontal, + this + ); // Paper - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent } + // Leaf end this.setDeltaMovement(this.getDeltaMovement().multiply(0.6D, 1.0D, 0.6D)); // Paper start - Configurable sprint interruption on attack @@ -1406,27 +1418,27 @@ public abstract class Player extends LivingEntity { this.sweepAttack(); } - if (target instanceof ServerPlayer && target.hurtMarked) { - // CraftBukkit start - Add Velocity Event - boolean cancelled = false; - org.bukkit.entity.Player player = (org.bukkit.entity.Player) target.getBukkitEntity(); - org.bukkit.util.Vector velocity = CraftVector.toBukkit(vec3d); + // Leaf start - Configurable PandaSpigot knockback + if (target instanceof ServerPlayer attackedPlayer && target.hurtMarked) { + // If the attack caused knockback, send the new velocity to the victim's client immediately, + // and undo the change. Otherwise, if movement packets from the victim are processed before + // the end of the tick, then friction may reduce the velocity considerably before it's sent + // to the client, particularly if the victim was standing on the ground when those packets + // were generated. And because this glitch is also likely to make server-side velocity very + // inconsistent, we simply reverse the knockback after sending it so that KB, like most other + // things, doesn't affect server velocity at all. + PlayerVelocityEvent event = new PlayerVelocityEvent(attackedPlayer.getBukkitEntity(), + attackedPlayer.getBukkitEntity().getVelocity()); - PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone()); this.level().getCraftServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - cancelled = true; - } else if (!velocity.equals(event.getVelocity())) { - player.setVelocity(event.getVelocity()); + if (!event.isCancelled()) { + attackedPlayer.getBukkitEntity().setVelocityDirect(event.getVelocity()); + attackedPlayer.connection.send(new ClientboundSetEntityMotionPacket(attackedPlayer)); } - if (!cancelled) { - ((ServerPlayer) target).connection.send(new ClientboundSetEntityMotionPacket(target)); - target.hurtMarked = false; - target.setDeltaMovement(vec3d); - } - // CraftBukkit end + attackedPlayer.hurtMarked = false; + attackedPlayer.setDeltaMovement(vec3d); + // Leaf end } if (flag2) { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 2b4279efe6698e786f9db8066f73b7934c8660d4..d246d49b510160a9a53f6f4fc18398596d4e305a 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -67,6 +67,7 @@ import net.minecraft.network.protocol.game.ClientboundSetBorderSizePacket; import net.minecraft.network.protocol.game.ClientboundSetBorderWarningDelayPacket; import net.minecraft.network.protocol.game.ClientboundSetBorderWarningDistancePacket; import net.minecraft.network.protocol.game.ClientboundSetDefaultSpawnPositionPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; import net.minecraft.network.protocol.game.ClientboundSetExperiencePacket; import net.minecraft.network.protocol.game.ClientboundSetHealthPacket; @@ -3242,6 +3243,40 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().resetAttackStrengthTicker(); } // Paper end + + // Leaf start - Configurable PandaSpigot knockback + @Override + public void setVelocity(org.bukkit.util.Vector vec) { + // To be consistent with old behavior, set the velocity before firing the event + this.setVelocityDirect(vec); + + org.bukkit.event.player.PlayerVelocityEvent event = new org.bukkit.event.player.PlayerVelocityEvent(this, vec.clone()); + this.getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + // Set the velocity again in case it was changed by event handlers + this.setVelocityDirect(event.getVelocity()); + + // Send the new velocity to the player's client immediately, so it isn't affected by + // any movement packets from this player that may be processed before the end of the tick. + // Without this, player velocity changes tend to be very inconsistent. + this.getHandle().connection.send(new ClientboundSetEntityMotionPacket(this.getHandle())); + } + + // Note that cancelling the event does not restore the old velocity, it only prevents + // the packet from sending. Again, this is to be consistent with old behavior. + } + + public void setVelocityDirect(org.bukkit.util.Vector vec) { + entity.xo = vec.getX(); + entity.yo = vec.getY(); + entity.zo = vec.getZ(); + if (entity.yo > 0) { + entity.fallDistance = 0.0f; + } + } + // Leaf end + // Spigot start private final Player.Spigot spigot = new Player.Spigot() { diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java index 8db4b1268e7bc18d60cc53f2753a73c2fc3ec242..26c7a5020e9fd9dac212c6540dc79cad26e64796 100644 --- a/src/main/java/org/dreeam/leaf/LeafConfig.java +++ b/src/main/java/org/dreeam/leaf/LeafConfig.java @@ -305,4 +305,19 @@ public class LeafConfig { private static void vanillaEndTeleport() { useVanillaEndTeleport = getBoolean("use-vanilla-end-teleport", useVanillaEndTeleport, "Vanilla End Gateway Teleport"); } + + public static double friction = 2.0D; + public static double horizontal = 0.4D; + public static double vertical = 0.4D; + public static double verticalLimit = 0.4000000059604645D; + public static double extraHorizontal = 0.5D; + public static double extraVertical = 0.1D; + private static void knockback() { + friction = getDouble("playerKnockback.friction", friction); + horizontal = getDouble("playerKnockback.horizontal", horizontal); + vertical = getDouble("playerKnockback.vertical", vertical); + verticalLimit = getDouble("playerKnockback.verticalLimit", verticalLimit); + extraHorizontal = getDouble("playerKnockback.extraHorizontal", extraHorizontal); + extraVertical = getDouble("playerKnockback.extraVertical", extraVertical); + } }