Merge pull request #984 from CarbonNeuron/master

Add Entity Action handling
This commit is contained in:
ORelio 2020-05-03 21:01:53 +02:00 committed by GitHub
commit 43c2b4b73b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 168 additions and 10 deletions

View file

@ -156,6 +156,8 @@ namespace MinecraftClient
/// <param name="entity">Entity with updated location</param> /// <param name="entity">Entity with updated location</param>
public virtual void OnEntityMove(Mapping.Entity entity) { } public virtual void OnEntityMove(Mapping.Entity entity) { }
public virtual void OnInternalCommand(string commandName,string commandParams, string Result) { }
/// <summary> /// <summary>
/// Called when an entity spawned nearby /// Called when an entity spawned nearby
/// </summary> /// </summary>
@ -662,6 +664,32 @@ namespace MinecraftClient
{ {
return Handler.GetInventoryEnabled(); return Handler.GetInventoryEnabled();
} }
public Dictionary<int, Container> GetInventories()
{
return Handler.GetInventories();
}
/// <summary>
/// start Sneaking
/// </summary>
protected bool Sneak(bool on)
{
return SendEntityAction(on ? Protocol.EntityActionType.StartSneaking : Protocol.EntityActionType.StopSneaking);
}
/// <summary>
/// Send Entity Action
/// </summary>
private bool SendEntityAction(Protocol.EntityActionType entityAction)
{
return Handler.sendEntityAction(entityAction);
}
/// <summary>
/// SetSlot
/// </summary>
protected void SetSlot(int slotNum)
{
Handler.ChangeSlot((short) slotNum);
}
/// <summary> /// <summary>
/// Get the current Minecraft World /// Get the current Minecraft World
@ -862,7 +890,7 @@ namespace MinecraftClient
/// Use item currently in the player's hand (active inventory bar slot) /// Use item currently in the player's hand (active inventory bar slot)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
protected bool UseItemOnHand() protected bool UseItemInHand()
{ {
return Handler.UseItemOnHand(); return Handler.UseItemOnHand();
} }

View file

@ -83,7 +83,7 @@ namespace MinecraftClient.ChatBots
} }
} }
} }
if (found) UseItemOnHand(); if (found) UseItemInHand();
return found; return found;
} }
} }

View file

@ -95,7 +95,7 @@ namespace MinecraftClient.ChatBots
{ {
LogToConsole(GetTimestamp() + ": Caught a fish!"); LogToConsole(GetTimestamp() + ": Caught a fish!");
// retract fishing rod // retract fishing rod
UseItemOnHand(); UseItemInHand();
if (inventoryEnabled) if (inventoryEnabled)
{ {
if (!hasFishingRod()) if (!hasFishingRod())
@ -110,7 +110,7 @@ namespace MinecraftClient.ChatBots
// retract fishing rod need some time // retract fishing rod need some time
Thread.Sleep(800); Thread.Sleep(800);
// throw again // throw again
UseItemOnHand(); UseItemInHand();
}); });
} }

View file

@ -12,11 +12,12 @@ namespace MinecraftClient.ChatBots
public class ChatLog : ChatBot public class ChatLog : ChatBot
{ {
public enum MessageFilter { AllText, AllMessages, OnlyChat, OnlyWhispers }; public enum MessageFilter { AllText, AllMessages, OnlyChat, OnlyWhispers, OnlyInternalCommands };
private bool dateandtime; private bool dateandtime;
private bool saveOther = true; private bool saveOther = true;
private bool saveChat = true; private bool saveChat = true;
private bool savePrivate = true; private bool savePrivate = true;
private bool saveInternal = true;
private string logfile; private string logfile;
/// <summary> /// <summary>
@ -52,6 +53,12 @@ namespace MinecraftClient.ChatBots
savePrivate = true; savePrivate = true;
saveChat = false; saveChat = false;
break; break;
case MessageFilter.OnlyInternalCommands:
saveOther = false;
savePrivate = false;
saveChat = false;
saveInternal = true;
break;
} }
if (String.IsNullOrEmpty(file) || file.IndexOfAny(Path.GetInvalidPathChars()) >= 0) if (String.IsNullOrEmpty(file) || file.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
{ {
@ -68,6 +75,7 @@ namespace MinecraftClient.ChatBots
case "messages": return MessageFilter.AllMessages; case "messages": return MessageFilter.AllMessages;
case "chat": return MessageFilter.OnlyChat; case "chat": return MessageFilter.OnlyChat;
case "private": return MessageFilter.OnlyWhispers; case "private": return MessageFilter.OnlyWhispers;
case "internal": return MessageFilter.OnlyInternalCommands;
default: return MessageFilter.AllText; default: return MessageFilter.AllText;
} }
} }
@ -92,6 +100,14 @@ namespace MinecraftClient.ChatBots
} }
} }
public override void OnInternalCommand(string commandName,string commandParams, string result)
{
if (saveInternal)
{
save(string.Format("Internal {0}({1}): {2}", commandName, commandParams, result));
}
}
private void save(string tosave) private void save(string tosave)
{ {
if (dateandtime) if (dateandtime)

View file

@ -0,0 +1,32 @@
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 "Sneak"; } }
public override string CMDDesc { get { return "Sneak: Toggles sneaking"; } }
public override string Run(McTcpClient handler, string command, Dictionary<string, object> localVars)
{
Console.WriteLine(command);
if (sneaking)
{
var result = handler.sendEntityAction(Protocol.EntityActionType.StopSneaking);
sneaking = false;
return result ? "Success" : "Fail";
}
else
{
var result = handler.sendEntityAction(Protocol.EntityActionType.StartSneaking);
sneaking = true;
return result ? "Success" : "Fail";
}
}
}
}

