Fix HeroChat public messages treated as private

See issue #63 - Also includes minor fixes and optimizations
This commit is contained in:
ORelio 2015-05-18 16:15:58 +02:00
parent dd5e2f8e39
commit 5b662e2d07
2 changed files with 46 additions and 52 deletions

View file

@ -14,12 +14,12 @@ namespace MinecraftClient
/// Once your bot is created, read the explanations below to start using it in the MinecraftClient app. /// Once your bot is created, read the explanations below to start using it in the MinecraftClient app.
/// ///
/// Pieces of code to add in other parts of the program for your bot. Line numbers are approximative. /// Pieces of code to add in other parts of the program for your bot. Line numbers are approximative.
/// McTcpClient:110 | if (Settings.YourBot_Enabled) { handler.BotLoad(new ChatBots.YourBot()); }
/// Settings.cs:73 | public static bool YourBot_Enabled = false; /// Settings.cs:73 | public static bool YourBot_Enabled = false;
/// Settings.cs:74 | private enum ParseMode { /* [...] */, YourBot }; /// Settings.cs:74 | private enum ParseMode { /* [...] */, YourBot };
/// Settings.cs:106 | case "yourbot": pMode = ParseMode.YourBot; break; /// Settings.cs:106 | case "yourbot": pMode = ParseMode.YourBot; break;
/// Settings.cs:197 | case ParseMode.YourBot: switch (argName.ToLower()) { case "enabled": YourBot_Enabled = str2bool(argValue); break; } break; /// Settings.cs:197 | case ParseMode.YourBot: switch (argName.ToLower()) { case "enabled": YourBot_Enabled = str2bool(argValue); break; } break;
/// Settings.cs:267 | + "[YourBot]\r\n" + "enabled=false\r\n" /// Settings.cs:267 | + "[YourBot]\r\n" + "enabled=false\r\n"
/// McTcpClient:110 | if (Settings.YourBot_Enabled) { handler.BotLoad(new ChatBots.YourBot()); }
/// Here your are. Now you will have a setting in MinecraftClient.ini for enabling your brand new bot. /// Here your are. Now you will have a setting in MinecraftClient.ini for enabling your brand new bot.
/// Delete MinecraftClient.ini to re-generate it or add the lines [YourBot] and enabled=true to the existing one. /// Delete MinecraftClient.ini to re-generate it or add the lines [YourBot] and enabled=true to the existing one.
/// ///
@ -213,17 +213,6 @@ namespace MinecraftClient
return isValidName(sender); return isValidName(sender);
} }
//Detect HeroChat Messages
//[Channel] [Rank] User: Message
else if (text.StartsWith("[") && text.Contains(':') && tmp.Length > 2)
{
int name_end = text.IndexOf(':');
int name_start = text.Substring(0, name_end).LastIndexOf(']') + 2;
sender = text.Substring(name_start, name_end - name_start);
message = text.Substring(name_end + 2);
return isValidName(sender);
}
else return false; else return false;
} }
catch (IndexOutOfRangeException) { return false; } catch (IndexOutOfRangeException) { return false; }
@ -239,31 +228,46 @@ namespace MinecraftClient
protected static bool isChatMessage(string text, ref string message, ref string sender) protected static bool isChatMessage(string text, ref string message, ref string sender)
{ {
//Detect chat messages
//<Someone> message
//<*Faction Someone> message
//<*Faction Someone>: message
//<*Faction ~Nicknamed>: message
text = getVerbatim(text); text = getVerbatim(text);
if (text == "") { return false; } string[] tmp = text.Split(' ');
if (text[0] == '<') if (text.Length > 0)
{ {
try //Detect vanilla/factions Messages
//<Someone> message
//<*Faction Someone> message
//<*Faction Someone>: message
//<*Faction ~Nicknamed>: message
if (text[0] == '<')
{ {
text = text.Substring(1); try
string[] tmp = text.Split('>'); {
sender = tmp[0]; text = text.Substring(1);
message = text.Substring(sender.Length + 2); string[] tmp2 = text.Split('>');
if (message.Length > 1 && message[0] == ' ') sender = tmp2[0];
{ message = message.Substring(1); } message = text.Substring(sender.Length + 2);
tmp = sender.Split(' '); if (message.Length > 1 && message[0] == ' ')
sender = tmp[tmp.Length - 1]; { message = message.Substring(1); }
if (sender[0] == '~') { sender = sender.Substring(1); } tmp2 = sender.Split(' ');
sender = tmp2[tmp2.Length - 1];
if (sender[0] == '~') { sender = sender.Substring(1); }
return isValidName(sender);
}
catch (IndexOutOfRangeException) { return false; }
}
//Detect HeroChat Messages
//[Channel] [Rank] User: Message
else if (text[0] == '[' && text.Contains(':') && tmp.Length > 2)
{
int name_end = text.IndexOf(':');
int name_start = text.Substring(0, name_end).LastIndexOf(']') + 2;
sender = text.Substring(name_start, name_end - name_start);
message = text.Substring(name_end + 2);
return isValidName(sender); return isValidName(sender);
} }
catch (IndexOutOfRangeException) { return false; }
} }
else return false; return false;
} }
/// <summary> /// <summary>
@ -311,18 +315,11 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Disconnect from the server and restart the program /// Disconnect from the server and restart the program
/// It will unload & 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>
protected void ReconnectToTheServer() { ReconnectToTheServer(3); }
/// <summary>
/// Disconnect from the server and restart the program
/// It will unload & 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) protected void ReconnectToTheServer(int ExtraAttempts = 3)
{ {
McTcpClient.AttemptsLeft = ExtraAttempts; McTcpClient.AttemptsLeft = ExtraAttempts;
Program.Restart(); Program.Restart();
@ -369,22 +366,19 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// Get a D-M-Y 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;
return String.Format("{0}-{1}-{2} {3}:{4}:{5}",
string D = time.Day.ToString("00"); time.Year.ToString("0000"),
string M = time.Month.ToString("00"); time.Month.ToString("00"),
string Y = time.Year.ToString("0000"); time.Day.ToString("00"),
time.Hour.ToString("00"),
string h = time.Hour.ToString("00"); time.Minute.ToString("00"),
string m = time.Minute.ToString("00"); time.Second.ToString("00"));
string s = time.Second.ToString("00");
return "" + D + '-' + M + '-' + Y + ' ' + h + ':' + m + ':' + s;
} }
} }
} }

View file

@ -284,7 +284,7 @@ namespace MinecraftClient
if (Settings.chatTimeStamps) if (Settings.chatTimeStamps)
{ {
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second; int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
ConsoleIO.Write(hour.ToString("00") + ':' + minute.ToString("00") + ':' + second.ToString("00") + ' '); ConsoleIO.Write(String.Format("{0}:{1}:{2} ", hour.ToString("00"), minute.ToString("00"), second.ToString("00")));
} }
if (!acceptnewlines) { str = str.Replace('\n', ' '); } if (!acceptnewlines) { str = str.Replace('\n', ' '); }
if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(str); return; } if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(str); return; }