aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0987-Properly-resend-entities.patch
blob: 5fae130fa569faec8dca4df55352da99e95b32d4 (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com>
Date: Wed, 7 Dec 2022 17:25:19 -0500
Subject: [PATCH] Properly resend entities

This resolves some issues which caused entities to not be resent correctly.
Entities that are interacted with need to be resent to the client, so we resend all the entity
data to the player whilst making sure not to clear dirty entries from the tracker. This makes
sure that values will be correctly updated to other players.

This also adds utilities to aid in further preventing entity desyncs.

This also also fixes the bug causing cancelling PlayerInteractEvent to cause items to continue
to be used despite being cancelled on the server.

For example, items being consumed but never finishing, shields being put up, etc.
The underlying issue of this is that the client modifies their synced data values,
and so we have to (forcibly) resend them in order for the client to reset their using item state.

See: https://github.com/PaperMC/Paper/pull/1896

Feature patch

== AT ==
public net.minecraft.server.level.ChunkMap$TrackedEntity serverEntity

diff --git a/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java b/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java
index 02bf2705ca1c99023a83a22d92e1962181102297..0f99733660f91280e4c6262cf75b3c9cae86f65a 100644
--- a/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java
+++ b/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java
@@ -50,7 +50,7 @@ public class SynchedEntityData {
         }
     }
 
-    private <T> SynchedEntityData.DataItem<T> getItem(EntityDataAccessor<T> key) {
+    public <T> SynchedEntityData.DataItem<T> getItem(EntityDataAccessor<T> key) { // Paper - public
         return (SynchedEntityData.DataItem<T>) this.itemsById[key.id()]; // CraftBukkit - decompile error
     }
 
@@ -151,6 +151,20 @@ public class SynchedEntityData {
         }
     }
 
+    // Paper start
+    // We need to pack all as we cannot rely on "non default values" or "dirty" ones.
+    // Because these values can possibly be desynced on the client.
+    @Nullable
+    public List<SynchedEntityData.DataValue<?>> packAll() {
+        final List<SynchedEntityData.DataValue<?>> list = new ArrayList<>();
+        for (final DataItem<?> dataItem : this.itemsById) {
+            list.add(dataItem.value());
+        }
+
+        return list;
+    }
+    // Paper end
+
     public static class DataItem<T> {
 
         final EntityDataAccessor<T> accessor;
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
index f2dd272a01b4e946a6746865d55ebc9861f8361b..5d189ba60d40f5c42b2dacc339594ed067418e95 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
@@ -567,6 +567,7 @@ public class ServerPlayerGameMode {
             }
             // Paper end - extend Player Interact cancellation
             player.getBukkitEntity().updateInventory(); // SPIGOT-2867
+            this.player.resyncUsingItem(this.player); // Paper - Properly cancel usable items
             return (event.useItemInHand() != Event.Result.ALLOW) ? InteractionResult.SUCCESS : InteractionResult.PASS;
         } else if (this.gameModeForPlayer == GameType.SPECTATOR) {
             MenuProvider itileinventory = iblockdata.getMenuProvider(world, blockposition);
@@ -618,6 +619,11 @@ public class ServerPlayerGameMode {
 
                 return enuminteractionresult;
             } else {
+                // Paper start - Properly cancel usable items; Cancel only if cancelled + if the interact result is different from default response
+                if (this.interactResult && this.interactResult != cancelledItem) {
+                    this.player.resyncUsingItem(this.player);
+                }
+                // Paper end - Properly cancel usable items
                 return InteractionResult.PASS;
             }
         }
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index d99ad1200bfd2621d21184b0f106a255ba009ba6..0319ec3689f2fdb23bdcfa6f93e9a471b42c04b0 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2025,6 +2025,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
                 }
 
                 if (cancelled) {
+                this.player.resyncUsingItem(this.player); // Paper - Properly cancel usable items
                     this.player.getBukkitEntity().updateInventory(); // SPIGOT-2524
                     return;
                 }
