Skip to content

Commit 43c2b4b

Browse files
authored
Merge pull request #984 from CarbonNeuron/master
Add Entity Action handling
2 parents 9dae401 + 5a0f106 commit 43c2b4b

File tree

14 files changed

+168
-10
lines changed

14 files changed

+168
-10
lines changed

MinecraftClient/ChatBot.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public virtual void OnServerTpsUpdate(Double tps) { }
156156
/// <param name="entity">Entity with updated location</param>
157157
public virtual void OnEntityMove(Mapping.Entity entity) { }
158158

159+
public virtual void OnInternalCommand(string commandName,string commandParams, string Result) { }
160+
159161
/// <summary>
160162
/// Called when an entity spawned nearby
161163
/// </summary>
@@ -662,6 +664,32 @@ public bool GetInventoryEnabled()
662664
{
663665
return Handler.GetInventoryEnabled();
664666
}
667+
public Dictionary<int, Container> GetInventories()
668+
{
669+
return Handler.GetInventories();
670+
}
671+
672+
/// <summary>
673+
/// start Sneaking
674+
/// </summary>
675+
protected bool Sneak(bool on)
676+
{
677+
return SendEntityAction(on ? Protocol.EntityActionType.StartSneaking : Protocol.EntityActionType.StopSneaking);
678+
}
679+
/// <summary>
680+
/// Send Entity Action
681+
/// </summary>
682+
private bool SendEntityAction(Protocol.EntityActionType entityAction)
683+
{
684+
return Handler.sendEntityAction(entityAction);
685+
}
686+
/// <summary>
687+
/// SetSlot
688+
/// </summary>
689+
protected void SetSlot(int slotNum)
690+
{
691+
Handler.ChangeSlot((short) slotNum);
692+
}
665693

666694
/// <summary>
667695
/// Get the current Minecraft World
@@ -862,7 +890,7 @@ protected bool InteractEntity(int EntityID, int type)
862890
/// Use item currently in the player's hand (active inventory bar slot)
863891
/// </summary>
864892
/// <returns></returns>
865-
protected bool UseItemOnHand()
893+
protected bool UseItemInHand()
866894
{
867895
return Handler.UseItemOnHand();
868896
}

MinecraftClient/ChatBots/AutoEat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public bool FindFoodAndEat()
8383
}
8484
}
8585
}
86-
if (found) UseItemOnHand();
86+
if (found) UseItemInHand();
8787
return found;
8888
}
8989
}

MinecraftClient/ChatBots/AutoFishing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void OnCaughtFish()
9595
{
9696
LogToConsole(GetTimestamp() + ": Caught a fish!");
9797
// retract fishing rod
98-
UseItemOnHand();
98+
UseItemInHand();
9999
if (inventoryEnabled)
100100
{
101101
if (!hasFishingRod())
@@ -110,7 +110,7 @@ public void OnCaughtFish()
110110
// retract fishing rod need some time
111111
Thread.Sleep(800);
112112
// throw again
113-
UseItemOnHand();
113+
UseItemInHand();
114114
});
115115
}
116116

MinecraftClient/ChatBots/ChatLog.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ namespace MinecraftClient.ChatBots
1212

1313
public class ChatLog : ChatBot
1414
{
15-
public enum MessageFilter { AllText, AllMessages, OnlyChat, OnlyWhispers };
15+
public enum MessageFilter { AllText, AllMessages, OnlyChat, OnlyWhispers, OnlyInternalCommands };
1616
private bool dateandtime;
1717
private bool saveOther = true;
1818
private bool saveChat = true;
1919
private bool savePrivate = true;
20+
private bool saveInternal = true;
2021
private string logfile;
2122

2223
/// <summary>
@@ -52,6 +53,12 @@ public ChatLog(string file, MessageFilter filter, bool AddDateAndTime)
5253
savePrivate = true;
5354
saveChat = false;
5455
break;
56+
case MessageFilter.OnlyInternalCommands:
57+
saveOther = false;
58+
savePrivate = false;
59+
saveChat = false;
60+
saveInternal = true;
61+
break;
5562
}
5663
if (String.IsNullOrEmpty(file) || file.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
5764
{
@@ -68,6 +75,7 @@ public static MessageFilter str2filter(string filtername)
6875
case "messages": return MessageFilter.AllMessages;
6976
case "chat": return MessageFilter.OnlyChat;
7077
case "private": return MessageFilter.OnlyWhispers;
78+
case "internal": return MessageFilter.OnlyInternalCommands;
7179
default: return MessageFilter.AllText;
7280
}
7381
}
@@ -92,6 +100,14 @@ public override void GetText(string text)
92100
}
93101
}
94102