View file

@ -6,6 +6,7 @@ using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.IO; using System.IO;
using System.Net; using System.Net;
using MinecraftClient.ChatBots;
using MinecraftClient.Protocol; using MinecraftClient.Protocol;
using MinecraftClient.Proxy; using MinecraftClient.Proxy;
using MinecraftClient.Protocol.Handlers.Forge; using MinecraftClient.Protocol.Handlers.Forge;
@ -170,6 +171,7 @@ namespace MinecraftClient
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); } if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); }
if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); } if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); }
if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); } if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); }
//Add your ChatBot here by uncommenting and adapting //Add your ChatBot here by uncommenting and adapting
//BotLoad(new ChatBots.YourBot()); //BotLoad(new ChatBots.YourBot());
} }
@ -325,6 +327,7 @@ namespace MinecraftClient
while (true); while (true);
} }
/// <summary> /// <summary>
/// Perform an internal MCC command (not a server command, use SendText() instead for that!) /// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary> /// </summary>
@ -334,6 +337,7 @@ namespace MinecraftClient
/// <returns>TRUE if the command was indeed an internal MCC command</returns> /// <returns>TRUE if the command was indeed an internal MCC command</returns>
public bool PerformInternalCommand(string command, ref string response_msg, Dictionary<string, object> localVars = null) public bool PerformInternalCommand(string command, ref string response_msg, Dictionary<string, object> localVars = null)
{ {
/* Load commands from the 'Commands' namespace */ /* Load commands from the 'Commands' namespace */
if (cmds.Count == 0) if (cmds.Count == 0)
@ -382,12 +386,28 @@ namespace MinecraftClient
else if (cmds.ContainsKey(command_name)) else if (cmds.ContainsKey(command_name))
{ {
response_msg = cmds[command_name].Run(this, command, localVars); response_msg = cmds[command_name].Run(this, command, localVars);
foreach (ChatBot bot in bots.ToArray())
{
try
{
bot.OnInternalCommand(command_name, string.Join(" ",Command.getArgs(command)),response_msg);
}
catch (Exception e)
{
if (!(e is ThreadAbortException))
{
ConsoleIO.WriteLogLine("OnInternalCommand: Got error from " + bot.ToString() + ": " + e.ToString());
}
else throw; //ThreadAbortException should not be caught
}
}
} }
else else
{ {
response_msg = "Unknown command '" + command_name + "'. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help."; response_msg = "Unknown command '" + command_name + "'. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.";
return false; return false;
} }
return true; return true;
} }
@ -1356,6 +1376,7 @@ namespace MinecraftClient
} }
} }
/// <summary> /// <summary>
/// Called when an entity moved over 8 block. /// Called when an entity moved over 8 block.
/// </summary> /// </summary>
@ -1469,6 +1490,14 @@ namespace MinecraftClient
playerEntityID = EntityID; playerEntityID = EntityID;
} }
/// <summary>
/// Send the Entity Action packet with the Specified ID
/// </summary>
/// <returns>TRUE if the item was successfully used</returns>
public bool sendEntityAction(EntityActionType entityAction)
{
return handler.SendEntityAction(playerEntityID, (int) entityAction);
}
/// <summary> /// <summary>
/// Use the item currently in the player's hand /// Use the item currently in the player's hand
/// </summary> /// </summary>

