mirror of
https://github.com/aikar/commands.git
synced 2026-06-22 23:00:36 +00:00
start with abstracting help messages out
This commit is contained in:
@@ -56,7 +56,7 @@ public class CommandHelp {
|
||||
Set<RegisteredCommand> seen = new HashSet<>();
|
||||
subCommands.entries().forEach(e -> {
|
||||
String key = e.getKey();
|
||||
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHUNKNOWN)){
|
||||
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHUNKNOWN)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -106,9 +106,9 @@ public class CommandHelp {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public boolean isExactMatch(String command){
|
||||
public boolean isExactMatch(String command) {
|
||||
for (HelpEntry helpEntry : helpEntries) {
|
||||
if(helpEntry.getCommand().endsWith(" " + command)){
|
||||
if (helpEntry.getCommand().endsWith(" " + command)) {
|
||||
selectedEntry = helpEntry;
|
||||
return true;
|
||||
}
|
||||
@@ -117,15 +117,11 @@ public class CommandHelp {
|
||||
}
|
||||
|
||||
public void showHelp() {
|
||||
showHelp(issuer, MessageKeys.HELP_FORMAT);
|
||||
showHelp(issuer);
|
||||
}
|
||||
|
||||
public void showHelp(CommandIssuer issuer) {
|
||||
showHelp(issuer, MessageKeys.HELP_FORMAT);
|
||||
}
|
||||
|
||||
public void showHelp(CommandIssuer issuer, MessageKeyProvider format) {
|
||||
if(selectedEntry != null){
|
||||
if (selectedEntry != null) {
|
||||
showDetailedHelp(selectedEntry, issuer);
|
||||
return;
|
||||
}
|
||||
@@ -140,20 +136,19 @@ public class CommandHelp {
|
||||
results = helpEntries.iterator();
|
||||
}
|
||||
int totalResults = helpEntries.size();
|
||||
int min = (this.page-1) * this.perPage; // TODO: per page configurable?
|
||||
int min = (this.page - 1) * this.perPage; // TODO: per page configurable?
|
||||
int max = min + this.perPage;
|
||||
int totalPages = (int) Math.ceil((float) totalResults / (float) this.perPage);
|
||||
int i = 0;
|
||||
if (min >= totalResults) {
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_NO_RESULTS);
|
||||
return;
|
||||
}
|
||||
|
||||
if(search == null){
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, "{command}", commandName);
|
||||
}else{
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER,
|
||||
"{command}", commandName,
|
||||
"{search}", String.join(" ", search));
|
||||
if (search == null) {
|
||||
manager.getHelpFormatter().printHelpHeader(issuer, commandName, page, totalPages, totalResults);
|
||||
} else {
|
||||
manager.getHelpFormatter().printSearchHeader(issuer, commandName, page, totalPages, totalResults, search);
|
||||
}
|
||||
|
||||
while (results.hasNext()) {
|
||||
@@ -165,61 +160,33 @@ public class CommandHelp {
|
||||
continue;
|
||||
}
|
||||
|
||||
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, format, getFormatReplacements(e));
|
||||
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
|
||||
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
|
||||
if (search == null) {
|
||||
manager.getHelpFormatter().printHelpLine(issuer, commandName, e);
|
||||
} else {
|
||||
manager.getHelpFormatter().printSearchLine(issuer, commandName, e, e.getSearchScore());
|
||||
}
|
||||
}
|
||||
if (min > 0 || results.hasNext()) {
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION,
|
||||
"{page}", "" + this.page,
|
||||
"{totalpages}", ""+ (int)Math.ceil((float)totalResults / (float)this.perPage),
|
||||
"{results}", "" + totalResults
|
||||
);
|
||||
|
||||
boolean lastPage = !(min > 0 || results.hasNext());
|
||||
if (search == null) {
|
||||
manager.getHelpFormatter().printHelpFooter(issuer, commandName, page, totalPages, totalResults, lastPage);
|
||||
} else {
|
||||
manager.getHelpFormatter().printSearchFooter(issuer, commandName, page, totalPages, totalResults, search, lastPage);
|
||||
}
|
||||
}
|
||||
|
||||
public void showDetailedHelp(HelpEntry page, CommandIssuer issuer){
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER, "{command}", page.getCommand());
|
||||
public void showDetailedHelp(HelpEntry page, CommandIssuer issuer) {
|
||||
// header
|
||||
manager.getHelpFormatter().printDetailedHelpHeader(issuer, commandName, page);
|
||||
|
||||
// normal help line
|
||||
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page));
|
||||
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
|
||||
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
|
||||
}
|
||||
manager.getHelpFormatter().printHelpLine(issuer, commandName, page);
|
||||
|
||||
// additionally detailed help for params
|
||||
page.getParameters().forEach((cmd, help) -> {
|
||||
String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_FORMAT, getDetailedFormatReplacements(cmd, help, page));
|
||||
for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
|
||||
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
|
||||
}
|
||||
});
|
||||
}
|
||||
page.getParameters().forEach((cmd, help) -> manager.getHelpFormatter().printDetailedHelpLine(issuer, commandName, page, cmd, help));
|
||||
|
||||
/**
|
||||
* Override this to control replacements
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@NotNull
|
||||
public String[] getFormatReplacements(HelpEntry e) {
|
||||
//{command} {parameters} {separator} {description}
|
||||
return new String[] {
|
||||
"{command}", e.getCommand(),
|
||||
"{parameters}", e.getParameterSyntax(),
|
||||
"{separator}", e.getDescription().isEmpty() ? "" : "-",
|
||||
"{description}", e.getDescription()
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String[] getDetailedFormatReplacements(String cmd, String help, HelpEntry page) {
|
||||
//{name} {description}
|
||||
return new String[] {
|
||||
"{name}", cmd,
|
||||
"{description}", help
|
||||
};
|
||||
// footer
|
||||
manager.getHelpFormatter().printDetailedHelpFooter(issuer, commandName, page);
|
||||
}
|
||||
|
||||
public List<HelpEntry> getHelpEntries() {
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2018 Daniel Ennis (Aikar) - MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package co.aikar.commands;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CommandHelpFormatter {
|
||||
|
||||
private final CommandManager manager;
|
||||
|
||||
public CommandHelpFormatter(CommandManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
// ########
|
||||
// # help #
|
||||
// ########
|
||||
|
||||
public void printHelpHeader(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults) {
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, "{command}", commandName);
|
||||
}
|
||||
|
||||
public void printHelpLine(CommandIssuer issuer, String commandName, HelpEntry page) {
|
||||
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page));
|
||||
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
|
||||
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
|
||||
}
|
||||
}
|
||||
|
||||
public void printHelpFooter(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults, boolean lastPage) {
|
||||
if(lastPage)return;
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION,
|
||||
"{page}", "" + page,
|
||||
"{totalpages}", ""+ totalPages,
|
||||
"{results}", "" + totalResults
|
||||
);
|
||||
}
|
||||
|
||||
// ##########
|
||||
// # search #
|
||||
// ##########
|
||||
|
||||
public void printSearchHeader(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults, List<String> search) {
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER,
|
||||
"{command}", commandName,
|
||||
"{search}", String.join(" ", search));
|
||||
}
|
||||
|
||||
public void printSearchLine(CommandIssuer issuer, String commandName, HelpEntry page, int score) {
|
||||
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page));
|
||||
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
|
||||
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
|
||||
}
|
||||
}
|
||||
|
||||
public void printSearchFooter(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults, List<String> search, boolean lastPage) {
|
||||
if(lastPage)return;
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION,
|
||||
"{page}", "" + page,
|
||||
"{totalpages}", ""+ totalPages,
|
||||
"{results}", "" + totalResults
|
||||
);
|
||||
}
|
||||
|
||||
// ############
|
||||
// # detailed #
|
||||
// ############
|
||||
|
||||
public void printDetailedHelpHeader(CommandIssuer issuer, String commandName, HelpEntry page) {
|
||||
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER, "{command}", page.getCommand());
|
||||
}
|
||||
|
||||
public void printDetailedHelpLine(CommandIssuer issuer, String commandName, HelpEntry page, String subCommand, String paramDescription) {
|
||||
String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_FORMAT, getDetailedFormatReplacements(subCommand, paramDescription, page));
|
||||
for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
|
||||
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
|
||||
}
|
||||
}
|
||||
|
||||
public void printDetailedHelpFooter(CommandIssuer issuer, String commandName, HelpEntry page) {
|
||||
// default doesn't have a footer
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this to control replacements
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@NotNull
|
||||
public String[] getFormatReplacements(HelpEntry e) {
|
||||
//{command} {parameters} {separator} {description}
|
||||
return new String[] {
|
||||
"{command}", e.getCommand(),
|
||||
"{parameters}", e.getParameterSyntax(),
|
||||
"{separator}", e.getDescription().isEmpty() ? "" : "-",
|
||||
"{description}", e.getDescription()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this to control replacements
|
||||
* @param cmd
|
||||
* @param help
|
||||
* @param page
|
||||
* @return
|
||||
*/
|
||||
@NotNull
|
||||
public String[] getDetailedFormatReplacements(String cmd, String help, HelpEntry page) {
|
||||
//{name} {description}
|
||||
return new String[] {
|
||||
"{name}", cmd,
|
||||
"{description}", help
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,7 @@ public abstract class CommandManager <
|
||||
protected final CommandConditions<I, CEC, CC> conditions = new CommandConditions<>(this);
|
||||
protected ExceptionHandler defaultExceptionHandler = null;
|
||||
protected Table<Class<?>, String, Object> dependencies = HashBasedTable.create();
|
||||
protected CommandHelpFormatter helpFormatter = new CommandHelpFormatter(this);
|
||||
|
||||
protected boolean usePerIssuerLocale = false;
|
||||
protected List<IssuerLocaleChangedCallback<I>> localeChangedCallbacks = Lists.newArrayList();
|
||||
@@ -185,6 +186,15 @@ public abstract class CommandManager <
|
||||
verifyUnstableAPI("help");
|
||||
this.defaultHelpPerPage = defaultHelpPerPage;
|
||||
}
|
||||
/** @deprecated Unstable API */ @Deprecated @UnstableAPI
|
||||
public void setHelpFormatter(CommandHelpFormatter helpFormatter) {
|
||||
this.helpFormatter = helpFormatter;
|
||||
}
|
||||
|
||||
/** @deprecated Unstable API */ @Deprecated @UnstableAPI
|
||||
public CommandHelpFormatter getHelpFormatter() {
|
||||
return helpFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a command with ACF
|
||||
|
||||
Reference in New Issue
Block a user