Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions .github/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
---
version: 1.0.0
version: 2.0.0
---

# Commandant 1.0.0 - Stable commands
# Commandant 2.0.0 - Help in every reader's language

Commandant 1.0.0 is the first stable release and builds on Keystone 1.0.0. Plugins parse their commands themselves and hand them to `Commandant.process`, which applies definitions, help and pagination; plugins written against 0.0.1 need the upgrade steps below.
Commandant 2.0.0 builds help and pagination from translatable components, so one help page is shown in each reader's language by the translator the plugin registers with Adventure. Plugins that build help need the upgrade steps below.

## Highlights

* Commands are parsed by your own parser and handed to `Commandant.process(command, manager)`, which applies a definition's overrides through a `DefinitionAdapter` before registering them.
* Definitions can declare root aliases, and usage tokens that do not match the command's arguments fail fast.
* Help and pagination are configured through fluent builders; help can list a command once per definition.
* Exception messages format their placeholder values through `ExceptionFormatting`, with built-in formatters for Cloud permissions.
* `CommandantSyntaxFormatter` gives Cloud's syntax messages the argument format of Commandant's help.
* `HelpBuilder.build` returns a `Component` made of translatable keys under a prefix the plugin chooses, with named arguments made by the plugin's `ArgumentFactory`, such as semantica's `TextArgument::of`.
* `HelpBuilder.Text` and `PaginationBuilder.Text` list every key with the named arguments it is given, so a plugin writes its translations in its own wording.
* `TranslatableDescription.of(key)` gives a command a description that the help shows in the reader's language.
* The page arrows run the page command themselves, so translations need no click tags.

## Upgrading

