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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
--- a/net/minecraft/server/level/ServerEntity.java
+++ b/net/minecraft/server/level/ServerEntity.java
@@ -28,7 +28,6 @@
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
import net.minecraft.network.protocol.game.VecDeltaCodec;
import net.minecraft.network.syncher.SynchedEntityData;
-import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
@@ -42,6 +41,13 @@
import net.minecraft.world.phys.Vec3;
import org.slf4j.Logger;
+// CraftBukkit start
+import net.minecraft.server.network.ServerPlayerConnection;
+import net.minecraft.util.Mth;
+import org.bukkit.entity.Player;
+import org.bukkit.event.player.PlayerVelocityEvent;
+// CraftBukkit end
+
public class ServerEntity {
private static final Logger LOGGER = LogUtils.getLogger();
@@ -66,11 +72,15 @@
private boolean wasOnGround;
@Nullable
private List<SynchedEntityData.DataValue<?>> trackedDataValues;
+ // CraftBukkit start
+ private final Set<ServerPlayerConnection> trackedPlayers;
- public ServerEntity(ServerLevel serverlevel, Entity entity, int i, boolean flag, Consumer<Packet<?>> consumer) {
+ public ServerEntity(ServerLevel worldserver, Entity entity, int i, boolean flag, Consumer<Packet<?>> consumer, Set<ServerPlayerConnection> trackedPlayers) {
+ this.trackedPlayers = trackedPlayers;
+ // CraftBukkit end
this.ap = Vec3.ZERO;
this.lastPassengers = Collections.emptyList();
- this.level = serverlevel;
+ this.level = worldserver;
this.broadcast = consumer;
this.entity = entity;
this.updateInterval = i;
@@ -87,12 +97,12 @@
List<Entity> list = this.entity.getPassengers();
if (!list.equals(this.lastPassengers)) {
- this.broadcast.accept(new ClientboundSetPassengersPacket(this.entity));
+ this.broadcastAndSend(new ClientboundSetPassengersPacket(this.entity)); // CraftBukkit
removedPassengers(list, this.lastPassengers).forEach((entity) -> {
if (entity instanceof ServerPlayer) {
- ServerPlayer serverplayer = (ServerPlayer) entity;
+ ServerPlayer entityplayer = (ServerPlayer) entity;
- serverplayer.connection.teleport(serverplayer.getX(), serverplayer.getY(), serverplayer.getZ(), serverplayer.getYRot(), serverplayer.getXRot());
+ entityplayer.connection.teleport(entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(), entityplayer.getYRot(), entityplayer.getXRot());
}
});
@@ -102,26 +112,26 @@
Entity entity = this.entity;
if (entity instanceof ItemFrame) {
- ItemFrame itemframe = (ItemFrame) entity;
+ ItemFrame entityitemframe = (ItemFrame) entity;
- if (this.tickCount % 10 == 0) {
- ItemStack itemstack = itemframe.getItem();
+ if (true || this.tickCount % 10 == 0) { // CraftBukkit - Moved below, should always enter this block
+ ItemStack itemstack = entityitemframe.getItem();
- if (itemstack.getItem() instanceof MapItem) {
+ if (this.tickCount % 10 == 0 && itemstack.getItem() instanceof MapItem) { // CraftBukkit - Moved this.tickCounter % 10 logic here so item frames do not enter the other blocks
Integer integer = MapItem.getMapId(itemstack);
- MapItemSavedData mapitemsaveddata = MapItem.getSavedData(integer, this.level);
+ MapItemSavedData worldmap = MapItem.getSavedData(integer, this.level);
- if (mapitemsaveddata != null) {
- Iterator iterator = this.level.players().iterator();
+ if (worldmap != null) {
+ Iterator<ServerPlayerConnection> iterator = this.trackedPlayers.iterator(); // CraftBukkit
while (iterator.hasNext()) {
- ServerPlayer serverplayer = (ServerPlayer) iterator.next();
+ ServerPlayer entityplayer = iterator.next().getPlayer(); // CraftBukkit
- mapitemsaveddata.tickCarriedBy(serverplayer, itemstack);
- Packet<?> packet = mapitemsaveddata.getUpdatePacket(integer, serverplayer);
+ worldmap.tickCarriedBy(entityplayer, itemstack);
+ Packet<?> packet = worldmap.getUpdatePacket(integer, entityplayer);
if (packet != null) {
- serverplayer.connection.send(packet);
+ entityplayer.connection.send(packet);
}
}
}
@@ -153,8 +163,8 @@
++this.teleportDelay;
i = Mth.floor(this.entity.getYRot() * 256.0F / 360.0F);
j = Mth.floor(this.entity.getXRot() * 256.0F / 360.0F);
- Vec3 vec3 = this.entity.trackingPosition();
- boolean flag1 = this.positionCodec.delta(vec3).lengthSqr() >= 7.62939453125E-6D;
+ Vec3 vec3d = this.entity.trackingPosition();
+ boolean flag1 = this.positionCodec.delta(vec3d).lengthSqr() >= 7.62939453125E-6D;
Packet<?> packet1 = null;
boolean flag2 = flag1 || this.tickCount % 60 == 0;
boolean flag3 = Math.abs(i - this.yRotp) >= 1 || Math.abs(j - this.xRotp) >= 1;
@@ -162,9 +172,9 @@
boolean flag5 = false;
if (this.tickCount > 0 || this.entity instanceof AbstractArrow) {
- long k = this.positionCodec.encodeX(vec3);
- long l = this.positionCodec.encodeY(vec3);
- long i1 = this.positionCodec.encodeZ(vec3);
+ long k = this.positionCodec.encodeX(vec3d);
+ long l = this.positionCodec.encodeY(vec3d);
+ long i1 = this.positionCodec.encodeZ(vec3d);
boolean flag6 = k < -32768L || k > 32767L || l < -32768L || l > 32767L || i1 < -32768L || i1 > 32767L;
if (!flag6 && this.teleportDelay <= 400 && !this.wasRiding && this.wasOnGround == this.entity.onGround()) {
@@ -191,11 +201,11 @@
}
if ((this.trackDelta || this.entity.hasImpulse || this.entity instanceof LivingEntity && ((LivingEntity) this.entity).isFallFlying()) && this.tickCount > 0) {
- Vec3 vec31 = this.entity.getDeltaMovement();
- double d0 = vec31.distanceToSqr(this.ap);
+ Vec3 vec3d1 = this.entity.getDeltaMovement();
+ double d0 = vec3d1.distanceToSqr(this.ap);
- if (d0 > 1.0E-7D || d0 > 0.0D && vec31.lengthSqr() == 0.0D) {
- this.ap = vec31;
+ if (d0 > 1.0E-7D || d0 > 0.0D && vec3d1.lengthSqr() == 0.0D) {
+ this.ap = vec3d1;
this.broadcast.accept(new ClientboundSetEntityMotionPacket(this.entity.getId(), this.ap));
}
}
@@ -206,7 +216,7 @@
this.sendDirtyEntityData();
if (flag4) {
- this.positionCodec.setBase(vec3);
+ this.positionCodec.setBase(vec3d);
}
if (flag5) {
@@ -228,35 +238,58 @@
++this.tickCount;
if (this.entity.hurtMarked) {
- this.broadcastAndSend(new ClientboundSetEntityMotionPacket(this.entity));
+ // CraftBukkit start - Create PlayerVelocity event
+ boolean cancelled = false;
+
+ if (this.entity instanceof ServerPlayer) {
+ Player player = (Player) this.entity.getBukkitEntity();
+ org.bukkit.util.Vector velocity = player.getVelocity();
+
+ PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone());
+ this.entity.level().getCraftServer().getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
+ cancelled = true;
+ } else if (!velocity.equals(event.getVelocity())) {
+ player.setVelocity(event.getVelocity());
+ }
+ }
+
+ if (!cancelled) {
+ this.broadcastAndSend(new ClientboundSetEntityMotionPacket(this.entity));
+ }
+ // CraftBukkit end
this.entity.hurtMarked = false;
}
}
- private static Stream<Entity> removedPassengers(List<Entity> list, List<Entity> list1) {
- return list1.stream().filter((entity) -> {
- return !list.contains(entity);
+ private static Stream<Entity> removedPassengers(List<Entity> initialPassengers, List<Entity> currentPassengers) {
+ return currentPassengers.stream().filter((entity) -> {
+ return !initialPassengers.contains(entity);
});
}
- public void removePairing(ServerPlayer serverplayer) {
- this.entity.stopSeenByPlayer(serverplayer);
- serverplayer.connection.send(new ClientboundRemoveEntitiesPacket(new int[]{this.entity.getId()}));
+ public void removePairing(ServerPlayer player) {
+ this.entity.stopSeenByPlayer(player);
+ player.connection.send(new ClientboundRemoveEntitiesPacket(new int[]{this.entity.getId()}));
}
- public void addPairing(ServerPlayer serverplayer) {
+ public void addPairing(ServerPlayer player) {
List<Packet<ClientGamePacketListener>> list = new ArrayList();
Objects.requireNonNull(list);
- this.sendPairingData(serverplayer, list::add);
- serverplayer.connection.send(new ClientboundBundlePacket(list));
- this.entity.startSeenByPlayer(serverplayer);
+ this.sendPairingData(player, list::add);
+ player.connection.send(new ClientboundBundlePacket(list));
+ this.entity.startSeenByPlayer(player);
}
- public void sendPairingData(ServerPlayer serverplayer, Consumer<Packet<ClientGamePacketListener>> consumer) {
+ public void sendPairingData(ServerPlayer player, Consumer<Packet<ClientGamePacketListener>> consumer) {
if (this.entity.isRemoved()) {
- ServerEntity.LOGGER.warn("Fetching packet for removed entity {}", this.entity);
+ // CraftBukkit start - Remove useless error spam, just return
+ // EntityTrackerEntry.LOGGER.warn("Fetching packet for removed entity {}", this.entity);
+ return;
+ // CraftBukkit end
}
Packet<ClientGamePacketListener> packet = this.entity.getAddEntityPacket();
@@ -272,6 +305,12 @@
if (this.entity instanceof LivingEntity) {
Collection<AttributeInstance> collection = ((LivingEntity) this.entity).getAttributes().getSyncableAttributes();
+ // CraftBukkit start - If sending own attributes send scaled health instead of current maximum health
+ if (this.entity.getId() == player.getId()) {
+ ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(collection, false);
+ }
+ // CraftBukkit end
+
if (!collection.isEmpty()) {
consumer.accept(new ClientboundUpdateAttributesPacket(this.entity.getId(), collection));
}
@@ -288,23 +327,30 @@
if (this.entity instanceof LivingEntity) {
List<Pair<EquipmentSlot, ItemStack>> list = Lists.newArrayList();
- EquipmentSlot[] aequipmentslot = EquipmentSlot.values();
- int i = aequipmentslot.length;
+ EquipmentSlot[] aenumitemslot = EquipmentSlot.values();
+ int i = aenumitemslot.length;
for (int j = 0; j < i; ++j) {
- EquipmentSlot equipmentslot = aequipmentslot[j];
- ItemStack itemstack = ((LivingEntity) this.entity).getItemBySlot(equipmentslot);
+ EquipmentSlot enumitemslot = aenumitemslot[j];
+ ItemStack itemstack = ((LivingEntity) this.entity).getItemBySlot(enumitemslot);
if (!itemstack.isEmpty()) {
- list.add(Pair.of(equipmentslot, itemstack.copy()));
+ list.add(Pair.of(enumitemslot, itemstack.copy()));
}
}
if (!list.isEmpty()) {
consumer.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list));
}
+ ((LivingEntity) this.entity).detectEquipmentUpdatesPublic(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending
}
+ // CraftBukkit start - MC-109346: Fix for nonsensical head yaw
+ if (this.entity instanceof ServerPlayer) {
+ consumer.accept(new ClientboundRotateHeadPacket(this.entity, (byte) Mth.floor(this.entity.getYHeadRot() * 256.0F / 360.0F)));
+ }
+ // CraftBukkit end
+
if (!this.entity.getPassengers().isEmpty()) {
consumer.accept(new ClientboundSetPassengersPacket(this.entity));
}
@@ -316,21 +362,21 @@
Entity entity = this.entity;
if (entity instanceof Mob) {
- Mob mob = (Mob) entity;
+ Mob entityinsentient = (Mob) entity;
- if (mob.isLeashed()) {
- consumer.accept(new ClientboundSetEntityLinkPacket(mob, mob.getLeashHolder()));
+ if (entityinsentient.isLeashed()) {
+ consumer.accept(new ClientboundSetEntityLinkPacket(entityinsentient, entityinsentient.getLeashHolder()));
}
}
}
private void sendDirtyEntityData() {
- SynchedEntityData synchedentitydata = this.entity.getEntityData();
- List<SynchedEntityData.DataValue<?>> list = synchedentitydata.packDirty();
+ SynchedEntityData datawatcher = this.entity.getEntityData();
+ List<SynchedEntityData.DataValue<?>> list = datawatcher.packDirty();
if (list != null) {
- this.trackedDataValues = synchedentitydata.getNonDefaultValues();
+ this.trackedDataValues = datawatcher.getNonDefaultValues();
this.broadcastAndSend(new ClientboundSetEntityDataPacket(this.entity.getId(), list));
}
@@ -338,6 +384,11 @@
Set<AttributeInstance> set = ((LivingEntity) this.entity).getAttributes().getDirtyAttributes();
if (!set.isEmpty()) {
+ // CraftBukkit start - Send scaled max health
+ if (this.entity instanceof ServerPlayer) {
+ ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(set, false);
+ }
+ // CraftBukkit end
this.broadcastAndSend(new ClientboundUpdateAttributesPacket(this.entity.getId(), set));
}
|