Starting the remake of /plot help

This commit is contained in:
Sauilitired 2015-07-27 17:37:21 +02:00
parent 870495ac1a
commit 86777a2fc2
5 changed files with 197 additions and 51 deletions

View File

@ -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<PlotPlayer> {
return false;
}
public static ArrayList<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) {
ArrayList<Command<PlotPlayer>> cmds = instance.getCommands();
for (Iterator<Command<PlotPlayer>> iter = cmds.iterator(); iter.hasNext();){
Command<PlotPlayer> cmd = iter.next();
if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) {
iter.remove();
public static List<Command> getCommands(final CommandCategory category, final PlotPlayer player) {
List<Command> 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<String> helpMenu(final PlotPlayer player, final CommandCategory category, int page) {
List<Command<PlotPlayer>> 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<String> 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<PlotPlayer> 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<String> helpMenu(final PlotPlayer player, final CommandCategory category, int page) {
// List<Command<PlotPlayer>> 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<String> 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<PlotPlayer> 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<PlotPlayer> {
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) {

View File

@ -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"),

View File

@ -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<Command> _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);
}
}

View File

@ -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) : "";
}
}

View File

@ -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<HelpObject> _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);
}
}