Allow access to raw JSON messages from ChatBot

See #275
This commit is contained in:
ORelio 2017-05-31 20:54:16 +02:00
parent 7c9c12bee7
commit dc09896959
8 changed files with 28 additions and 11 deletions

View file

@ -237,7 +237,8 @@ namespace MinecraftClient
/// It will unload and reload all the bots and then reconnect to the server
/// </summary>
/// <param name="extraAttempts">If connection fails, the client will make X extra attempts</param>
new public void ReconnectToTheServer(int extraAttempts = -999999)
/// <param name="delaySeconds">Optional delay, in seconds, before restarting</param>
new public void ReconnectToTheServer(int extraAttempts = -999999, int delaySeconds = 0)
{
if (extraAttempts == -999999)
base.ReconnectToTheServer();

View file

@ -109,6 +109,16 @@ namespace MinecraftClient
/// <param name="text">Text from the server</param>
public virtual void GetText(string text) { }
/// <summary>
/// Any text sent by the server will be sent here by MinecraftCom (extended variant)
/// </summary>
/// <remarks>
/// You can use Json.ParseJson() to process the JSON string.
/// </remarks>
/// <param name="text">Text from the server</param>
/// <param name="json">Raw JSON from the server. This parameter will be NULL on MC 1.5 or lower!</param>
public virtual void GetText(string text, string json) { }
/// <summary>
/// Is called when the client has been disconnected fom the server
/// </summary>

View file

@ -439,9 +439,17 @@ namespace MinecraftClient
/// Received some text from the server
/// </summary>
/// <param name="text">Text received</param>
/// <param name="isJson">TRUE if the text is JSON-Encoded</param>
/// <param name="links">Links embedded in text</param>
public void OnTextReceived(string text, IEnumerable<string> links)
public void OnTextReceived(string text, bool isJson)
{
List<string> links = new List<string>();
string json = null;
if (isJson)
{
json = text;
text = ChatParser.ParseText(json, links);
}
ConsoleIO.WriteLineFormatted(text, false);
if (Settings.DisplayChatLinks)
foreach (string link in links)
@ -451,6 +459,7 @@ namespace MinecraftClient
try
{
bots[i].GetText(text);
bots[i].GetText(text, json);
}
catch (Exception e)
{

View file

@ -125,6 +125,7 @@
<Compile Include="Mapping\Material.cs" />
<Compile Include="Mapping\Movement.cs" />
<Compile Include="Mapping\World.cs" />
<Compile Include="Protocol\ChatParser.cs" />
<Compile Include="Protocol\Dns\Header.cs" />
<Compile Include="Protocol\Dns\Question.cs" />
<Compile Include="Protocol\Dns\RecordReader.cs" />
@ -208,7 +209,6 @@
<Compile Include="Protocol\Handlers\Compression\ZlibCodec.cs" />
<Compile Include="Protocol\Handlers\Compression\ZlibConstants.cs" />
<Compile Include="Protocol\Handlers\ZlibUtils.cs" />
<Compile Include="Protocol\Handlers\ChatParser.cs" />
<Compile Include="Crypto\IAesStream.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Protocol.Handlers
namespace MinecraftClient.Protocol
{
/// <summary>
/// This class parses JSON chat data from MC 1.6+ and returns the appropriate string to be printed.

View file

@ -88,9 +88,7 @@ namespace MinecraftClient.Protocol.Handlers
case 0x02: readData(1); readNextString(); readNextString(); readData(4); break;
case 0x03:
string message = readNextString();
List<string> links = new List<string>();
if (protocolversion >= 72) { message = ChatParser.ParseText(message, links); }
handler.OnTextReceived(message, links); break;
handler.OnTextReceived(message, protocolversion >= 72); break;
case 0x04: readData(16); break;
case 0x05: readData(6); readNextItemSlot(); break;
case 0x06: readData(12); break;

View file

@ -258,8 +258,7 @@ namespace MinecraftClient.Protocol.Handlers
break;
}
catch (ArgumentOutOfRangeException) { /* No message type */ }
List<string> links = new List<string>();
handler.OnTextReceived(ChatParser.ParseText(message, links), links);
handler.OnTextReceived(message, true);
break;
case PacketIncomingType.Respawn:
this.currentDimension = readNextInt(packetData);

View file

@ -35,8 +35,8 @@ namespace MinecraftClient.Protocol
/// This method is called when the protocol handler receives a chat message
/// </summary>
/// <param name="text">Text received from the server</param>
/// <param name="links">Links embedded in text (for click events)</param>
void OnTextReceived(string text, IEnumerable<string> links);
/// <param name="isJson">TRUE if the text is JSON-Encoded</param>
void OnTextReceived(string text, bool isJson);
/// <summary>
/// This method is called when a new player joins the game