103+
public override void OnInternalCommand(string commandName,string commandParams, string result)
104+
{
105+
if (saveInternal)
106+
{
107+
save(string.Format("Internal {0}({1}): {2}", commandName, commandParams, result));
108+
}
109+
}
110+
95111
private void save(string tosave)
96112
{
97113
if (dateandtime)

MinecraftClient/Commands/Sneak.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MinecraftClient.Commands
7+
{
8+
public class Sneak : Command
9+
{
10+
private bool sneaking = false;
11+
public override string CMDName { get { return "Sneak"; } }
12+
public override string CMDDesc { get { return "Sneak: Toggles sneaking"; } }
13+
14+
public override string Run(McTcpClient handler, string command, Dictionary<string, object> localVars)
15+
{
16+
Console.WriteLine(command);
17+
if (sneaking)
18+
{
19+
var result = handler.sendEntityAction(Protocol.EntityActionType.StopSneaking);
20+
sneaking = false;
21+
return result ? "Success" : "Fail";
22+
}
23+
else
24+
{
25+
var result = handler.sendEntityAction(Protocol.EntityActionType.StartSneaking);
26+
sneaking = true;
27+
return result ? "Success" : "Fail";
28+
}
29+
30+
}
31+
}
32+
}

MinecraftClient/McTcpClient.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using System.IO;
88
using System.Net;
9+
using MinecraftClient.ChatBots;
910
using MinecraftClient.Protocol;
1011
using MinecraftClient.Proxy;
1112
using MinecraftClient.Protocol.Handlers.Forge;
@@ -170,6 +171,7 @@ private void StartClient(string user, string uuid, string sessionID, string serv
170171
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); }
171172
if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); }
172173
if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); }
174+
173175
//Add your ChatBot here by uncommenting and adapting
174176
//BotLoad(new ChatBots.YourBot());
175177
}
@@ -324,6 +326,7 @@ private void TimeoutDetector()
324326
}
325327
while (true);
326328
}
329+
327330

328331
/// <summary>
329332
/// Perform an internal MCC command (not a server command, use SendText() instead for that!)
@@ -334,6 +337,7 @@ private void TimeoutDetector()
334337
/// <returns>TRUE if the command was indeed an internal MCC command</returns>
335338
public bool PerformInternalCommand(string command, ref string response_msg, Dictionary<string, object> localVars = null)
336339
{
340+
337341
/* Load commands from the 'Commands' namespace */
338342

339343
if (cmds.Count == 0)
@@ -382,12 +386,28 @@ public bool PerformInternalCommand(string command, ref string response_msg, Dict
382386
else if (cmds.ContainsKey(command_name))
383387
{
384388
response_msg = cmds[command_name].Run(this, command, localVars);
389+
foreach (ChatBot bot in bots.ToArray())
390+
{
391+
try
392+
{
393+
bot.OnInternalCommand(command_name, string.Join(" ",Command.getArgs(command)),response_msg);
394+
}
395+
catch (Exception e)
396+
{
397+
if (!(e is ThreadAbortException))
398+
{
399+
ConsoleIO.WriteLogLine("OnInternalCommand: Got error from " + bot.ToString() + ": " + e.ToString());
400+
}
401+
else throw; //ThreadAbortException should not be caught
402+
}
403+
}
385404
}
386405
else
387406
{
388407
response_msg = "Unknown command '" + command_name + "'. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.";
389408
return false;
390409
}
410+
391411
return true;
392412
}
393413

@@ -1356,6 +1376,7 @@ public void OnEntityPosition(int EntityID, Double Dx, Double Dy, Double Dz,bool
13561376
}
13571377

13581378
}
1379+
13591380
/// <summary>
13601381
/// Called when an entity moved over 8 block.
13611382
/// </summary>
@@ -1469,6 +1490,14 @@ public void SetPlayerEntityID(int EntityID)
14691490
playerEntityID = EntityID;
14701491
}
14711492

1493+
/// <summary>
1494+
/// Send the Entity Action packet with the Specified ID
1495+
/// </summary>
1496+
/// <returns>TRUE if the item was successfully used</returns>
1497+
public bool sendEntityAction(EntityActionType entityAction)
1498+
{
1499+
return handler.SendEntityAction(playerEntityID, (int) entityAction);
1500+
}
14721501
/// <summary>
14731502
/// Use the item currently in the player's hand
14741503
/// </summary>

MinecraftClient/MinecraftClient.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="Commands\Send.cs" />
104104
<Compile Include="Commands\Set.cs" />
105105
<Compile Include="Commands\Health.cs" />
106+
<Compile Include="Commands\Sneak.cs" />
106107
<Compile Include="Commands\UseItem.cs" />
107108
<Compile Include="Inventory\Container.cs" />
108109
<Compile Include="Inventory\ContainerType.cs" />
@@ -119,6 +120,7 @@
119120
<Compile Include="Mapping\Entity.cs" />
120121
<Compile Include="Mapping\EntityType.cs" />
121122
<Compile Include="Mapping\MaterialExtensions.cs" />
123+
<Compile Include="Protocol\EntityActionType.cs" />
122124
<Compile Include="Protocol\Handlers\DataTypes.cs" />
123125
<Compile Include="Protocol\Handlers\PacketIncomingType.cs" />
124126
<Compile Include="Protocol\Handlers\PacketOutgoingType.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MinecraftClient.Protocol
7+
{
8+
public enum EntityActionType
9+
{
10+
StartSneaking,
11+
StopSneaking,
12+
LeaveBed,
13+
StartSprinting,
14+
StopSprinting
15+
}
16+
}

MinecraftClient/Protocol/Handlers/PacketOutgoingType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum PacketOutgoingType
1717
ClientSettings,
1818
PluginMessage,
1919
TabComplete,
20+
EntityAction,
2021
PlayerPosition,
2122
PlayerPositionAndLook,
2223
TeleportConfirm,
@@ -27,4 +28,4 @@ enum PacketOutgoingType
2728
CloseWindow,
2829
PlayerBlockPlacement
2930
}
30-
}
31+
}

MinecraftClient/Protocol/Handlers/Protocol16.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ private string readNextString()
239239
}
240240
else return "";
241241
}
242+
public bool SendEntityAction(int PlayerEntityID, int ActionID)
243+
{
244+
return false;
245+
}
242246

243247
private byte[] readNextByteArray()
244248
{

0 commit comments

Comments
 (0)