Skip to content

Commit

Permalink
Add ExtEntityTeleport CPE
Browse files Browse the repository at this point in the history
TODO: check if SendPosition should be used for respawn and ride
  • Loading branch information
Goodlyay committed Oct 30, 2022
1 parent 374e853 commit fe4e758
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/other/CmdTp.cs
Expand Up @@ -68,7 +68,7 @@ public sealed class CmdTp : Command2 {
Position pos = bot != null ? bot.Pos : target.Pos;
Orientation rot = bot != null ? bot.Rot : target.Rot;
p.BlockUntilLoad(10); //Wait for player to spawn in new map
p.SendPos(Entities.SelfID, pos, rot);
p.SendPosition(pos, rot);
}

internal static bool GetTeleportCoords(Player p, Entity ori, string[] args, bool precise,
Expand Down Expand Up @@ -105,7 +105,7 @@ public sealed class CmdTp : Command2 {
if (!GetTeleportCoords(p, p, args, precise, out pos, out yaw, out pitch)) return;

SavePreTeleportState(p);
p.SendPos(Entities.SelfID, pos, new Orientation(yaw, pitch));
p.SendPosition(pos, new Orientation(yaw, pitch));
}

static void SavePreTeleportState(Player p) {
Expand Down
2 changes: 2 additions & 0 deletions MCGalaxy/Network/CPESupport.cs
Expand Up @@ -91,6 +91,7 @@ public class CpeExt
public const string CustomParticles = "CustomParticles";
public const string CustomModels = "CustomModels";
public const string PluginMessages = "PluginMessages";
public const string ExtEntityTeleport = "ExtEntityTeleport";
}

public sealed class CpeExtension
Expand Down Expand Up @@ -149,6 +150,7 @@ public sealed class CpeExtension
new CpeExtension(CpeExt.CustomParticles, "Allows defining and spawning custom particles"),
new CpeExtension(CpeExt.CustomModels, "Allows defining custom models for entities", 2),
new CpeExtension(CpeExt.PluginMessages, "Allows sending and receiving plugin messages from clients"),
new CpeExtension(CpeExt.ExtEntityTeleport, "Allows sending more precisely controlled teleports"),
#if TEN_BIT_BLOCKS
new CpeExtension(CpeExt.ExtBlocks, "Allows using block IDs over 255 in block definitions"),
#endif
Expand Down
11 changes: 11 additions & 0 deletions MCGalaxy/Network/ClassicProtocol.cs
Expand Up @@ -359,6 +359,17 @@ public class ClassicProtocol : IGameSession

Send(Packet.Teleport(id, pos, rot, player.hasExtPositions));
}
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)) { player.Message("You dont SUPPORT"); return false; }

// NOTE: Classic clients require offseting own entity by 22 units vertically when using absolute location updates
if ((moveMode == Packet.TeleportMoveMode.AbsoluteInstant || moveMode == Packet.TeleportMoveMode.AbsoluteSmooth) && id == Entities.SelfID)
{ pos.Y -= 22; }

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

public override void SendRemoveEntity(byte id) {
Send(Packet.RemoveEntity(id));
Expand Down
3 changes: 3 additions & 0 deletions MCGalaxy/Network/IGameSession.cs
Expand Up @@ -82,6 +82,9 @@ public abstract class IGameSession : INetProtocol

/// <summary> Sends an entity teleport (absolute location update) packet to the client </summary>
public abstract void SendTeleport(byte id, Position pos, Orientation rot);
/// <summary> Sends an ext entity teleport with more control over behavior </summary>
public virtual bool SendTeleport(byte id, Position pos, Orientation rot,
Packet.TeleportMoveMode moveMode, bool usePos = true, bool interpolateOri = false, bool useOri = true) { return false; }
/// <summary> Sends a spawn/add entity packet to the client </summary>
public abstract void SendSpawnEntity(byte id, string name, string skin, Position pos, Orientation rot);
/// <summary> Sends a despawn/remove entity to the client </summary>
Expand Down
1 change: 1 addition & 0 deletions MCGalaxy/Network/Packets/Opcode.cs
Expand Up @@ -77,5 +77,6 @@ public static class Opcode {
public const byte CpeDefineModelPart = 51;
public const byte CpeUndefineModel = 52;
public const byte CpePluginMessage = 53;
public const byte CpeEntityTeleportExt = 54;
}
}
25 changes: 22 additions & 3 deletions MCGalaxy/Network/Packets/Packet.cs
Expand Up @@ -651,11 +651,30 @@ public static class Packet
return buffer;
}

public enum TeleportMoveMode { AbsoluteInstant, AbsoluteSmooth, RelativeSmooth, RelativeShift }
public static byte[] TeleportExt(byte entityID, bool usePos, TeleportMoveMode moveMode, bool useOri, bool interpolateOri,
Position pos, Orientation rot, bool extPos) {
byte flags = 0;
if (usePos) { flags |= 1; }
flags |= (byte)((byte)moveMode << 1);
if (useOri) { flags |= 16; }
if (interpolateOri) { flags |= 32; }

byte[] buffer = new byte[11 + (extPos ? 6 : 0)];
buffer[0] = Opcode.CpeEntityTeleportExt;
buffer[1] = entityID;
buffer[2] = flags;

int offset = NetUtils.WritePos(pos, buffer, 3, extPos);
buffer[3 + offset] = rot.RotY;
buffer[4 + offset] = rot.HeadX;
return buffer;
}
#endregion


#region Block definitions

public static byte[] DefineBlock(BlockDefinition def, bool hasCP437,
bool extBlocks, bool extTexs) {
byte[] buffer = new byte[(extBlocks ? 81 : 80) + (extTexs ? 3 : 0)];
Expand Down
7 changes: 4 additions & 3 deletions MCGalaxy/Network/Player.Networking.cs
Expand Up @@ -123,11 +123,12 @@ public partial class Player : IDisposable
}
Session.SendTeleport(id, pos, rot);
}

/// <summary> Sends a packet indicating an absolute position + orientation change for this player. </summary>
public void SendPosition(Position pos, Orientation rot) {
Session.SendTeleport(Entities.SelfID, pos, rot);

if (!Session.SendTeleport(Entities.SelfID, pos, rot, Packet.TeleportMoveMode.AbsoluteInstant)) {
Session.SendTeleport(Entities.SelfID, pos, rot);
}
// when frozen, position updates from the client are ignored
if (frozen) Pos = pos;
}
Expand Down

0 comments on commit fe4e758

Please sign in to comment.