* `Command`, `CommandDefinition` and `CommandRegistrar` are removed; parse commands yourself and register them through `Commandant.process`.
* Replace `HelpMessages` and `PaginationMessages` with translations of each `HelpBuilder.Text` under the help's prefix, for example `arbor.help.title`, and of each `PaginationBuilder.Text` under `<prefix>.pagination`. Send the `Component` that `HelpBuilder.build` returns instead of deserializing a string.
* Build help with `Help.builder(prefix, arguments).host(name).pageCommand(page -> "/<command> help " + page)`. `customArgumentNames` is now `argumentNames`, and `itemsPerPage` defaults to 7. Placeholders move from `{previousPage}`/`{nextPage}` to `{page}`, and from `{argument}` to `{name}`.
* A pagination of your own is built with `Pagination.builder(prefix, arguments)`, whose `showIfOnePage`, `showPreviousOnFirst` and `showNextOnLast` replace the flags of `PaginationMessages`. `PaginationBuilder.build` returns the page line, or nothing when it is hidden.
* `CommandantSyntaxFormatter` takes the required and optional argument templates as two strings instead of `HelpMessages.Format`.
6 changes: 6 additions & 0 deletions commandant-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ plugins {

dependencies {
api(libs.keystone)

testImplementation(libs.adventure.api)
testImplementation(libs.cloud.core)
testImplementation(libs.junit.jupiter)

testRuntimeOnly(libs.junit.platform)
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package me.whereareiam.commandant.builder;

import net.kyori.adventure.text.ComponentLike;
import org.jetbrains.annotations.NotNull;

/**
* Makes a named argument of a translatable component, so a translation refers to it by name, such as
* {@code {host}}. The plugin's translator decides how a named argument is represented; with semantica it is
* {@code TextArgument::of}.
*
* <p>The help and the pagination pass a value as a {@link ComponentLike}, a {@link String} or an {@link Integer},
* so a translation can format numbers, for example in a plural.</p>
*
* <pre>{@code
* HelpBuilder<S> help = Help.<S>builder("arbor.help", TextArgument::of)
* .host("Arbor")
* .build();
* }</pre>
*/
@FunctionalInterface
public interface ArgumentFactory {
/**
* Makes a named argument.
*
* @param name the argument's name, as a translation refers to it
* @param value the argument's value: a {@link ComponentLike}, a {@link String} or an {@link Integer}
* @return the argument, to pass to a translatable component
*/
@NotNull
ComponentLike create(@NotNull String name, @NotNull Object value);

/**
* Makes a named argument of a number.
*
* @param argument the argument
* @param value its value
* @return the argument, to pass to a translatable component
*/
@NotNull
default ComponentLike create(@NotNull TranslationArgument argument, int value) {
return create(argument.getId(), value);
}

/**
* Makes a named argument of a text.
*
* @param argument the argument
* @param value its value
* @return the argument, to pass to a translatable component
*/
@NotNull
default ComponentLike create(@NotNull TranslationArgument argument, @NotNull String value) {
return create(argument.getId(), value);
}

/**
* Makes a named argument of a component.
*
* @param argument the argument
* @param value its value
* @return the argument, to pass to a translatable component
*/
@NotNull
default ComponentLike create(@NotNull TranslationArgument argument, @NotNull ComponentLike value) {
return create(argument.getId(), value);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,118 @@
package me.whereareiam.commandant.builder;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import org.incendo.cloud.Command;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Set;

/**
* Builder interface for creating help command outputs.
* Formats a list of commands with their arguments and descriptions,
* optionally including pagination.
* <p>
* This builder is generic and works with any sender type.
* Builds a page of help: a heading, a line for each command with its arguments and description, and a page
* line. The page is made of translatable components with named arguments, so the plugin's translator renders it
* in each reader's language. Each component's key is the help's prefix, a dot and the key of a {@link Text}, such
* as {@code arbor.help.title}; the page line's keys are those of {@link PaginationBuilder.Text} under
* {@code <prefix>.}{@value #PAGINATION}.
*
* @param <S> The sender type (e.g., Actor, DummyPlayer, CommandSender)
* <pre>{@code
* Collection<Command<S>> visible = manager.commands().stream()
* .filter(command -> manager.testPermission(sender, command.commandPermission()).allowed())
* .toList();
* sender.sendMessage(help.build(visible, page));
* }</pre>
*
* @param <S> the sender type
*/
@SuppressWarnings("unused")
public interface HelpBuilder<S> {
/**
* Builds a help message for the given commands.
* The key under the help's prefix that holds the page line's texts, those of {@link PaginationBuilder.Text}.
*/
String PAGINATION = "pagination";

/**
* Builds one page of help for the given commands. A page past the last lists no command and shows the
* {@link Text#EMPTY} line.
*
* @param commands Collection of commands to display
* @param page Current page number (1-indexed)
* @return Formatted help message string
* @param commands the commands to list, usually those the reader may use
* @param page the page, from {@code 1}
* @return the page, its lines joined by line breaks
*/
@NotNull
String build(@NotNull Collection<Command<S>> commands, int page);
}
Component build(@NotNull Collection<Command<S>> commands, int page);

/**
* A text of the help, with the named arguments it is given. A plugin writes a translation for each under the
* help's prefix.
*/
@Getter
enum Text implements TranslationKey {
/**
* The heading above the commands.
*/
TITLE("title", Argument.HOST),

/**
* One command's line. Its {@link Argument#ARGUMENTS} is empty or starts with a space.
*/
COMMAND("command", Argument.COMMAND, Argument.ARGUMENTS, Argument.DESCRIPTION),

/**
* A required argument inside a command's {@link Argument#ARGUMENTS}.
*/
ARGUMENT("argument", Argument.NAME),

/**
* An optional argument inside a command's {@link Argument#ARGUMENTS}.
*/
OPTIONAL_ARGUMENT("optionalArgument", Argument.NAME),

/**
* The line shown instead of commands when a page has none.
*/
EMPTY("empty");

private final String key;
private final Set<Argument> arguments;

Text(@NotNull String key, @NotNull Argument... arguments) {
this.key = key;
this.arguments = Set.of(arguments);
}
}

/**
* A named argument of a {@link Text}.
*/
@Getter
@RequiredArgsConstructor
enum Argument implements TranslationArgument {
/**
* The name of the plugin whose help it is.
*/
HOST("host"),

/**
* The root command's name.
*/
COMMAND("command"),

/**
* The command's arguments, each after a space.
*/
ARGUMENTS("arguments"),

/**
* The command's description.
*/
DESCRIPTION("description"),

/**
* An argument's name.
*/
NAME("name");

private final String id;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,94 @@
package me.whereareiam.commandant.builder;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.Set;

/**
* Builder interface for creating pagination controls in command outputs.
* Formats navigation elements (previous/next buttons) and page indicators.
* Builds the page line of a paginated list: the current and last page, and arrows to the pages around it. The
* line is a translatable component with named arguments, whose keys are the pagination's prefix, a dot and the key
* of a {@link Text}, such as {@code arbor.help.pagination.line}.
*/
public interface PaginationBuilder {
/**
* Builds pagination by replacing the <pagination> placeholder in the message.
* Builds the page line for a list.
*
* @param message The message containing <pagination> placeholder
* @param totalItems Total number of items to paginate
* @param currentPage Current page number (1-indexed)
* @param itemsPerPage Number of items per page
* @return The message with the pagination placeholder replaced by formatted controls
* @param totalItems the number of items in the whole list
* @param currentPage the page shown, from {@code 1}
* @param itemsPerPage the number of items on one page
* @return the page line, or empty when the list fits on one page and the pagination hides it then
*/
@NotNull
String build(@NotNull String message, int totalItems, int currentPage, int itemsPerPage);
}
Optional<Component> build(int totalItems, int currentPage, int itemsPerPage);

/**
* A text of the page line, with the named arguments it is given. A plugin writes a translation for each under
* the pagination's prefix.
*/
@Getter
enum Text implements TranslationKey {
/**
* The page line. Its {@link Argument#PREVIOUS} and {@link Argument#NEXT} are the arrows, or empty where
* there is no page to go to.
*/
LINE("line", Argument.CURRENT, Argument.MAX, Argument.PREVIOUS, Argument.NEXT),

/**
* The arrow to the previous page. It needs no click tag: when the pagination has a page command, the whole
* arrow runs it.
*/
PREVIOUS("previous", Argument.PAGE),

/**
* The arrow to the next page. It needs no click tag: when the pagination has a page command, the whole
* arrow runs it.
*/
NEXT("next", Argument.PAGE);

private final String key;
private final Set<Argument> arguments;

Text(@NotNull String key, @NotNull Argument... arguments) {
this.key = key;
this.arguments = Set.of(arguments);
}
}

/**
* A named argument of a {@link Text}.
*/
@Getter
@RequiredArgsConstructor
enum Argument implements TranslationArgument {
/**
* The page shown, a number.
*/
CURRENT("current"),

/**
* The last page, a number.
*/
MAX("max"),

/**
* The arrow to the previous page, or empty.
*/
PREVIOUS("previous"),

/**
* The arrow to the next page, or empty.
*/
NEXT("next"),

/**
* The page an arrow opens, a number.
*/
PAGE("page");

private final String id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.whereareiam.commandant.builder;

import org.jetbrains.annotations.NotNull;

/**
* A named argument of a {@link TranslationKey}.
*/
public interface TranslationArgument {
/**
* Returns the name a translation refers to the argument by.
*
* @return the name, such as {@code host} in {@code {host}}
*/
@NotNull
String getId();
}
Loading
Loading