All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.jorel.commandapi.AbstractCommandTree Maven / Gradle / Ivy

The newest version!
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
/// @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 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 tree : arguments) {
			executions.addAll(tree.getExecutions());
		}
		for (Execution execution : executions) {
			execution.register(this.meta, namespace);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy