Search in sources :

Example 6 with CommandArgs

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

the class CommandParticle method completeSpec.

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

        @Override
        protected Iterable<String> getChoices(CommandSource source) {
            return Sponge.getGame().getRegistry().getAllOf(ParticleType.class).stream().filter(type -> ((LanternParticleType) type).getInternalType().isPresent()).map(CatalogType::getId).collect(Collectors.toList());
        }

        @Override
        protected Object getValue(String choice) throws IllegalArgumentException {
            final Optional<ParticleType> ret = Sponge.getGame().getRegistry().getType(ParticleType.class, choice);
            if (!ret.isPresent() || !((LanternParticleType) ret.get()).getInternalType().isPresent()) {
                throw new IllegalArgumentException("Invalid input " + choice + " was found");
            }
            return ret.get();
        }
    }, GenericArguments2.targetedVector3d(Text.of("position")), // The default value should be 0 for x, y and z
    GenericArguments2.vector3d(Text.of("offset"), Vector3d.ZERO), GenericArguments2.doubleNum(Text.of("speed"), 1.0), GenericArguments.optional(GenericArguments2.integer(Text.of("count"), 1)), GenericArguments.optional(new CommandElement(Text.of("mode")) {

        @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) {
            Optional<String> arg = args.nextIfPresent();
            if (arg.isPresent()) {
                return Stream.of("normal", "force").filter(new StartsWithPredicate(arg.get())).collect(Collectors.toList());
            }
            return Collections.emptyList();
        }
    }), GenericArguments.optional(GenericArguments.player(Text.of("player"))), GenericArguments.optional(new CommandElement(Text.of("params")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            List<Integer> params = new ArrayList<>();
            while (args.hasNext()) {
                String arg = args.next();
                try {
                    params.add(Integer.parseInt(arg));
                } catch (NumberFormatException e) {
                    throw args.createError(t("Expected an integer, but input '%s' was not", arg));
                }
            }
            return params.stream().mapToInt(i -> i).toArray();
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            return Collections.emptyList();
        }
    })).executor((src, args) -> {
        final LanternParticleType particleType = args.<LanternParticleType>getOne("type").get();
        final int particleId = particleType.getInternalType().getAsInt();
        final Vector3f position = args.<Vector3d>getOne("position").get().toFloat();
        final Vector3f offset = args.<Vector3d>getOne("offset").get().toFloat();
        final float speed = args.<Double>getOne("speed").get().floatValue();
        final int count = args.<Integer>getOne("count").orElse(1);
        final boolean longDistance = args.<String>getOne("mode").map(mode -> mode.equalsIgnoreCase("force")).orElse(false);
        final int[] params = args.<int[]>getOne("params").orElse(new int[0]);
        final LanternWorld world = CommandHelper.getWorld(src, args);
        final int dataLength;
        if (particleType == ParticleTypes.BLOCK_CRACK || particleType == ParticleTypes.BLOCK_DUST || particleType == ParticleTypes.FALLING_DUST) {
            dataLength = 1;
        } else if (particleType == ParticleTypes.ITEM_CRACK) {
            dataLength = 2;
        } else {
            dataLength = 0;
        }
        if (params.length != dataLength) {
            throw new CommandException(t("Invalid parameters (%s), length mismatch (got %s, expected %s) for the particle type %s", Arrays.toString(params), params.length, dataLength, particleType.getId()));
        }
        final MessagePlayOutSpawnParticle message = new MessagePlayOutSpawnParticle(particleId, position, offset, speed, count, params, longDistance);
        if (args.hasAny("player")) {
            args.<LanternPlayer>getOne("player").get().getConnection().send(message);
        } else {
            for (LanternPlayer player : world.getRawPlayers()) {
                player.getConnection().send(message);
            }
        }
        src.sendMessage(t("commands.particle.success", particleType.getName(), count));
        return CommandResult.success();
    });
}
Also used : Arrays(java.util.Arrays) MessagePlayOutSpawnParticle(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnParticle) Vector3d(com.flowpowered.math.vector.Vector3d) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType) GenericArguments(org.spongepowered.api.command.args.GenericArguments) ArrayList(java.util.ArrayList) ParticleType(org.spongepowered.api.effect.particle.ParticleType) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) ParticleTypes(org.spongepowered.api.effect.particle.ParticleTypes) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) LanternWorld(org.lanternpowered.server.world.LanternWorld) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) Stream(java.util.stream.Stream) Vector3f(com.flowpowered.math.vector.Vector3f) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) Collections(java.util.Collections) CommandContext(org.spongepowered.api.command.args.CommandContext) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType) ParticleType(org.spongepowered.api.effect.particle.ParticleType) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) LanternWorld(org.lanternpowered.server.world.LanternWorld) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) ArrayList(java.util.ArrayList) List(java.util.List) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Optional(java.util.Optional) MessagePlayOutSpawnParticle(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnParticle) CommandException(org.spongepowered.api.command.CommandException) CommandSource(org.spongepowered.api.command.CommandSource) Vector3d(com.flowpowered.math.vector.Vector3d) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) Vector3f(com.flowpowered.math.vector.Vector3f) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType) CommandElement(org.spongepowered.api.command.args.CommandElement) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) Nullable(javax.annotation.Nullable) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 7 with CommandArgs

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

