Search in sources :

Example 16 with CommandException

use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.

the class CommandTest method onRegisterSpongeCommand.

@Listener
public void onRegisterSpongeCommand(final RegisterCommandEvent<Command.Parameterized> event) {
    final Parameter.Value<ServerPlayer> playerKey = Parameter.player().key("player").usage(key -> "[any player]").build();
    event.register(this.plugin, Command.builder().addParameter(playerKey).executor(context -> {
        final ServerPlayer player = context.one(playerKey).orElse(context.cause().root() instanceof ServerPlayer ? ((ServerPlayer) context.cause().root()) : null);
        this.logger.info(player.name());
        return CommandResult.success();
    }).build(), "getplayer");
    final Parameter.Value<String> playerParameterKey = Parameter.string().key("name").optional().build();
    event.register(this.plugin, Command.builder().addParameter(playerParameterKey).executor(context -> {
        final Optional<String> result = context.one(playerParameterKey);
        final Collection<GameProfile> collection;
        if (result.isPresent()) {
            // check to see if the string matches
            collection = Sponge.game().server().userManager().streamOfMatches(result.get().toLowerCase(Locale.ROOT)).collect(Collectors.toList());
        } else {
            collection = Sponge.game().server().userManager().streamAll().collect(Collectors.toList());
        }
        collection.forEach(x -> this.logger.info("GameProfile - UUID: {}, Name - {}", x.uniqueId().toString(), x.name().orElse("---")));
        return CommandResult.success();
    }).build(), "checkuser");
    final Parameter.Value<String> choicesKey = Parameter.choices("bacon", "eggs", "spam", "spam spam spam").key("choices").build();
    event.register(this.plugin, Command.builder().addParameter(choicesKey).executor(context -> {
        context.sendMessage(Identity.nil(), Component.text(context.requireOne(choicesKey)));
        return CommandResult.success();
    }).build(), "testchoices");
    final Parameter.Key<String> testKey = Parameter.key("testKey", String.class);
    final Parameter.Key<Component> requiredKey = Parameter.key("requiredKey", Component.class);
    event.register(this.plugin, Command.builder().addFlag(Flag.builder().alias("f").alias("flag").build()).addFlag(Flag.builder().alias("t").alias("text").setParameter(Parameter.string().key(testKey).build()).build()).addParameter(Parameter.formattingCodeText().key(requiredKey).build()).executor(context -> {
        context.sendMessage(Identity.nil(), Component.text(context.flagInvocationCount("flag")));
        context.sendMessage(Identity.nil(), Component.text(context.flagInvocationCount("t")));
        context.all(testKey).forEach(x -> context.sendMessage(Identity.nil(), Component.text(x)));
        context.sendMessage(Identity.nil(), context.requireOne(requiredKey));
        return CommandResult.success();
    }).build(), "flagtest");
    event.register(this.plugin, Command.builder().addFlag(Flag.builder().alias("t").alias("text").setParameter(Parameter.string().key(testKey).build()).build()).addParameter(Parameter.formattingCodeText().key(requiredKey).build()).executor(context -> {
        context.sendMessage(Identity.nil(), Component.text("optional_test"));
        context.sendMessage(Identity.nil(), Component.text(context.flagInvocationCount("t")));
        context.all(testKey).forEach(x -> context.sendMessage(Identity.nil(), Component.text(x)));
        context.sendMessage(Identity.nil(), context.requireOne(requiredKey));
        return CommandResult.success();
    }).build(), "optionalflagtest");
    event.register(this.plugin, Command.builder().executor(x -> {
        x.sendMessage(Identity.nil(), Component.text().content("Click Me").clickEvent(SpongeComponents.executeCallback(ctx -> ctx.sendMessage(Identity.nil(), Component.text("Hello")))).build());
        return CommandResult.success();
    }).build(), "testCallback");
    event.register(this.plugin, Command.builder().executor(x -> {
        final Collection<Entity> collection = Selector.builder().applySelectorType(SelectorTypes.ALL_ENTITIES.get()).addEntityType(EntityTypes.PLAYER.get(), false).addGameMode(GameModes.CREATIVE.get()).limit(1).includeSelf().build().select(x.cause());
        for (final Entity entity : collection) {
            x.sendMessage(Identity.nil(), Component.text(entity.toString()));
        }
        return CommandResult.success();
    }).build(), "testselector");
    final Parameter.Key<ServerLocation> serverLocationKey = Parameter.key("serverLocation", ServerLocation.class);
    final Parameter.Value<ServerLocation> serverLocationParameter = Parameter.location().key(serverLocationKey).build();
    event.register(this.plugin, Command.builder().addParameter(serverLocationParameter).executor(x -> {
        x.sendMessage(Identity.nil(), Component.text(x.requireOne(serverLocationKey).toString()));
        return CommandResult.success();
    }).build(), "testlocation");
    final Parameter.Key<ValueParameter<?>> commandParameterKey = Parameter.key("valueParameter", new TypeToken<ValueParameter<?>>() {
    });
    final TypeToken<ValueParameter<?>> typeToken = new TypeToken<ValueParameter<?>>() {
    };
    event.register(this.plugin, Command.builder().addParameter(serverLocationParameter).addParameter(Parameter.registryElement(typeToken, commandContext -> Sponge.game(), RegistryTypes.REGISTRY_KEYED_VALUE_PARAMETER, "sponge").key(commandParameterKey).build()).executor(x -> {
        x.sendMessage(Identity.nil(), Component.text(x.requireOne(serverLocationKey).toString()));
        x.sendMessage(Identity.nil(), Component.text(x.requireOne(commandParameterKey).toString()));
        return CommandResult.success();
    }).build(), "testcatalogcompletion");
    final Parameter.Key<TestEnum> enumParameterKey = Parameter.key("enum", TestEnum.class);
    final Parameter.Key<String> stringKey = Parameter.key("stringKey", String.class);
    event.register(this.plugin, Command.builder().addParameter(Parameter.enumValue(TestEnum.class).key(enumParameterKey).build()).addParameter(Parameter.string().key(stringKey).completer((context, currentInput) -> Arrays.asList("bacon", "eggs", "spam").stream().map(CommandCompletion::of).collect(Collectors.toList())).build()).executor(x -> {
        x.sendMessage(Identity.nil(), Component.text(x.one(enumParameterKey).orElse(TestEnum.ONE).name()));
        return CommandResult.success();
    }).build(), "testenum");
    final Parameter.Key<ResourceKey> resourceKeyKey = Parameter.key("rk", ResourceKey.class);
    event.register(this.plugin, Command.builder().addParameter(Parameter.resourceKey().key(resourceKeyKey).build()).executor(x -> {
        x.sendMessage(Identity.nil(), Component.text(x.requireOne(resourceKeyKey).formatted()));
        return CommandResult.success();
    }).build(), "testrk");
    final Parameter.Key<UUID> userKey = Parameter.key("user", UUID.class);
    event.register(this.plugin, Command.builder().addParameter(Parameter.user().key(userKey).build()).executor(context -> {
        Sponge.server().userManager().load(context.requireOne(userKey)).thenAcceptAsync(x -> context.sendMessage(Identity.nil(), Component.text(x.map(User::name).orElse("unknown"))), Sponge.server().scheduler().executor(this.plugin));
        return CommandResult.success();
    }).build(), "getuser");
    final Parameter.Key<String> stringLiteralKey = Parameter.key("literal", String.class);
    event.register(this.plugin, Command.builder().executor(context -> {
        context.sendMessage(Identity.nil(), Component.text("Collected literals: " + String.join(", ", context.all(stringLiteralKey))));
        return CommandResult.success();
    }).terminal(true).addParameter(Parameter.firstOfBuilder(Parameter.literal(String.class, "1", "1").key(stringLiteralKey).build()).or(Parameter.literal(String.class, "2", "2").key(stringLiteralKey).build()).terminal().build()).addParameter(Parameter.seqBuilder(Parameter.literal(String.class, "3", "3").key(stringLiteralKey).build()).then(Parameter.literal(String.class, "4", "4").key(stringLiteralKey).build()).terminal().build()).addParameter(Parameter.literal(String.class, "5", "5").optional().key(stringLiteralKey).build()).addParameter(Parameter.literal(String.class, "6", "6").key(stringLiteralKey).build()).build(), "testnesting");
    final Parameter.Value<SystemSubject> systemSubjectValue = Parameter.builder(SystemSubject.class).key("systemsubject").addParser(VariableValueParameters.literalBuilder(SystemSubject.class).literal(Collections.singleton("-")).returnValue(Sponge::systemSubject).build()).build();
    event.register(this.plugin, Command.builder().addParameter(Parameter.firstOf(systemSubjectValue, CommonParameters.PLAYER)).addParameter(Parameter.remainingJoinedStrings().key(stringKey).build()).executor(context -> {
        final Audience audience;
        final String name;
        if (context.hasAny(systemSubjectValue)) {
            audience = context.requireOne(systemSubjectValue);
            name = "Console";
        } else {
            final ServerPlayer player = context.requireOne(CommonParameters.PLAYER);
            name = player.name();
            audience = player;
        }
        final String message = context.requireOne(stringKey);
        context.sendMessage(Identity.nil(), Component.text("To " + name + "> " + message));
        final Object root = context.cause().root();
        final Identity identity = root instanceof ServerPlayer ? ((ServerPlayer) root).identity() : Identity.nil();
        audience.sendMessage(identity, Component.text("From " + name + "> " + message));
        return CommandResult.success();
    }).build(), "testmessage");
    event.register(this.plugin, Command.builder().addParameter(CommonParameters.BOOLEAN).executor(ctx -> {
        if (ctx.requireOne(CommonParameters.BOOLEAN)) {
            // custom error
            return CommandResult.error(Component.text("custom error"));
        }
        return CommandResult.builder().error(null).build();
    }).build(), "errormessage");
    final Command.Builder builder = Command.builder();
    final ValueCompleter stringValueCompleter = (c, s) -> s.isEmpty() ? Collections.singletonList(CommandCompletion.of("x")) : Arrays.asList(s, s + "bar", "foo_" + s).stream().map(CommandCompletion::of).collect(Collectors.toList());
    // final ValueCompleter stringValueCompleter = null;
    final Parameter.Value<String> r_opt = Parameter.remainingJoinedStrings().key("r_def").optional().build();
    final Parameter.Value<String> r_req = Parameter.remainingJoinedStrings().key("r_req").completer(stringValueCompleter).build();
    final Parameter.Value<String> opt1 = Parameter.string().optional().key("opt1").build();
    final Parameter.Value<String> opt2 = Parameter.string().optional().key("opt2").build();
    final Parameter.Value<String> topt = Parameter.string().optional().key("topt").terminal().build();
    final Parameter.Value<String> req1 = Parameter.string().key("req1").completer(stringValueCompleter).build();
    final Parameter.Value<String> req2 = Parameter.string().key("req2").build();
    final Parameter.Value<Boolean> lit1 = Parameter.literal(Boolean.class, true, "lit1").key("lit1").build();
    final Parameter.Value<Boolean> lit2 = Parameter.literal(Boolean.class, true, "lit2").key("lit2").build();
    final Parameter optSeq_lit_req1 = Parameter.seqBuilder(lit1).then(req1).optional().build();
    final Parameter optSeq_lit_req2 = Parameter.seqBuilder(lit2).then(req2).optional().build();
    final Parameter seq_req_2 = Parameter.seqBuilder(req1).then(req2).build();
    final Parameter seq_opt_2 = Parameter.seqBuilder(opt1).then(opt2).build();
    final Parameter seq_opt_2_req = Parameter.seqBuilder(opt1).then(opt2).then(req1).build();
    // <req1>
    builder.addChild(Command.builder().executor(context -> CommandTest.printParameters(context, req1)).addParameter(req1).build(), "required");
    // subcommand|<r_def>
    builder.addChild(Command.builder().executor(context -> CommandTest.printParameters(context, r_opt)).addParameter(r_opt).addChild(Command.builder().executor(c -> CommandTest.printParameters(c, r_opt)).build(), "subcommand").build(), "optional_or_subcmd");
    // https://bugs.mojang.com/browse/MC-165562 usage does not show up after a space if there are no completions
    // [def1] <r_req>
    // TODO missing executed command when only providing a single value
    builder.addChild(Command.builder().executor(context -> CommandTest.printParameters(context, opt1, r_req)).addParameters(opt1, r_req).build(), "optional_r_required");
    // [opt1] [opt2] <r_req>
    // TODO missing executed command when only providing a single value
    builder.addChild(Command.builder().executor(context -> CommandTest.printParameters(context, opt1, opt2, r_req)).addParameters(opt1, opt2, r_req).build(), "optional_optional_required");
    // [opt1] [opt2]
    // TODO some redundancy in generated nodes because opt1 node can terminate early
    builder.addChild(Command.builder().executor(context -> CommandTest.printParameters(context, opt1, opt2)).addParameters(opt1, opt2).build(), "optional_optional");
    // [opt1] [literal <req1>]
    // TODO completion does not include req1 when opt1/literal is ambigous
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, opt1, lit1, req1)).addParameters(opt1, optSeq_lit_req1).build(), "optional_optsequence_literal_required");
    // [literal <req1>] [literal2 <req2>]
    // TODO sequences are not optional
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, lit1, req1, lit2, req2)).addParameters(optSeq_lit_req1, optSeq_lit_req2).build(), "opt_sequence_2_literal_required");
    // <<req1> <req2>>
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, req1, req2)).addParameters(seq_req_2).build(), "seq_required_required");
    // <[opt1] [opt2]>
    // TODO some redundancy in generated nodes because opt1 node can terminate early
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, opt1, opt2)).addParameters(seq_opt_2).build(), "seq_optional_optional");
    // <[opt1] [opt2] <req>>
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, opt1, opt2, req1)).addParameters(seq_opt_2_req).build(), "seq_optional_optional_required");
    // [opt1] <req> [opt2]
    // TODO some redundancy in generated nodes because req1 node can terminate early
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, opt1, req1, opt2)).addParameters(opt1, req1, opt2).build(), "optional_required_optional");
    // [opt1] [topt] !terminal
    // or
    // [opt1] [topt] <req1>
    builder.addChild(Command.builder().executor(c -> CommandTest.printParameters(c, opt1, topt, req1)).addParameters(opt1, topt, req1).build(), "optional_toptional_optional");
    event.register(this.plugin, builder.build(), "testcommand", "testcmd");
    // Adapted from https://github.com/SpongePowered/Sponge/issues/3238#issuecomment-750456173
    final Command.Parameterized firstSub = Command.builder().addParameter(CommonParameters.BOOLEAN).executor(c -> {
        c.sendMessage(Identity.nil(), Component.text("first"));
        return CommandResult.success();
    }).build();
    final Command.Parameterized secondSub = Command.builder().addParameter(CommonParameters.BOOLEAN).executor(c -> {
        c.sendMessage(Identity.nil(), Component.text("second"));
        return CommandResult.success();
    }).build();
    final Command.Parameterized parent = Command.builder().executor(c -> {
        c.sendMessage(Identity.nil(), Component.text("parent"));
        return CommandResult.success();
    }).addParameters(CommonParameters.WORLD).addParameters(Parameter.firstOf(Parameter.subcommand(firstSub, "first"), Parameter.subcommand(secondSub, "second"))).terminal(true).build();
    event.register(this.plugin, parent, "testterminal");
    // exceptions
    event.register(this.plugin, Command.builder().shortDescription(Component.text("test throwing execptions")).addChild(Command.builder().executor(ctx -> {
        throw new CommandException(Component.text("Exit via exception"));
    }).build(), "exception").addChild(Command.builder().executor(ctx -> {
        return CommandResult.error(Component.text("Exit via failed result"));
    }).build(), "failedresult").build(), "testfailure");
    event.register(this.plugin, Command.builder().addParameter(Parameter.enumValue(TestEnum.class).modifier(new ValueParameterModifier<TestEnum>() {

        @Override
        @NotNull
        public Optional<? extends TestEnum> modifyResult(final Parameter.@NotNull Key<? super TestEnum> parameterKey, final ArgumentReader.@NotNull Immutable reader, final CommandContext.@NotNull Builder context, @Nullable final TestEnum value) throws ArgumentParseException {
            if (value == TestEnum.THREE) {
                throw reader.createException(Component.text("Can't select three!"));
            }
            return Optional.ofNullable(value);
        }

        @Override
        public List<CommandCompletion> modifyCompletion(@NotNull final CommandContext context, @NotNull final String currentInput, final List<CommandCompletion> completions) {
            return completions.stream().filter(x -> !x.completion().equalsIgnoreCase(TestEnum.THREE.name())).collect(Collectors.toList());
        }

        @Override
        @Nullable
        public Component modifyExceptionMessage(@Nullable final Component exceptionMessage) {
            if (exceptionMessage == null) {
                return null;
            }
            return exceptionMessage.replaceText(builder -> {
                builder.match(", three").replacement("").once();
            });
        }
    }).key(enumParameterKey).build()).executor(x -> {
        x.sendMessage(Identity.nil(), Component.text(x.one(enumParameterKey).orElse(TestEnum.ONE).name()));
        return CommandResult.success();
    }).build(), "testenummodified");
    final Parameter.Value<String> testString = Parameter.string().key("string").build();
    final Parameter.Value<Component> jsonTextParameter = Parameter.jsonText().key("text").build();
    event.register(this.plugin, Command.builder().addParameter(jsonTextParameter).addParameter(testString).executor(ctx -> {
        ctx.sendMessage(Identity.nil(), ctx.requireOne(jsonTextParameter));
        ctx.sendMessage(Identity.nil(), Component.text(ctx.requireOne(testString)));
        return CommandResult.success();
    }).build(), "testcomponentjson");
    final Parameter.Value<Integer> firstIntParameter = Parameter.integerNumber().key("int1").build();
    final Parameter.Value<Integer> secondIntParameter = Parameter.integerNumber().key("int2").build();
    final Parameter.Value<Operator> operatorParameter = Parameter.operator().key("operator").build();
    event.register(this.plugin, Command.builder().addParameter(firstIntParameter).addParameter(operatorParameter).addParameter(secondIntParameter).executor(ctx -> {
        final int first = ctx.requireOne(firstIntParameter);
        final int second = ctx.requireOne(secondIntParameter);
        final Operator operator = ctx.requireOne(operatorParameter);
        ctx.sendMessage(Identity.nil(), Component.text(first));
        ctx.sendMessage(Identity.nil(), RegistryTypes.OPERATOR.get().findValueKey(operator).map(key -> Component.text(key.asString())).orElse(Component.text("Not set")));
        ctx.sendMessage(Identity.nil(), Component.text(second));
        if (operator instanceof Operator.Simple) {
            ctx.sendMessage(Identity.nil(), Component.text(((Operator.Simple) operator).apply(first, second)));
        }
        return CommandResult.success();
    }).build(), "testoperator");
    final Parameter.Value<Color> colorParameter = Parameter.color().key("color").build();
    event.register(this.plugin, Command.builder().addParameter(colorParameter).executor(ctx -> {
        final Color color = ctx.requireOne(colorParameter);
        final TextColor textColor = TextColor.color(color);
        final String colorString = color.toString();
        ctx.sendMessage(Identity.nil(), Component.text().color(textColor).content(colorString).build());
        return CommandResult.success();
    }).build(), "textcolor");
    final Parameter.Value<Integer> rangedInt1 = Parameter.rangedInteger(1, Integer.MAX_VALUE).key("quantity").build();
    event.register(this.plugin, Command.builder().addParameter(Parameter.firstOf(playerKey, Parameter.user().key(userKey).build())).addParameter(choicesKey).addParameter(rangedInt1).executor(ctx -> {
        if (ctx.hasAny(playerKey)) {
            ctx.sendMessage(Identity.nil(), Component.text(ctx.requireOne(playerKey).name()));
        } else {
            ctx.sendMessage(Identity.nil(), Component.text(ctx.requireOne(userKey).toString()));
        }
        ctx.sendMessage(Identity.nil(), Component.text(ctx.requireOne(choicesKey)));
        ctx.sendMessage(Identity.nil(), Component.text(ctx.requireOne(rangedInt1)));
        return CommandResult.success();
    }).build(), "firstoftest");
}
Also used : Operator(org.spongepowered.api.command.parameter.managed.operator.Operator) Arrays(java.util.Arrays) Command(org.spongepowered.api.command.Command) Inject(com.google.inject.Inject) ValueCompleter(org.spongepowered.api.command.parameter.managed.ValueCompleter) Locale(java.util.Locale) GameProfile(org.spongepowered.api.profile.GameProfile) Selector(org.spongepowered.api.command.selector.Selector) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) CommandCompletion(org.spongepowered.api.command.CommandCompletion) User(org.spongepowered.api.entity.living.player.User) TextColor(net.kyori.adventure.text.format.TextColor) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) UUID(java.util.UUID) TypeToken(io.leangen.geantyref.TypeToken) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) ValueParameter(org.spongepowered.api.command.parameter.managed.ValueParameter) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Flag(org.spongepowered.api.command.parameter.managed.Flag) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) EntityTypes(org.spongepowered.api.entity.EntityTypes) Parameter(org.spongepowered.api.command.parameter.Parameter) Component(net.kyori.adventure.text.Component) ResourceKey(org.spongepowered.api.ResourceKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CommonParameters(org.spongepowered.api.command.parameter.CommonParameters) RegisterCommandEvent(org.spongepowered.api.event.lifecycle.RegisterCommandEvent) CommandResult(org.spongepowered.api.command.CommandResult) Identity(net.kyori.adventure.identity.Identity) Entity(org.spongepowered.api.entity.Entity) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) ValueParameterModifier(org.spongepowered.api.command.parameter.managed.ValueParameterModifier) VariableValueParameters(org.spongepowered.api.command.parameter.managed.standard.VariableValueParameters) SelectorTypes(org.spongepowered.api.command.selector.SelectorTypes) PluginContainer(org.spongepowered.plugin.PluginContainer) Audience(net.kyori.adventure.audience.Audience) CommandContext(org.spongepowered.api.command.parameter.CommandContext) SystemSubject(org.spongepowered.api.SystemSubject) Color(org.spongepowered.api.util.Color) SpongeComponents(org.spongepowered.api.adventure.SpongeComponents) Listener(org.spongepowered.api.event.Listener) Collections(java.util.Collections) CommandException(org.spongepowered.api.command.exception.CommandException) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Entity(org.spongepowered.api.entity.Entity) CommandContext(org.spongepowered.api.command.parameter.CommandContext) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CommandCompletion(org.spongepowered.api.command.CommandCompletion) List(java.util.List) TextColor(net.kyori.adventure.text.format.TextColor) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) UUID(java.util.UUID) Identity(net.kyori.adventure.identity.Identity) Optional(java.util.Optional) Audience(net.kyori.adventure.audience.Audience) TextColor(net.kyori.adventure.text.format.TextColor) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) Color(org.spongepowered.api.util.Color) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) ResourceKey(org.spongepowered.api.ResourceKey) GameProfile(org.spongepowered.api.profile.GameProfile) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Operator(org.spongepowered.api.command.parameter.managed.operator.Operator) User(org.spongepowered.api.entity.living.player.User) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) NotNull(org.jetbrains.annotations.NotNull) ValueParameter(org.spongepowered.api.command.parameter.managed.ValueParameter) Component(net.kyori.adventure.text.Component) ValueCompleter(org.spongepowered.api.command.parameter.managed.ValueCompleter) SystemSubject(org.spongepowered.api.SystemSubject) CommandException(org.spongepowered.api.command.exception.CommandException) Command(org.spongepowered.api.command.Command) TypeToken(io.leangen.geantyref.TypeToken) ValueParameter(org.spongepowered.api.command.parameter.managed.ValueParameter) Parameter(org.spongepowered.api.command.parameter.Parameter) Listener(org.spongepowered.api.event.Listener)

