forked from hpfxd/PandaSpigot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0016-Configurable-knockback.patch
181 lines (172 loc) · 10.1 KB
/
0016-Configurable-knockback.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: hpfxd <[email protected]>
Date: Sat, 30 Oct 2021 14:26:06 -0400
Subject: [PATCH] Configurable knockback
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/com/hpfxd/pandaspigot/config/PandaSpigotWorldConfig.java b/src/main/java/com/hpfxd/pandaspigot/config/PandaSpigotWorldConfig.java
index 9afaf630481a9533fb014f306d4d90b8de80a129..3dd3d0702c17541083d0d365ca46e93602105bce 100644
--- a/src/main/java/com/hpfxd/pandaspigot/config/PandaSpigotWorldConfig.java
+++ b/src/main/java/com/hpfxd/pandaspigot/config/PandaSpigotWorldConfig.java
@@ -6,4 +6,16 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
@ConfigSerializable
@SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"})
public class PandaSpigotWorldConfig {
+ @Comment("These options control velocity players receive when damaged.")
+ public KnockbackConfig knockback;
+
+ @ConfigSerializable
+ public static class KnockbackConfig {
+ public double friction = 2.0;
+ public double horizontal = 0.4;
+ public double vertical = 0.4;
+ public double verticalLimit = 0.4000000059604645;
+ public double extraHorizontal = 0.5;
+ public double extraVertical = 0.1;
+ }
}
diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java
index 0ad4edb8390f396d935d321caf67af0f209d5420..9034e1ad7a013c7288e68322a65798e40d8b851d 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
@@ -1003,35 +1003,43 @@ public abstract class EntityHuman extends EntityLiving {
if (flag2) {
if (i > 0) {
- entity.g((double) (-MathHelper.sin(this.yaw * 3.1415927F / 180.0F) * (float) i * 0.5F), 0.1D, (double) (MathHelper.cos(this.yaw * 3.1415927F / 180.0F) * (float) i * 0.5F));
+ // PandaSpigot start - Configurable knockback
+ com.hpfxd.pandaspigot.config.PandaSpigotWorldConfig.KnockbackConfig knockbackConfig = entity.world.pandaSpigotConfig.knockback;
+ entity.g(
+ -MathHelper.sin(this.yaw * 3.1415927F / 180.0F) * (float) i * knockbackConfig.extraHorizontal,
+ knockbackConfig.extraVertical,
+ MathHelper.cos(this.yaw * 3.1415927F / 180.0F) * (float) i * knockbackConfig.extraHorizontal);
+ // PandaSpigot end
this.motX *= 0.6D;
this.motZ *= 0.6D;
this.setSprinting(false);
}
if (entity instanceof EntityPlayer && entity.velocityChanged) {
- // CraftBukkit start - Add Velocity Event
- boolean cancelled = false;
- Player player = (Player) entity.getBukkitEntity();
- org.bukkit.util.Vector velocity = new Vector( d0, d1, d2 );
-
- PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone());
- world.getServer().getPluginManager().callEvent(event);
-
- if (event.isCancelled()) {
- cancelled = true;
- } else if (!velocity.equals(event.getVelocity())) {
- player.setVelocity(event.getVelocity());
+ // PandaSpigot start - Configurable knockback
+ // 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.
+
+ EntityPlayer attackedPlayer = (EntityPlayer) entity;
+ PlayerVelocityEvent event = new PlayerVelocityEvent(attackedPlayer.getBukkitEntity(),
+ attackedPlayer.getBukkitEntity().getVelocity());
+
+ this.world.getServer().getPluginManager().callEvent(event);
+ if (!event.isCancelled()) {
+ attackedPlayer.getBukkitEntity().setVelocityDirect(event.getVelocity());
+ attackedPlayer.playerConnection.sendPacket(new PacketPlayOutEntityVelocity(attackedPlayer));
}
- if (!cancelled) {
- ( (EntityPlayer) entity ).playerConnection.sendPacket( new PacketPlayOutEntityVelocity( entity ) );
- entity.velocityChanged = false;
- entity.motX = d0;
- entity.motY = d1;
- entity.motZ = d2;
- }
- // CraftBukkit end
+ attackedPlayer.velocityChanged = false;
+ attackedPlayer.motX = d0;
+ attackedPlayer.motY = d1;
+ attackedPlayer.motZ = d2;
+ // PandaSpigot end
}
if (flag) {
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
index c8b4e292849486eadfb437110466d6eafe275411..72c7e6fc8bb0a71877d6759af44d39030bcf51f5 100644
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
@@ -897,18 +897,23 @@ public abstract class EntityLiving extends Entity {
public void a(Entity entity, float f, double d0, double d1) {
if (this.random.nextDouble() >= this.getAttributeInstance(GenericAttributes.c).getValue()) {
this.ai = true;
- float f1 = MathHelper.sqrt(d0 * d0 + d1 * d1);
- float f2 = 0.4F;
-
- this.motX /= 2.0D;
- this.motY /= 2.0D;
- this.motZ /= 2.0D;
- this.motX -= d0 / (double) f1 * (double) f2;
- this.motY += (double) f2;
- this.motZ -= d1 / (double) f1 * (double) f2;
- if (this.motY > 0.4000000059604645D) {
- this.motY = 0.4000000059604645D;
+ // PandaSpigot start - Configurable knockback
+ com.hpfxd.pandaspigot.config.PandaSpigotWorldConfig.KnockbackConfig knockbackConfig = entity.world.pandaSpigotConfig.knockback;
+ double magnitude = MathHelper.sqrt(d0 * d0 + d1 * d1);
+
+ double friction = knockbackConfig.friction;
+ this.motX /= friction;
+ this.motY /= friction;
+ this.motZ /= friction;
+
+ this.motX -= d0 / magnitude * knockbackConfig.horizontal;
+ this.motY += knockbackConfig.vertical;
+ this.motZ -= d1 / magnitude * knockbackConfig.horizontal;
+
+ if (this.motY > knockbackConfig.verticalLimit) {
+ this.motY = knockbackConfig.verticalLimit;
}
+ // PandaSpigot end
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index d44fc62b9d9454dad79514731efac024be35aa76..801e19baa711b517eb437a173ea1edcc38b2e7e8 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1447,6 +1447,38 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
PacketPlayOutTitle packetReset = new PacketPlayOutTitle(EnumTitleAction.RESET, null);
getHandle().playerConnection.sendPacket(packetReset);
}
+ // PandaSpigot start
+ @Override
+ public void setVelocity(org.bukkit.util.Vector vel) {
+ // To be consistent with old behavior, set the velocity before firing the event
+ this.setVelocityDirect(vel);
+
+ org.bukkit.event.player.PlayerVelocityEvent event = new org.bukkit.event.player.PlayerVelocityEvent(this, vel.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().playerConnection.sendPacket(new PacketPlayOutEntityVelocity(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 vel) {
+ entity.motX = vel.getX();
+ entity.motY = vel.getY();
+ entity.motZ = vel.getZ();
+ if (entity.motY > 0) {
+ entity.fallDistance = 0.0f;
+ }
+ }
+ // PandaSpigot end
// Spigot start
private final Player.Spigot spigot = new Player.Spigot()