use of org.spongepowered.common.command.brigadier.tree.SpongeCommandExecutorWrapper in project SpongeCommon by SpongePowered.
the class SpongeParameterTranslator method createCommandTree.
@SuppressWarnings({ "unchecked", "rawtypes" })
public Collection<LiteralCommandNode<CommandSourceStack>> createCommandTree(final Command.Parameterized command, final Collection<String> aliases) {
if (aliases.isEmpty()) {
throw new IllegalArgumentException("Aliases must not be empty.");
}
// create the first literal.
final Collection<String> sortedAliases = aliases.stream().sorted().collect(Collectors.toList());
final Iterator<String> aliasIterator = sortedAliases.iterator();
final String baseAlias = aliasIterator.next();
final SpongeCommandExecutorWrapper executor = command.executor().map(SpongeCommandExecutorWrapper::new).orElse(null);
// Create the defining characteristics of the node.
final LiteralArgumentBuilder<CommandSourceStack> basicNode = LiteralArgumentBuilder.literal(baseAlias);
basicNode.requires((Predicate) command.executionRequirements());
if (command.isTerminal() && executor != null) {
basicNode.executes(executor);
}
final SpongeLiteralCommandNode commandNode = new SpongeLiteralCommandNode(basicNode);
if (executor != null) {
this.createAndAttachNode(Collections.singleton(commandNode), command.parameters(), executor, true, true);
}
for (final Parameter.Subcommand subcommand : command.subcommands()) {
final Collection<LiteralCommandNode<CommandSourceStack>> builtSubcommand = this.createCommandTree(subcommand.command(), subcommand.aliases());
builtSubcommand.forEach(commandNode::addChild);
}
this.createFlags(commandNode, command.flags(), executor);
final List<LiteralCommandNode<CommandSourceStack>> allCommandNodes = new ArrayList<>();
allCommandNodes.add(commandNode);
final Collection<CommandNode<CommandSourceStack>> children = commandNode.getChildren();
while (aliasIterator.hasNext()) {
final LiteralArgumentBuilder<CommandSourceStack> redirectedNode = LiteralArgumentBuilder.literal(aliasIterator.next());
redirectedNode.executes(commandNode.getCommand());
redirectedNode.requires(commandNode.getRequirement());
// merged, the above can be substituted in here.
for (final CommandNode<CommandSourceStack> child : children) {
redirectedNode.then(child);
}
allCommandNodes.add(new SpongeLiteralCommandNode(redirectedNode));
}
return Collections.unmodifiableCollection(allCommandNodes);
}
Aggregations