View file

@ -103,6 +103,7 @@
<Compile Include="Commands\Send.cs" /> <Compile Include="Commands\Send.cs" />
<Compile Include="Commands\Set.cs" /> <Compile Include="Commands\Set.cs" />
<Compile Include="Commands\Health.cs" /> <Compile Include="Commands\Health.cs" />
<Compile Include="Commands\Sneak.cs" />
<Compile Include="Commands\UseItem.cs" /> <Compile Include="Commands\UseItem.cs" />
<Compile Include="Inventory\Container.cs" /> <Compile Include="Inventory\Container.cs" />
<Compile Include="Inventory\ContainerType.cs" /> <Compile Include="Inventory\ContainerType.cs" />
@ -119,6 +120,7 @@
<Compile Include="Mapping\Entity.cs" /> <Compile Include="Mapping\Entity.cs" />
<Compile Include="Mapping\EntityType.cs" /> <Compile Include="Mapping\EntityType.cs" />
<Compile Include="Mapping\MaterialExtensions.cs" /> <Compile Include="Mapping\MaterialExtensions.cs" />
<Compile Include="Protocol\EntityActionType.cs" />
<Compile Include="Protocol\Handlers\DataTypes.cs" /> <Compile Include="Protocol\Handlers\DataTypes.cs" />
<Compile Include="Protocol\Handlers\PacketIncomingType.cs" /> <Compile Include="Protocol\Handlers\PacketIncomingType.cs" />
<Compile Include="Protocol\Handlers\PacketOutgoingType.cs" /> <Compile Include="Protocol\Handlers\PacketOutgoingType.cs" />

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Protocol
{
public enum EntityActionType
{
StartSneaking,
StopSneaking,
LeaveBed,
StartSprinting,
StopSprinting
}
}

View file

@ -17,6 +17,7 @@ namespace MinecraftClient.Protocol.Handlers
ClientSettings, ClientSettings,
PluginMessage, PluginMessage,
TabComplete, TabComplete,
EntityAction,
PlayerPosition, PlayerPosition,
PlayerPositionAndLook, PlayerPositionAndLook,
TeleportConfirm, TeleportConfirm,

View file

@ -239,6 +239,10 @@ namespace MinecraftClient.Protocol.Handlers
} }
else return ""; else return "";
} }
public bool SendEntityAction(int PlayerEntityID, int ActionID)
{
return false;
}
private byte[] readNextByteArray() private byte[] readNextByteArray()
{ {

View file

@ -966,6 +966,22 @@ namespace MinecraftClient.Protocol.Handlers
catch (ObjectDisposedException) { return false; } catch (ObjectDisposedException) { return false; }
} }
public bool SendEntityAction(int PlayerEntityID, int ActionID)
{
try
{
List<byte> fields = new List<byte>();
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; }
}
/// <summary> /// <summary>
/// Send a respawn packet to the server /// Send a respawn packet to the server
/// </summary> /// </summary>

View file

