Search in sources :

Example 6 with CommandMapping

use of org.spongepowered.api.command.manager.CommandMapping in project SpongeCommon by SpongePowered.

the class BrigadierCommandRegistrar method registerInternal.

private Tuple<CommandMapping, LiteralCommandNode<CommandSourceStack>> registerInternal(final CommandRegistrar<?> registrar, @Nullable final PluginContainer container, final LiteralCommandNode<CommandSourceStack> namespacedCommand, final String[] secondaryAliases, final boolean allowDuplicates) {
    // Brig technically allows them...
    // Get the builder and the first literal.
    final String requestedAlias = namespacedCommand.getLiteral();
    final Optional<CommandMapping> existingMapping = this.manager.commandMapping(requestedAlias);
    if (allowDuplicates && existingMapping.isPresent()) {
        // then we just let it go, the requirements will be of the old node.
        this.dispatcher.register(namespacedCommand);
        return Tuple.of(existingMapping.get(), namespacedCommand);
    }
    // This will throw an error if there is an issue.
    final CommandMapping mapping = this.manager.registerNamespacedAlias(registrar, container, namespacedCommand, secondaryAliases);
    // Let the registration happen.
    this.dispatcher.register(namespacedCommand);
    // Redirect aliases
    for (final String alias : mapping.allAliases()) {
        if (!alias.equals(namespacedCommand.getLiteral())) {
            final LiteralArgumentBuilder<CommandSourceStack> redirecting = LiteralArgumentBuilder.<CommandSourceStack>literal(alias).executes(namespacedCommand.getCommand()).requires(namespacedCommand.getRequirement());
            if (namespacedCommand.getRedirect() == null) {
                redirecting.redirect(namespacedCommand);
            } else {
                // send it directly to the namespaced command's target.
                redirecting.forward(namespacedCommand.getRedirect(), namespacedCommand.getRedirectModifier(), namespacedCommand.isFork());
            }
            this.dispatcher.register(redirecting.build());
        }
    }
    return Tuple.of(mapping, namespacedCommand);
}
Also used : CommandMapping(org.spongepowered.api.command.manager.CommandMapping) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

Example 7 with CommandMapping

use of org.spongepowered.api.command.manager.CommandMapping in project SpongeCommon by SpongePowered.

the class SpongeRawCommandRegistrar method register.

@Override
public CommandMapping register(@NonNull final PluginContainer container, final Command.@NonNull Raw command, @NonNull final String primaryAlias, @NonNull final String@NonNull ... secondaryAliases) throws CommandFailedRegistrationException {
    final CommandMapping mapping = this.manager.registerAlias(this, container, command.commandTree(), primaryAlias, secondaryAliases);
    this.commands.put(mapping, command);
    return mapping;
}
Also used : CommandMapping(org.spongepowered.api.command.manager.CommandMapping)

Aggregations

CommandMapping (org.spongepowered.api.command.manager.CommandMapping)7 ArrayList (java.util.ArrayList)4 SuggestionsBuilder (com.mojang.brigadier.suggestion.SuggestionsBuilder)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CommandSourceStack (net.minecraft.commands.CommandSourceStack)3 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)2 Suggestions (com.mojang.brigadier.suggestion.Suggestions)2 CommandNode (com.mojang.brigadier.tree.CommandNode)2 LiteralCommandNode (com.mojang.brigadier.tree.LiteralCommandNode)2 RootCommandNode (com.mojang.brigadier.tree.RootCommandNode)2 Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)2 Timings (co.aikar.timings.Timings)1 SpongeTimingsFactory (co.aikar.timings.sponge.SpongeTimingsFactory)1 AmbiguityConsumer (com.mojang.brigadier.AmbiguityConsumer)1 Command (com.mojang.brigadier.Command)1 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)1