mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-26 06:43:25 -04:00
Make /search commands more sensible, cleanup CommandKeywords class.
This commit is contained in:
parent
a9e9b081ff
commit
2b3a64d2e7
@ -14,25 +14,27 @@
|
||||
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
or implied. See the Licenses for the specific language governing
|
||||
permissions and limitations under the Licenses.
|
||||
*/
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace MCGalaxy.Commands
|
||||
{
|
||||
public sealed class CommandKeywords
|
||||
{
|
||||
|
||||
namespace MCGalaxy.Commands {
|
||||
|
||||
public sealed class CommandKeywords {
|
||||
public static List<CommandKeywords> all = new List<CommandKeywords>();
|
||||
public Command Cmd;
|
||||
public string[] Keywords;
|
||||
public CommandKeywords(Command cmd, string key)
|
||||
{
|
||||
const StringComparison comp = StringComparison.OrdinalIgnoreCase;
|
||||
|
||||
public CommandKeywords(Command cmd, string key) {
|
||||
this.Cmd = cmd;
|
||||
string keyword = key + " " + cmd.name + " " + cmd.type;
|
||||
if (cmd.shortcut.Length > 3) { keyword += " " + cmd.shortcut; }
|
||||
this.Keywords = keyword.Split(' ');
|
||||
all.Add(this);
|
||||
}
|
||||
public static void SetKeyWords()
|
||||
{
|
||||
|
||||
public static void SetKeyWords() {
|
||||
new CommandKeywords((new CmdAbort()), "command action mode");
|
||||
new CommandKeywords((new CmdAbout()), "info block history grief");
|
||||
new CommandKeywords((new CmdAdminChat()), "admin chat opchat");
|
||||
@ -271,55 +273,36 @@ namespace MCGalaxy.Commands
|
||||
new CommandKeywords((new CmdZombieGame()), "zombie game");
|
||||
new CommandKeywords((new CmdZone()), "area");
|
||||
}
|
||||
public void Addcustom(Command cmd, string keywords)
|
||||
{
|
||||
|
||||
public void Addcustom(Command cmd, string keywords) {
|
||||
new CommandKeywords(cmd, keywords);
|
||||
}
|
||||
public static string[] Find(string word)
|
||||
{
|
||||
List<string> returnthing = new List<string>();
|
||||
bool found = false;
|
||||
|
||||
public static string[] Find(string word) {
|
||||
if (word == "") return null;
|
||||
List<string> list = new List<string>();
|
||||
|
||||
foreach (CommandKeywords ckw in CommandKeywords.all)
|
||||
{
|
||||
foreach (string key in ckw.Keywords)
|
||||
{
|
||||
if (word.ToLower().Contains(key.ToLower()) && word != "" && key != "")
|
||||
{
|
||||
found = true;
|
||||
returnthing.Add(ckw.Cmd.name);
|
||||
if (key == "" || key.IndexOf(word, comp) < 0) continue;
|
||||
if (!list.Contains(ckw.Cmd.name)) list.Add(ckw.Cmd.name);
|
||||
}
|
||||
return list.Count == 0 ? null : list.ToArray();
|
||||
}
|
||||
}
|
||||
if (!found) { return null; }
|
||||
else { return returnthing.ToArray(); }
|
||||
}
|
||||
public static string[] Find(string[] words)
|
||||
{
|
||||
List<CommandKeywords> returnthing = CommandKeywords.all;
|
||||
bool found = false;
|
||||
foreach (string word in words)
|
||||
{
|
||||
foreach (CommandKeywords ckw in returnthing.ToArray())
|
||||
{
|
||||
bool del = true;
|
||||
|
||||
public static string[] Find(string[] words) {
|
||||
List<string> list = new List<string>();
|
||||
|
||||
foreach (CommandKeywords ckw in CommandKeywords.all)
|
||||
foreach (string key in ckw.Keywords)
|
||||
{
|
||||
if (word.ToLower().Contains(key.ToLower()) && word != "" && key != "")
|
||||
{
|
||||
del = false;
|
||||
found = true;
|
||||
for (int i = 0; i < words.Length; i++) {
|
||||
if (key == "" || key.IndexOf(words[i], comp) < 0) continue;
|
||||
if (!list.Contains(ckw.Cmd.name)) list.Add(ckw.Cmd.name);
|
||||
}
|
||||
}
|
||||
if (del) { returnthing.Remove(ckw); }
|
||||
}
|
||||
}
|
||||
if (!found) { return null; }
|
||||
else
|
||||
{
|
||||
List<string> k = new List<string>();
|
||||
foreach (CommandKeywords ck in returnthing) { k.Add(ck.Cmd.name); }
|
||||
return k.ToArray();
|
||||
}
|
||||
return list.Count == 0 ? null : list.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,9 @@ namespace MCGalaxy.Commands {
|
||||
}
|
||||
|
||||
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[] found = keywords.Length == 1 ?
|
||||
CommandKeywords.Find(keyword) : CommandKeywords.Find(keywords);
|
||||
@ -61,12 +63,14 @@ namespace MCGalaxy.Commands {
|
||||
Player.SendMessage(p, "No commands found matching keyword(s): '" + keyword + "'"); return;
|
||||
}
|
||||
|
||||
StringBuilder cmds = new StringBuilder();
|
||||
bool mode = true;
|
||||
Player.SendMessage(p, "&bCommands found: ");
|
||||
foreach (string cmd in found) {
|
||||
string code = mode ? "&2/" : "&9/";
|
||||
Player.SendMessage(p, code + cmd);
|
||||
cmds.Append(mode ? "%S, &9" : "%S, &2").Append(cmd);
|
||||
mode = !mode;
|
||||
}
|
||||
Player.SendMessage(p, cmds.ToString(4, cmds.Length - 4));
|
||||
}
|
||||
|
||||
static void SearchBlocks(Player p, string keyword) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user