From 76a10c0cd87818fb7dd6362834c2e14f4f843cb9 Mon Sep 17 00:00:00 2001 From: xXjojaXx <73311246+xXjojaXx@users.noreply.github.com> Date: Wed, 17 Nov 2021 17:33:52 +0100 Subject: [PATCH] Spectator-Teleport Implementation (#1825) --- MinecraftClient/ChatBot.cs | 18 ++++++++++ MinecraftClient/McClient.cs | 35 +++++++++++++++++++ .../Protocol/Handlers/DataTypes.cs | 10 ++++++ .../Protocol/Handlers/Protocol16.cs | 5 +++ .../Protocol/Handlers/Protocol18.cs | 14 ++++++++ MinecraftClient/Protocol/IMinecraftCom.cs | 6 ++++ 6 files changed, 88 insertions(+) diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 1e476a7c..f1505ddb 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -1338,6 +1338,24 @@ namespace MinecraftClient { return Handler.SelectTrade(selectedSlot); } + + /// + /// Teleport to player in spectator mode + /// + /// player to teleport to + protected bool SpectatorTeleport(Entity entity) + { + return Handler.Spectate(entity); + } + + /// + /// Teleport to player/entity in spectator mode + /// + /// uuid of entity to teleport to + protected bool SpectatorTeleport(Guid UUID) + { + return Handler.SpectateByUUID(UUID); + } /// /// Update command block diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 22ffdf4a..391bfe07 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -1695,6 +1695,41 @@ namespace MinecraftClient { return InvokeOnMainThread(() => handler.UpdateCommandBlock(location, command, mode, flags)); } + + /// + /// Teleport to player in spectator mode + /// + /// Player to teleport to + /// Teleporting to other entityies is NOT implemented yet + public bool Spectate(Entity entity) + { + if(entity.Type == EntityType.Player) + { + return SpectateByUUID(entity.UUID); + } + else + { + return false; + } + } + + /// + /// Teleport to player/entity in spectator mode + /// + /// UUID of player/entity to teleport to + public bool SpectateByUUID(Guid UUID) + { + if(GetGamemode() == 3) + { + if(InvokeRequired) + return InvokeOnMainThread(() => SpectateByUUID(UUID)); + return handler.SendSpectate(UUID); + } + else + { + return false; + } + } #endregion #region Event handlers: An event occurs on the Server diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index dfe0c194..93c6562e 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -1047,6 +1047,16 @@ namespace MinecraftClient.Protocol.Handlers } } + /// + /// Get a byte array from the given uuid + /// + /// UUID of Player/Entity + /// UUID representation + public byte[] GetUUID(Guid UUID) + { + return UUID.ToBigEndianBytes(); + } + /// /// Easily append several byte arrays /// diff --git a/MinecraftClient/Protocol/Handlers/Protocol16.cs b/MinecraftClient/Protocol/Handlers/Protocol16.cs index aaf5affd..c0b04c33 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol16.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol16.cs @@ -860,5 +860,10 @@ namespace MinecraftClient.Protocol.Handlers { return false; //MC 1.13+ } + + public bool SendSpectate(Guid UUID) + { + return false; //Currently not implemented + } } } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 3fec7721..ade1d959 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -2057,5 +2057,19 @@ namespace MinecraftClient.Protocol.Handlers } else { return false; } } + + public bool SendSpectate(Guid UUID) + { + try + { + List packet = new List(); + packet.AddRange(dataTypes.GetUUID(UUID)); + SendPacket(PacketTypesOut.Spectate, packet); + return true; + } + catch (SocketException) { return false; } + catch (System.IO.IOException) { return false; } + catch (ObjectDisposedException) { return false; } + } } } diff --git a/MinecraftClient/Protocol/IMinecraftCom.cs b/MinecraftClient/Protocol/IMinecraftCom.cs index 42d66fcd..54d19e77 100644 --- a/MinecraftClient/Protocol/IMinecraftCom.cs +++ b/MinecraftClient/Protocol/IMinecraftCom.cs @@ -236,6 +236,12 @@ namespace MinecraftClient.Protocol /// The slot of the trade, starts at 0. bool SelectTrade(int selectedSlot); + /// + /// Spectate a player/entity + /// + /// The uuid of the player/entity to spectate/teleport to. + bool SendSpectate(Guid uuid); + /// /// Get net read thread (main thread) ID ///