Search in sources :

Example 11 with ArgumentParseException

use of org.spongepowered.api.command.args.ArgumentParseException in project LanternServer by LanternPowered.

the class CommandOp method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(new CommandElement(Text.of("player")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            return args.next();
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            final String prefix = args.nextIfPresent().orElse("");
            final UserConfig<OpsEntry> config = Lantern.getGame().getOpsConfig();
            return Lantern.getGame().getGameProfileManager().getCache().getProfiles().stream().filter(p -> p.getName().isPresent() && !config.getEntryByUUID(p.getUniqueId()).isPresent()).map(p -> p.getName().get()).filter(new StartsWithPredicate(prefix)).collect(ImmutableList.toImmutableList());
        }
    }, GenericArguments.optional(GenericArguments.integer(Text.of("level")))).executor((src, args) -> {
        String playerName = args.<String>getOne("player").get();
        UserConfig<OpsEntry> config = Lantern.getGame().getOpsConfig();
        if (!(src instanceof ConsoleSource) && args.hasAny("level")) {
            throw new CommandException(Text.of("Only the console may specify the op level."));
        }
        int opLevel = args.<Integer>getOne("level").orElse(Lantern.getGame().getGlobalConfig().getDefaultOpPermissionLevel());
        Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
            if (error != null) {
                src.sendMessage(t("commands.op.failed", playerName));
            } else {
                src.sendMessage(t("commands.op.success", playerName));
                config.addEntry(new OpsEntry(((LanternGameProfile) profile).withoutProperties(), opLevel));
            }
        });
        return CommandResult.success();
    });
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandResult(org.spongepowered.api.command.CommandResult) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) OpsEntry(org.lanternpowered.server.config.user.OpsEntry) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) UserConfig(org.lanternpowered.server.config.user.UserConfig) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) CommandContext(org.spongepowered.api.command.args.CommandContext) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandException(org.spongepowered.api.command.CommandException) CommandSource(org.spongepowered.api.command.CommandSource) OpsEntry(org.lanternpowered.server.config.user.OpsEntry) CommandElement(org.spongepowered.api.command.args.CommandElement) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource)

Example 12 with ArgumentParseException

use of org.spongepowered.api.command.args.ArgumentParseException in project Nucleus by NucleusPowered.

the class NoCostArgumentTests method testNoCostException.

@Test
public void testNoCostException() {
    CommandContext cc = new CommandContext();
    try {
        getNoCostArgument(new ThrowsElement(), cc);
    } catch (ArgumentParseException e) {
    /* Swallow */
    }
    Assert.assertFalse(cc.<Boolean>getOne(NoModifiersArgument.NO_COST_ARGUMENT).isPresent());
}
Also used : CommandContext(org.spongepowered.api.command.args.CommandContext) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) Test(org.junit.Test)

Aggregations

ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)12 CommandContext (org.spongepowered.api.command.args.CommandContext)11 List (java.util.List)9 Text (org.spongepowered.api.text.Text)8 Nullable (javax.annotation.Nullable)7 GenericArguments (org.spongepowered.api.command.args.GenericArguments)7 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)7 CommandSource (org.spongepowered.api.command.CommandSource)6 CommandArgs (org.spongepowered.api.command.args.CommandArgs)6 CommandElement (org.spongepowered.api.command.args.CommandElement)6 ImmutableList (com.google.common.collect.ImmutableList)5 StartsWithPredicate (org.spongepowered.api.util.StartsWithPredicate)5 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)4 CommandException (org.spongepowered.api.command.CommandException)4 CommandResult (org.spongepowered.api.command.CommandResult)4 PluginContainer (org.spongepowered.api.plugin.PluginContainer)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Lists (com.google.common.collect.Lists)3 Collectors (java.util.stream.Collectors)3 Test (org.junit.Test)3