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

@ -1,38 +1,40 @@
/* /*
Copyright 2011 MCGalaxy Copyright 2011 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS" software distributed under the Licenses are distributed on an "AS IS"
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);
}
}
} }
if (!found) { return null; } return list.Count == 0 ? null : list.ToArray();
else { return returnthing.ToArray(); }
} }
public static string[] Find(string[] words)
{ public static string[] Find(string[] words) {
List<CommandKeywords> returnthing = CommandKeywords.all; List<string> list = new List<string>();
bool found = false;
foreach (string word in words) foreach (CommandKeywords ckw in CommandKeywords.all)
foreach (string key in ckw.Keywords)
{ {
foreach (CommandKeywords ckw in returnthing.ToArray()) for (int i = 0; i < words.Length; i++) {
{ if (key == "" || key.IndexOf(words[i], comp) < 0) continue;
bool del = true; if (!list.Contains(ckw.Cmd.name)) list.Add(ckw.Cmd.name);
foreach (string key in ckw.Keywords)
{
if (word.ToLower().Contains(key.ToLower()) && word != "" && key != "")
{
del = false;
found = true;
}
}
if (del) { returnthing.Remove(ckw); }
} }
} }
if (!found) { return null; } return list.Count == 0 ? null : list.ToArray();
else
{
List<string> k = new List<string>();
foreach (CommandKeywords ck in returnthing) { k.Add(ck.Cmd.name); }
return k.ToArray();
}
} }
} }
} }

View File

@ -53,29 +53,33 @@ 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);
if (found == null) { if (found == null) {
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) {
StringBuilder blocks = new StringBuilder(); StringBuilder blocks = new StringBuilder();
bool mode = true; bool mode = true;
for (byte id = 0; id < 255; id++) { for (byte id = 0; id < 255; id++) {
string name = Block.Name(id); string name = Block.Name(id);
if (name.ToLower() != "unknown" && name.Contains(keyword)) { if (name.ToLower() != "unknown" && name.Contains(keyword)) {
blocks.Append(mode ? "%S, &9" : "%S, &2").Append(name); blocks.Append(mode ? "%S, &9" : "%S, &2").Append(name);
mode = !mode; mode = !mode;
} }
} }
@ -122,7 +126,7 @@ namespace MCGalaxy.Commands {
foreach (FileInfo file in fi) { foreach (FileInfo file in fi) {
string level = file.Name.Replace(".lvl", ""); string level = file.Name.Replace(".lvl", "");
if (level.IndexOf(keyword, comp) >= 0) if (level.IndexOf(keyword, comp) >= 0)
searched.Append(", ").Append(level); searched.Append(", ").Append(level);
} }
if (searched.Length == 0) { Player.SendMessage(p, "No levels found containing &b" + keyword); return; } if (searched.Length == 0) { Player.SendMessage(p, "No levels found containing &b" + keyword); return; }