Add GetOnlinePlayers() in ChatBot API

Suggestion by mattjbenson
This commit is contained in:
ORelio 2016-10-14 21:14:26 +02:00
parent e4d93041fa
commit b1d5d0b32e

View file

@ -25,7 +25,6 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// The virtual class containing anything you need for creating chat bots. /// The virtual class containing anything you need for creating chat bots.
/// </summary> /// </summary>
public abstract class ChatBot public abstract class ChatBot
{ {
public enum DisconnectReason { InGameKick, LoginRejected, ConnectionLost }; public enum DisconnectReason { InGameKick, LoginRejected, ConnectionLost };
@ -75,7 +74,6 @@ namespace MinecraftClient
/// NOTE: Chat messages cannot be sent at this point in the login process. If you want to send /// NOTE: Chat messages cannot be sent at this point in the login process. If you want to send
/// a message when the bot is loaded, use AfterGameJoined. /// a message when the bot is loaded, use AfterGameJoined.
/// </summary> /// </summary>
public virtual void Initialize() { } public virtual void Initialize() { }
/// <summary> /// <summary>
@ -84,20 +82,17 @@ namespace MinecraftClient
/// NOTE: This is not always right after joining the server - if the bot was loaded after logging /// NOTE: This is not always right after joining the server - if the bot was loaded after logging
/// in this is still called. /// in this is still called.
/// </summary> /// </summary>
public virtual void AfterGameJoined() { } public virtual void AfterGameJoined() { }
/// <summary> /// <summary>
/// Will be called every ~100ms (10fps) if loaded in MinecraftCom /// Will be called every ~100ms (10fps) if loaded in MinecraftCom
/// </summary> /// </summary>
public virtual void Update() { } public virtual void Update() { }
/// <summary> /// <summary>
/// Any text sent by the server will be sent here by MinecraftCom /// Any text sent by the server will be sent here by MinecraftCom
/// </summary> /// </summary>
/// <param name="text">Text from the server</param> /// <param name="text">Text from the server</param>
public virtual void GetText(string text) { } public virtual void GetText(string text) { }
/// <summary> /// <summary>
@ -106,7 +101,6 @@ namespace MinecraftClient
/// <param name="reason">Disconnect Reason</param> /// <param name="reason">Disconnect Reason</param>
/// <param name="message">Kick message, if any</param> /// <param name="message">Kick message, if any</param>
/// <returns>Return TRUE if the client is about to restart</returns> /// <returns>Return TRUE if the client is about to restart</returns>
public virtual bool OnDisconnect(DisconnectReason reason, string message) { return false; } public virtual bool OnDisconnect(DisconnectReason reason, string message) { return false; }
/// <summary> /// <summary>
@ -117,7 +111,6 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="channel">The name of the channel</param> /// <param name="channel">The name of the channel</param>
/// <param name="data">The payload for the message</param> /// <param name="data">The payload for the message</param>
public virtual void OnPluginMessage(string channel, byte[] data) { } public virtual void OnPluginMessage(string channel, byte[] data) { }
/* =================================================================== */ /* =================================================================== */
@ -132,7 +125,6 @@ namespace MinecraftClient
/// <param name="text">Text to send to the server</param> /// <param name="text">Text to send to the server</param>
/// <param name="sendImmediately">Whether the message should be sent immediately rather than being queued to avoid chat spam</param> /// <param name="sendImmediately">Whether the message should be sent immediately rather than being queued to avoid chat spam</param>
/// <returns>True if the text was sent with no error</returns> /// <returns>True if the text was sent with no error</returns>
protected bool SendText(string text, bool sendImmediately = false) protected bool SendText(string text, bool sendImmediately = false)
{ {
if (Settings.botMessageDelay.TotalSeconds > 0 && !sendImmediately) if (Settings.botMessageDelay.TotalSeconds > 0 && !sendImmediately)
@ -156,7 +148,6 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="command">The command to process</param> /// <param name="command">The command to process</param>
/// <returns>TRUE if the command was indeed an internal MCC command</returns> /// <returns>TRUE if the command was indeed an internal MCC command</returns>
protected bool PerformInternalCommand(string command) protected bool PerformInternalCommand(string command)
{ {
string temp = ""; string temp = "";
@ -169,7 +160,6 @@ namespace MinecraftClient
/// <param name="command">The command to process</param> /// <param name="command">The command to process</param>
/// <param name="response_msg">May contain a confirmation or error message after processing the command, or "" otherwise.</param> /// <param name="response_msg">May contain a confirmation or error message after processing the command, or "" otherwise.</param>
/// <returns>TRUE if the command was indeed an internal MCC command</returns> /// <returns>TRUE if the command was indeed an internal MCC command</returns>
protected bool PerformInternalCommand(string command, ref string response_msg) protected bool PerformInternalCommand(string command, ref string response_msg)
{ {
return Handler.PerformInternalCommand(command, ref response_msg); return Handler.PerformInternalCommand(command, ref response_msg);
@ -178,7 +168,6 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Remove color codes ("§c") from a text message received from the server /// Remove color codes ("§c") from a text message received from the server
/// </summary> /// </summary>
protected static string GetVerbatim(string text) protected static string GetVerbatim(string text)
{ {
if ( String.IsNullOrEmpty(text) ) if ( String.IsNullOrEmpty(text) )
@ -199,7 +188,6 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Verify that a string contains only a-z A-Z 0-9 and _ characters. /// Verify that a string contains only a-z A-Z 0-9 and _ characters.
/// </summary> /// </summary>
public static bool IsValidName(string username) public static bool IsValidName(string username)
{ {
if (String.IsNullOrEmpty(username)) if (String.IsNullOrEmpty(username))
@ -222,7 +210,6 @@ namespace MinecraftClient
/// <param name="message">if it's a private message, this will contain the message</param> /// <param name="message">if it's a private message, this will contain the message</param>
/// <param name="sender">if it's a private message, this will contain the player name that sends the message</param> /// <param name="sender">if it's a private message, this will contain the player name that sends the message</param>
/// <returns>Returns true if the text is a private message</returns> /// <returns>Returns true if the text is a private message</returns>
protected static bool IsPrivateMessage(string text, ref string message, ref string sender) protected static bool IsPrivateMessage(string text, ref string message, ref string sender)
{ {
if (String.IsNullOrEmpty(text)) if (String.IsNullOrEmpty(text))
@ -334,7 +321,6 @@ namespace MinecraftClient
/// <param name="message">if it's message, this will contain the message</param> /// <param name="message">if it's message, this will contain the message</param>
/// <param name="sender">if it's message, this will contain the player name that sends the message</param> /// <param name="sender">if it's message, this will contain the player name that sends the message</param>
/// <returns>Returns true if the text is a chat message</returns> /// <returns>Returns true if the text is a chat message</returns>
protected static bool IsChatMessage(string text, ref string message, ref string sender) protected static bool IsChatMessage(string text, ref string message, ref string sender)
{ {
if (String.IsNullOrEmpty(text)) if (String.IsNullOrEmpty(text))
@ -438,7 +424,6 @@ namespace MinecraftClient
/// <param name="text">Text to parse</param> /// <param name="text">Text to parse</param>
/// <param name="sender">Will contain the sender's username, if it's a teleport request</param> /// <param name="sender">Will contain the sender's username, if it's a teleport request</param>
/// <returns>Returns true if the text is a teleport request</returns> /// <returns>Returns true if the text is a teleport request</returns>
protected static bool IsTeleportRequest(string text, ref string sender) protected static bool IsTeleportRequest(string text, ref string sender)
{ {
if (String.IsNullOrEmpty(text)) if (String.IsNullOrEmpty(text))
@ -493,7 +478,6 @@ namespace MinecraftClient
/// Write some text in the console. Nothing will be sent to the server. /// Write some text in the console. Nothing will be sent to the server.
/// </summary> /// </summary>
/// <param name="text">Log text to write</param> /// <param name="text">Log text to write</param>
protected void LogToConsole(object text) protected void LogToConsole(object text)
{ {
ConsoleIO.WriteLogLine(String.Format("[{0}] {1}", this.GetType().Name, text)); ConsoleIO.WriteLogLine(String.Format("[{0}] {1}", this.GetType().Name, text));
@ -518,7 +502,6 @@ namespace MinecraftClient
/// It will unload and reload all the bots and then reconnect to the server /// It will unload and reload all the bots and then reconnect to the server
/// </summary> /// </summary>
/// <param name="attempts">If connection fails, the client will make X extra attempts</param> /// <param name="attempts">If connection fails, the client will make X extra attempts</param>
protected void ReconnectToTheServer(int ExtraAttempts = 3) protected void ReconnectToTheServer(int ExtraAttempts = 3)
{ {
McTcpClient.ReconnectionAttemptsLeft = ExtraAttempts; McTcpClient.ReconnectionAttemptsLeft = ExtraAttempts;
@ -528,7 +511,6 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Disconnect from the server and exit the program /// Disconnect from the server and exit the program
/// </summary> /// </summary>
protected void DisconnectAndExit() protected void DisconnectAndExit()
{ {
Program.Exit(); Program.Exit();
@ -537,7 +519,6 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Unload the chatbot, and release associated memory. /// Unload the chatbot, and release associated memory.
/// </summary> /// </summary>
protected void UnloadBot() protected void UnloadBot()
{ {
Handler.BotUnLoad(this); Handler.BotUnLoad(this);
@ -548,7 +529,6 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="player">Player name</param> /// <param name="player">Player name</param>
/// <param name="message">Message</param> /// <param name="message">Message</param>
protected void SendPrivateMessage(string player, string message) protected void SendPrivateMessage(string player, string message)
{ {
SendText(String.Format("/{0} {1} {2}", Settings.PrivateMsgsCmdName, player, message)); SendText(String.Format("/{0} {1} {2}", Settings.PrivateMsgsCmdName, player, message));
@ -559,7 +539,6 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="filename">File name</param> /// <param name="filename">File name</param>
/// <param name="playername">Player name to send error messages, if applicable</param> /// <param name="playername">Player name to send error messages, if applicable</param>
protected void RunScript(string filename, string playername = "") protected void RunScript(string filename, string playername = "")
{ {
Handler.BotLoad(new ChatBots.Script(filename, playername)); Handler.BotLoad(new ChatBots.Script(filename, playername));
@ -569,7 +548,6 @@ namespace MinecraftClient
/// Get the current Minecraft World /// Get the current Minecraft World
/// </summary> /// </summary>
/// <returns>Minecraft world or null if associated setting is disabled</returns> /// <returns>Minecraft world or null if associated setting is disabled</returns>
protected Mapping.World GetWorld() protected Mapping.World GetWorld()
{ {
if (Settings.TerrainAndMovements) if (Settings.TerrainAndMovements)
@ -581,7 +559,6 @@ namespace MinecraftClient
/// Get the current location of the player /// Get the current location of the player
/// </summary> /// </summary>
/// <returns>Minecraft world or null if associated setting is disabled</returns> /// <returns>Minecraft world or null if associated setting is disabled</returns>
protected Mapping.Location GetCurrentLocation() protected Mapping.Location GetCurrentLocation()
{ {
return Handler.GetCurrentLocation(); return Handler.GetCurrentLocation();
@ -593,7 +570,6 @@ namespace MinecraftClient
/// <param name="location">Location to reach</param> /// <param name="location">Location to reach</param>
/// <param name="allowUnsafe">Allow possible but unsafe locations</param> /// <param name="allowUnsafe">Allow possible but unsafe locations</param>
/// <returns>True if a path has been found</returns> /// <returns>True if a path has been found</returns>
protected bool MoveToLocation(Mapping.Location location, bool allowUnsafe = false) protected bool MoveToLocation(Mapping.Location location, bool allowUnsafe = false)
{ {
return Handler.MoveTo(location, allowUnsafe); return Handler.MoveTo(location, allowUnsafe);
@ -602,7 +578,6 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Get a Y-M-D h:m:s timestamp representing the current system date and time /// Get a Y-M-D h:m:s timestamp representing the current system date and time
/// </summary> /// </summary>
protected static string GetTimestamp() protected static string GetTimestamp()
{ {
DateTime time = DateTime.Now; DateTime time = DateTime.Now;
@ -620,7 +595,6 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="file">File to load</param> /// <param name="file">File to load</param>
/// <returns>The string array or an empty array if failed to load the file</returns> /// <returns>The string array or an empty array if failed to load the file</returns>
protected string[] LoadDistinctEntriesFromFile(string file) protected string[] LoadDistinctEntriesFromFile(string file)
{ {
if (File.Exists(file)) if (File.Exists(file))
@ -639,11 +613,19 @@ namespace MinecraftClient
} }
} }
/// <summary>
/// Return the list of currently online players
/// </summary>
/// <returns>List of online players</returns>
protected string[] GetOnlinePlayers()
{
return Handler.GetOnlinePlayers();
}
/// <summary> /// <summary>
/// Registers the given plugin channel for use by this chatbot. /// Registers the given plugin channel for use by this chatbot.
/// </summary> /// </summary>
/// <param name="channel">The name of the channel to register</param> /// <param name="channel">The name of the channel to register</param>
protected void RegisterPluginChannel(string channel) protected void RegisterPluginChannel(string channel)
{ {
this.registeredPluginChannels.Add(channel); this.registeredPluginChannels.Add(channel);
@ -654,7 +636,6 @@ namespace MinecraftClient
/// Unregisters the given plugin channel, meaning this chatbot can no longer use it. /// Unregisters the given plugin channel, meaning this chatbot can no longer use it.
/// </summary> /// </summary>
/// <param name="channel">The name of the channel to unregister</param> /// <param name="channel">The name of the channel to unregister</param>
protected void UnregisterPluginChannel(string channel) protected void UnregisterPluginChannel(string channel)
{ {
this.registeredPluginChannels.RemoveAll(chan => chan == channel); this.registeredPluginChannels.RemoveAll(chan => chan == channel);
@ -669,7 +650,6 @@ namespace MinecraftClient
/// <param name="data">The data to send.</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> /// <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> /// <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) protected bool SendPluginChannelMessage(string channel, byte[] data, bool sendEvenIfNotRegistered = false)
{ {
if (!sendEvenIfNotRegistered) if (!sendEvenIfNotRegistered)