From 384c804e549dcd42d3310238943848166f80921e Mon Sep 17 00:00:00 2001 From: CarbonNeuron Date: Fri, 1 May 2020 08:28:22 -0500 Subject: [PATCH] Added Entity Action handling, and A TSneak command that will Toggle Sneak. --- MinecraftClient/ChatBots/AutoLook.cs | 66 +++++++++++++++++++ MinecraftClient/Commands/Sneak.cs | 31 +++++++++ MinecraftClient/McTcpClient.cs | 11 ++++ MinecraftClient/MinecraftClient.csproj | 3 + MinecraftClient/Protocol/Action.cs | 16 +++++ .../Protocol/Handlers/PacketOutgoingType.cs | 3 +- .../Protocol/Handlers/Protocol16.cs | 4 ++ .../Protocol/Handlers/Protocol18.cs | 16 +++++ MinecraftClient/Protocol/IMinecraftCom.cs | 10 ++- MinecraftClient/Settings.cs | 21 +++++- 10 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 MinecraftClient/ChatBots/AutoLook.cs create mode 100644 MinecraftClient/Commands/Sneak.cs create mode 100644 MinecraftClient/Protocol/Action.cs diff --git a/MinecraftClient/ChatBots/AutoLook.cs b/MinecraftClient/ChatBots/AutoLook.cs new file mode 100644 index 00000000..053d63b0 --- /dev/null +++ b/MinecraftClient/ChatBots/AutoLook.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using MinecraftClient.Mapping; + +namespace MinecraftClient.ChatBots +{ + public class AutoLook : ChatBot + { + private Entity _entityToLookAt; + public override void Initialize() + { + if (GetEntityHandlingEnabled() && GetTerrainEnabled()) return; + LogToConsole("Entity Handling or Terrain Handling is not enabled in the config file!"); + LogToConsole("This bot will be unloaded."); + UnloadBot(); + } + + public override void OnEntityDespawn(Entity entity) + { + if (entity == _entityToLookAt) + { + _entityToLookAt = null; + } + } + public override void OnEntitySpawn(Entity entity) + { + HandleEntity(entity); + } + public override void OnEntityMove(Entity entity) + { + var tempBool = HandleEntity(entity); + LogDebugToConsole(tempBool); + if (!tempBool) return; + LookAtLocation(entity.Location); + } + + /// + /// Handles an entity, and tracks it if it is closer then the one we are currently tracking + /// + /// True if found + private bool HandleEntity(Entity entity) + { + if (entity.Type != EntityType.Player) + { + return false; + } + if (_entityToLookAt == null) + { + _entityToLookAt = entity; + return true; + } + if (GetCurrentLocation().Distance(entity.Location) < GetCurrentLocation().Distance(_entityToLookAt.Location)) + { + _entityToLookAt = entity; + return true; + } + + if (entity.ID != _entityToLookAt.ID) return false; + _entityToLookAt = entity; //Handle looking at the same entity + return true; + + } + + } +} \ No newline at end of file diff --git a/MinecraftClient/Commands/Sneak.cs b/MinecraftClient/Commands/Sneak.cs new file mode 100644 index 00000000..ada8b00b --- /dev/null +++ b/MinecraftClient/Commands/Sneak.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftClient.Commands +{ + public class Sneak : Command + { + private bool sneaking = false; + public override string CMDName { get { return "TSneak"; } } + public override string CMDDesc { get { return "Sneak: Toggles sneaking"; } } + + public override string Run(McTcpClient handler, string command, Dictionary localVars) + { + if (sneaking) + { + var result = handler.sendEntityAction(Protocol.ActionType.StopSneaking); + sneaking = false; + return result ? "Success" : "Fail"; + } + else + { + var result = handler.sendEntityAction(Protocol.ActionType.StartSneaking); + sneaking = true; + return result ? "Success" : "Fail"; + } + + } + } +} \ No newline at end of file diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index d95af69d..340883a1 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -170,6 +170,8 @@ namespace MinecraftClient if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); } if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); } if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); } + if (Settings.AutoLook_Enabled) { BotLoad(new ChatBots.AutoLook()); } + //Add your ChatBot here by uncommenting and adapting //BotLoad(new ChatBots.YourBot()); } @@ -1356,6 +1358,7 @@ namespace MinecraftClient } } + /// /// Called when an entity moved over 8 block. /// @@ -1469,6 +1472,14 @@ namespace MinecraftClient playerEntityID = EntityID; } + /// + /// Send the Entity Action packet with the Specified ID + /// + /// TRUE if the item was successfully used + public bool sendEntityAction(ActionType action) + { + return handler.SendEntityAction(playerEntityID, (int) action); + } /// /// Use the item currently in the player's hand /// diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index b13b53a3..84871504 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -78,6 +78,7 @@ + @@ -103,6 +104,7 @@ + @@ -119,6 +121,7 @@ + diff --git a/MinecraftClient/Protocol/Action.cs b/MinecraftClient/Protocol/Action.cs new file mode 100644 index 00000000..9e854f6e --- /dev/null +++ b/MinecraftClient/Protocol/Action.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftClient.Protocol +{ + public enum ActionType + { + StartSneaking, + StopSneaking, + LeaveBed, + StartSprinting, + StopSprinting + } +} \ No newline at end of file diff --git a/MinecraftClient/Protocol/Handlers/PacketOutgoingType.cs b/MinecraftClient/Protocol/Handlers/PacketOutgoingType.cs index 753558fa..5236aada 100644 --- a/MinecraftClient/Protocol/Handlers/PacketOutgoingType.cs +++ b/MinecraftClient/Protocol/Handlers/PacketOutgoingType.cs @@ -17,6 +17,7 @@ namespace MinecraftClient.Protocol.Handlers ClientSettings, PluginMessage, TabComplete, + EntityAction, PlayerPosition, PlayerPositionAndLook, TeleportConfirm, @@ -27,4 +28,4 @@ namespace MinecraftClient.Protocol.Handlers CloseWindow, PlayerBlockPlacement } -} +} \ No newline at end of file diff --git a/MinecraftClient/Protocol/Handlers/Protocol16.cs b/MinecraftClient/Protocol/Handlers/Protocol16.cs index 58b2aa1f..cf28f67c 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol16.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol16.cs @@ -239,6 +239,10 @@ namespace MinecraftClient.Protocol.Handlers } else return ""; } + public bool SendEntityAction(int PlayerEntityID, int ActionID) + { + return true; + } private byte[] readNextByteArray() { diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index fab2c9fc..d63241a1 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -965,6 +965,22 @@ namespace MinecraftClient.Protocol.Handlers catch (System.IO.IOException) { return false; } catch (ObjectDisposedException) { return false; } } + + public bool SendEntityAction(int PlayerEntityID, int ActionID) + { + try + { + List fields = new List(); + fields.AddRange(dataTypes.GetVarInt(PlayerEntityID)); + fields.AddRange(dataTypes.GetVarInt(ActionID)); + fields.AddRange(dataTypes.GetVarInt(0)); + SendPacket(PacketOutgoingType.EntityAction, fields); + return true; + } + catch (SocketException) { return false; } + catch (System.IO.IOException) { return false; } + catch (ObjectDisposedException) { return false; } + } /// /// Send a respawn packet to the server diff --git a/MinecraftClient/Protocol/IMinecraftCom.cs b/MinecraftClient/Protocol/IMinecraftCom.cs index 1b876db1..00e57790 100644 --- a/MinecraftClient/Protocol/IMinecraftCom.cs +++ b/MinecraftClient/Protocol/IMinecraftCom.cs @@ -85,7 +85,15 @@ namespace MinecraftClient.Protocol /// packet Data /// True if message was successfully sent bool SendPluginChannelPacket(string channel, byte[] data); - + + /// + /// Send Entity Action packet to the server. + /// + /// PlayerID + /// Type of packet to send + /// True if packet was successfully sent + bool SendEntityAction(int EntityID, int type); + /// /// Send a held item change packet to the server. /// diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index fca92a25..bebf12bb 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -161,13 +161,16 @@ namespace MinecraftClient //Auto Eating public static bool AutoEat_Enabled = false; public static int AutoEat_hungerThreshold = 6; + + //Auto Looking + public static bool AutoLook_Enabled = false; //Custom app variables and Minecraft accounts private static readonly Dictionary AppVars = new Dictionary(); private static readonly Dictionary> Accounts = new Dictionary>(); private static readonly Dictionary> Servers = new Dictionary>(); - private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing, AutoEat }; + private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing, AutoEat, AutoLook }; /// /// Load settings from the give INI file @@ -211,6 +214,7 @@ namespace MinecraftClient case "autoattack": pMode = ParseMode.AutoAttack; break; case "autofishing": pMode = ParseMode.AutoFishing; break; case "autoeat": pMode = ParseMode.AutoEat; break; + case "autolook": pMode = ParseMode.AutoLook; break; default: pMode = ParseMode.Default; break; } } @@ -466,6 +470,12 @@ namespace MinecraftClient case "matchesfile": AutoRespond_Matches = argValue; break; } break; + case ParseMode.AutoLook: + switch (argName.ToLower()) + { + case "enabled": AutoLook_Enabled = str2bool(argValue); break; + } + break; case ParseMode.AutoAttack: switch (argName.ToLower()) @@ -689,12 +699,17 @@ namespace MinecraftClient + "\r\n" + "[AutoFishing]\r\n" + "# Entity Handling NEED to be enabled first\r\n" - + "enabled=false" + + "enabled=false\r\n" + "\r\n" + "[AutoEat]\r\n" + "# Inventory Handling NEED to be enabled first\r\n" + "enabled=false\r\n" - + "threshold=6", Encoding.UTF8); + + "threshold=6\r\n" + + "\r\n" + + "[AutoLook]\r\n" + + "# Entity Handling AND Terrain Handling NEEDS to be enabled first\r\n" + + "enabled=false" + + "\r\n", Encoding.UTF8); } ///