Skip to content

Commit

Permalink
Initial commit for usage implementation for #363
Browse files Browse the repository at this point in the history
  • Loading branch information
JorelAli committed Dec 7, 2022
1 parent fa7985d commit 0b1c801
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,6 @@ void register(CommandMetaData meta, final Argument<?>[] args,
CommandPermission permission = meta.permission;
String[] aliases = meta.aliases;
Predicate<CommandSender> requirements = meta.requirements;
Optional<String> shortDescription = meta.shortDescription;
Optional<String> fullDescription = meta.fullDescription;

// Handle command conflicts
boolean hasRegisteredCommand = false;
Expand All @@ -747,7 +745,7 @@ void register(CommandMetaData meta, final Argument<?>[] args,
for (Argument<?> arg : args) {
argumentsString.add(arg.getNodeName() + ":" + arg.getClass().getSimpleName());
}
registeredCommands.add(new RegisteredCommand(commandName, argumentsString, shortDescription, fullDescription, aliases, permission));
registeredCommands.add(new RegisteredCommand(commandName, argumentsString, meta.shortDescription, meta.fullDescription, meta.usage, aliases, permission));
}

// Handle previewable arguments
Expand Down Expand Up @@ -1032,14 +1030,18 @@ private void generateHelpUsage(StringBuilder sb, RegisteredCommand command) {

// Generate usages
List<String> usages = new ArrayList<>();
for (RegisteredCommand rCommand : registeredCommands) {
if (rCommand.commandName().equals(command.commandName())) {
StringBuilder usageString = new StringBuilder();
usageString.append("/" + command.commandName() + " ");
for (String arg : rCommand.argsAsStr()) {
usageString.append("<" + arg.split(":")[0] + "> ");
if(command.usage().isPresent()) {
usages = Arrays.asList(command.usage().get());
} else {
for (RegisteredCommand rCommand : registeredCommands) {
if (rCommand.commandName().equals(command.commandName())) {
StringBuilder usageString = new StringBuilder();
usageString.append("/" + command.commandName() + " ");
for (String arg : rCommand.argsAsStr()) {
usageString.append("<" + arg.split(":")[0] + "> ");
}
usages.add(usageString.toString());
}
usages.add(usageString.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ final class CommandMetaData {
Predicate<CommandSender> requirements = s -> true;

/**
* An optional short description for the command
* An optional short description for the command's help
*/
Optional<String> shortDescription = Optional.empty();

/**
* An optional full description for the command
* An optional full description for the command's help
*/
Optional<String> fullDescription = Optional.empty();

/**
* An optional usage text for the command's help
*/
Optional<String[]> usage = Optional.empty();;

/**
* Create command metadata
Expand All @@ -63,6 +68,7 @@ public CommandMetaData(CommandMetaData original) {
this.requirements = original.requirements;
this.shortDescription = original.shortDescription.isPresent() ? Optional.of(original.shortDescription.get()) : Optional.empty();
this.fullDescription = original.fullDescription.isPresent() ? Optional.of(original.fullDescription.get()) : Optional.empty();
this.usage = original.usage.isPresent() ? Optional.of(original.usage.get()) : Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,26 @@ public T withFullDescription(String description) {
this.meta.fullDescription = Optional.ofNullable(description);
return (T) this;
}

/**
* Returns the usage help text for this command
* @return the usage help text for this command
*/
public String[] getUsage() {
return this.meta.usage.isPresent() ? this.meta.usage.get() : null;
}

/**
* Sets the usage text for this command. This is shown in the help which is
* shown in the specific /help page for this command (e.g. /help mycommand) and is designed to help users understand the syntax of your command.
* @param usage a list entry of usage commands
* @return this command builder
*/
@SuppressWarnings("unchecked")
public T withUsage(String... usage) {
this.meta.usage = Optional.ofNullable(usage);
return (T) this;
}

/**
* Sets the short and full description for this command. This is a short-hand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public record RegisteredCommand(
*/
Optional<String> fullDescription,

/**
* @return An {@link Optional} containing this command's usage text
*/
Optional<String[]> usage,

/**
* @return a {@link String}{@code []} of aliases for this command
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,21 @@ public void onLoad() {
@Override
public void onEnable() {
CommandAPI.onEnable(this);

new CommandTree("configcommands")
.withUsage(
"configcommands help",
"configcommands help <section>",
"configcommands <functions>",
"configcommands <functions> <addOn> <internalArgument> <(non)static> <function>",
"configcommands <build>",
"configcommands <reload> <commandName> <arguments>",
"configcommands <debug>",
"configcommands <debug> <enable/disable>",
"configcommands <debug> <local> <command>",
"configcommands <debug> <local> <command> <enable/disable>"
)
.executes((sender, args) -> {})
.register();
}
}

0 comments on commit 0b1c801

Please sign in to comment.