Search in sources :

Example 1 with SpongeCommandExecutorWrapper

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);
}
Also used : LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) SpongeFlagLiteralCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeFlagLiteralCommandNode) SpongeLiteralCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeLiteralCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) ArrayList(java.util.ArrayList) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SpongeCommandExecutorWrapper(org.spongepowered.common.command.brigadier.tree.SpongeCommandExecutorWrapper) LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) SpongeFlagLiteralCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeFlagLiteralCommandNode) SpongeLiteralCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeLiteralCommandNode) SpongeLiteralCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeLiteralCommandNode) Parameter(org.spongepowered.api.command.parameter.Parameter) SpongeMultiParameter(org.spongepowered.common.command.parameter.multi.SpongeMultiParameter) SpongeFirstOfParameter(org.spongepowered.common.command.parameter.multi.SpongeFirstOfParameter)

Aggregations

CommandNode (com.mojang.brigadier.tree.CommandNode)1 LiteralCommandNode (com.mojang.brigadier.tree.LiteralCommandNode)1 ArrayList (java.util.ArrayList)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 Parameter (org.spongepowered.api.command.parameter.Parameter)1 SpongeCommandExecutorWrapper (org.spongepowered.common.command.brigadier.tree.SpongeCommandExecutorWrapper)1 SpongeFlagLiteralCommandNode (org.spongepowered.common.command.brigadier.tree.SpongeFlagLiteralCommandNode)1 SpongeLiteralCommandNode (org.spongepowered.common.command.brigadier.tree.SpongeLiteralCommandNode)1 SpongeFirstOfParameter (org.spongepowered.common.command.parameter.multi.SpongeFirstOfParameter)1 SpongeMultiParameter (org.spongepowered.common.command.parameter.multi.SpongeMultiParameter)1