@ -310,6 +310,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.TeleportConfirm: throw new InvalidOperationException("Teleport confirm is not supported in protocol " + protocol); case PacketOutgoingType.TeleportConfirm: throw new InvalidOperationException("Teleport confirm is not supported in protocol " + protocol);
case PacketOutgoingType.ClickWindow: return 0x0E; case PacketOutgoingType.ClickWindow: return 0x0E;
case PacketOutgoingType.CloseWindow: return 0x0D; case PacketOutgoingType.CloseWindow: return 0x0D;
case PacketOutgoingType.EntityAction: return 0x0B;
} }
} }
else if (protocol <= Protocol18Handler.MC1112Version) // MC 1.9, 1,10 and 1.11 else if (protocol <= Protocol18Handler.MC1112Version) // MC 1.9, 1,10 and 1.11
@ -330,6 +331,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.InteractEntity: return 0x0A; case PacketOutgoingType.InteractEntity: return 0x0A;
case PacketOutgoingType.ClickWindow: return 0x07; case PacketOutgoingType.ClickWindow: return 0x07;
case PacketOutgoingType.CloseWindow: return 0x08; case PacketOutgoingType.CloseWindow: return 0x08;
case PacketOutgoingType.EntityAction: return 0x0B;
} }
} }
else if (protocol <= Protocol18Handler.MC112Version) // MC 1.12 else if (protocol <= Protocol18Handler.MC112Version) // MC 1.12
@ -350,6 +352,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.InteractEntity: return 0x0B; case PacketOutgoingType.InteractEntity: return 0x0B;
case PacketOutgoingType.ClickWindow: return 0x07; case PacketOutgoingType.ClickWindow: return 0x07;
case PacketOutgoingType.CloseWindow: return 0x08; case PacketOutgoingType.CloseWindow: return 0x08;
case PacketOutgoingType.EntityAction: return 0x0B;
} }
} }
else if (protocol <= Protocol18Handler.MC1122Version) // 1.12.2 else if (protocol <= Protocol18Handler.MC1122Version) // 1.12.2
@ -370,6 +373,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.InteractEntity: return 0x0A; case PacketOutgoingType.InteractEntity: return 0x0A;
case PacketOutgoingType.ClickWindow: return 0x07; case PacketOutgoingType.ClickWindow: return 0x07;
case PacketOutgoingType.CloseWindow: return 0x08; case PacketOutgoingType.CloseWindow: return 0x08;
case PacketOutgoingType.EntityAction: return 0x0B;
} }
} }
else if (protocol < Protocol18Handler.MC114Version) // MC 1.13 to 1.13.2 else if (protocol < Protocol18Handler.MC114Version) // MC 1.13 to 1.13.2
@ -390,6 +394,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.InteractEntity: return 0x0D; case PacketOutgoingType.InteractEntity: return 0x0D;
case PacketOutgoingType.ClickWindow: return 0x08; case PacketOutgoingType.ClickWindow: return 0x08;
case PacketOutgoingType.CloseWindow: return 0x09; case PacketOutgoingType.CloseWindow: return 0x09;
case PacketOutgoingType.EntityAction: return 0x19;
} }
} }
else // MC 1.14 to 1.15 else // MC 1.14 to 1.15
@ -412,6 +417,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.PlayerBlockPlacement: return 0x2C; case PacketOutgoingType.PlayerBlockPlacement: return 0x2C;
case PacketOutgoingType.ClickWindow: return 0x09; case PacketOutgoingType.ClickWindow: return 0x09;
case PacketOutgoingType.CloseWindow: return 0x0A; case PacketOutgoingType.CloseWindow: return 0x0A;
case PacketOutgoingType.EntityAction: return 0x19;
} }
} }

View file

@ -86,6 +86,14 @@ namespace MinecraftClient.Protocol
/// <returns>True if message was successfully sent</returns> /// <returns>True if message was successfully sent</returns>
bool SendPluginChannelPacket(string channel, byte[] data); bool SendPluginChannelPacket(string channel, byte[] data);
/// <summary>
/// Send Entity Action packet to the server.
/// </summary>
/// <param name="entityID">PlayerID</param>
/// <param name="type">Type of packet to send</param>
/// <returns>True if packet was successfully sent</returns>
bool SendEntityAction(int EntityID, int type);
/// <summary> /// <summary>
/// Send a held item change packet to the server. /// Send a held item change packet to the server.
/// </summary> /// </summary>

View file

@ -466,7 +466,6 @@ namespace MinecraftClient
case "matchesfile": AutoRespond_Matches = argValue; break; case "matchesfile": AutoRespond_Matches = argValue; break;
} }
break; break;
case ParseMode.AutoAttack: case ParseMode.AutoAttack:
switch (argName.ToLower()) switch (argName.ToLower())
{ {
@ -689,12 +688,13 @@ namespace MinecraftClient
+ "\r\n" + "\r\n"
+ "[AutoFishing]\r\n" + "[AutoFishing]\r\n"
+ "# Entity Handling NEED to be enabled first\r\n" + "# Entity Handling NEED to be enabled first\r\n"
+ "enabled=false" + "enabled=false\r\n"
+ "\r\n" + "\r\n"
+ "[AutoEat]\r\n" + "[AutoEat]\r\n"
+ "# Inventory Handling NEED to be enabled first\r\n" + "# Inventory Handling NEED to be enabled first\r\n"
+ "enabled=false\r\n" + "enabled=false\r\n"
+ "threshold=6", Encoding.UTF8); + "threshold=6\r\n"
+ "\r\n", Encoding.UTF8);
} }
/// <summary> /// <summary>