From 86777a2fc27588da37cd0accb52866134bf7699d Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Mon, 27 Jul 2015 17:37:21 +0200 Subject: [PATCH] Starting the remake of /plot help --- .../plot/commands/MainCommand.java | 113 ++++++++++-------- .../intellectualcrafters/plot/config/C.java | 4 +- .../plot/util/helpmenu/HelpMenu.java | 56 +++++++++ .../plot/util/helpmenu/HelpObject.java | 35 ++++++ .../plot/util/helpmenu/HelpPage.java | 40 +++++++ 5 files changed, 197 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java create mode 100644 src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpObject.java create mode 100644 src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java diff --git a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index af03a6196..8089ebbcc 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -34,6 +34,7 @@ import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.StringComparison; import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.helpmenu.HelpMenu; import com.intellectualsites.commands.Argument; import com.intellectualsites.commands.Command; import com.intellectualsites.commands.CommandHandlingOutput; @@ -90,55 +91,61 @@ public class MainCommand extends CommandManager { return false; } - public static ArrayList> getCommands(final CommandCategory category, final PlotPlayer player) { - ArrayList> cmds = instance.getCommands(); - for (Iterator> iter = cmds.iterator(); iter.hasNext();){ - Command cmd = iter.next(); - if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) { - iter.remove(); + public static List getCommands(final CommandCategory category, final PlotPlayer player) { + List commands = new ArrayList<>(); + for (Command command : instance.getCommands()) { + if (category != null && !command.getCategory().equals(category)) { + continue; } + if (player != null && !player.hasPermission(command.getPermission())) { + continue; + } + commands.add(command); } - return cmds; - } - - public static List helpMenu(final PlotPlayer player, final CommandCategory category, int page) { - List> commands; - commands = getCommands(category, player); - // final int totalPages = ((int) Math.ceil(12 * (commands.size()) / - // 100)); - final int perPage = 5; - final int totalPages = (commands.size() / perPage) + (commands.size() % perPage == 0 ? 0 : 1); - if (page > totalPages) { - page = totalPages; - } - int max = (page * perPage) + perPage; - if (max > commands.size()) { - max = commands.size(); - } - final List help = new ArrayList<>(); - help.add(C.HELP_HEADER.s()); - // HELP_CATEGORY("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"), - help.add(C.HELP_CATEGORY.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + perPage).replace("%total%", "" + commands.size())); - Command cmd; - final int start = page * perPage; - for (int x = start; x < max; x++) { - cmd = commands.get(x); - String s = C.HELP_ITEM.s(); - if (cmd.getAliases().size() > 0) { - s = s.replace("%alias%", StringMan.join(cmd.getAliases(), "|")); - } - else { - s = s.replace("%alias%", ""); - } - s = s.replace("%usage%", cmd.getUsage().contains("plot") ? cmd.getUsage() : "/plot " + cmd.getUsage()).replace("%cmd%", cmd.getCommand()).replace("%desc%", cmd.getDescription()).replace("[]", ""); - help.add(s); - } - if (help.size() < 2) { - help.add(C.NO_COMMANDS.s()); - } - return help; + return commands; } +//// public static List helpMenu(final PlotPlayer player, final CommandCategory category, int page) { +// List> commands; +// // commands = getCommands(category, player); +// // final int totalPages = ((int) Math.ceil(12 * (commands.size()) / +// // 100)); +// final int perPage = 5; +// // final int totalPages = (commands.size() / perPage) + (commands.size() % perPage == 0 ? 0 : 1); +// // if (page > totalPages) { +// // page = totalPages; +// // } +// int max = (page * perPage) + perPage; +// // if (max > commands.size()) { +// // max = commands.size(); +// // } +// final List help = new ArrayList<>(); +// help.add(C.HELP_HEADER.s()); +// // HELP_PAGE_HEADER("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"), +// // help.add(C.HELP_PAGE_HEADER.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + perPage).replace("%total%", "" + commands.size())); +// Command cmd; +// // HELP_CATEGORY("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"), +// // help.add(C.HELP_CATEGORY.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + perPage).replace("%total%", "" + commands.size())); +// // Command cmd; +// final int start = page * perPage; +// for (int x = start; x < max; x++) { +// // cmd = commands.get(x); +// String s = C.HELP_ITEM.s(); +// if (cmd.getAliases().size() > 0) { +// s = s.replace("%alias%", StringMan.join(cmd.getAliases(), "|")); +// } +// else { +// s = s.replace("%alias%", ""); +// } +// s = s.replace("%usage%", cmd.getUsage().contains("plot") ? cmd.getUsage() : "/plot " + cmd.getUsage()).replace("%cmd%", cmd.getCommand()).replace("%desc%", cmd.getDescription()).replace("[]", ""); +// help.add(s); +// } +// if (help.size() < 2) { +// help.add(C.NO_COMMANDS.s()); +// } +// return help; +// } +// public static void displayHelp(PlotPlayer player, String cat, int page) { if (cat != null && StringMan.isEqualIgnoreCase(cat, "all")) { cat = null; @@ -169,11 +176,17 @@ public class MainCommand extends CommandManager { MainUtil.sendMessage(player, builder.toString(), false); return; } - final StringBuilder help = new StringBuilder(); - for (final String string : helpMenu(player, cato, page)) { - help.append(string).append("\n"); - } - MainUtil.sendMessage(player, help.toString()); + new HelpMenu(player) + .setCategory(cato) + .getCommands() + .generateMaxPages() + .generatePage(page) + .render(); + // final StringBuilder help = new StringBuilder(); + // for (final String string : helpMenu(player, cato, page)) { + // help.append(string).append("\n"); + // } + // MainUtil.sendMessage(player, help.toString()); } public static boolean onCommand(final PlotPlayer player, final String cmd, final String... args) { diff --git a/src/main/java/com/intellectualcrafters/plot/config/C.java b/src/main/java/com/intellectualcrafters/plot/config/C.java index 8848ae51e..eacabc27d 100644 --- a/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -506,7 +506,9 @@ public enum C { * Help */ HELP_HEADER("$3====== $1Plot\u00B2 Help $3======", "Help"), - HELP_CATEGORY("$1Category: $2%category%$2,$1 Page: $2%current%$3/$2%max%$2,$1 Displaying: $2%dis%$3/$2%total%", "Help"), + HELP_PAGE_HEADER("$1Category: $2%category%$2,$1 Page: $2%current%$3/$2%max%$2", "Help"), + HELP_FOOTER("$3====== $1Plot\u00B2 Help $3======", "Help"), + HELP_INFO("$3====== $1Choose a Category $3======", false, "Help"), HELP_INFO_ITEM("$1/plots help %category% $3- $2%category_desc%", "Help"), HELP_ITEM("$1%usage% [%alias%]&- $3- $2%desc%&-", "Help"), diff --git a/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java b/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java new file mode 100644 index 000000000..7aea263c7 --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpMenu.java @@ -0,0 +1,56 @@ +package com.intellectualcrafters.plot.util.helpmenu; + +import com.intellectualcrafters.plot.commands.CommandCategory; +import com.intellectualcrafters.plot.commands.MainCommand; +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualsites.commands.Command; +import com.plotsquared.bukkit.util.bukkit.BukkitUtil; + +import java.util.List; + +public class HelpMenu { + + public static final int PER_PAGE = 5; + + private final PlotPlayer _player; + private HelpPage _page = new HelpPage(CommandCategory.ACTIONS, 0, 0); + private int _maxPage; + private CommandCategory _commandCategory; + private List _commands; + + public HelpMenu(final PlotPlayer player) { + _player = player; + } + + public HelpMenu setCategory(final CommandCategory commandCategory) { + _commandCategory = commandCategory; + return this; + } + + public HelpMenu getCommands() { + _commands = MainCommand.getCommands(_commandCategory, _player); + return this; + } + + public HelpMenu generateMaxPages() { + this._maxPage = Math.min(_commands.size() / PER_PAGE, 1); + return this; + } + + public HelpMenu generatePage(int currentPage) { + if (currentPage > _maxPage) { + currentPage = _maxPage; + } + _page = new HelpPage(_commandCategory, currentPage, _maxPage); + int max = Math.min((currentPage * PER_PAGE) + PER_PAGE, _commands.size()); + for (int i = currentPage * PER_PAGE; i < max; i++) { + _page.addHelpItem(new HelpObject(_commands.get(i))); + } + return this; + } + + public void render() { + _page.render(_player); + } + +} diff --git a/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpObject.java b/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpObject.java new file mode 100644 index 000000000..15390b0a6 --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpObject.java @@ -0,0 +1,35 @@ +package com.intellectualcrafters.plot.util.helpmenu; + +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualsites.commands.Argument; +import com.intellectualsites.commands.Command; + +public class HelpObject { + + private final Command _command; + private final String _rendered; + + public HelpObject(final Command command) { + this._command = command; + String rendered = C.HELP_ITEM.s(); + this._rendered = rendered + .replace("%usage%", _command.getUsage()) + .replace("%alias%", _command.getAliases().size() > 0 ? StringMan.join(_command.getAliases(), "|") : "") + .replace("%desc%", _command.getDescription()) + .replace("%arguments%", buildArgumentList(_command.getRequiredArguments())); // TODO Make configurable + } + + @Override + public String toString() { + return _rendered; + } + + private String buildArgumentList(Argument[] arguments) { + StringBuilder builder = new StringBuilder(); + for (final Argument argument : arguments) { + builder.append("[").append(argument.getName()).append(" (").append(argument.getExample()).append(")],"); + } + return arguments.length > 0 ? builder.substring(0, builder.length() - 1) : ""; + } +} diff --git a/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java b/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java new file mode 100644 index 000000000..d2e3e2b19 --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/helpmenu/HelpPage.java @@ -0,0 +1,40 @@ +package com.intellectualcrafters.plot.util.helpmenu; + +import com.intellectualcrafters.plot.commands.CommandCategory; +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.MainUtil; + +import java.util.HashSet; +import java.util.Set; + +public class HelpPage { + + private final Set _helpObjecs; + private final String _header; + + public HelpPage(CommandCategory category, int currentPage, int maxPages) { + _helpObjecs = new HashSet<>(); + _header = C.HELP_PAGE_HEADER.s() + .replace("%category%", category == null ? "ALL" : category.toString()) + .replace("%current%", currentPage + "") + .replace("%max%", maxPages + ""); + } + + public void render(final PlotPlayer player) { + if (_helpObjecs.size() < 2) { + MainUtil.sendMessage(player, C.NO_COMMANDS.s(), false); + } else { + MainUtil.sendMessage(player, C.HELP_HEADER.s(), false); + MainUtil.sendMessage(player, _header, false); + for (final HelpObject object : _helpObjecs) { + MainUtil.sendMessage(player, object.toString(), false); + } + MainUtil.sendMessage(player, C.HELP_FOOTER.s(), false); + } + } + + public void addHelpItem(final HelpObject object) { + _helpObjecs.add(object); + } +}