From f1a7d8478e8cbae794057c5cf4aba9933088d4ac Mon Sep 17 00:00:00 2001 From: SamB440 Date: Thu, 23 May 2024 12:29:46 +0100 Subject: [PATCH] Fix some respawn issues for modern clients --- .../events/packets/PacketPlayerRespawn.java | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/java/ac/grim/grimac/events/packets/PacketPlayerRespawn.java b/src/main/java/ac/grim/grimac/events/packets/PacketPlayerRespawn.java index a439b702ed..3c39526e6c 100644 --- a/src/main/java/ac/grim/grimac/events/packets/PacketPlayerRespawn.java +++ b/src/main/java/ac/grim/grimac/events/packets/PacketPlayerRespawn.java @@ -24,12 +24,39 @@ import java.util.List; import java.util.Objects; +/** + * PlayerRespawnS2CPacket info (1.20.2+): + * If the world is different (check via registry key), world is recreated (all entities etc destroyed). + *

+ * Client player is ALWAYS recreated + *

+ * If the packet has the `KEEP_TRACKED_DATA` flag: + * Sneaking and Sprinting fields are kept on the new client player. + *

+ * If the packet has the `KEEP_ATTRIBUTES` flag: + * Attributes are kept. + *

+ * New client player is initialised: + * Pose is set to standing. + * Velocity is set to zero. + * Pitch is set to 0. + * Yaw is set to -180. + */ +// TODO update for 1.20.2- public class PacketPlayerRespawn extends PacketListenerAbstract { public PacketPlayerRespawn() { super(PacketListenerPriority.HIGH); } + private static final byte KEEP_ATTRIBUTES = 1; + private static final byte KEEP_TRACKED_DATA = 2; + private static final byte KEEP_ALL = 3; + + private boolean hasFlag(WrapperPlayServerRespawn respawn, byte flag) { + return (respawn.getKeptData() & flag) != 0; + } + @Override public void onPacketSend(PacketSendEvent event) { if (event.getPacketType() == PacketType.Play.Server.UPDATE_HEALTH) { @@ -101,11 +128,17 @@ public void onPacketSend(PacketSendEvent event) { player.latencyUtils.addRealTimeTask(player.lastTransactionSent.get() + 1, () -> { player.isSneaking = false; player.lastOnGround = false; + player.onGround = false; player.isInBed = false; player.packetStateData.packetPlayerOnGround = false; // If somewhere else pulls last ground to fix other issues player.packetStateData.lastClaimedPosition = new Vector3d(); player.filterMojangStupidityOnMojangStupidity = new Vector3d(); - player.lastSprintingForSpeed = false; // This is reverted even on 1.18 clients + + if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_19_4)) { + player.isSprinting = !this.hasFlag(respawn, KEEP_TRACKED_DATA); + } else { + player.lastSprintingForSpeed = false; + } player.checkManager.getPacketCheck(BadPacketsE.class).handleRespawn(); // Reminder ticks reset @@ -141,6 +174,20 @@ public void onPacketSend(PacketSendEvent event) { if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_17)) { player.compensatedWorld.setDimension(respawn.getDimension(), event.getUser()); } + + // TODO this needs to be done for other client versions as well. And there should probably be some attribute holder that we can just call reset() on. + if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_20_5) && !this.hasFlag(respawn, KEEP_ATTRIBUTES)) { + // Reset attributes if not kept + final PacketEntitySelf self = player.compensatedEntities.getSelf(); + self.gravityAttribute = 0.08d; + self.stepHeight = 0.6f; + self.scale = 1.0f; + self.setJumpStrength(0.42f); + self.setBreakSpeedMultiplier(1.0f); + self.setBlockInteractRange(4.5); + self.setEntityInteractRange(3.0); + player.compensatedEntities.hasSprintingAttributeEnabled = false; + } }); } }