Search in sources :

Example 6 with Locatable

use of org.spongepowered.api.world.Locatable in project SpongeCommon by SpongePowered.

the class SpongeContextCalculator method accumulateContexts.

@Override
public void accumulateContexts(Subject subject, Set<Context> accumulator) {
    Optional<CommandSource> subjSource = subject.getCommandSource();
    if (subjSource.isPresent()) {
        CommandSource source = subjSource.get();
        if (source instanceof Locatable) {
            World currentExt = ((Locatable) source).getWorld();
            accumulator.add(currentExt.getContext());
            accumulator.add((currentExt.getDimension().getContext()));
        }
        if (source instanceof RemoteSource) {
            RemoteSource rem = (RemoteSource) source;
            accumulator.addAll(this.remoteIpCache.getUnchecked(rem));
            accumulator.addAll(this.localIpCache.getUnchecked(rem));
            accumulator.add(new Context(Context.LOCAL_PORT_KEY, String.valueOf(rem.getConnection().getVirtualHost().getPort())));
            accumulator.add(new Context(Context.LOCAL_HOST_KEY, rem.getConnection().getVirtualHost().getHostName()));
        }
    }
}
Also used : Context(org.spongepowered.api.service.context.Context) RemoteSource(org.spongepowered.api.command.source.RemoteSource) CommandSource(org.spongepowered.api.command.CommandSource) World(org.spongepowered.api.world.World) Locatable(org.spongepowered.api.world.Locatable)

Example 7 with Locatable

use of org.spongepowered.api.world.Locatable in project LanternServer by LanternPowered.

the class CommandParticleEffect method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.catalogedElement(Text.of("type"), ParticleType.class), GenericArguments.vector3d(Text.of("position")), GenericArguments.optional(GenericArguments.world(Text.of("world"))), // TODO: Tab complaining is currently throwing errors, but it's a small bug in SpongeAPI
    GenericArguments.flags().valueFlag(GenericArguments.integer(Text.of("quantity")), "-quantity", "q").valueFlag(GenericArguments.vector3d(Text.of("offset")), "-offset", "o").valueFlag(GenericArguments.vector3d(Text.of("velocity")), "-velocity", "v").valueFlag(GenericArguments.vector3d(Text.of("color")), "-color", "c").valueFlag(GenericArguments.doubleNum(Text.of("scale")), "-scale", "s").valueFlag(GenericArguments.catalogedElement(Text.of("note"), NotePitch.class), "-note", "n").valueFlag(GenericArguments.catalogedElement(Text.of("block"), BlockState.class), "-block", "b").valueFlag(GenericArguments.catalogedElement(Text.of("item"), ItemType.class), "-item", "i").valueFlag(GenericArguments.catalogedElement(Text.of("potion"), PotionEffectType.class), "-potion", "p").buildWith(GenericArguments.none())).executor((src, args) -> {
        final ParticleType particleType = args.<ParticleType>getOne("type").get();
        final Vector3d position = args.<Vector3d>getOne("position").get();
        final World world = args.<WorldProperties>getOne("world").map(props -> Sponge.getServer().getWorld(props.getUniqueId()).get()).orElseGet(((Locatable) src)::getWorld);
        final ParticleEffect.Builder builder = ParticleEffect.builder().type(particleType);
        args.<Integer>getOne("quantity").ifPresent(builder::quantity);
        args.<Vector3d>getOne("offset").ifPresent(builder::offset);
        args.<Vector3d>getOne("velocity").ifPresent(builder::velocity);
        args.<Vector3d>getOne("color").ifPresent(color -> builder.option(ParticleOptions.COLOR, Color.of(color.toInt())));
        args.<NotePitch>getOne("note").ifPresent(note -> builder.option(ParticleOptions.NOTE, note));
        args.<Double>getOne("scale").ifPresent(scale -> builder.option(ParticleOptions.SCALE, scale));
        args.<BlockState>getOne("block").ifPresent(blockState -> builder.option(ParticleOptions.BLOCK_STATE, blockState));
        args.<ItemType>getOne("item").ifPresent(item -> builder.option(ParticleOptions.ITEM_STACK_SNAPSHOT, new LanternItemStack(item).createSnapshot()));
        args.<PotionEffectType>getOne("potion").ifPresent(type -> builder.option(ParticleOptions.POTION_EFFECT_TYPE, type));
        world.spawnParticles(builder.build(), position);
        src.sendMessage(t("Successfully spawned the particle %s", particleType.getName()));
        return CommandResult.success();
    });
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) ParticleOptions(org.spongepowered.api.effect.particle.ParticleOptions) BlockTypes(org.spongepowered.api.block.BlockTypes) Sponge(org.spongepowered.api.Sponge) Vector3d(com.flowpowered.math.vector.Vector3d) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) BlockState(org.spongepowered.api.block.BlockState) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) ParticleType(org.spongepowered.api.effect.particle.ParticleType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) Locatable(org.spongepowered.api.world.Locatable) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) NotePitch(org.spongepowered.api.data.type.NotePitch) Color(org.spongepowered.api.util.Color) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) ItemType(org.spongepowered.api.item.ItemType) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) Vector3d(com.flowpowered.math.vector.Vector3d) ItemType(org.spongepowered.api.item.ItemType) ParticleType(org.spongepowered.api.effect.particle.ParticleType) World(org.spongepowered.api.world.World) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) NotePitch(org.spongepowered.api.data.type.NotePitch)

