Search in sources :

Example 1 with SpongeFlagLiteralCommandNode

use of org.spongepowered.common.command.brigadier.tree.SpongeFlagLiteralCommandNode in project SpongeCommon by SpongePowered.

the class SpongeParameterTranslator method createFlags.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void createFlags(final LiteralCommandNode<CommandSourceStack> node, final List<Flag> flags, @Nullable final SpongeCommandExecutorWrapper wrapper) {
    final Collection<CommandNode<CommandSourceStack>> nodesToAddChildrenTo = new ArrayList<>();
    for (final Flag flag : flags) {
        // first create the literal.
        final Iterator<String> aliasIterator = flag.aliases().iterator();
        final LiteralArgumentBuilder<CommandSourceStack> flagLiteral = LiteralArgumentBuilder.literal(aliasIterator.next());
        flagLiteral.requires((Predicate) flag.requirement());
        final Collection<? extends CommandNode<CommandSourceStack>> toBeRedirected;
        final SpongeFlagLiteralCommandNode flagNode = new SpongeFlagLiteralCommandNode(flagLiteral, flag);
        if (flag.associatedParameter().isPresent()) {
            toBeRedirected = this.createAndAttachNode(Collections.singleton(flagNode), Collections.singletonList(flag.associatedParameter().get()), wrapper, node.getCommand() != null, false);
        } else {
            toBeRedirected = Collections.singletonList(flagNode);
        }
        for (final CommandNode<CommandSourceStack> candidate : toBeRedirected) {
            if (candidate instanceof SpongeNode && ((SpongeNode) candidate).canForceRedirect()) {
                ((SpongeNode) candidate).forceRedirect(node);
            } else {
                nodesToAddChildrenTo.add(candidate);
            }
        }
        node.addChild(flagNode);
        while (aliasIterator.hasNext()) {
            final LiteralArgumentBuilder<CommandSourceStack> nextFlag = LiteralArgumentBuilder.<CommandSourceStack>literal(aliasIterator.next()).executes(flagNode.getCommand());
            if (flagNode.getRedirect() != null) {
                nextFlag.redirect(flagNode.getRedirect());
            } else {
                nextFlag.redirect(flagNode);
            }
            node.addChild(new SpongeFlagLiteralCommandNode(nextFlag, flag));
        }
    }
    if (!nodesToAddChildrenTo.isEmpty()) {
        for (final CommandNode<CommandSourceStack> target : node.getChildren()) {
            if (!target.getChildren().isEmpty() && target instanceof SpongeFlagLiteralCommandNode) {
                nodesToAddChildrenTo.forEach(x -> x.addChild(((SpongeFlagLiteralCommandNode) target).cloneWithRedirectToThis()));
            } else {
                nodesToAddChildrenTo.forEach(x -> x.addChild(target));
            }
        }
    }
}
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) SpongeFlagLiteralCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeFlagLiteralCommandNode) ArrayList(java.util.ArrayList) SpongeNode(org.spongepowered.common.command.brigadier.tree.SpongeNode) Flag(org.spongepowered.api.command.parameter.managed.Flag) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

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 Flag (org.spongepowered.api.command.parameter.managed.Flag)1 SpongeFlagLiteralCommandNode (org.spongepowered.common.command.brigadier.tree.SpongeFlagLiteralCommandNode)1 SpongeLiteralCommandNode (org.spongepowered.common.command.brigadier.tree.SpongeLiteralCommandNode)1 SpongeNode (org.spongepowered.common.command.brigadier.tree.SpongeNode)1