Skip to content

Commit

Permalink
chat component can parse non-json strings now
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Dec 29, 2023
1 parent 2b38762 commit fc67cf1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
15 changes: 12 additions & 3 deletions Components/MineSharp.ChatComponent/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using System.Text.RegularExpressions;
using MineSharp.Data;
using Newtonsoft.Json;

namespace MineSharp.ChatComponent;

Expand Down Expand Up @@ -44,9 +45,17 @@ public Chat(string json, MinecraftData data)
{
this.Json = json;
this.data = data;

this.StyledMessage = this.ParseComponent(JToken.Parse(this.Json));
this.Message = Regex.Replace(this.StyledMessage, "\\$[0-9a-fk-r]", "");

try
{
this.StyledMessage = this.ParseComponent(JToken.Parse(this.Json));
this.Message = Regex.Replace(this.StyledMessage, "\\$[0-9a-fk-r]", "");
}
catch (JsonReaderException)
{
this.StyledMessage = this.Json;
this.Message = this.StyledMessage;
}
}

private string ParseComponent(JToken token, string styleCode = "")
Expand Down
62 changes: 39 additions & 23 deletions MineSharp.Bot/Plugins/ChatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using MineSharp.Protocol.Packets.Serverbound.Play;
using MineSharp.Protocol;
using System.Diagnostics;
using MineSharp.ChatComponent;
using NLog;
using ChatPacket = MineSharp.Protocol.Packets.Clientbound.Play.ChatPacket;
using SBChatMessage = MineSharp.Protocol.Packets.Serverbound.Play.ChatPacket;
using SBChatMessagePacket = MineSharp.Protocol.Packets.Serverbound.Play.ChatMessagePacket;
Expand All @@ -17,6 +17,8 @@ namespace MineSharp.Bot.Plugins;
/// </summary>
public class ChatPlugin : Plugin
{
private readonly ILogger Logger = LogManager.GetCurrentClassLogger();

private readonly LastSeenMessageCollector? _messageCollector;
private readonly UUID _chatSession = new Guid();
private int _messageCount = 0;
Expand Down Expand Up @@ -417,7 +419,14 @@ private Task SendCommand1_19_3(string command, List<(string, string)> arguments)

private Task HandleDeclareCommandsPacket(DeclareCommandsPacket packet)
{
this._commandTree = CommandTree.Parse(packet.RawBuffer, this.Bot.Data);
try
{
this._commandTree = CommandTree.Parse(packet.RawBuffer, this.Bot.Data);
}
catch (Exception e)
{
Logger.Error($"Could not parse command tree: {e}");
}
return Task.CompletedTask;
}

Expand All @@ -429,33 +438,40 @@ private Task HandleDeclareCommandsPacket(DeclareCommandsPacket packet)
/// <exception cref="UnreachableException"></exception>
private Task HandleChatMessagePacket(PlayerChatPacket packet)
{
// TODO: Advanced chat stuff like filtering and signature verification.

if (packet.Body is PlayerChatPacket.V1_19_2_3Body body && body.Signature != null)
try
{
if (this._messageCollector is LastSeenMessageCollector1_19_2 collector1_19_2)
// TODO: Advanced chat stuff like filtering and signature verification.

if (packet.Body is PlayerChatPacket.V1_19_2_3Body body && body.Signature != null)
{
collector1_19_2.Push(new AcknowledgedMessage(true, body.Sender, body.Signature));
if (collector1_19_2.PendingAcknowledgements++ > 64)
if (this._messageCollector is LastSeenMessageCollector1_19_2 collector1_19_2)
{
collector1_19_2.Push(new AcknowledgedMessage(true, body.Sender, body.Signature));
if (collector1_19_2.PendingAcknowledgements++ > 64)
{
var messages = collector1_19_2.AcknowledgeMessages().Select(x => x.ToProtocolMessage()).ToArray();
this.Bot.Client.SendPacket(new MessageAcknowledgementPacket(messages, null));
}
} else if (this._messageCollector is LastSeenMessageCollector1_19_3 collector1_19_3)
{
var messages = collector1_19_2.AcknowledgeMessages().Select(x => x.ToProtocolMessage()).ToArray();
this.Bot.Client.SendPacket(new MessageAcknowledgementPacket(messages, null));
int count = collector1_19_3.ResetCount();
if (count > 0)
this.Bot.Client.SendPacket(new MessageAcknowledgementPacket(count));
}
} else if (this._messageCollector is LastSeenMessageCollector1_19_3 collector1_19_3)
{
int count = collector1_19_3.ResetCount();
if (count > 0)
this.Bot.Client.SendPacket(new MessageAcknowledgementPacket(count));
}
}

(UUID sender, string message, ChatMessageType type) = packet.Body switch {
PlayerChatPacket.V1_19Body v19 => (v19.Sender, v19.SignedChat, (ChatMessageType)v19.MessageType),
PlayerChatPacket.V1_19_2_3Body v19_2 => (v19_2.Sender, v19_2.PlainMessage, (ChatMessageType)v19_2.Type),
_ => throw new UnreachableException()
};

this.HandleChatInternal(sender, message, type);
(UUID sender, string message, ChatMessageType type) = packet.Body switch {
PlayerChatPacket.V1_19Body v19 => (v19.Sender, v19.SignedChat, (ChatMessageType)v19.MessageType),
PlayerChatPacket.V1_19_2_3Body v19_2 => (v19_2.Sender, v19_2.PlainMessage, (ChatMessageType)v19_2.Type),
_ => throw new UnreachableException()
};

this.HandleChatInternal(sender, message, type);
}
catch (Exception e)
{
Logger.Error("Error in chat message: " + e);
}

return Task.CompletedTask;
}
Expand Down

0 comments on commit fc67cf1

Please sign in to comment.