Search in sources :

Example 1 with BrigadierBasedRegistrar

use of org.spongepowered.common.command.registrar.BrigadierBasedRegistrar in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$getSuggestionsFromNonBrigCommand.

@Inject(method = "handleCustomCommandSuggestions", at = @At(value = "NEW", target = "com/mojang/brigadier/StringReader", remap = false), cancellable = true)
private void impl$getSuggestionsFromNonBrigCommand(final ServerboundCommandSuggestionPacket packet, final CallbackInfo ci) {
    final String rawCommand = packet.getCommand();
    final String[] command = CommandUtil.extractCommandString(rawCommand);
    final CommandCause cause = CommandCause.create();
    final SpongeCommandManager manager = SpongeCommandManager.get(this.server);
    if (!rawCommand.contains(" ")) {
        final SuggestionsBuilder builder = new SuggestionsBuilder(command[0], 0);
        if (command[0].isEmpty()) {
            manager.getAliasesForCause(cause).forEach(builder::suggest);
        } else {
            manager.getAliasesThatStartWithForCause(cause, command[0]).forEach(builder::suggest);
        }
        this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.build()));
        ci.cancel();
    } else {
        final Optional<CommandMapping> mappingOptional = manager.commandMapping(command[0].toLowerCase(Locale.ROOT)).filter(x -> !(x.registrar() instanceof BrigadierBasedRegistrar));
        if (mappingOptional.isPresent()) {
            final CommandMapping mapping = mappingOptional.get();
            if (mapping.registrar().canExecute(cause, mapping)) {
                final SuggestionsBuilder builder = CommandUtil.createSuggestionsForRawCommand(rawCommand, command, cause, mapping);
                this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.build()));
            } else {
                this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), Suggestions.empty().join()));
            }
            ci.cancel();
        }
    }
}
Also used : CommandMapping(org.spongepowered.api.command.manager.CommandMapping) SpongeCommandManager(org.spongepowered.common.command.manager.SpongeCommandManager) CommandCause(org.spongepowered.api.command.CommandCause) SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder) ClientboundCommandSuggestionsPacket(net.minecraft.network.protocol.game.ClientboundCommandSuggestionsPacket) BrigadierBasedRegistrar(org.spongepowered.common.command.registrar.BrigadierBasedRegistrar) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with BrigadierBasedRegistrar

use of org.spongepowered.common.command.registrar.BrigadierBasedRegistrar in project SpongeCommon by SpongePowered.

the class ForgeCommandManager method processCommand.

@Override
protected CommandResult processCommand(final CommandCause cause, final CommandMapping mapping, final String original, final String command, final String args) throws Throwable {
    final CommandRegistrar<?> registrar = mapping.registrar();
    final boolean isBrig = registrar instanceof BrigadierBasedRegistrar;
    final ParseResults<CommandSourceStack> parseResults;
    if (isBrig) {
        parseResults = this.getDispatcher().parse(original, (CommandSourceStack) cause);
    } else {
        // We have a non Brig registrar, so we just create a dummy result for mods to inspect.
        final CommandContextBuilder<CommandSourceStack> contextBuilder = new CommandContextBuilder<>(this.getDispatcher(), (CommandSourceStack) cause, this.getDispatcher().getRoot(), 0);
        contextBuilder.withCommand(ctx -> 1);
        if (!args.isEmpty()) {
            contextBuilder.withArgument("parsed", new ParsedArgument<>(command.length(), original.length(), args));
        }
        parseResults = new ParseResults<>(contextBuilder, new SpongeStringReader(original), Collections.emptyMap());
    }
    // Relocated from Commands (injection short circuits it there)
    final CommandEvent event = new CommandEvent(parseResults);
    if (MinecraftForge.EVENT_BUS.post(event)) {
        if (event.getException() != null) {
            Throwables.throwIfUnchecked(event.getException());
        }
        // As per Forge, we just treat it as a zero success, and do nothing with it.
        return CommandResult.builder().result(0).build();
    }
    if (isBrig) {
        return CommandResult.builder().result(this.getDispatcher().execute(parseResults)).build();
    } else {
        return mapping.registrar().process(cause, mapping, command, args);
    }
}
Also used : SpongeStringReader(org.spongepowered.common.command.brigadier.SpongeStringReader) CommandContextBuilder(com.mojang.brigadier.context.CommandContextBuilder) CommandEvent(net.minecraftforge.event.CommandEvent) BrigadierBasedRegistrar(org.spongepowered.common.command.registrar.BrigadierBasedRegistrar) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

Aggregations

BrigadierBasedRegistrar (org.spongepowered.common.command.registrar.BrigadierBasedRegistrar)2 CommandContextBuilder (com.mojang.brigadier.context.CommandContextBuilder)1 SuggestionsBuilder (com.mojang.brigadier.suggestion.SuggestionsBuilder)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 ClientboundCommandSuggestionsPacket (net.minecraft.network.protocol.game.ClientboundCommandSuggestionsPacket)1 CommandEvent (net.minecraftforge.event.CommandEvent)1 CommandCause (org.spongepowered.api.command.CommandCause)1 CommandMapping (org.spongepowered.api.command.manager.CommandMapping)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1 SpongeStringReader (org.spongepowered.common.command.brigadier.SpongeStringReader)1 SpongeCommandManager (org.spongepowered.common.command.manager.SpongeCommandManager)1