Skip to content

Commit

Permalink
Merge pull request #1481 from SamB440/fix/respawn-attributes-modern
Browse files Browse the repository at this point in the history
Fix some respawn issues for modern clients
  • Loading branch information
AoElite authored May 23, 2024
2 parents 412789b + f1a7d84 commit afb2814
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
* <p>
* Client player is ALWAYS recreated
* <p>
* If the packet has the `KEEP_TRACKED_DATA` flag:
* Sneaking and Sprinting fields are kept on the new client player.
* <p>
* If the packet has the `KEEP_ATTRIBUTES` flag:
* Attributes are kept.
* <p>
* 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) {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
});
}
}
Expand Down

0 comments on commit afb2814

Please sign in to comment.