the class WorldTimeArgument method getValue.

private LongFunction<Long> getValue(String arg, CommandArgs args) throws ArgumentParseException {
    if (tickAliases.containsKey(arg)) {
        return tickAliases.get(arg);
    }
    // <number>h
    Matcher m1 = tfh.matcher(arg);
    if (m1.matches()) {
        // Get the number, multiply by 1000, return.
        long i = Long.parseLong(m1.group(1));
        if (i > 23 || i < 0) {
            throw args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.worldtime.24herror"));
        }
        i -= 6;
        if (i < 0) {
            i += 24;
        }
        final long res = i * 1000;
        return x -> res;
    }
    // <number>am,pm
    Matcher m2 = ampm.matcher(arg);
    if (m2.matches()) {
        // Get the number, multiply by 1000, return.
        int i = Integer.parseInt(m2.group(1));
        if (i > 12 || i < 1) {
            throw args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.worldtime.12herror"));
        }
        // Modify to 24 hour time, based on am/pm
        String id = m2.group(2).toLowerCase();
        if (id.startsWith("p") && i < 12) {
            // 11 pm -> 23, 12 pm -> 12.
            i += 12;
        } else if (id.startsWith("a") && i == 12) {
            // 12 am -> 0
            i = 0;
        }
        // Adjust for Minecraft time.
        i -= 6;
        if (i < 0) {
            i += 24;
        }
        final long res = i * 1000;
        return x -> res;
    }
    // 0 -> 23999
    if (ticks.matcher(arg).matches()) {
        final long i = Long.parseLong(arg);
        if (i >= 0 && i <= 23999) {
            return x -> i;
        }
        throw args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.worldtime.ticks"));
    }
    throw args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.worldtime.error", arg));
}
Also used : Nucleus(io.github.nucleuspowered.nucleus.Nucleus) LongFunction(java.util.function.LongFunction) CommandSource(org.spongepowered.api.command.CommandSource) HashMap(java.util.HashMap) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Maps(com.google.common.collect.Maps) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Matcher(java.util.regex.Matcher) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Pattern(java.util.regex.Pattern) Nullable(javax.annotation.Nullable) Matcher(java.util.regex.Matcher)

Example 8 with CommandArgs

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

the class NicknameArgumentTests method testWhenTwoPlayersWithTheSameNameAreInTheUserDatabaseOnlyAnExactMatchIsReturned.

@Test
public void testWhenTwoPlayersWithTheSameNameAreInTheUserDatabaseOnlyAnExactMatchIsReturned() throws ArgumentParseException {
    List<?> list = getParser().accept("test", mockSource(), new CommandArgs("", new ArrayList<>()));
    Assert.assertEquals(1, list.size());
    Assert.assertEquals("test", ((User) list.get(0)).getName());
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 9 with CommandArgs

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

the class NicknameArgumentTests method testWhenTwoPlayersWithTheSameNameAreInTheUserDatabaseWithNoExactMatchReturnsNoneReturnIfNoneMatch.

@Test
public void testWhenTwoPlayersWithTheSameNameAreInTheUserDatabaseWithNoExactMatchReturnsNoneReturnIfNoneMatch() throws ArgumentParseException {
    List<?> list = getParser().accept("blah", mockSource(), new CommandArgs("", new ArrayList<>()));
    Assert.assertTrue(list.isEmpty());
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with CommandArgs

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

the class NicknameArgumentTests method testWhenTwoPlayersWithTheSameNameAreInTheUserDatabaseWithNoExactMatchReturnsBothIfTheyOtherwiseMatch.

@Test
public void testWhenTwoPlayersWithTheSameNameAreInTheUserDatabaseWithNoExactMatchReturnsBothIfTheyOtherwiseMatch() throws ArgumentParseException {
    List<?> list = getParser().accept("tes", mockSource(), new CommandArgs("", new ArrayList<>()));
    Assert.assertEquals(2, list.size());
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

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