Allow command managers to provide default command meta instances

This commit is contained in:
Alexander Söderberg 2020-09-10 13:19:50 +02:00
parent 90d37f1df8
commit 90aba634a7
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
5 changed files with 51 additions and 7 deletions

View file

@ -63,4 +63,10 @@ public class BukkitCommandManager extends CommandManager<BukkitCommandSender, Bu
return this.owningPlugin; return this.owningPlugin;
} }
@Nonnull
@Override
public BukkitCommandMeta createDefaultCommandMeta() {
return BukkitCommandMetaBuilder.builder().withDescription("").build();
}
} }

View file

@ -57,8 +57,9 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>(); private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>();
public CommandManager(@Nonnull final Function<CommandTree<C, M>, CommandExecutionCoordinator<C, M>> commandExecutionCoordinator, public CommandManager(
@Nonnull final CommandRegistrationHandler<M> commandRegistrationHandler) { @Nonnull final Function<CommandTree<C, M>, CommandExecutionCoordinator<C, M>> commandExecutionCoordinator,
@Nonnull final CommandRegistrationHandler<M> commandRegistrationHandler) {
this.commandTree = CommandTree.newTree(this, commandRegistrationHandler); this.commandTree = CommandTree.newTree(this, commandRegistrationHandler);
this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree); this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree);
this.commandRegistrationHandler = commandRegistrationHandler; this.commandRegistrationHandler = commandRegistrationHandler;
@ -73,7 +74,8 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
*/ */
@Nonnull @Nonnull
public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) { public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
return this.commandExecutionCoordinator.coordinateExecution(this.commandContextFactory.create(commandSender), tokenize(input)); return this.commandExecutionCoordinator.coordinateExecution(this.commandContextFactory.create(commandSender),
tokenize(input));
} }
/** /**
@ -135,7 +137,7 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
* @return Command registration handler * @return Command registration handler
*/ */
@Nonnull @Nonnull
protected CommandRegistrationHandler getCommandRegistrationHandler() { protected CommandRegistrationHandler<M> getCommandRegistrationHandler() {
return this.commandRegistrationHandler; return this.commandRegistrationHandler;
} }
@ -151,14 +153,38 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
return Command.newBuilder(name, meta); return Command.newBuilder(name, meta);
} }
/**
* Create a new command builder using a default command meta instance.
*
* @param name Command name
* @return Builder instance
* @throws UnsupportedOperationException If the command manager does not support default command meta creation
* @see #createDefaultCommandMeta() Default command meta creation
*/
@Nonnull
public Command.Builder<C, M> commandBuilder(@Nonnull final String name) {
return Command.newBuilder(name, this.createDefaultCommandMeta());
}
/** /**
* Get the internal command tree. This should not be accessed unless you know what you * Get the internal command tree. This should not be accessed unless you know what you
* are doing * are doing
* *
* @return Command tree * @return Command tree
*/ */
@Nonnull CommandTree<C, M> getCommandTree() { @Nonnull
CommandTree<C, M> getCommandTree() {
return this.commandTree; return this.commandTree;
} }
/**
* Construct a default command meta instance
*
* @return Default command meta
* @throws UnsupportedOperationException If the command manager does not support this operation
*/
@Nonnull
public abstract M createDefaultCommandMeta();
} }

View file

@ -25,9 +25,16 @@ package com.intellectualsites.commands.meta;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
/**
* Object that is associated with a {@link com.intellectualsites.commands.Command}.
* Command meta should not be mutable, as one fixed instance will be used per command.
* <p>
* Appropriate use for command meta would be fixed state, such as command descriptions.
*/
public class CommandMeta { public class CommandMeta {
@Nonnull public static SimpleCommandMeta.Builder simple() { @Nonnull
public static SimpleCommandMeta.Builder simple() {
return SimpleCommandMeta.builder(); return SimpleCommandMeta.builder();
} }

View file

@ -60,7 +60,6 @@ public class SimpleCommandMeta extends CommandMeta {
return SimpleCommandMeta.builder().build(); return SimpleCommandMeta.builder().build();
} }
/** /**
* Get the value associated with a key * Get the value associated with a key
* *

View file

@ -138,4 +138,10 @@ public class JLineCommandManager extends CommandManager<JLineCommandSender, Simp
System.out.printf("Trying to complete '%s'\n", line); System.out.printf("Trying to complete '%s'\n", line);
} }
@Nonnull
@Override
public SimpleCommandMeta createDefaultCommandMeta() {
return SimpleCommandMeta.empty();
}
} }