Example 8 with Locatable

use of org.spongepowered.api.world.Locatable in project Nucleus by NucleusPowered.

the class KillEntityCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    if (!(src instanceof Locatable) && args.hasAny(radius)) {
        // We can't do that.
        throw ReturnMessageException.fromKey("command.killentity.commandsourceradius");
    }
    if (args.hasAny(radius) && args.hasAny(world)) {
        // Can't do that, either.
        throw ReturnMessageException.fromKey("command.killentity.radiusworld");
    }
    Set<Entity> currentEntities;
    if (args.hasAny(radius)) {
        Locatable l = ((Locatable) src);
        Vector3d locationTest = l.getLocation().getPosition();
        int r = args.<Integer>getOne(radius).get();
        currentEntities = Sets.newHashSet(l.getWorld().getEntities(entity -> entity.getTransform().getPosition().distance(locationTest) <= r));
    } else {
        WorldProperties worldProperties = this.getWorldFromUserOrArgs(src, world, args);
        currentEntities = Sets.newHashSet(Sponge.getServer().getWorld(worldProperties.getUniqueId()).get().getEntities());
    }
    Predicate<Entity> entityPredicate = args.<Predicate<Entity>>getAll(type).stream().reduce(Predicate::or).orElseThrow(() -> ReturnMessageException.fromKey("command.killentity.noselection"));
    Set<Entity> toKill = currentEntities.stream().filter(entityPredicate).collect(Collectors.toSet());
    if (toKill.isEmpty()) {
        throw ReturnMessageException.fromKey("command.killentity.nothing");
    }
    int killCount = toKill.size();
    toKill.forEach(x -> {
        x.offer(Keys.HEALTH, 0d);
        x.remove();
    });
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.killentity.success", String.valueOf(killCount)));
    return CommandResult.affectedEntities(killCount);
}
Also used : Entity(org.spongepowered.api.entity.Entity) Vector3d(com.flowpowered.math.vector.Vector3d) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Locatable(org.spongepowered.api.world.Locatable)

Example 9 with Locatable

use of org.spongepowered.api.world.Locatable in project Nucleus by NucleusPowered.

the class SetBorderCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties wp = getWorldFromUserOrArgs(src, worldKey, args);
    int x;
    int z;
    int dia = args.<Integer>getOne(diameter).get();
    int delay = args.<Integer>getOne(delayKey).orElse(0);
    if (src instanceof Locatable) {
        Location<World> lw = ((Locatable) src).getLocation();
        if (args.hasAny(zKey)) {
            x = args.<Integer>getOne(xKey).get();
            z = args.<Integer>getOne(zKey).get();
        } else {
            x = lw.getBlockX();
            z = lw.getBlockZ();
        }
    } else {
        x = args.<Integer>getOne(xKey).get();
        z = args.<Integer>getOne(zKey).get();
    }
    // Now, if we have an x and a z key, get the centre from that.
    wp.setWorldBorderCenter(x, z);
    Optional<World> world = Sponge.getServer().getWorld(wp.getUniqueId());
    world.ifPresent(w -> w.getWorldBorder().setCenter(x, z));
    wp.setWorldBorderCenter(x, z);
    if (delay == 0) {
        world.ifPresent(w -> w.getWorldBorder().setDiameter(dia));
        wp.setWorldBorderDiameter(dia);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.setborder.set", wp.getWorldName(), String.valueOf(x), String.valueOf(z), String.valueOf(dia)));
    } else {
        world.ifPresent(w -> w.getWorldBorder().setDiameter(dia, delay * 1000L));
        wp.setWorldBorderTimeRemaining(delay * 1000L);
        wp.setWorldBorderTargetDiameter(dia);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.setborder.setdelay", wp.getWorldName(), String.valueOf(x), String.valueOf(z), String.valueOf(dia), String.valueOf(delay)));
    }
    return CommandResult.success();
}
Also used : World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Locatable(org.spongepowered.api.world.Locatable)

Aggregations

Locatable (org.spongepowered.api.world.Locatable)9 World (org.spongepowered.api.world.World)5 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)5 Vector3d (com.flowpowered.math.vector.Vector3d)4 CommandSource (org.spongepowered.api.command.CommandSource)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 CommandException (org.spongepowered.api.command.CommandException)2 RemoteSource (org.spongepowered.api.command.source.RemoteSource)2 Entity (org.spongepowered.api.entity.Entity)2 Context (org.spongepowered.api.service.context.Context)2 RelativeDouble (org.lanternpowered.server.command.element.RelativeDouble)1 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)1 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)1 Sponge (org.spongepowered.api.Sponge)1 BlockState (org.spongepowered.api.block.BlockState)1 BlockTypes (org.spongepowered.api.block.BlockTypes)1 CommandResult (org.spongepowered.api.command.CommandResult)1 GenericArguments (org.spongepowered.api.command.args.GenericArguments)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 NotePitch (org.spongepowered.api.data.type.NotePitch)1