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));
}
}
}
}
Aggregations