mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-29 04:04:43 +02:00
Merge remote-tracking branch 'origin/master'
Conflicts: src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java
This commit is contained in:
@ -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<PlotPlayer>> _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);
|
||||
}
|
||||
|
||||
}
|
@ -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) : "";
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user