Example 17 with CommandException

use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.

the class SpongeCommandDispatcher method parseNodes.

private ParseResults<CommandSourceStack> parseNodes(// Sponge: used in permission checks
final boolean isRoot, // Sponge: needed for handling what we do with defaults.
final boolean isSuggestion, final CommandNode<CommandSourceStack> node, final SpongeStringReader originalReader, final SpongeCommandContextBuilder contextSoFar) {
    final CommandSourceStack source = contextSoFar.getSource();
    // Sponge Start
    Map<CommandNode<CommandSourceStack>, CommandSyntaxException> errors = null;
    List<ParseResults<CommandSourceStack>> potentials = null;
    // Sponge End
    final int cursor = originalReader.getCursor();
    // Sponge Start: get relevant nodes if we're completing
    final Collection<? extends CommandNode<CommandSourceStack>> nodes;
    if (isSuggestion && node instanceof SpongeNode) {
        nodes = ((SpongeNode) node).getRelevantNodesForSuggestions(originalReader);
    } else if (originalReader.canRead()) {
        nodes = node.getRelevantNodes(originalReader);
    } else {
        // Reader cannot read anymore so ONLY nodes with parsers that do not read can be processed
        nodes = node.getChildren().stream().filter(n -> n instanceof SpongeArgumentCommandNode && (((SpongeArgumentCommandNode<?>) n).getParser().doesNotRead())).collect(Collectors.toList());
    }
    for (final CommandNode<CommandSourceStack> child : nodes) {
        final boolean doesNotRead = child instanceof SpongeArgumentCommandNode && ((SpongeArgumentCommandNode<?>) child).getParser().doesNotRead();
        // if (!child.canUse(source)) {
        if (!SpongeNodePermissionCache.canUse(isRoot, this, child, source)) {
            // Sponge End
            continue;
        }
        // Sponge Start
        final SpongeCommandContextBuilder context = contextSoFar.copy();
        final SpongeStringReader reader = new SpongeStringReader(originalReader);
        // Sponge End
        try {
            try {
                child.parse(reader, context);
            } catch (final RuntimeException ex) {
                throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherParseException().createWithContext(reader, ex.getMessage());
            }
            // Sponge Start: if we didn't consume anything we don't want Brig to complain at us.
            if (reader.getCursor() == cursor) {
                // of suggestions.
                if (isSuggestion && !reader.canRead()) {
                    throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherExpectedArgumentSeparator().createWithContext(reader);
                }
                reader.unskipWhitespace();
            } else if (reader.canRead()) {
                // Sponge End
                if (reader.peek() != CommandDispatcher.ARGUMENT_SEPARATOR_CHAR) {
                    throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherExpectedArgumentSeparator().createWithContext(reader);
                }
            }
        } catch (final CommandSyntaxException ex) {
            if (errors == null) {
                errors = new LinkedHashMap<>();
            }
            errors.put(child, ex);
            reader.setCursor(cursor);
            continue;
        }
        context.withCommand(child.getCommand());
        // must let it do so.
        if (this.shouldContinueTraversing(reader, child)) {
            // if (reader.canRead(child.getRedirect() == null ? 2 : 1)) {
            // Sponge End
            reader.skip();
            // Sponge Start: redirect is now in a local variable as we use it a fair bit
            final CommandNode<CommandSourceStack> redirect = child.getRedirect();
            if (redirect != null) {
                // If we've redirected to the root node, we may need to head back to our command
                // manager - so we should check to see if that's going to actually be the case.
                final boolean redirectingToRoot = redirect == this.getRoot();
                final SpongeCommandContextBuilder childContext = new SpongeCommandContextBuilder(this, source, child.getRedirect(), reader.getCursor());
                // For a redirect, we want to ensure all of our currently parsed information is available.
                context.applySpongeElementsTo(childContext, false);
                ParseResults<CommandSourceStack> parse = null;
                if (redirectingToRoot) {
                    // check to see if we can get an element
                    final ArgumentReader.Immutable snapshot = reader.immutable();
                    try {
                        final String commandToAttempt = reader.peekString();
                        final int offset = reader.cursor();
                        if (redirect.getChild(commandToAttempt) == null) {
                            // The redirect fails, so we intercept here, and reroute to our command
                            // manager with the rest of the string and the new context.
                            reader.parseString();
                            final boolean hasMore = reader.canRead();
                            reader.skipWhitespace();
                            final Optional<CommandMapping> optionalMapping = this.commandManager.commandMapping(commandToAttempt);
                            if (optionalMapping.isPresent()) {
                                final CommandMapping mapping = optionalMapping.get();
                                final String remaining = reader.remaining();
                                childContext.withCommand(commandContext -> {
                                    final SpongeCommandContext spongeContext = (SpongeCommandContext) commandContext;
                                    try {
                                        return mapping.registrar().process(spongeContext.cause(), mapping, commandToAttempt, remaining).result();
                                    } catch (final CommandException e) {
                                        throw new SpongeCommandSyntaxException(e, spongeContext);
                                    }
                                });
                                childContext.withNode(new DummyCommandNode(childContext.getCommand()), StringRange.between(offset, reader.totalLength()));
                                childContext.setNonBrigCommand(hasMore ? new String[] { commandToAttempt, remaining } : new String[] { commandToAttempt });
                                reader.setCursor(reader.totalLength());
                                parse = new ParseResults<>(childContext, reader, Collections.emptyMap());
                            } else {
                                // We'll just let this parser fail as normal.
                                reader.setState(snapshot);
                            }
                        }
                    } catch (final ArgumentParseException ignored) {
                        // ignore it, it'll handle it later.
                        reader.setState(snapshot);
                    }
                }
                if (parse == null) {
                    parse = this.parseNodes(redirect instanceof RootCommandNode, isSuggestion, child.getRedirect(), reader, childContext);
                }
                // It worked out, so let's apply it back. We clear and reapply, it's simpler than comparing and conditional adding.
                childContext.applySpongeElementsTo(context, true);
                // Sponge End
                context.withChild(parse.getContext());
                final ParseResults<CommandSourceStack> parse2 = new ParseResults<>(context, parse.getReader(), parse.getExceptions());
                if (doesNotRead && potentials != null) {
                    // If this is a optional or default parameter we only add the redirect as a potential option
                    potentials.add(parse2);
                    continue;
                }
                return parse2;
            } else {
                final ParseResults<CommandSourceStack> parse = this.parseNodes(false, isSuggestion, child, reader, context);
                if (potentials == null) {
                    potentials = new ArrayList<>(1);
                }
                potentials.add(parse);
            }
        } else {
            if (potentials == null) {
                potentials = new ArrayList<>(1);
            }
            potentials.add(new ParseResults<>(context, reader, Collections.emptyMap()));
        }
    }
    if (potentials != null) {
        if (potentials.size() > 1) {
            potentials.sort((a, b) -> {
                if (!a.getReader().canRead() && b.getReader().canRead()) {
                    return -1;
                }
                if (a.getReader().canRead() && !b.getReader().canRead()) {
                    return 1;
                }
                if (a.getExceptions().isEmpty() && !b.getExceptions().isEmpty()) {
                    return -1;
                }
                if (!a.getExceptions().isEmpty() && b.getExceptions().isEmpty()) {
                    return 1;
                }
                // If we get here both potentials parsed everything and there was no exception
                // BUT if parsing stopped at a non-terminal node this will cause an error later
                // see at the end of #execute() where !foundCommand
                // Instead we attempt to sort commands before that happens
                final Command<CommandSourceStack> aCommand = SpongeCommandDispatcher.getCommand(a.getContext());
                final Command<CommandSourceStack> bCommand = SpongeCommandDispatcher.getCommand(b.getContext());
                if (aCommand == null && bCommand != null) {
                    return 1;
                } else if (aCommand != null && bCommand == null) {
                    return -1;
                }
                return 0;
            });
        }
        return potentials.get(0);
    }
    return new ParseResults<>(contextSoFar, originalReader, errors == null ? Collections.emptyMap() : errors);
}
Also used : LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) EventContextKeys(org.spongepowered.api.event.EventContextKeys) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Command(com.mojang.brigadier.Command) CommandContextBuilder(com.mojang.brigadier.context.CommandContextBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) RedirectModifier(com.mojang.brigadier.RedirectModifier) SuggestionContext(com.mojang.brigadier.context.SuggestionContext) SpongeCommandContextBuilder(org.spongepowered.common.command.brigadier.context.SpongeCommandContextBuilder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) Map(java.util.Map) DummyCommandNode(org.spongepowered.common.command.brigadier.tree.DummyCommandNode) StringReader(com.mojang.brigadier.StringReader) SpongeRootCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeRootCommandNode) CauseStackManager(org.spongepowered.api.event.CauseStackManager) SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeCommandSyntaxException(org.spongepowered.common.command.exception.SpongeCommandSyntaxException) Suggestions(com.mojang.brigadier.suggestion.Suggestions) CommandContext(com.mojang.brigadier.context.CommandContext) CommandUtil(org.spongepowered.common.util.CommandUtil) Collection(java.util.Collection) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Collectors(java.util.stream.Collectors) CommandMapping(org.spongepowered.api.command.manager.CommandMapping) AmbiguityConsumer(com.mojang.brigadier.AmbiguityConsumer) List(java.util.List) SpongeStringReader(org.spongepowered.common.command.brigadier.SpongeStringReader) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) SpongeCommandContext(org.spongepowered.common.command.brigadier.context.SpongeCommandContext) ParseResults(com.mojang.brigadier.ParseResults) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) Optional(java.util.Optional) ResultConsumer(com.mojang.brigadier.ResultConsumer) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) CommandManager(org.spongepowered.api.command.manager.CommandManager) SpongeNode(org.spongepowered.common.command.brigadier.tree.SpongeNode) Collections(java.util.Collections) StringRange(com.mojang.brigadier.context.StringRange) CommandException(org.spongepowered.api.command.exception.CommandException) SpongeCommandManager(org.spongepowered.common.command.manager.SpongeCommandManager) DummyCommandNode(org.spongepowered.common.command.brigadier.tree.DummyCommandNode) LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) DummyCommandNode(org.spongepowered.common.command.brigadier.tree.DummyCommandNode) SpongeRootCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeRootCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) SpongeRootCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeRootCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SpongeCommandContextBuilder(org.spongepowered.common.command.brigadier.context.SpongeCommandContextBuilder) LinkedHashMap(java.util.LinkedHashMap) SpongeStringReader(org.spongepowered.common.command.brigadier.SpongeStringReader) CommandMapping(org.spongepowered.api.command.manager.CommandMapping) SpongeNode(org.spongepowered.common.command.brigadier.tree.SpongeNode) SpongeCommandSyntaxException(org.spongepowered.common.command.exception.SpongeCommandSyntaxException) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) CommandException(org.spongepowered.api.command.exception.CommandException) SpongeCommandSyntaxException(org.spongepowered.common.command.exception.SpongeCommandSyntaxException) ParseResults(com.mojang.brigadier.ParseResults) SpongeCommandContext(org.spongepowered.common.command.brigadier.context.SpongeCommandContext)

