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

== AT ==
public net.minecraft.server.level.ChunkMap$TrackedEntity serverEntity
This commit is contained in:
Owen1212055
2022-12-07 17:25:19 -05:00
parent ea24e2c6aa
commit d300c94ec2
13 changed files with 358 additions and 246 deletions

View File

@@ -48,7 +48,7 @@
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.Level;
@@ -192,12 +196,73 @@
@@ -192,11 +196,72 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
@@ -59,7 +59,7 @@
import net.minecraft.world.phys.shapes.VoxelShape;
+import org.bukkit.NamespacedKey;
import org.slf4j.Logger;
+
+// CraftBukkit start
+import io.papermc.paper.adventure.ChatProcessor; // Paper
+import io.papermc.paper.adventure.PaperAdventure; // Paper
@@ -118,10 +118,9 @@
+import org.bukkit.inventory.InventoryView;
+import org.bukkit.inventory.SmithingInventory;
+// CraftBukkit end
+
public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl implements ServerGamePacketListener, ServerPlayerConnection, TickablePacketListener {
static final Logger LOGGER = LogUtils.getLogger();
@@ -212,7 +277,9 @@
private int tickCount;
private int ackBlockChangesUpTo = -1;
@@ -339,7 +338,7 @@
boolean flag1 = entity.verticalCollisionBelow;
if (entity instanceof LivingEntity) {
@@ -449,20 +607,73 @@
@@ -449,19 +607,72 @@
d10 = d6 * d6 + d7 * d7 + d8 * d8;
boolean flag2 = false;
@@ -358,8 +357,8 @@
+ this.player.absMoveTo(d0, d1, d2, this.player.getYRot(), this.player.getXRot()); // CraftBukkit
this.send(ClientboundMoveVehiclePacket.fromEntity(entity));
return;
}
+ }
+
+ // CraftBukkit start - fire PlayerMoveEvent
+ Player player = this.getCraftPlayer();
+ if (!this.hasMoved) {
@@ -408,12 +407,11 @@
+ this.justTeleported = false;
+ return;
+ }
+ }
}
+ // CraftBukkit end
+
this.player.serverLevel().getChunkSource().move(this.player);
entity.recordMovementThroughBlocks(new Vec3(d0, d1, d2), entity.position());
Vec3 vec3d = new Vec3(entity.getX() - d0, entity.getY() - d1, entity.getZ() - d2);
@@ -489,16 +700,17 @@
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
if (packet.getId() == this.awaitingTeleport) {
@@ -1264,7 +1262,7 @@
if (this.player.hasClientLoaded()) {
this.ackBlockChangesUpTo(packet.getSequence());
ServerLevel worldserver = this.player.serverLevel();
@@ -1296,6 +1993,47 @@
@@ -1296,6 +1993,48 @@
this.player.absRotateTo(f, f1);
}
@@ -1301,6 +1299,7 @@
+ }
+
+ if (cancelled) {
+ this.player.resyncUsingItem(this.player); // Paper - Properly cancel usable items
+ this.player.getBukkitEntity().updateInventory(); // SPIGOT-2524
+ return;
+ }
@@ -1312,7 +1311,7 @@
InteractionResult enuminteractionresult = this.player.gameMode.useItem(this.player, worldserver, itemstack, enumhand);
if (enuminteractionresult instanceof InteractionResult.Success) {
@@ -1321,7 +2059,7 @@
@@ -1321,7 +2060,7 @@
Entity entity = packet.getEntity(worldserver);
if (entity != null) {
@@ -1321,7 +1320,7 @@
return;
}
}
@@ -1342,22 +2080,52 @@
@@ -1342,22 +2081,52 @@
@Override
public void onDisconnect(DisconnectionDetails info) {
@@ -1378,7 +1377,7 @@
throw new IllegalArgumentException("Expected packet sequence nr >= 0");
} else {
this.ackBlockChangesUpTo = Math.max(sequence, this.ackBlockChangesUpTo);
@@ -1367,7 +2135,17 @@
@@ -1367,7 +2136,17 @@
@Override
public void handleSetCarriedItem(ServerboundSetCarriedItemPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
@@ -1396,7 +1395,7 @@
if (this.player.getInventory().selected != packet.getSlot() && this.player.getUsedItemHand() == InteractionHand.MAIN_HAND) {
this.player.stopUsingItem();
}
@@ -1376,11 +2154,18 @@
@@ -1376,11 +2155,18 @@
this.player.resetLastActionTime();
} else {
ServerGamePacketListenerImpl.LOGGER.warn("{} tried to set an invalid carried item", this.player.getName().getString());
@@ -1415,7 +1414,7 @@
Optional<LastSeenMessages> optional = this.unpackAndApplyLastSeen(packet.lastSeenMessages());
if (!optional.isEmpty()) {
@@ -1394,27 +2179,46 @@
@@ -1394,27 +2180,46 @@
return;
}
@@ -1469,7 +1468,7 @@
ParseResults<CommandSourceStack> parseresults = this.parseCommand(command);
if (this.server.enforceSecureProfile() && SignableCommand.hasSignableArguments(parseresults)) {
@@ -1431,30 +2235,58 @@
@@ -1431,30 +2236,58 @@
if (!optional.isEmpty()) {
this.tryHandleChat(packet.command(), () -> {
@@ -1534,7 +1533,7 @@
}
private void handleMessageDecodeFailure(SignedMessageChain.DecodeException exception) {
@@ -1530,14 +2362,20 @@
@@ -1530,14 +2363,20 @@
return com_mojang_brigadier_commanddispatcher.parse(command, this.player.createCommandSourceStack());
}
@@ -1560,7 +1559,7 @@
}
}
@@ -1549,7 +2387,7 @@
@@ -1549,7 +2388,7 @@
if (optional.isEmpty()) {
ServerGamePacketListenerImpl.LOGGER.warn("Failed to validate message acknowledgements from {}", this.player.getName().getString());
@@ -1569,7 +1568,7 @@
}
return optional;
@@ -1566,6 +2404,117 @@
@@ -1566,6 +2405,117 @@
return false;
}
@@ -1687,7 +1686,7 @@
private PlayerChatMessage getSignedMessage(ServerboundChatPacket packet, LastSeenMessages lastSeenMessages) throws SignedMessageChain.DecodeException {
SignedMessageBody signedmessagebody = new SignedMessageBody(packet.message(), packet.timeStamp(), packet.salt(), lastSeenMessages);
@@ -1573,15 +2522,44 @@
@@ -1573,15 +2523,44 @@
}
private void broadcastChatMessage(PlayerChatMessage message) {
@@ -1738,7 +1737,7 @@
}
@@ -1592,7 +2570,7 @@
@@ -1592,7 +2571,7 @@
synchronized (this.lastSeenMessages) {
if (!this.lastSeenMessages.applyOffset(packet.offset())) {
ServerGamePacketListenerImpl.LOGGER.warn("Failed to validate message acknowledgements from {}", this.player.getName().getString());
@@ -1747,7 +1746,7 @@
}
}
@@ -1601,7 +2579,40 @@
@@ -1601,7 +2580,40 @@
@Override
public void handleAnimate(ServerboundSwingPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
@@ -1788,7 +1787,7 @@
this.player.swing(packet.getHand());
}
@@ -1609,6 +2620,29 @@
@@ -1609,6 +2621,29 @@
public void handlePlayerCommand(ServerboundPlayerCommandPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
if (this.player.hasClientLoaded()) {
@@ -1818,7 +1817,7 @@
this.player.resetLastActionTime();
Entity entity;
PlayerRideableJumping ijumpable;
@@ -1616,6 +2650,11 @@
@@ -1616,6 +2651,11 @@
switch (packet.getAction()) {
case PRESS_SHIFT_KEY:
this.player.setShiftKeyDown(true);
@@ -1830,7 +1829,7 @@
break;
case RELEASE_SHIFT_KEY:
this.player.setShiftKeyDown(false);
@@ -1684,15 +2723,25 @@
@@ -1684,15 +2724,25 @@
}
if (i > 4096) {
@@ -1857,7 +1856,7 @@
}
public void sendDisguisedChatMessage(Component message, ChatType.Bound params) {
@@ -1703,6 +2752,18 @@
@@ -1703,6 +2753,18 @@
return this.connection.getRemoteAddress();
}
@@ -1876,7 +1875,7 @@
public void switchToConfig() {
this.waitingForSwitchToConfig = true;
this.removePlayerFromWorld();
@@ -1718,9 +2779,17 @@
@@ -1718,9 +2780,17 @@
@Override
public void handleInteract(ServerboundInteractPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
@@ -1894,7 +1893,7 @@
this.player.resetLastActionTime();
this.player.setShiftKeyDown(packet.isUsingSecondaryAction());
@@ -1731,22 +2800,61 @@
@@ -1731,22 +2801,61 @@
AABB axisalignedbb = entity.getBoundingBox();
@@ -1918,7 +1917,7 @@
+
+ // 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();
+ }
+
@@ -1961,7 +1960,7 @@
}
}
@@ -1755,19 +2863,20 @@
@@ -1755,19 +2864,20 @@
@Override
public void onInteraction(InteractionHand hand) {
@@ -1985,7 +1984,7 @@
label23:
{
if (entity instanceof AbstractArrow) {
@@ -1785,17 +2894,41 @@
@@ -1785,17 +2895,41 @@
}
ServerGamePacketListenerImpl.this.player.attack(entity);
@@ -2028,7 +2027,7 @@
}
}
@@ -1809,7 +2942,7 @@
@@ -1809,7 +2943,7 @@
case PERFORM_RESPAWN:
if (this.player.wonGame) {
this.player.wonGame = false;
@@ -2037,7 +2036,7 @@
this.resetPosition();
CriteriaTriggers.CHANGED_DIMENSION.trigger(this.player, Level.END, Level.OVERWORLD);
} else {
@@ -1817,11 +2950,11 @@
@@ -1817,11 +2951,11 @@
return;
}
@@ -2052,7 +2051,7 @@
}
}
break;
@@ -1833,16 +2966,27 @@
@@ -1833,16 +2967,27 @@
@Override
public void handleContainerClose(ServerboundContainerClosePacket packet) {
@@ -2082,7 +2081,7 @@
this.player.containerMenu.sendAllDataToRemote();
} else if (!this.player.containerMenu.stillValid(this.player)) {
ServerGamePacketListenerImpl.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, this.player.containerMenu);
@@ -1855,7 +2999,315 @@
@@ -1855,7 +3000,315 @@
boolean flag = packet.getStateId() != this.player.containerMenu.getStateId();
this.player.containerMenu.suppressRemoteUpdates();
@@ -2399,7 +2398,7 @@
ObjectIterator objectiterator = Int2ObjectMaps.fastIterable(packet.getChangedSlots()).iterator();
while (objectiterator.hasNext()) {
@@ -1879,6 +3331,14 @@
@@ -1879,6 +3332,14 @@
@Override
public void handlePlaceRecipe(ServerboundPlaceRecipePacket packet) {
@@ -2414,7 +2413,7 @@
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
this.player.resetLastActionTime();
if (!this.player.isSpectator() && this.player.containerMenu.containerId == packet.containerId()) {
@@ -1900,9 +3360,43 @@
@@ -1900,9 +3361,43 @@
ServerGamePacketListenerImpl.LOGGER.debug("Player {} tried to place impossible recipe {}", this.player, recipeholder.id().location());
return;
}
@@ -2459,7 +2458,7 @@
if (containerrecipebook_a == RecipeBookMenu.PostPlaceAction.PLACE_GHOST_RECIPE) {
this.player.connection.send(new ClientboundPlaceGhostRecipePacket(this.player.containerMenu.containerId, craftingmanager_d.display().display()));
}
@@ -1917,6 +3411,7 @@
@@ -1917,6 +3412,7 @@
@Override
public void handleContainerButtonClick(ServerboundContainerButtonClickPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
@@ -2467,7 +2466,7 @@
this.player.resetLastActionTime();
if (this.player.containerMenu.containerId == packet.containerId() && !this.player.isSpectator()) {
if (!this.player.containerMenu.stillValid(this.player)) {
@@ -1945,7 +3440,44 @@
@@ -1945,7 +3441,44 @@
boolean flag1 = packet.slotNum() >= 1 && packet.slotNum() <= 45;
boolean flag2 = itemstack.isEmpty() || itemstack.getCount() <= itemstack.getMaxStackSize();
@@ -2512,7 +2511,7 @@
if (flag1 && flag2) {
this.player.inventoryMenu.getSlot(packet.slotNum()).setByPlayer(itemstack);
this.player.inventoryMenu.setRemoteSlot(packet.slotNum(), itemstack);
@@ -1964,7 +3496,19 @@
@@ -1964,7 +3497,19 @@
@Override
public void handleSignUpdate(ServerboundSignUpdatePacket packet) {
@@ -2533,7 +2532,7 @@
this.filterTextPacket(list).thenAcceptAsync((list1) -> {
this.updateSignText(packet, list1);
@@ -1972,6 +3516,7 @@
@@ -1972,6 +3517,7 @@
}
private void updateSignText(ServerboundSignUpdatePacket packet, List<FilteredText> signText) {
@@ -2541,7 +2540,7 @@
this.player.resetLastActionTime();
ServerLevel worldserver = this.player.serverLevel();
BlockPos blockposition = packet.getPos();
@@ -1993,15 +3538,33 @@
@@ -1993,15 +3539,33 @@
@Override
public void handlePlayerAbilities(ServerboundPlayerAbilitiesPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
@@ -2576,7 +2575,7 @@
if (this.player.isModelPartShown(PlayerModelPart.HAT) != flag) {
this.server.getPlayerList().broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_HAT, this.player));
}
@@ -2012,7 +3575,7 @@
@@ -2012,7 +3576,7 @@
public void handleChangeDifficulty(ServerboundChangeDifficultyPacket packet) {
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
if (this.player.hasPermissions(2) || this.isSingleplayerOwner()) {
@@ -2585,7 +2584,7 @@
}
}
@@ -2033,7 +3596,7 @@
@@ -2033,7 +3597,7 @@
if (!Objects.equals(profilepublickey_a, profilepublickey_a1)) {
if (profilepublickey_a != null && profilepublickey_a1.expiresAt().isBefore(profilepublickey_a.expiresAt())) {
@@ -2594,7 +2593,7 @@
} else {
try {
SignatureValidator signaturevalidator = this.server.getProfileKeySignatureValidator();
@@ -2045,8 +3608,8 @@
@@ -2045,8 +3609,8 @@
this.resetPlayerChatState(remotechatsession_a.validate(this.player.getGameProfile(), signaturevalidator));
} catch (ProfilePublicKey.ValidationException profilepublickey_b) {
@@ -2605,7 +2604,7 @@
}
}
@@ -2058,7 +3621,7 @@
@@ -2058,7 +3622,7 @@
if (!this.waitingForSwitchToConfig) {
throw new IllegalStateException("Client acknowledged config, but none was requested");
} else {
@@ -2614,7 +2613,7 @@
}
}
@@ -2076,15 +3639,18 @@
@@ -2076,15 +3640,18 @@
private void resetPlayerChatState(RemoteChatSession session) {
this.chatSession = session;
@@ -2636,7 +2635,7 @@
@Override
public void handleClientTickEnd(ServerboundClientTickEndPacket packet) {
@@ -2115,4 +3681,17 @@
@@ -2115,4 +3682,17 @@
InteractionResult run(ServerPlayer player, Entity entity, InteractionHand hand);
}