From e93f03bd4e60aebf33f70acf9e462ab283385e37 Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Wed, 1 Apr 2020 18:28:00 +0800 Subject: [PATCH] Add auto repsawn if player was dead --- MinecraftClient/McTcpClient.cs | 23 +++++++++++++++++++ .../Protocol/Handlers/PacketIncomingType.cs | 1 + .../Protocol/Handlers/Protocol18.cs | 7 ++++++ .../Handlers/Protocol18PacketTypes.cs | 1 + .../Protocol/IMinecraftComHandler.cs | 2 ++ MinecraftClient/Settings.cs | 3 +++ 6 files changed, 37 insertions(+) diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 0aabe342..f4596f5f 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -11,6 +11,7 @@ using MinecraftClient.Proxy; using MinecraftClient.Protocol.Handlers.Forge; using MinecraftClient.Mapping; using MinecraftClient.Inventory; +using System.Threading.Tasks; namespace MinecraftClient { @@ -1380,5 +1381,27 @@ namespace MinecraftClient return false; } } + + /// + /// Called when client player's health changed, e.g. getting attack + /// + /// Player current health + public void OnUpdateHealth(float health) + { + if (Settings.AutoRespawn) + { + if (health <= 0) + { + ConsoleIO.WriteLine("Client player dead."); + ConsoleIO.WriteLine("Respawn after 1 second..."); + Task.Factory.StartNew(delegate + { + // wait before respawn + Thread.Sleep(1000); + SendRespawnPacket(); + }); + } + } + } } } diff --git a/MinecraftClient/Protocol/Handlers/PacketIncomingType.cs b/MinecraftClient/Protocol/Handlers/PacketIncomingType.cs index 115958d3..aaecc466 100644 --- a/MinecraftClient/Protocol/Handlers/PacketIncomingType.cs +++ b/MinecraftClient/Protocol/Handlers/PacketIncomingType.cs @@ -41,6 +41,7 @@ namespace MinecraftClient.Protocol.Handlers TimeUpdate, EntityTeleport, EntityStatus, + UpdateHealth, UnknownPacket } } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 6800769f..8ba05ea6 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -725,6 +725,13 @@ namespace MinecraftClient.Protocol.Handlers handler.OnEntityTeleport(EntityID, X, Y, Z, OnGround); } break; + case PacketIncomingType.UpdateHealth: + float health = dataTypes.ReadNextFloat(packetData); + // don't need them + dataTypes.ReadNextVarInt(packetData); + dataTypes.ReadNextFloat(packetData); + handler.OnUpdateHealth(health); + break; default: return false; //Ignored packet } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18PacketTypes.cs b/MinecraftClient/Protocol/Handlers/Protocol18PacketTypes.cs index 032bce34..210da952 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18PacketTypes.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18PacketTypes.cs @@ -277,6 +277,7 @@ namespace MinecraftClient.Protocol.Handlers case 0x59: return PacketIncomingType.EntityProperties; case 0x57: return PacketIncomingType.EntityTeleport; case 0x1C: return PacketIncomingType.EntityStatus; + case 0x49: return PacketIncomingType.UpdateHealth; // TODO: Add backwards support for this packet default: return PacketIncomingType.UnknownPacket; } } diff --git a/MinecraftClient/Protocol/IMinecraftComHandler.cs b/MinecraftClient/Protocol/IMinecraftComHandler.cs index db7f5a02..43baa9c6 100644 --- a/MinecraftClient/Protocol/IMinecraftComHandler.cs +++ b/MinecraftClient/Protocol/IMinecraftComHandler.cs @@ -212,6 +212,8 @@ namespace MinecraftClient.Protocol /// Item (may be null for empty slot) void OnSetSlot(byte inventoryID, short slotID, Item item); + void OnUpdateHealth(float health); + /// /// Called when the Player entity ID has been received from the server /// diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index b693c881..0706cf55 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -96,6 +96,7 @@ namespace MinecraftClient public static bool ResolveSrvRecords = true; public static bool ResolveSrvRecordsShortTimeout = true; public static bool EntityHandling = false; + public static bool AutoRespawn = false; //AntiAFK Settings public static bool AntiAFK_Enabled = false; @@ -240,6 +241,7 @@ namespace MinecraftClient case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break; case "debugmessages": DebugMessages = str2bool(argValue); break; case "enableentityhandling": EntityHandling = str2bool(argValue); break; + case "autorespawn": AutoRespawn = str2bool(argValue); break; case "botowners": Bots_Owners.Clear(); @@ -583,6 +585,7 @@ namespace MinecraftClient + "scriptcache=true # Cache compiled scripts for faster load on low-end devices\r\n" + "timestamps=false # Prepend timestamps to chat messages\r\n" + "enableentityhandling=false # Toggle entities handling\r\n" + + "autorespawn=false # Toggle auto respawn if client player was dead (make sure your spawn point is safe)\r\n" + "\r\n" + "[AppVars]\r\n" + "# yourvar=yourvalue\r\n"