Skip to content

Command Help Provider (BETA)

Mazen edited this page Jun 18, 2023 · 1 revision

From it's name, it's a way to provide or define how the help topic of a command is displayed to the sender.

e.g: when you have alot of sub commands and want to add /<command> help this can be done automatically by adding a helper provider

Note to be considered: subcommands that have children will be displayed as 1 syntax and will have automatically the subcommand help attached to them as one of their children

Implementing Help Provider

Create a class and simply implement the interface CommandHelpProvider

Here's the example (using spigot platform) explaining other things you need to know

public final class TestCommandHelpProvider implements SpigotCommandHelpProvider //or just CommandHelpProvider<CommandSender> {
	
	/**
	 * The menu style of the command help
	 * that will be sent
	 *
	 * @see CommandHelpStyle
	 *
	 * @return the help style of the help menu that will be displayed
	 */
	@Override
	public CommandHelpStyle<CommandSender> menuStyle() {
		return CommandHelpStyle.<CommandSender>builder()
			.lineStyle(Style.style(NamedTextColor.DARK_GRAY).decorate(TextDecoration.BOLD, TextDecoration.STRIKETHROUGH))
			//line style is the style of the header lines
			
			.syntaxStyle((syntax) -> Style.style(NamedTextColor.YELLOW))
			// the style of the syntax line
			
			.header((commandLabel) -> Component.text(commandLabel + "'s usages", NamedTextColor.GOLD))
			// the header of the help menus
			
			.build();
	}
	
}

Registering your help provider

Simple and easy just like this:

commandManager.setHelpProvider(new TestCommandHelpProvider());

Extras

When you want to modify the whole way of displaying the help menus from scratch, you need to override the method syntaxDisplayer

Here's an example:

	@Override
	public ItemPageTextDisplayer<CommandSender, CommandSyntax<CommandSender>> syntaxDisplayer(@NotNull CommandManager<?, CommandSender> manager,
	                                                                                          @NotNull Command<CommandSender> command, 
	                                                                                          @NotNull CommandHelpStyle<CommandSender> provider) {
		return new TestCommandSyntaxPageDisplayer<>(manager, command, provider);
	}
public final class TestCommandSyntaxPageDisplayer<S> extends CommandSyntaxPageDisplayer<S> {
	
	private final CommandManager<?, S> manager;
	private final Command<S> command;
	private final CommandHelpStyle<S> provider;
	
	public TestCommandSyntaxPageDisplayer(@NotNull CommandManager<?, S> manager,
	                                      @NotNull Command<S> command,
	                                      @NotNull CommandHelpStyle<S> provider) {
		super(manager, command, provider);
		this.manager = manager;
		this.command = command;
		this.provider = provider;
	}
	@Override
	public TextComponent displayPageItem(@NotNull S sender,
	                                     @NotNull CommandSyntax<S> convertible,
	                                     int index) {
		
		Information syntaxInfo = convertible.getInfo();
		
		TextComponent comp = convertible.toText(manager, sender);
		TextComponent description = (TextComponent) Component.text("|")
			.style(Style.style(NamedTextColor.DARK_AQUA))
			.appendSpace()
			.append(Component.text((syntaxInfo == null || syntaxInfo.description().isEmpty()
					|| syntaxInfo.description().isBlank() ? "Unknown purpose" : syntaxInfo.description()))
				.style(Style.style(NamedTextColor.GRAY)));
		
		
		TextComponent result = Component.text("> ", NamedTextColor.AQUA)
			.append(comp.style(provider.syntaxStyle(convertible)));
		
		//SHOULD NOT BE CHANGED UNLESS IT'S NECESSARY
		String format = ArgumentSyntaxUtility.format(manager, command.name(), CommandSyntax.getArguments(command.tree(), convertible));
		if (convertible instanceof SubCommandSyntax<S> sub && sub.hasChildren()) format = format + " help";
		result = result.clickEvent(ClickEvent.suggestCommand(format));
		
		return (TextComponent) result.appendSpace()
			.append(description);
	}
	
}

In the above examples, we have created a custom syntax page displayer for our help menu and defined a new way for how it's displayed.

It's highly recommended that you stick to just modifying the style it's self and to use the default command syntax page displayer, unless it's necessary to create your own help page displayer.