Search in sources :

Example 1 with RelativeDouble

use of org.lanternpowered.server.command.element.RelativeDouble in project LanternServer by LanternPowered.

the class CommandTeleport method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(// TODO: Replace with entity selector
    GenericArguments.player(Text.of("target")), GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), GenericArguments.vector3d(Text.of("position")), GenericArguments.optional(GenericArguments.seq(GenericArguments2.relativeDoubleNum(Text.of("y-rot")), GenericArguments2.relativeDoubleNum(Text.of("x-rot"))))).executor((src, args) -> {
        // TODO: Replace with selected entities
        final Entity target = args.<Entity>getOne("target").get();
        final World world = CommandHelper.getWorld(src, args);
        final Location<World> location = new Location<>(world, args.<Vector3d>getOne("position").get());
        if (args.hasAny("y-rot")) {
            RelativeDouble yRot = args.<RelativeDouble>getOne("y-rot").get();
            RelativeDouble xRot = args.<RelativeDouble>getOne("x-rot").get();
            boolean rel = yRot.isRelative() || xRot.isRelative();
            if (rel && !(src instanceof Locatable)) {
                throw new CommandException(t("Relative rotation specified but source does not have a rotation."));
            }
            double xRot0 = xRot.getValue();
            double yRot0 = yRot.getValue();
            double zRot0 = 0;
            // is locatable, just handle it then as absolute
            if (src instanceof Entity) {
                final Vector3d rot = ((Entity) src).getRotation();
                xRot0 = xRot.applyToValue(rot.getX());
                yRot0 = yRot.applyToValue(rot.getY());
                zRot0 = rot.getZ();
            }
            target.setLocationAndRotation(location, new Vector3d(xRot0, yRot0, zRot0));
        } else {
            target.setLocation(location);
        }
        src.sendMessage(t("commands.teleport.success.coordinates", location.getX(), location.getY(), location.getZ()));
        return CommandResult.success();
    });
}
Also used : Entity(org.spongepowered.api.entity.Entity) Vector3d(com.flowpowered.math.vector.Vector3d) RelativeDouble(org.lanternpowered.server.command.element.RelativeDouble) CommandException(org.spongepowered.api.command.CommandException) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) Locatable(org.spongepowered.api.world.Locatable)

Example 2 with RelativeDouble

use of org.lanternpowered.server.command.element.RelativeDouble in project LanternServer by LanternPowered.

the class CommandTp method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.optional(GenericArguments.player(Text.of("target"))), GenericArguments.firstParsing(GenericArguments.player(Text.of("destination")), GenericArguments.seq(/*
                                        GenericArguments.flags()
                                                .valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY),
                                                        "-world", "w")
                                                .buildWith(GenericArguments.none()),
                                        */
    GenericArguments.optional(GenericArguments.world(CommandHelper.WORLD_KEY)), GenericArguments2.targetedRelativeVector3d(Text.of("coordinates")), GenericArguments.optional(GenericArguments.seq(GenericArguments2.relativeDoubleNum(Text.of("y-rot")), GenericArguments2.relativeDoubleNum(Text.of("x-rot"))))))).executor((src, args) -> {
        Player target = args.<Player>getOne("target").orElse(null);
        if (target == null) {
            if (!(src instanceof Player)) {
                throw new CommandException(t("The target parameter is only optional for players."));
            }
            target = (Player) src;
        }
        final Optional<Player> optDestination = args.getOne("destination");
        if (optDestination.isPresent()) {
            final Player destination = optDestination.get();
            target.setTransform(destination.getTransform());
            src.sendMessage(t("commands.tp.success", target.getName(), destination.getName()));
        } else {
            final RelativeVector3d coords = args.<RelativeVector3d>getOne("coordinates").get();
            final Transform<World> transform = target.getTransform();
            World world = args.<WorldProperties>getOne(CommandHelper.WORLD_KEY).flatMap(p -> Lantern.getServer().getWorld(p.getUniqueId())).orElse(transform.getExtent());
            Vector3d position = coords.applyToValue(transform.getPosition());
            final Optional<RelativeDouble> optYRot = args.getOne("y-rot");
            if (optYRot.isPresent()) {
                final Vector3d rot = transform.getRotation();
                double xRot = args.<RelativeDouble>getOne("x-rot").get().applyToValue(rot.getX());
                double yRot = args.<RelativeDouble>getOne("y-rot").get().applyToValue(rot.getY());
                double zRot = rot.getZ();
                target.setLocationAndRotation(new Location<>(world, position), new Vector3d(xRot, yRot, zRot));
            } else {
                target.setLocation(new Location<>(world, position));
            }
            src.sendMessage(t("commands.tp.success.position", target.getName(), formatDouble(position.getX()), formatDouble(position.getY()), formatDouble(position.getZ()), world.getName()));
        }
        return CommandResult.success();
    });
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) RelativeDouble(org.lanternpowered.server.command.element.RelativeDouble) Location(org.spongepowered.api.world.Location) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) Vector3d(com.flowpowered.math.vector.Vector3d) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RelativeVector3d(org.lanternpowered.server.command.element.RelativeVector3d) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) Transform(org.spongepowered.api.entity.Transform) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Player(org.spongepowered.api.entity.living.player.Player) RelativeVector3d(org.lanternpowered.server.command.element.RelativeVector3d) CommandException(org.spongepowered.api.command.CommandException) World(org.spongepowered.api.world.World) Vector3d(com.flowpowered.math.vector.Vector3d) RelativeVector3d(org.lanternpowered.server.command.element.RelativeVector3d) RelativeDouble(org.lanternpowered.server.command.element.RelativeDouble) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

Vector3d (com.flowpowered.math.vector.Vector3d)2 RelativeDouble (org.lanternpowered.server.command.element.RelativeDouble)2 CommandException (org.spongepowered.api.command.CommandException)2 Location (org.spongepowered.api.world.Location)2 World (org.spongepowered.api.world.World)2 Optional (java.util.Optional)1 GenericArguments2 (org.lanternpowered.server.command.element.GenericArguments2)1 RelativeVector3d (org.lanternpowered.server.command.element.RelativeVector3d)1 Lantern (org.lanternpowered.server.game.Lantern)1 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)1 CommandResult (org.spongepowered.api.command.CommandResult)1 GenericArguments (org.spongepowered.api.command.args.GenericArguments)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 Entity (org.spongepowered.api.entity.Entity)1 Transform (org.spongepowered.api.entity.Transform)1 Player (org.spongepowered.api.entity.living.player.Player)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1 Text (org.spongepowered.api.text.Text)1 Locatable (org.spongepowered.api.world.Locatable)1 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)1