@@ -2803,7 +2804,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
 
                                 // Entity in bucket - SPIGOT-4048 and SPIGOT-6859a
                                 if ((entity instanceof Bucketable && entity instanceof LivingEntity && origItem != null && origItem.asItem() == Items.WATER_BUCKET) && (event.isCancelled() || ServerGamePacketListenerImpl.this.player.getInventory().getSelected() == null || ServerGamePacketListenerImpl.this.player.getInventory().getSelected().getItem() != origItem)) {
-                                    entity.getBukkitEntity().update(ServerGamePacketListenerImpl.this.player);
+                                entity.resendPossiblyDesyncedEntityData(ServerGamePacketListenerImpl.this.player); // Paper - The entire mob gets deleted, so resend it
                                     ServerGamePacketListenerImpl.this.player.containerMenu.sendAllDataToRemote();
                                 }
 
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index b451842d6848af883cc3abf200f03c9a4680106c..88299abf563a041ade1683b66b43103b0eeeea0d 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -396,7 +396,7 @@ public abstract class PlayerList {
         ((ServerLevel)player.level()).getChunkSource().chunkMap.addEntity(player); // Paper - Fire PlayerJoinEvent when Player is actually ready; track entity now
         // CraftBukkit end
 
-        player.refreshEntityData(player); // CraftBukkit - BungeeCord#2321, send complete data to self on spawn
+        //player.refreshEntityData(player); // CraftBukkit - BungeeCord#2321, send complete data to self on spawn // Paper - THIS IS NOT NEEDED ANYMORE
 
         this.sendLevelInfo(player, worldserver1);
 
@@ -907,12 +907,17 @@ public abstract class PlayerList {
     }
 
     public void sendActiveEffects(LivingEntity entity, ServerGamePacketListenerImpl networkHandler) {
+        // Paper start - collect packets
+        this.sendActiveEffects(entity, networkHandler::send);
+    }
+    public void sendActiveEffects(LivingEntity entity, java.util.function.Consumer<Packet<? super net.minecraft.network.protocol.game.ClientGamePacketListener>> packetConsumer) {
+        // Paper end - collect packets
         Iterator iterator = entity.getActiveEffects().iterator();
 
         while (iterator.hasNext()) {
             MobEffectInstance mobeffect = (MobEffectInstance) iterator.next();
 
-            networkHandler.send(new ClientboundUpdateMobEffectPacket(entity.getId(), mobeffect, false));
+            packetConsumer.accept(new ClientboundUpdateMobEffectPacket(entity.getId(), mobeffect, false)); // Paper - collect packets
         }
 
     }
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 4e2754d28fc387987a191f28910f3bbefa36e724..1167e3a378bf2f83848e3a732d407d6e5e689743 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -599,13 +599,45 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
 
     // CraftBukkit start
     public void refreshEntityData(ServerPlayer to) {
-        List<SynchedEntityData.DataValue<?>> list = this.getEntityData().getNonDefaultValues();
+        List<SynchedEntityData.DataValue<?>> list = this.entityData.packAll(); // Paper - Update EVERYTHING not just not default
 
-        if (list != null) {
+        if (list != null && to.getBukkitEntity().canSee(this.getBukkitEntity())) { // Paper
             to.connection.send(new ClientboundSetEntityDataPacket(this.getId(), list));
         }
     }
     // CraftBukkit end
+    // Paper start
+    // This method should only be used if the data of an entity could have become desynced
+    // due to interactions on the client.
+    public void resendPossiblyDesyncedEntityData(net.minecraft.server.level.ServerPlayer player) {
+        if (player.getBukkitEntity().canSee(this.getBukkitEntity())) {
+            ServerLevel world = (net.minecraft.server.level.ServerLevel)this.level();
+            net.minecraft.server.level.ChunkMap.TrackedEntity tracker = world == null ? null : world.getChunkSource().chunkMap.entityMap.get(this.getId());
+            if (tracker == null) {
+                return;
+            }
+            final net.minecraft.server.level.ServerEntity serverEntity = tracker.serverEntity;
+            final List<net.minecraft.network.protocol.Packet<? super net.minecraft.network.protocol.game.ClientGamePacketListener>> list = new java.util.ArrayList<>();
+            serverEntity.sendPairingData(player, list::add);
+            player.connection.send(new net.minecraft.network.protocol.game.ClientboundBundlePacket(list));
+        }
+    }
+
+    // This method allows you to specifically resend certain data accessor keys to the client
+    public void resendPossiblyDesyncedDataValues(List<EntityDataAccessor<?>> keys, ServerPlayer to) {
+        if (!to.getBukkitEntity().canSee(this.getBukkitEntity())) {
+            return;
+        }
+
+        final List<SynchedEntityData.DataValue<?>> values = new java.util.ArrayList<>(keys.size());
+        for (final EntityDataAccessor<?> key : keys) {
+            final SynchedEntityData.DataItem<?> synchedValue = this.entityData.getItem(key);
+            values.add(synchedValue.value());
+        }
+
+        to.connection.send(new ClientboundSetEntityDataPacket(this.id, values));
+    }
+    // Paper end
 
     public boolean equals(Object object) {
         return object instanceof Entity ? ((Entity) object).id == this.id : false;
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 16fdc55c398f1ce07fbcb09c386c7f3703c9bd68..11f2176ce70fd2e0c323906d39fac95b75e10551 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -4037,6 +4037,11 @@ public abstract class LivingEntity extends Entity implements Attackable {
         return ((Byte) this.entityData.get(LivingEntity.DATA_LIVING_ENTITY_FLAGS) & 2) > 0 ? InteractionHand.OFF_HAND : InteractionHand.MAIN_HAND;
     }
 
+    // Paper start - Properly cancel usable items
+    public void resyncUsingItem(ServerPlayer serverPlayer) {
+        this.resendPossiblyDesyncedDataValues(java.util.List.of(DATA_LIVING_ENTITY_FLAGS), serverPlayer);
+    }
+    // Paper end - Properly cancel usable items
     private void updatingUsingItem() {
         if (this.isUsingItem()) {
             if (ItemStack.isSameItem(this.getItemInHand(this.getUsedItemHand()), this.useItem)) {
diff --git a/src/main/java/net/minecraft/world/entity/animal/Bucketable.java b/src/main/java/net/minecraft/world/entity/animal/Bucketable.java
index 5a12f4c1de2d020e84af933d491397b38d227824..4eca5996a867086be22d22d99db81ab001467516 100644
--- a/src/main/java/net/minecraft/world/entity/animal/Bucketable.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Bucketable.java
@@ -108,8 +108,7 @@ public interface Bucketable {
             itemstack1 = CraftItemStack.asNMSCopy(playerBucketFishEvent.getEntityBucket());
             if (playerBucketFishEvent.isCancelled()) {
                 ((ServerPlayer) player).containerMenu.sendAllDataToRemote(); // We need to update inventory to resync client's bucket
-                entity.getBukkitEntity().update((ServerPlayer) player); // We need to play out these packets as the client assumes the fish is gone
-                entity.refreshEntityData((ServerPlayer) player); // Need to send data such as the display name to client
+                entity.resendPossiblyDesyncedEntityData((ServerPlayer) player); // Paper
                 return Optional.of(InteractionResult.FAIL);
             }
             entity.playSound(((Bucketable) entity).getPickupSound(), 1.0F, 1.0F);
diff --git a/src/main/java/net/minecraft/world/item/component/Consumable.java b/src/main/java/net/minecraft/world/item/component/Consumable.java
index fe8618451e3a3f5185704f791723f7897870b6f2..6be5e7b0ce975702ae7c337a06faa59ff3414d64 100644
--- a/src/main/java/net/minecraft/world/item/component/Consumable.java
+++ b/src/main/java/net/minecraft/world/item/component/Consumable.java
@@ -97,10 +97,12 @@ public record Consumable(float consumeSeconds, ItemUseAnimation animation, Holde
 
     // CraftBukkit start
     public void cancelUsingItem(net.minecraft.server.level.ServerPlayer entityplayer, ItemStack itemstack) {
+        final java.util.List<net.minecraft.network.protocol.Packet<? super net.minecraft.network.protocol.game.ClientGamePacketListener>> packets = new it.unimi.dsi.fastutil.objects.ObjectArrayList<>(); // Paper - properly resend entities - collect packets for bundle
         itemstack.getAllOfType(ConsumableListener.class).forEach((consumablelistener) -> {
-            consumablelistener.cancelUsingItem(entityplayer, itemstack);
+            consumablelistener.cancelUsingItem(entityplayer, itemstack, packets); // Paper - properly resend entities - collect packets for bundle
         });
-        entityplayer.server.getPlayerList().sendActivePlayerEffects(entityplayer);
+        entityplayer.server.getPlayerList().sendActiveEffects(entityplayer, packets::add); // Paper - properly resend entities - collect packets for bundle
+        entityplayer.connection.send(new net.minecraft.network.protocol.game.ClientboundBundlePacket(packets));
     }
     // CraftBukkit end
 
diff --git a/src/main/java/net/minecraft/world/item/component/ConsumableListener.java b/src/main/java/net/minecraft/world/item/component/ConsumableListener.java
index 03ff8a5aa5083d87d77b68371557cf4e97003114..c66475c7e20b5752dcc9263e44f541f9f2b8e6de 100644
--- a/src/main/java/net/minecraft/world/item/component/ConsumableListener.java
+++ b/src/main/java/net/minecraft/world/item/component/ConsumableListener.java
@@ -8,5 +8,5 @@ public interface ConsumableListener {
 
     void onConsume(Level world, LivingEntity user, ItemStack stack, Consumable consumable);
 
-    default void cancelUsingItem(net.minecraft.server.level.ServerPlayer entityplayer, ItemStack itemstack) {} // CraftBukkit
+    default void cancelUsingItem(net.minecraft.server.level.ServerPlayer entityplayer, ItemStack itemstack, java.util.List<net.minecraft.network.protocol.Packet<? super net.minecraft.network.protocol.game.ClientGamePacketListener>> collectedPackets) {} // CraftBukkit // Paper - properly resend entities - collect packets for bundle
 }
diff --git a/src/main/java/net/minecraft/world/item/component/OminousBottleAmplifier.java b/src/main/java/net/minecraft/world/item/component/OminousBottleAmplifier.java
index 202d3c4741f3f6468a09bfd8e661b9823a332ea7..6b5723b817e48a4c231014f28e45b20754c2c090 100644
--- a/src/main/java/net/minecraft/world/item/component/OminousBottleAmplifier.java
+++ b/src/main/java/net/minecraft/world/item/component/OminousBottleAmplifier.java
@@ -28,8 +28,14 @@ public record OminousBottleAmplifier(int value) implements ConsumableListener, T
 
     @Override
     public void onConsume(Level world, LivingEntity user, ItemStack stack, Consumable consumable) {
-        user.addEffect(new MobEffectInstance(MobEffects.BAD_OMEN, 120000, this.value, false, false, true));
+        user.addEffect(new MobEffectInstance(MobEffects.BAD_OMEN, 120000, this.value, false, false, true)); // Paper - properly resend entities - diff on change for below
     }
+    // Paper start - properly resend entities - collect packets for bundle
+    @Override
+    public void cancelUsingItem(net.minecraft.server.level.ServerPlayer entityplayer, ItemStack itemstack, java.util.List<net.minecraft.network.protocol.Packet<? super net.minecraft.network.protocol.game.ClientGamePacketListener>> collectedPackets) {
+        collectedPackets.add(new net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket(entityplayer.getId(), MobEffects.BAD_OMEN));
+    }
+    // Paper end - properly resend entities - collect packets for bundle
 
     @Override
     public void addToTooltip(Item.TooltipContext context, Consumer<Component> tooltip, TooltipFlag type) {
diff --git a/src/main/java/net/minecraft/world/item/component/SuspiciousStewEffects.java b/src/main/java/net/minecraft/world/item/component/SuspiciousStewEffects.java
index 04760d8ba7c560bd9d11191c666715ae8c3e4bff..021169d709964b1bb65e49bf3fcf3119f0749448 100644
--- a/src/main/java/net/minecraft/world/item/component/SuspiciousStewEffects.java
+++ b/src/main/java/net/minecraft/world/item/component/SuspiciousStewEffects.java
@@ -46,9 +46,9 @@ public record SuspiciousStewEffects(List<SuspiciousStewEffects.Entry> effects) i
 
     // CraftBukkit start
     @Override
-    public void cancelUsingItem(net.minecraft.server.level.ServerPlayer entityplayer, ItemStack itemstack) {
+    public void cancelUsingItem(net.minecraft.server.level.ServerPlayer entityplayer, ItemStack itemstack, java.util.List<net.minecraft.network.protocol.Packet<? super net.minecraft.network.protocol.game.ClientGamePacketListener>> collectedPackets) { // Paper - properly resend entities - collect packets for bundle
         for (SuspiciousStewEffects.Entry suspicioussteweffects_a : this.effects) {
-            entityplayer.connection.send(new net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket(entityplayer.getId(), suspicioussteweffects_a.effect()));
+            collectedPackets.add(new net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket(entityplayer.getId(), suspicioussteweffects_a.effect())); // Paper - bundlize packets
         }
     }
     // CraftBukkit end
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
index 2b1f3e27ce5cecc94297c27debe5e3bd64c08967..d1d9916db7bd4886d30355e6a0ecb69cd21c2364 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -1008,7 +1008,11 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
             return;
         }
 
-        entityTracker.broadcast(this.getHandle().getAddEntityPacket(entityTracker.serverEntity));
+        // Paper start - resend possibly desynced entity instead of add entity packet
+        for (final ServerPlayerConnection connection : entityTracker.seenBy) {
+            this.getHandle().resendPossiblyDesyncedEntityData(connection.getPlayer());
+        }
+        // Paper end - resend possibly desynced entity instead of add entity packet
     }
 
     public void update(ServerPlayer player) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java
index f3a9b3380246fb2dd4b60a8d1a94c5dfed98d316..350ad61ab3fe66abd528e353b431a4a6dac17506 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java
@@ -39,9 +39,11 @@ public class CraftItemFrame extends CraftHanging implements ItemFrame {
     protected void update() {
         super.update();
 
+        // Paper start, don't mark as dirty as this is handled in super.update()
         // mark dirty, so that the client gets updated with item and rotation
-        this.getHandle().getEntityData().markDirty(net.minecraft.world.entity.decoration.ItemFrame.DATA_ITEM);
-        this.getHandle().getEntityData().markDirty(net.minecraft.world.entity.decoration.ItemFrame.DATA_ROTATION);
+        //this.getHandle().getEntityData().markDirty(net.minecraft.world.entity.decoration.ItemFrame.DATA_ITEM);
+        //this.getHandle().getEntityData().markDirty(net.minecraft.world.entity.decoration.ItemFrame.DATA_ROTATION);
+        // Paper end
 
         // update redstone
         if (!this.getHandle().generation) {