Skip to content

Commit

Permalink
Merge teleport and ping packet into one
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodlyay committed Feb 29, 2024
1 parent 42d9d82 commit 06a2df9
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions MCGalaxy/Network/ClassicProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,32 +358,37 @@ public override void SendTeleport(byte id, Position pos, Orientation rot) {
}

// NOTE: Classic clients require offseting own entity by 22 units vertically
if (id == Entities.SelfID) pos.Y -= 22;
bool self = id == Entities.SelfID;
if (self) pos.Y -= 22;

Send(Packet.Teleport(id, pos, rot, player.hasExtPositions));
OnTeleported(id, pos, rot);
SendTeleportCore(self, Packet.Teleport(id, pos, rot, player.hasExtPositions), id, pos, rot);
}
public override bool SendTeleport(byte id, Position pos, Orientation rot,
Packet.TeleportMoveMode moveMode, bool usePos = true, bool interpolateOri = false, bool useOri = true) {
if (!Supports(CpeExt.ExtEntityTeleport)) { return false; }

bool absoluteSelf = (moveMode == Packet.TeleportMoveMode.AbsoluteInstant ||
moveMode == Packet.TeleportMoveMode.AbsoluteSmooth) && id == Entities.SelfID;

// NOTE: Classic clients require offseting own entity by 22 units vertically when using absolute location updates
if (absoluteSelf) pos.Y -= 22;
if (absoluteSelf) pos.Y -= 22;

Send(Packet.TeleportExt(id, usePos, moveMode, useOri, interpolateOri, pos, rot, player.hasExtPositions));
if (absoluteSelf) OnTeleported(id, pos, rot);
SendTeleportCore(absoluteSelf, Packet.TeleportExt(id, usePos, moveMode, useOri, interpolateOri, pos, rot, player.hasExtPositions), id, pos, rot);
return true;
}

void OnTeleported(byte id, Position pos, Orientation rot) {
if (id != Entities.SelfID || !hasTwoWayPing) { return; }
void SendTeleportCore(bool absoluteSelf, byte[] packet, byte id, Position pos, Orientation rot) {
if (!absoluteSelf || !hasTwoWayPing) {
Send(packet);
return;
}

byte[] pingPacket = Packet.TwoWayPing(true, Ping.NextTwoWayPingData(true));

ushort data = Ping.NextTwoWayPingData(true);
SendTwoWayPing(data);
byte[] merged = new byte[packet.Length + pingPacket.Length];
Buffer.BlockCopy(packet, 0, merged, 0, packet.Length);
Buffer.BlockCopy(pingPacket, 0, merged, packet.Length, pingPacket.Length);

Send(merged);
//Update server-side position and check MB/portals/zones
player.ProcessMovementCore(pos, rot.RotY, rot.HeadX, false);
}
Expand Down Expand Up @@ -538,15 +543,11 @@ public override void SendMotd(string motd) {

public override void SendPing() {
if (hasTwoWayPing) {
SendTwoWayPing(Ping.NextTwoWayPingData());
Send(Packet.TwoWayPing(true, Ping.NextTwoWayPingData()));
} else {
Send(Packet.Ping());
}
}
void SendTwoWayPing(BlockID data) {
if (!hasTwoWayPing) { return; }
Send(Packet.TwoWayPing(true, data));
}

public override void SendSetSpawnpoint(Position pos, Orientation rot) {
if (Supports(CpeExt.SetSpawnpoint)) {
Expand All @@ -559,29 +560,33 @@ public override void SendSetSpawnpoint(Position pos, Orientation rot) {

public override void SendSpawnEntity(byte id, string name, string skin, Position pos, Orientation rot) {
name = CleanupColors(name);
bool self = id == Entities.SelfID;
// NOTE: Classic clients require offseting own entity by 22 units vertically
if (id == Entities.SelfID) pos.Y -= 22;
if (self) pos.Y -= 22;

// SpawnEntity for self ID behaves differently in Classic 0.0.16a
// - yaw and pitch fields are swapped
// - pitch is inverted
// (other entities do NOT require this adjustment however)
if (id == Entities.SelfID && ProtocolVersion <= Server.VERSION_0016) {
if (self && ProtocolVersion <= Server.VERSION_0016) {
byte temp = rot.HeadX;
rot.HeadX = rot.RotY;
rot.RotY = (byte)(256 - temp);
}


byte[] packet;
if (Supports(CpeExt.ExtPlayerList, 2)) {
Send(Packet.ExtAddEntity2(id, skin, name, pos, rot, player.hasCP437, player.hasExtPositions));
packet = Packet.ExtAddEntity2(id, skin, name, pos, rot, player.hasCP437, player.hasExtPositions);
} else if (player.hasExtList) {
Send(Packet.ExtAddEntity(id, skin, name, player.hasCP437));
Send(Packet.Teleport(id, pos, rot, player.hasExtPositions));
byte[] addEntity = Packet.ExtAddEntity(id, skin, name, player.hasCP437);
byte[] teleport = Packet.Teleport(id, pos, rot, player.hasExtPositions);
packet = new byte[addEntity.Length + teleport.Length];
Buffer.BlockCopy(addEntity, 0, packet, 0, addEntity.Length);
Buffer.BlockCopy(teleport, 0, packet, addEntity.Length, teleport.Length);
} else {
Send(Packet.AddEntity(id, name, pos, rot, player.hasCP437, player.hasExtPositions));
packet = Packet.AddEntity(id, name, pos, rot, player.hasCP437, player.hasExtPositions);
}
OnTeleported(id, pos, rot);
SendTeleportCore(self, packet, id, pos, rot);
}

public override void SendLevel(Level prev, Level level) {
Expand Down

0 comments on commit 06a2df9

Please sign in to comment.