From 6d4ec86619d6a9b1129c7275e71adc3b459a7208 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sun, 7 Feb 2016 14:47:03 -0800 Subject: [PATCH] Add an "AfterGameJoined" method to ChatBot.cs. Since messages can't be sent in Initialize(), a method that's called when the chat bot can first send messages is needed. This method is called when the login completes or when the bot is loaded if the login is already been completed. --- MinecraftClient/ChatBot.cs | 12 ++++++++++++ MinecraftClient/McTcpClient.cs | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index c84ff57d..fd58719a 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -75,10 +75,22 @@ namespace MinecraftClient /// /// Anything you want to initialize your bot, will be called on load by MinecraftCom + /// + /// 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. /// public virtual void Initialize() { } + /// + /// Called after the server has been joined successfully and chat messages are able to be sent. + /// + /// NOTE: This is not always right after joining the server - if the bot was loaded after logging + /// in this is still called. + /// + + public virtual void AfterGameJoined() { } + /// /// Will be called every ~100ms (10fps) if loaded in MinecraftCom /// diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 7e9c95b7..35eaa124 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -27,7 +27,16 @@ namespace MinecraftClient private readonly List bots = new List(); private static readonly List scripts_on_hold = new List(); - public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; } + public void BotLoad(ChatBot b) { + b.SetHandler(this); + bots.Add(b); + b.Initialize(); + if (this.handler != null) + { + b.AfterGameJoined(); + } + Settings.SingleCommand = ""; + } public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); @@ -351,6 +360,8 @@ namespace MinecraftClient { if (!String.IsNullOrWhiteSpace(Settings.BrandInfo)) handler.SendBrandInfo(Settings.BrandInfo.Trim()); + foreach (ChatBot bot in bots) + bot.AfterGameJoined(); } ///