mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Refactor Alerts Bot + Fix crash
More efficient, succint and readable code with less bugs! Fixed crash when displaying alert under certain conditions.
This commit is contained in:
parent
4752094f1f
commit
1499f8cbfc
1 changed files with 54 additions and 83 deletions
|
|
@ -2,125 +2,96 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace MinecraftClient.ChatBots
|
namespace MinecraftClient.ChatBots
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This bot make the console beep on some specified words. Useful to detect when someone is talking to you, for example.
|
/// This bot make the console beep on some specified words. Useful to detect when someone is talking to you, for example.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
public class Alerts : ChatBot
|
public class Alerts : ChatBot
|
||||||
{
|
{
|
||||||
private string[] dictionary = new string[0];
|
private string[] dictionary = new string[0];
|
||||||
private string[] excludelist = new string[0];
|
private string[] excludelist = new string[0];
|
||||||
|
|
||||||
public override void Initialize()
|
/// <summary>
|
||||||
|
/// Import alerts from the specified file
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="file"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static string[] FromFile(string file)
|
||||||
{
|
{
|
||||||
if (System.IO.File.Exists(Settings.Alerts_MatchesFile))
|
if (File.Exists(file))
|
||||||
{
|
{
|
||||||
List<string> tmp_dictionary = new List<string>();
|
//Read all lines from file, remove lines with no text, convert to lowercase,
|
||||||
string[] file_lines = System.IO.File.ReadAllLines(Settings.Alerts_MatchesFile);
|
//remove duplicate entries, convert to a string array, and return the result.
|
||||||
foreach (string line in file_lines)
|
return File.ReadAllLines(file)
|
||||||
if (line.Trim().Length > 0 && !tmp_dictionary.Contains(line.ToLower()))
|
.Where(line => !String.IsNullOrWhiteSpace(line))
|
||||||
tmp_dictionary.Add(line.ToLower());
|
.Select(line => line.ToLower())
|
||||||
dictionary = tmp_dictionary.ToArray();
|
.Distinct().ToArray();
|
||||||
}
|
}
|
||||||
else LogToConsole("File not found: " + Settings.Alerts_MatchesFile);
|
else
|
||||||
|
|
||||||
if (System.IO.File.Exists(Settings.Alerts_ExcludesFile))
|
|
||||||
{
|
{
|
||||||
List<string> tmp_excludelist = new List<string>();
|
LogToConsole("File not found: " + Settings.Alerts_MatchesFile);
|
||||||
string[] file_lines = System.IO.File.ReadAllLines(Settings.Alerts_ExcludesFile);
|
return new string[0];
|
||||||
foreach (string line in file_lines)
|
|
||||||
if (line.Trim().Length > 0 && !tmp_excludelist.Contains(line.Trim().ToLower()))
|
|
||||||
tmp_excludelist.Add(line.ToLower());
|
|
||||||
excludelist = tmp_excludelist.ToArray();
|
|
||||||
}
|
}
|
||||||
else LogToConsole("File not found : " + Settings.Alerts_ExcludesFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Intitialize the Alerts bot
|
||||||
|
/// </summary>
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
dictionary = FromFile(Settings.Alerts_MatchesFile);
|
||||||
|
excludelist = FromFile(Settings.Alerts_ExcludesFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process text received from the server to display alerts
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">Received text</param>
|
||||||
public override void GetText(string text)
|
public override void GetText(string text)
|
||||||
{
|
{
|
||||||
text = getVerbatim(text);
|
//Remove color codes and convert to lowercase
|
||||||
string comp = text.ToLower();
|
text = getVerbatim(text).ToLower();
|
||||||
foreach (string alert in dictionary)
|
|
||||||
|
//Proceed only if no exclusions are found in text
|
||||||
|
if (!excludelist.Any(exclusion => text.Contains(exclusion)))
|
||||||
{
|
{
|
||||||
if (comp.Contains(alert))
|
//Show an alert for each alert item found in text, if any
|
||||||
|
foreach (string alert in dictionary.Where(alert => text.Contains(alert)))
|
||||||
{
|
{
|
||||||
bool ok = true;
|
if (Settings.Alerts_Beep_Enabled)
|
||||||
|
Console.Beep(); //Text found !
|
||||||
|
|
||||||
foreach (string exclusion in excludelist)
|
if (ConsoleIO.basicIO) //Using a GUI? Pass text as is.
|
||||||
|
ConsoleIO.WriteLine(text.Replace(alert, "§c" + alert + "§r"));
|
||||||
|
|
||||||
|
else //Using Consome Prompt : Print text with alert highlighted
|
||||||
{
|
{
|
||||||
if (comp.Contains(exclusion))
|
string[] splitted = text.Split(new string[] { alert }, StringSplitOptions.None);
|
||||||
|
|
||||||
|
if (splitted.Length > 0)
|
||||||
{
|
{
|
||||||
ok = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ok)
|
|
||||||
{
|
|
||||||
if (Settings.Alerts_Beep_Enabled) { Console.Beep(); } //Text found !
|
|
||||||
|
|
||||||
if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(comp.Replace(alert, "§c" + alert + "§r")); }
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
#region Displaying the text with the interesting part highlighted
|
|
||||||
|
|
||||||
Console.BackgroundColor = ConsoleColor.DarkGray;
|
Console.BackgroundColor = ConsoleColor.DarkGray;
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
ConsoleIO.Write(splitted[0]);
|
||||||
|
|
||||||
//Will be used for text displaying
|
for (int i = 1; i < splitted.Length; i++)
|
||||||
string[] temp = comp.Split(alert.Split(','), StringSplitOptions.None);
|
|
||||||
int p = 0;
|
|
||||||
|
|
||||||
//Special case : alert in the beginning of the text
|
|
||||||
string test = "";
|
|
||||||
for (int i = 0; i < alert.Length; i++)
|
|
||||||
{
|
|
||||||
test += comp[i];
|
|
||||||
}
|
|
||||||
if (test == alert)
|
|
||||||
{
|
{
|
||||||
Console.BackgroundColor = ConsoleColor.Yellow;
|
Console.BackgroundColor = ConsoleColor.Yellow;
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
for (int i = 0; i < alert.Length; i++)
|
ConsoleIO.Write(alert);
|
||||||
{
|
|
||||||
ConsoleIO.Write(text[p]);
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Displaying the rest of the text
|
|
||||||
for (int i = 0; i < temp.Length; i++)
|
|
||||||
{
|
|
||||||
Console.BackgroundColor = ConsoleColor.DarkGray;
|
Console.BackgroundColor = ConsoleColor.DarkGray;
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
for (int j = 0; j < temp[i].Length; j++)
|
ConsoleIO.Write(splitted[i]);
|
||||||
{
|
|
||||||
ConsoleIO.Write(text[p]);
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
Console.BackgroundColor = ConsoleColor.Yellow;
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
for (int j = 0; j < alert.Length; j++)
|
|
||||||
{
|
|
||||||
ConsoleIO.Write(text[p]);
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (IndexOutOfRangeException) { }
|
|
||||||
}
|
}
|
||||||
Console.BackgroundColor = ConsoleColor.Black;
|
|
||||||
Console.ForegroundColor = ConsoleColor.Gray;
|
|
||||||
ConsoleIO.Write('\n');
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.BackgroundColor = ConsoleColor.Black;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
|
ConsoleIO.Write('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue