dev.jorel.commandapi.AbstractCommandTree Maven / Gradle / Ivy
package dev.jorel.commandapi;
import dev.jorel.commandapi.arguments.AbstractArgument;
import java.util.ArrayList;
import java.util.List;
/**
* This is the root node for creating a command as a tree
* @param The class extending this class, used as the return type for chain calls
* @param The implementation of AbstractArgument being used by this class
* @param The CommandSender class used by the class extending this class
*/
public abstract class AbstractCommandTree
/// @endcond
, Argument
/// @cond DOX
extends AbstractArgument, ?, Argument, CommandSender>
/// @endcond
, CommandSender> extends ExecutableCommand {
private final List> arguments = new ArrayList<>();
/**
* Creates a main root node for a command tree with a given command name
*
* @param commandName The name of the command to create
*/
protected AbstractCommandTree(final String commandName) {
super(commandName);
}
/**
* Create a child branch on the tree
*
* @param tree the child node
* @return this root node
*/
public Impl then(final AbstractArgumentTree, Argument, CommandSender> tree) {
this.arguments.add(tree);
return instance();
}
/**
* Registers the command with a given namespace
*
* @param namespace The namespace of this command. This cannot be null
* @throws NullPointerException if the namespace is null
*/
@Override
public void register(String namespace) {
if (namespace == null) {
// Only reachable through Velocity
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + this.meta.commandName + "!");
}
List> executions = new ArrayList<>();
if (this.executor.hasAnyExecutors()) {
executions.add(new Execution<>(List.of(), this.executor));
}
for (AbstractArgumentTree, Argument, CommandSender> tree : arguments) {
executions.addAll(tree.getExecutions());
}
for (Execution execution : executions) {
execution.register(this.meta, namespace);
}
}
}