Give ChatBots access to plugin channels.

Chatbots may find it useful to send messages over plugin channels.  This allows REGISTERing, UNREGISTERing, and sending over plugin channels, with built-in checking if the server also registered the channel (which can be disabled by the bot if needed). Unused channels are UNREGISTERed when a bot is disabled.
This commit is contained in:
Pokechu22 2016-02-07 14:24:01 -08:00
parent bdbd8ab0b8
commit 1ea8f119d9
5 changed files with 215 additions and 3 deletions

View file

@ -41,6 +41,7 @@ namespace MinecraftClient
private McTcpClient Handler { get { return master != null ? master.Handler : _handler; } }
private McTcpClient _handler = null;
private ChatBot master = null;
private List<string> registeredPluginChannels = new List<String>();
private Queue<string> chatQueue = new Queue<string>();
private DateTime? lastMessageSentTime = DateTime.MinValue;
private bool CanSendTextNow
@ -100,6 +101,17 @@ namespace MinecraftClient
public virtual bool OnDisconnect(DisconnectReason reason, string message) { return false; }
/// <summary>
/// Called when a plugin channel message is received.
/// The given channel must have previously been registered with RegisterPluginChannel.
/// This can be used to communicate with server mods or plugins. See wiki.vg for more
/// information about plugin channels: http://wiki.vg/Plugin_channel
/// </summary>
/// <param name="channel">The name of the channel</param>
/// <param name="data">The payload for the message</param>
public virtual void OnPluginMessage(string channel, byte[] data) { }
/* =================================================================== */
/* ToolBox - Methods below might be useful while creating your bot. */
/* You should not need to interact with other classes of the program. */
@ -596,5 +608,48 @@ namespace MinecraftClient
return new string[0];
}
}
/// <summary>
/// Registers the given plugin channel for use by this chatbot.
/// </summary>
/// <param name="channel">The name of the channel to register</param>
protected void RegisterPluginChannel(string channel)
{
this.registeredPluginChannels.Add(channel);
Handler.RegisterPluginChannel(channel, this);
}
/// <summary>
/// Unregisters the given plugin channel, meaning this chatbot can no longer use it.
/// </summary>
/// <param name="channel">The name of the channel to unregister</param>
protected void UnregisterPluginChannel(string channel)
{
this.registeredPluginChannels.RemoveAll(chan => chan == channel);
Handler.UnregisterPluginChannel(channel, this);
}
/// <summary>
/// Sends the given plugin channel message to the server, if the channel has been registered.
/// See http://wiki.vg/Plugin_channel for more information about plugin channels.
/// </summary>
/// <param name="channel">The channel to send the message on.</param>
/// <param name="data">The data to send.</param>
/// <param name="sendEvenIfNotRegistered">Should the message be sent even if it hasn't been registered by the server or this bot? (Some Minecraft channels aren't registered)</param>
/// <returns>Whether the message was successfully sent. False if there was a network error or if the channel wasn't registered.</returns>
protected bool SendPluginChannelMessage(string channel, byte[] data, bool sendEvenIfNotRegistered = false)
{
if (!sendEvenIfNotRegistered)
{
if (!this.registeredPluginChannels.Contains(channel))
{
return false;
}
}
return Handler.SendPluginChannelMessage(channel, data, sendEvenIfNotRegistered);
}
}
}