Skip to content

Commit

Permalink
Redesign and simplify Paginator API
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 4, 2022
1 parent 744ac54 commit 602b46f
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 101 deletions.
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Bots/CmdBotAI.cs
Expand Up @@ -110,8 +110,8 @@ public sealed class CmdBotAI : Command2 {

void HandleList(Player p, string modifier) {
string[] files = Directory.GetFiles("bots");
MultiPageOutput.Output(p, files, f => Path.GetFileName(f),
"BotAI list", "bot AIs", modifier, false);
Paginator.Output(p, files, f => Path.GetFileName(f),
"BotAI list", "bot AIs", modifier);
}

void HandleInfo(Player p, string ai) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Bots/CmdBots.cs
Expand Up @@ -41,7 +41,7 @@ public sealed class CmdBots : Command2 {
string modifier = args.Length > offset ? args[offset] : "";

p.Message("Bots in " + lvl.ColoredName + ":");
MultiPageOutput.Output(p, bots, FormatBot, cmd, "Bots", modifier, false);
Paginator.Output(p, bots, FormatBot, cmd, "Bots", modifier);
}

static string FormatBot(PlayerBot bot) {
Expand Down
15 changes: 9 additions & 6 deletions MCGalaxy/Commands/CPE/CmdCustomColors.cs
Expand Up @@ -79,19 +79,22 @@ public sealed class CmdCustomColors : Command2 {
}

static void ListHandler(Player p, string cmd, string modifier) {
List<ColorDesc> validCols = new List<ColorDesc>(Colors.List.Length);
foreach (ColorDesc col in Colors.List) {
if (col.IsModified()) validCols.Add(col);
List<ColorDesc> validColors = new List<ColorDesc>(Colors.List.Length);
foreach (ColorDesc color in Colors.List)
{
if (color.IsModified()) validColors.Add(color);
}
MultiPageOutput.Output(p, validCols, FormatColor, cmd, "Colors", modifier, true);

Paginator.Output(p, validColors, PrintColor,
cmd, "Colors", modifier);
}

// Not very elegant, because we don't want the % to be escaped like everywhere else
internal static string FormatColor(ColorDesc col) {
internal static void PrintColor(Player p, ColorDesc col) {
string format = "{0} &{1}({2})&S - %&S{1}, falls back to &{3}%&{3}{3}";
if (col.Code == col.Fallback) format = "{0} &{1}({2})&S - %&S{1}";

return string.Format(format, col.Name, col.Code, Utils.Hex(col.R, col.G, col.B), col.Fallback);
p.Message(format, col.Name, col.Code, Utils.Hex(col.R, col.G, col.B), col.Fallback);
}

void EditHandler(Player p, string[] args) {
Expand Down
8 changes: 4 additions & 4 deletions MCGalaxy/Commands/CPE/CustomBlockCommand.cs
Expand Up @@ -271,12 +271,12 @@ internal static class CustomBlockCommand {
if (!ExistsInScope(def, block, global)) continue;
defsInScope.Add(def);
}
MultiPageOutput.Output(p, defsInScope, FormatBlock, cmd.Substring(1) + " list",
"custom blocks", modifier, true);
Paginator.Output(p, defsInScope, PrintBlock,
cmd.Substring(1) + " list", "custom blocks", modifier);
}

static string FormatBlock(BlockDefinition def) {
return "Custom block &T" + def.RawID + " &Shas name &T" + def.Name;
static void PrintBlock(Player p, BlockDefinition def) {
p.Message("Custom block &T{0} &Shas name &T{1}", def.RawID, def.Name);
}

static bool DoRemove(Player p, Level lvl, BlockID block,
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Fun/CmdTeam.cs
Expand Up @@ -203,8 +203,8 @@ public sealed class CmdTeam : Command2 {

void HandleList(Player p, string[] args) {
string modifier = args.Length > 1 ? args[1] : "";
MultiPageOutput.Output(p, Team.Teams, team => team.Color + team.Name,
"team list", "teams", modifier, false);
Paginator.Output(p, Team.Teams, team => team.Color + team.Name,
"team list", "teams", modifier);
}

public override void Help(Player p) {
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Information/CmdBlocks.cs
Expand Up @@ -64,8 +64,8 @@ public sealed class CmdBlocks : Command2 {
if (Block.ExistsFor(p, b) && selector(b)) blocks.Add(b);
}

MultiPageOutput.Output(p, blocks, b => FormatBlockName(p, b),
"Blocks " + type, "blocks", modifier, false);
Paginator.Output(p, blocks, b => FormatBlockName(p, b),
"Blocks " + type, "blocks", modifier);
}

internal static string FormatBlockName(Player p, BlockID block) {
Expand Down
21 changes: 9 additions & 12 deletions MCGalaxy/Commands/Information/CmdCommands.cs
Expand Up @@ -70,9 +70,9 @@ public sealed class CmdCommands : Command2 {
shortcuts.Add(cmd);
}

MultiPageOutput.Output(p, shortcuts,
(cmd) => "&b" + cmd.shortcut + " &S[" + cmd.name + "]",
"Commands shortcuts", "shortcuts", modifier, false);
Paginator.Output(p, shortcuts,
(cmd) => "&b" + cmd.shortcut + " &S[" + cmd.name + "]",
"Commands shortcuts", "shortcuts", modifier);
}

static void PrintRankCommands(Player p, string sort, string modifier, Group group, bool own) {
Expand All @@ -93,9 +93,8 @@ public sealed class CmdCommands : Command2 {

string type = "Cmds " + group.Name;
if (sort.Length > 0) type += " " + sort;
MultiPageOutput.Output(p, cmds,
(cmd) => CmdHelp.GetColor(cmd) + cmd.name,
type, "commands", modifier, false);
Paginator.Output(p, cmds, (cmd) => CmdHelp.GetColor(cmd) + cmd.name,
type, "commands", modifier);
p.Message("Type &T/Help <command> &Sfor more help on a command.");
}

Expand All @@ -106,9 +105,8 @@ public sealed class CmdCommands : Command2 {

string type = "Commands all";
if (sort.Length > 0) type += " " + sort;
MultiPageOutput.Output(p, cmds,
(cmd) => CmdHelp.GetColor(cmd) + cmd.name,
type, "commands", modifier, false);
Paginator.Output(p, cmds, (cmd) => CmdHelp.GetColor(cmd) + cmd.name,
type, "commands", modifier);
p.Message("Type &T/Help <command> &Sfor more help on a command.");
}

Expand Down Expand Up @@ -139,9 +137,8 @@ public sealed class CmdCommands : Command2 {

type = "Commands " + type;
if (sort.Length > 0) type += " " + sort;
MultiPageOutput.Output(p, cmds,
(cmd) => CmdHelp.GetColor(cmd) + cmd.name,
type, "commands", modifier, false);
Paginator.Output(p, cmds, (cmd) => CmdHelp.GetColor(cmd) + cmd.name,
type, "commands", modifier);

p.Message("Type &T/Help <command> &Sfor more help on a command.");
return true;
Expand Down
26 changes: 14 additions & 12 deletions MCGalaxy/Commands/Information/CmdHelp.cs
Expand Up @@ -71,18 +71,19 @@ public sealed class CmdHelp : Command2 {
p.Message("Colors Available:");

p.Message("0 - &0{0} &S| 1 - &1{1} &S| 2 - &2{2} &S| 3 - &3{3}",
Colors.Name('0'), Colors.Name('1'), Colors.Name('2'), Colors.Name('3'));
Colors.Name('0'), Colors.Name('1'), Colors.Name('2'), Colors.Name('3'));
p.Message("4 - &4{0} &S| 5 - &5{1} &S| 6 - &6{2} &S| 7 - &7{3}",
Colors.Name('4'), Colors.Name('5'), Colors.Name('6'), Colors.Name('7'));
Colors.Name('4'), Colors.Name('5'), Colors.Name('6'), Colors.Name('7'));

p.Message("8 - &8{0} &S| 9 - &9{1} &S| a - &a{2} &S| b - &b{3}",
Colors.Name('8'), Colors.Name('9'), Colors.Name('a'), Colors.Name('b'));
Colors.Name('8'), Colors.Name('9'), Colors.Name('a'), Colors.Name('b'));
p.Message("c - &c{0} &S| d - &d{1} &S| e - &e{2} &S| f - &f{3}",
Colors.Name('c'), Colors.Name('d'), Colors.Name('e'), Colors.Name('f'));
Colors.Name('c'), Colors.Name('d'), Colors.Name('e'), Colors.Name('f'));

foreach (ColorDesc col in Colors.List) {
if (col.Undefined || Colors.IsStandard(col.Code)) continue;
p.Message(CmdCustomColors.FormatColor(col));
foreach (ColorDesc color in Colors.List)
{
if (color.Undefined || Colors.IsStandard(color.Code)) continue;
CmdCustomColors.PrintColor(p, color);
}
}

Expand All @@ -92,16 +93,17 @@ public sealed class CmdHelp : Command2 {

string[] args = message.SplitSpaces(2);
string modifier = args.Length > 1 ? args[1] : "";
MultiPageOutput.Output(p, emotes, FormatEmote,
"Help emotes", "emotes", modifier, true);
Paginator.Output(p, emotes, PrintEmote,
"Help emotes", "emotes", modifier);
}

static string FormatEmote(char emote) {
static void PrintEmote(Player p, char emote) {
List<string> keywords = new List<string>();
foreach (var kvp in EmotesHandler.Keywords) {
foreach (var kvp in EmotesHandler.Keywords)
{
if (kvp.Value == emote) keywords.Add("(&S" + kvp.Key + ")");
}
return "&f" + emote + " &S- " + keywords.Join();
p.Message("&f{0} &S- {1}", emote, keywords.Join());
}

bool ParseCommand(Player p, string message) {
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Information/CmdLevels.cs
Expand Up @@ -34,8 +34,8 @@ public sealed class CmdLevels : Command2 {
Array.Sort(files);

p.Message("Levels (&c[no] &Sif not visitable):");
MultiPageOutput.Output(p, files, (file) => FormatMap(p, file),
"Levels", "levels", message, false);
Paginator.Output(p, files, (file) => FormatMap(p, file),
"Levels", "levels", message);
}

static string FormatMap(Player p, string file) {
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Information/CmdLoaded.cs
Expand Up @@ -26,8 +26,8 @@ public sealed class CmdLoaded : Command2 {
public override void Use(Player p, string message, CommandData data) {
Level[] loaded = LevelInfo.Loaded.Items;
p.Message("Loaded levels [physics level] (&c[no] &Sif not visitable): ");
MultiPageOutput.Output(p, loaded, (lvl) => FormatMap(p, lvl),
"Levels", "levels", message, false);
Paginator.Output(p, loaded, (lvl) => FormatMap(p, lvl),
"Levels", "levels", message);
p.Message("Use &T/Levels &Sfor all levels.");
}

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Information/CmdSearch.cs
Expand Up @@ -105,7 +105,7 @@ public class CmdSearch : Command2 {
if (items.Count == 0) {
p.Message("No {0} found containing \"{1}\"", type, keyword);
} else {
MultiPageOutput.Output(p, items, item => item, cmd + " " + keyword, type, modifier, false);
Paginator.Output(p, items, item => item, cmd + " " + keyword, type, modifier);
}
}

Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Moderation/CmdReport.cs
Expand Up @@ -61,8 +61,8 @@ public sealed class CmdReport : Command2 {
if (users.Length > 0) {
p.Message("The following players have been reported:");
string modifier = args.Length > 1 ? args[1] : "";
MultiPageOutput.Output(p, users, pl => p.FormatNick(pl),
"Review list", "players", modifier, false);
Paginator.Output(p, users, pl => p.FormatNick(pl),
"Review list", "players", modifier);

p.Message("Use &T/Report check [player] &Sto view report details.");
p.Message("Use &T/Report delete [player] &Sto delete a report");
Expand Down
12 changes: 7 additions & 5 deletions MCGalaxy/Commands/Moderation/ZoneCmds.cs
Expand Up @@ -229,13 +229,15 @@ public sealed class CmdZoneList : Command2 {

public override void Use(Player p, string message, CommandData data) {
Zone[] zones = p.level.Zones.Items;
MultiPageOutput.Output(p, zones, FormatZone, "ZoneList", "zones", message, true);
Paginator.Output(p, zones, PrintZone,
"ZoneList", "zones", message);
}

static string FormatZone(Zone zone) {
return zone.ColoredName
+ " &b- (" + zone.MinX + ", " + zone.MinY + ", " + zone.MinZ
+ ") to (" + zone.MaxX + ", " + zone.MaxY + ", " + zone.MaxZ + ")";
static void PrintZone(Player p, Zone zone) {
p.Message("{0} &b- ({1}, {2}, {3}) to ({4}, {5}, {6})",
zone.ColoredName,
zone.MinX, zone.MinY, zone.MinZ,
zone.MaxX, zone.MaxY, zone.MaxZ);
}

public override void Help(Player p) {
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/Scripting/CmdPlugin.cs
Expand Up @@ -38,8 +38,8 @@ public sealed class CmdPlugin : Command2
string modifier = args.Length > 1 ? args[1] : "";

p.Message("Loaded plugins:");
MultiPageOutput.Output(p, Plugin.all, pl => pl.name,
"Plugins", "plugins", modifier, false);
Paginator.Output(p, Plugin.all, pl => pl.name,
"Plugins", "plugins", modifier);
return;
}
if (args.Length == 1) { Help(p); return; }
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/World/CmdBlockProperties.cs
Expand Up @@ -132,8 +132,8 @@ public sealed class CmdBlockProperties : Command2 {
string cmd = "BlockProps " + args[0] + " list";
string modifier = args.Length > 2 ? args[2] : "";

MultiPageOutput.Output(p, filtered, b => BlockProps.ScopedName(scope, p, b),
cmd, "modified blocks", modifier, false);
Paginator.Output(p, filtered, b => BlockProps.ScopedName(scope, p, b),
cmd, "modified blocks", modifier);
}

void CopyProps(Player p, BlockProps[] scope, BlockID block, string[] args) {
Expand Down
8 changes: 5 additions & 3 deletions MCGalaxy/Commands/World/CmdWarp.cs
Expand Up @@ -33,9 +33,10 @@ public class CmdWarp : Command2 {
UseCore(p, message, data, WarpList.Global, "Warp");
}

static string FormatWarp(Warp warp) {
static void PrintWarp(Player p, Warp warp) {
Vec3S32 pos = warp.Pos.BlockCoords;
return warp.Name + " - (" + pos.X + ", " + pos.Y + ", " + pos.Z + ") on " + warp.Level;
p.Message("{0} - ({1}, {2}, {3}) on {4}",
warp.Name, pos.X, pos.Y, pos.Z, warp.Level);
}

protected void UseCore(Player p, string message, CommandData data,
Expand All @@ -47,7 +48,8 @@ public class CmdWarp : Command2 {

if (IsListCommand(cmd)) {
string modifier = args.Length > 1 ? args[1] : "";
MultiPageOutput.Output(p, warps.Items, FormatWarp, group + " list", group + "s", modifier, true);
Paginator.Output(p, warps.Items, PrintWarp,
group + " list", group + "s", modifier);
return;
} else if (args.Length == 1) {
Warp warp = Matcher.FindWarps(p, warps, cmd);
Expand Down
8 changes: 4 additions & 4 deletions MCGalaxy/Commands/building/CmdPalette.cs
Expand Up @@ -143,12 +143,12 @@ public sealed class CmdPalette : Command2 {
}

string modifer = args.Length > 2 ? args[2] : "";
MultiPageOutput.Output(p, palette.Entries, (e) => FormatEntry(e, p),
"Palette entries", "entries", modifer, true);
Paginator.Output(p, palette.Entries, PrintEntry,
"Palette entries", "entries", modifer);
}

static string FormatEntry(PaletteEntry e, Player p) {
return Block.GetName(p, e.Block) + " - " + Utils.Hex(e.R, e.G, e.B);
static void PrintEntry(Player p, PaletteEntry e) {
p.Message("{0} - {1}", Block.GetName(p, e.Block), Utils.Hex(e.R, e.G, e.B));
}

public override void Help(Player p) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/MCGalaxy_.csproj
Expand Up @@ -656,7 +656,7 @@
<Compile Include="util\FastList.cs" />
<Compile Include="util\Formatting\Formatter.cs" />
<Compile Include="util\Formatting\Matcher.cs" />
<Compile Include="util\Formatting\MultiPageOutput.cs" />
<Compile Include="util\Formatting\Paginator.cs" />
<Compile Include="util\IO\FileIO.cs" />
<Compile Include="Server\Paths.cs" />
<Compile Include="util\IO\TextFile.cs" />
Expand Down
11 changes: 6 additions & 5 deletions MCGalaxy/Modules/Awards/CmdAwards.cs
Expand Up @@ -42,19 +42,20 @@ public sealed class CmdAwards : Command2
if (awards.Count == 0) { p.Message("This server has no awards yet."); return; }

List<string> playerAwards = PlayerAwards.Get(name);
StringFormatter<Award> formatter = (award) => FormaAward(award, playerAwards);
ItemPrinter<Award> printer = (p_, award) => PrintAward(p_, award, playerAwards);

string cmd = name.Length == 0 ? "awards" : "awards " + name;
string modifier = args.Length > offset ? args[offset] : "";

p.Message("Awards {0} &Shas:", p.FormatNick(name));
MultiPageOutput.Output(p, awards, formatter,
cmd, "Awards", modifier, true);
Paginator.Output(p, awards, printer,
cmd, "Awards", modifier);
}

static string FormaAward(Award award, List<string> awards) {
static void PrintAward(Player p, Award award, List<string> awards) {
bool has = awards != null && awards.CaselessContains(award.Name);
return (has ? " &a" : " &c") + award.Name + ": &7" + award.Description;
p.Message(" {0}{1}: &7{2}",
has ? "&a" : "&c", award.Name, award.Description);
}

public override void Help(Player p) {
Expand Down
9 changes: 4 additions & 5 deletions MCGalaxy/Player/List/PlayerList.cs
Expand Up @@ -98,9 +98,8 @@ public class PlayerList {
p.Message("There are no {0}.", group);
} else {
p.Message("{0}:", group.Capitalize());
MultiPageOutput.Output(p, list,
(name) => p.FormatNick(name),
listCmd, "players", modifier, false);
Paginator.Output(p, list, (name) => p.FormatNick(name),
listCmd, "players", modifier);
}
}

Expand All @@ -112,8 +111,8 @@ public class PlayerList {
p.Message("There are no {0}.", group);
} else {
p.Message("{0}:", group.Capitalize());
MultiPageOutput.Output(p, list, (name) => name,
listCmd, "players", modifier, false);
Paginator.Output(p, list, (name) => name,
listCmd, "players", modifier);
}
}

Expand Down

0 comments on commit 602b46f

Please sign in to comment.