Example 18 with CommandException

use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.

the class ActivePagination method specificPage.

public void specificPage(final int page) throws CommandException {
    final Audience src = this.src.get().orElseThrow(() -> new CommandException(Component.text("Source for pagination " + this.getId() + " is no longer active!")));
    this.currentPage = page;
    final List<Component> toSend = new ArrayList<>();
    final Component title = this.title;
    if (title != null) {
        toSend.add(title);
    }
    if (this.header != null) {
        toSend.add(this.header);
    }
    for (final Component line : this.getLines(page)) {
        toSend.add(line);
    }
    final Component footer = this.calculateFooter(page);
    toSend.add(this.calc.center(footer, this.padding));
    if (this.footer != null) {
        toSend.add(this.footer);
    }
    for (final Component line : toSend) {
        src.sendMessage(Identity.nil(), line);
    }
}
Also used : Audience(net.kyori.adventure.audience.Audience) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.exception.CommandException) TextComponent(net.kyori.adventure.text.TextComponent) Component(net.kyori.adventure.text.Component)

Example 19 with CommandException

use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.

the class SpongePaginationList method sendTo.

@Override
public void sendTo(final Audience receiver, final int page) {
    checkNotNull(receiver, "The message receiver cannot be null!");
    final PaginationCalculator calculator = new PaginationCalculator(this.linesPerPage);
    final Iterable<Map.Entry<Component, Integer>> counts = StreamSupport.stream(this.contents.spliterator(), false).map(input -> {
        final int lines = calculator.getLines(input);
        return Maps.immutableEntry(input, lines);
    }).collect(Collectors.toList());
    Component title = this.title;
    if (title != null) {
        title = calculator.center(title, this.paginationSpacer);
    }
    // If the Audience is a Player, then upon death, they will become a different Audience object.
    // Thus, we use a supplier to supply the player from the server, if required.
    final Supplier<Optional<? extends Audience>> audienceSupplier;
    if (receiver instanceof Player) {
        final UUID playerUuid = ((Player) receiver).uniqueId();
        audienceSupplier = () -> Sponge.server().player(playerUuid);
    } else {
        final WeakReference<Audience> srcReference = new WeakReference<>(receiver);
        audienceSupplier = () -> Optional.ofNullable(srcReference.get());
    }
    final ActivePagination pagination;
    if (this.contents instanceof List) {
        // If it started out as a list, it's probably reasonable to copy it to another list
        pagination = new ListPagination(audienceSupplier, calculator, ImmutableList.copyOf(counts), title, this.header, this.footer, this.paginationSpacer);
    } else {
        pagination = new IterablePagination(audienceSupplier, calculator, counts, title, this.header, this.footer, this.paginationSpacer);
    }
    this.service.getPaginationState(receiver, true).put(pagination);
    try {
        pagination.specificPage(page);
    } catch (final CommandException e) {
        final Component text = e.componentMessage();
        if (text != null) {
            receiver.sendMessage(Identity.nil(), text.color(NamedTextColor.RED));
        }
    }
}
Also used : Identity(net.kyori.adventure.identity.Identity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Maps(com.google.common.collect.Maps) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Audience(net.kyori.adventure.audience.Audience) Component(net.kyori.adventure.text.Component) Map(java.util.Map) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) Player(org.spongepowered.api.entity.living.player.Player) WeakReference(java.lang.ref.WeakReference) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CommandException(org.spongepowered.api.command.exception.CommandException) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) Audience(net.kyori.adventure.audience.Audience) CommandException(org.spongepowered.api.command.exception.CommandException) WeakReference(java.lang.ref.WeakReference) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Component(net.kyori.adventure.text.Component) UUID(java.util.UUID)

