Now /search uses multi page output.

This commit is contained in:
UnknownShadow200 2016-09-15 09:35:05 +10:00
parent 475791b12e
commit 04f690e061
3 changed files with 85 additions and 73 deletions

View File

@ -29,93 +29,106 @@ namespace MCGalaxy.Commands {
const StringComparison comp = StringComparison.OrdinalIgnoreCase; const StringComparison comp = StringComparison.OrdinalIgnoreCase;
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
string[] args = message.SplitSpaces(2); string[] args = message.SplitSpaces(3);
if (args.Length < 2) { Help(p); return; } if (args.Length < 2) { Help(p); return; }
args[0] = args[0].ToLower(); args[0] = args[0].ToLower();
string keyword = args[1]; string keyword = args[1];
string modifier = args.Length > 2 ? args[2] : "";
if (args[0] == "block" || args[0] == "blocks") { if (args[0] == "block" || args[0] == "blocks") {
SearchBlocks(p, keyword); SearchBlocks(p, keyword, modifier);
} else if (args[0] == "rank" || args[0] == "ranks") { } else if (args[0] == "rank" || args[0] == "ranks") {
SearchRanks(p, keyword); SearchRanks(p, keyword, modifier);
} else if (args[0] == "user" || args[0] == "users" || args[0] == "player" || args[0] == "players") { } else if (args[0] == "user" || args[0] == "users" || args[0] == "player" || args[0] == "players") {
SearchPlayers(p, keyword); SearchPlayers(p, keyword, modifier);
} else if (args[0] == "loaded") { } else if (args[0] == "loaded") {
SearchLoaded(p, keyword); SearchLoaded(p, keyword, modifier);
} else if (args[0] == "level" || args[0] == "levels") { } else if (args[0] == "level" || args[0] == "levels") {
SearchUnloaded(p, keyword); SearchUnloaded(p, keyword, modifier);
} else { } else {
Help(p); Help(p);
} }
} }
static void SearchBlocks(Player p, string keyword) { static void SearchBlocks(Player p, string keyword, string modifier) {
StringBuilder blocks = new StringBuilder(); List<string> blocks = new List<string>();
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.CaselessContains(keyword) && !name.CaselessEq("unknown"))
blocks.Append(mode ? "%S, &9" : "%S, &2").Append(name); blocks.Add(name);
mode = !mode;
}
} }
if (blocks.Length == 0) { Player.Message(p, "No blocks found containing &b" + keyword); return; }
Player.Message(p, blocks.ToString(4, blocks.Length - 4)); OutputList(p, keyword, "search blocks", "blocks",
modifier, blocks, FormatBlockName);
} }
static void SearchRanks(Player p, string keyword) { static string FormatBlockName(string block, int i) {
StringBuilder ranks = new StringBuilder(); string prefix = (i & 1) == 0 ? "&2" : "&9";
return prefix + block;
}
static void SearchRanks(Player p, string keyword, string modifier) {
List<string> ranks = new List<string>();
foreach (Group g in Group.GroupList) { foreach (Group g in Group.GroupList) {
if (g.name.IndexOf(keyword, comp) >= 0) if (g.name.Contains(keyword))
ranks.Append(", ").Append(g.ColoredName); ranks.Add(g.ColoredName);
} }
if (ranks.Length == 0) { Player.Message(p, "No ranks found containing &b" + keyword); return; }
Player.Message(p, ranks.ToString(2, ranks.Length - 2)); OutputList(p, keyword, "search ranks", "ranks",
modifier, ranks, (name, i) => name);
} }
static void SearchPlayers(Player p, string keyword) { static void SearchPlayers(Player p, string keyword, string modifier) {
StringBuilder players = new StringBuilder(); List<string> players = new List<string>();
Player[] online = PlayerInfo.Online.Items; Player[] online = PlayerInfo.Online.Items;
foreach (Player who in online) { foreach (Player who in online) {
if (who.name.IndexOf(keyword, comp) >= 0 && Entities.CanSee(p, who)) if (who.name.CaselessContains(keyword) && Entities.CanSee(p, who))
players.Append(", ").Append(who.color).Append(who.name); players.Add(who.ColoredName);
} }
if (players.Length == 0) { Player.Message(p, "No usernames found containing &b" + keyword); return; }
Player.Message(p, players.ToString(2, players.Length - 2)); OutputList(p, keyword, "search players", "players",
modifier, players, (name, i) => name);
} }
static void SearchLoaded(Player p, string keyword) { static void SearchLoaded(Player p, string keyword, string modifier) {
StringBuilder levels = new StringBuilder(); List<string> levels = new List<string>();
Level[] loaded = LevelInfo.Loaded.Items; Level[] loaded = LevelInfo.Loaded.Items;
foreach (Level level in loaded) { foreach (Level level in loaded) {
if (level.name.IndexOf(keyword, comp) >= 0) if (level.name.CaselessContains(level.name))
levels.Append(", ").Append(level.name); levels.Add(level.name);
} }
if (levels.Length == 0) { Player.Message(p, "No loaded levels found containing &b" + keyword); return; }
Player.Message(p, levels.ToString(2, levels.Length - 2)); OutputList(p, keyword, "search loaded", "loaded levels",
modifier, levels, (level, i) => level);
} }
static void SearchUnloaded(Player p, string keyword) { static void SearchUnloaded(Player p, string keyword, string modifier) {
List<string> matches = new List<string>(); List<string> maps = new List<string>();
string[] files = Directory.GetFiles("levels", "*.lvl"); string[] files = Directory.GetFiles("levels", "*.lvl");
foreach (string file in files) { foreach (string file in files) {
string level = Path.GetFileNameWithoutExtension(file); string map = Path.GetFileNameWithoutExtension(file);
if (level.IndexOf(keyword, comp) >= 0) if (map.CaselessContains(keyword)) maps.Add(map);
matches.Add(level);
} }
if (matches.Count == 0) OutputList(p, keyword, "search levels", "maps",
Player.Message(p, "No levels found containing &b" + keyword); modifier, maps, (map, i) => map);
else }
Player.Message(p, matches.Join());
static void OutputList<T>(Player p, string keyword, string cmd, string type, string modifier,
List<T> items, Func<T, int, string> formatter) {
if (items.Count == 0) {
Player.Message(p, "No {0} found containing \"{1}\"", type, keyword);
} else {
MultiPageOutput.Output(p, items, formatter, cmd + " " + keyword, type, modifier, false);
}
} }
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/search blocks <keyword> %H- finds blocks with that keyword"); Player.Message(p, "%T/search blocks [keyword] %H- finds blocks with that keyword");
Player.Message(p, "%T/search ranks <keyword> %H- finds blocks with that keyword"); Player.Message(p, "%T/search ranks [keyword] %H- finds blocks with that keyword");
Player.Message(p, "%T/search players <keyword> %H- find players with that keyword"); Player.Message(p, "%T/search players [keyword] %H- find players with that keyword");
Player.Message(p, "%T/search loaded <keyword> %H- finds loaded levels with that keyword"); Player.Message(p, "%T/search loaded [keyword] %H- finds loaded levels with that keyword");
Player.Message(p, "%T/search levels <keyword> %H- find all levels with that keyword"); Player.Message(p, "%T/search levels [keyword] %H- find all levels with that keyword");
} }
} }
} }

