Make /search commands more sensible, cleanup CommandKeywords class.

This commit is contained in:
UnknownShadow200 2016-03-08 17:14:04 +11:00
parent a9e9b081ff
commit 2b3a64d2e7
2 changed files with 58 additions and 71 deletions

View File

@ -14,25 +14,27 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace MCGalaxy.Commands
{ namespace MCGalaxy.Commands {
public sealed class CommandKeywords
{ public sealed class CommandKeywords {
public static List<CommandKeywords> all = new List<CommandKeywords>(); public static List<CommandKeywords> all = new List<CommandKeywords>();
public Command Cmd; public Command Cmd;
public string[] Keywords; public string[] Keywords;
public CommandKeywords(Command cmd, string key) const StringComparison comp = StringComparison.OrdinalIgnoreCase;
{
public CommandKeywords(Command cmd, string key) {
this.Cmd = cmd; this.Cmd = cmd;
string keyword = key + " " + cmd.name + " " + cmd.type; string keyword = key + " " + cmd.name + " " + cmd.type;
if (cmd.shortcut.Length > 3) { keyword += " " + cmd.shortcut; } if (cmd.shortcut.Length > 3) { keyword += " " + cmd.shortcut; }
this.Keywords = keyword.Split(' '); this.Keywords = keyword.Split(' ');
all.Add(this); all.Add(this);
} }
public static void SetKeyWords()
{ public static void SetKeyWords() {
new CommandKeywords((new CmdAbort()), "command action mode"); new CommandKeywords((new CmdAbort()), "command action mode");
new CommandKeywords((new CmdAbout()), "info block history grief"); new CommandKeywords((new CmdAbout()), "info block history grief");
new CommandKeywords((new CmdAdminChat()), "admin chat opchat"); new CommandKeywords((new CmdAdminChat()), "admin chat opchat");
@ -271,55 +273,36 @@ namespace MCGalaxy.Commands
new CommandKeywords((new CmdZombieGame()), "zombie game"); new CommandKeywords((new CmdZombieGame()), "zombie game");
new CommandKeywords((new CmdZone()), "area"); new CommandKeywords((new CmdZone()), "area");
} }
public void Addcustom(Command cmd, string keywords)
{ public void Addcustom(Command cmd, string keywords) {
new CommandKeywords(cmd, keywords); new CommandKeywords(cmd, keywords);
} }
public static string[] Find(string word)
{ public static string[] Find(string word) {
List<string> returnthing = new List<string>(); if (word == "") return null;
bool found = false; List<string> list = new List<string>();
foreach (CommandKeywords ckw in CommandKeywords.all) foreach (CommandKeywords ckw in CommandKeywords.all)
{
foreach (string key in ckw.Keywords) foreach (string key in ckw.Keywords)
{ {
if (word.ToLower().Contains(key.ToLower()) && word != "" && key != "") if (key == "" || key.IndexOf(word, comp) < 0) continue;
{ if (!list.Contains(ckw.Cmd.name)) list.Add(ckw.Cmd.name);
found = true;
returnthing.Add(ckw.Cmd.name);
} }
return list.Count == 0 ? null : list.ToArray();
} }
}
if (!found) { return null; } public static string[] Find(string[] words) {
else { return returnthing.ToArray(); } List<string> list = new List<string>();
}
public static string[] Find(string[] words) foreach (CommandKeywords ckw in CommandKeywords.all)
{
List<CommandKeywords> returnthing = CommandKeywords.all;
bool found = false;
foreach (string word in words)
{
foreach (CommandKeywords ckw in returnthing.ToArray())
{
bool del = true;
foreach (string key in ckw.Keywords) foreach (string key in ckw.Keywords)
{ {
if (word.ToLower().Contains(key.ToLower()) && word != "" && key != "") for (int i = 0; i < words.Length; i++) {
{ if (key == "" || key.IndexOf(words[i], comp) < 0) continue;
del = false; if (!list.Contains(ckw.Cmd.name)) list.Add(ckw.Cmd.name);
found = true;
} }
} }
if (del) { returnthing.Remove(ckw); } return list.Count == 0 ? null : list.ToArray();
}
}
if (!found) { return null; }
else
{
List<string> k = new List<string>();
foreach (CommandKeywords ck in returnthing) { k.Add(ck.Cmd.name); }
return k.ToArray();
}
} }
} }
} }

View File

@ -53,7 +53,9 @@ namespace MCGalaxy.Commands {
} }
static void SearchCommands(Player p, string keyword) { static void SearchCommands(Player p, string keyword) {
bool mode = true; if (keyword.Length <= 2) {
Player.SendMessage(p, "You need to enter at least three characters to search for."); return;
}
string[] keywords = keyword.Split(' '); string[] keywords = keyword.Split(' ');
string[] found = keywords.Length == 1 ? string[] found = keywords.Length == 1 ?
CommandKeywords.Find(keyword) : CommandKeywords.Find(keywords); CommandKeywords.Find(keyword) : CommandKeywords.Find(keywords);
@ -61,12 +63,14 @@ namespace MCGalaxy.Commands {
Player.SendMessage(p, "No commands found matching keyword(s): '" + keyword + "'"); return; Player.SendMessage(p, "No commands found matching keyword(s): '" + keyword + "'"); return;
} }
StringBuilder cmds = new StringBuilder();
bool mode = true;
Player.SendMessage(p, "&bCommands found: "); Player.SendMessage(p, "&bCommands found: ");
foreach (string cmd in found) { foreach (string cmd in found) {
string code = mode ? "&2/" : "&9/"; cmds.Append(mode ? "%S, &9" : "%S, &2").Append(cmd);
Player.SendMessage(p, code + cmd);
mode = !mode; mode = !mode;
} }
Player.SendMessage(p, cmds.ToString(4, cmds.Length - 4));
} }
static void SearchBlocks(Player p, string keyword) { static void SearchBlocks(Player p, string keyword) {