Example 20 with CommandException

use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.

the class TagTest method requireItemInHand.

private static ItemType requireItemInHand(final CommandContext ctx) throws CommandException {
    final ServerPlayer serverPlayer = ctx.cause().first(ServerPlayer.class).orElseThrow(() -> new CommandException(Component.text("You must be a player to use this command! (Held item)")));
    final ItemStack heldItem = serverPlayer.itemInHand(HandTypes.MAIN_HAND);
    if (heldItem.isEmpty()) {
        throw new CommandException(Component.text("You must hold an item in your main hand!"));
    }
    return heldItem.type();
}
Also used : ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

CommandException (org.spongepowered.api.command.exception.CommandException)26 Player (org.spongepowered.api.entity.living.player.Player)19 ItemStack (org.spongepowered.api.item.inventory.ItemStack)17 MapInfo (org.spongepowered.api.map.MapInfo)10 Component (net.kyori.adventure.text.Component)8 Audience (net.kyori.adventure.audience.Audience)6 CommandResult (org.spongepowered.api.command.CommandResult)5 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)5 Inject (com.google.inject.Inject)4 UUID (java.util.UUID)4 Identity (net.kyori.adventure.identity.Identity)4 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)4 Sponge (org.spongepowered.api.Sponge)4 Command (org.spongepowered.api.command.Command)4 Listener (org.spongepowered.api.event.Listener)4 RegisterCommandEvent (org.spongepowered.api.event.lifecycle.RegisterCommandEvent)4 PluginContainer (org.spongepowered.plugin.PluginContainer)4 Plugin (org.spongepowered.plugin.builtin.jvm.Plugin)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3