View File

@ -54,8 +54,8 @@ namespace MCGalaxy.Commands.Building {
struct DrawArgs { public byte block, extBlock, newBlock, newExtBlock; } struct DrawArgs { public byte block, extBlock, newBlock, newExtBlock; }
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/outline [type] [type2]"); Player.Message(p, "%T/outline [block] [block2]");
Player.Message(p, "%HOutlines [type] with [type2]"); Player.Message(p, "%HOutlines [block] with [block2]");
} }
} }
} }

View File

@ -100,10 +100,9 @@ namespace MCGalaxy {
const StringComparison comp = StringComparison.OrdinalIgnoreCase; const StringComparison comp = StringComparison.OrdinalIgnoreCase;
public static bool CaselessEq(this string a, string b) { return a.Equals(b, comp); } public static bool CaselessEq(this string a, string b) { return a.Equals(b, comp); }
public static bool CaselessStarts(this string a, string b) { return a.StartsWith(b, comp); }
public static bool CaselessStarts(this string a, string b) { return a.StartsWith(b, comp); } public static bool CaselessEnds(this string a, string b) { return a.EndsWith(b, comp); }
public static bool CaselessContains(this string a, string b) { return a.IndexOf(b, comp) >= 0; }
public static bool CaselessEnds(this string a, string b) { return a.EndsWith(b, comp); }
public static bool CaselessContains(this List<string> items, string value) { public static bool CaselessContains(this List<string> items, string value) {
foreach (string item in items) { foreach (string item in items) {