Skip to content

Commit 76a10c0

Browse files
authored
Spectator-Teleport Implementation (#1825)
1 parent 4ba0975 commit 76a10c0

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

MinecraftClient/ChatBot.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,24 @@ protected bool SelectTrade(int selectedSlot)
13381338
{
13391339
return Handler.SelectTrade(selectedSlot);
13401340
}
1341+
1342+
/// <summary>
1343+
/// Teleport to player in spectator mode
1344+
/// </summary>
1345+
/// <param name="entity">player to teleport to</param>
1346+
protected bool SpectatorTeleport(Entity entity)
1347+
{
1348+
return Handler.Spectate(entity);
1349+
}
1350+
1351+
/// <summary>
1352+
/// Teleport to player/entity in spectator mode
1353+
/// </summary>
1354+
/// <param name="uuid">uuid of entity to teleport to</param>
1355+
protected bool SpectatorTeleport(Guid UUID)
1356+
{
1357+
return Handler.SpectateByUUID(UUID);
1358+
}
13411359

13421360
/// <summary>
13431361
/// Update command block

MinecraftClient/McClient.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,41 @@ public bool UpdateCommandBlock(Location location, string command, CommandBlockMo
16951695
{
16961696
return InvokeOnMainThread(() => handler.UpdateCommandBlock(location, command, mode, flags));
16971697
}
1698+
1699+
/// <summary>
1700+
/// Teleport to player in spectator mode
1701+
/// </summary>
1702+
/// <param name="entity">Player to teleport to</param>
1703+
/// Teleporting to other entityies is NOT implemented yet
1704+
public bool Spectate(Entity entity)
1705+
{
1706+
if(entity.Type == EntityType.Player)
1707+
{
1708+
return SpectateByUUID(entity.UUID);
1709+
}
1710+
else
1711+
{
1712+
return false;
1713+
}
1714+
}
1715+
1716+
/// <summary>
1717+
/// Teleport to player/entity in spectator mode
1718+
/// </summary>
1719+
/// <param name="UUID">UUID of player/entity to teleport to</param>
1720+
public bool SpectateByUUID(Guid UUID)
1721+
{
1722+
if(GetGamemode() == 3)
1723+
{
1724+
if(InvokeRequired)
1725+
return InvokeOnMainThread(() => SpectateByUUID(UUID));
1726+
return handler.SendSpectate(UUID);
1727+
}
1728+
else
1729+
{
1730+
return false;
1731+
}
1732+
}
16981733
#endregion
16991734

17001735
#region Event handlers: An event occurs on the Server

MinecraftClient/Protocol/Handlers/DataTypes.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,16 @@ public byte GetBlockFace(Direction direction)
10471047
}
10481048
}
10491049

1050+
/// <summary>
1051+
/// Get a byte array from the given uuid
1052+
/// </summary>
1053+
/// <param name="uuid">UUID of Player/Entity</param>
1054+
/// <returns>UUID representation</returns>
1055+
public byte[] GetUUID(Guid UUID)
1056+
{
1057+
return UUID.ToBigEndianBytes();
1058+
}
1059+
10501060
/// <summary>
10511061
/// Easily append several byte arrays
10521062
/// </summary>

MinecraftClient/Protocol/Handlers/Protocol16.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,5 +860,10 @@ public bool SelectTrade(int selectedSlot)
860860
{
861861
return false; //MC 1.13+
862862
}
863+
864+
public bool SendSpectate(Guid UUID)
865+
{
866+
return false; //Currently not implemented
867+
}
863868
}
864869
}

MinecraftClient/Protocol/Handlers/Protocol18.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,5 +2057,19 @@ public bool SelectTrade(int selectedSlot)
20572057
}
20582058
else { return false; }
20592059
}
2060+
2061+
public bool SendSpectate(Guid UUID)
2062+
{
2063+
try
2064+
{
2065+
List<byte> packet = new List<byte>();
2066+
packet.AddRange(dataTypes.GetUUID(UUID));
2067+
SendPacket(PacketTypesOut.Spectate, packet);
2068+
return true;
2069+
}
2070+
catch (SocketException) { return false; }
2071+
catch (System.IO.IOException) { return false; }
2072+
catch (ObjectDisposedException) { return false; }
2073+
}
20602074
}
20612075
}

MinecraftClient/Protocol/IMinecraftCom.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ public interface IMinecraftCom : IDisposable, IAutoComplete
236236
/// <param name="selectedSlot">The slot of the trade, starts at 0.</param>
237237
bool SelectTrade(int selectedSlot);
238238

239+
/// <summary>
240+
/// Spectate a player/entity
241+
/// </summary>
242+
/// <param name="uuid">The uuid of the player/entity to spectate/teleport to.</param>
243+
bool SendSpectate(Guid uuid);
244+
239245
/// <summary>
240246
/// Get net read thread (main thread) ID
241247
/// </summary>

0 commit comments

Comments
 (0)