Search in sources :

Example 1 with ParticleType

use of org.spongepowered.api.effect.particle.ParticleType in project SpongeCommon by SpongePowered.

the class SpongeParticleEffectBuilder method buildContent.

@Override
protected Optional<ParticleEffect> buildContent(DataView container) throws InvalidDataException {
    if (!container.contains(DataQueries.PARTICLE_TYPE, DataQueries.PARTICLE_OPTIONS)) {
        return Optional.empty();
    }
    ParticleType particleType = container.getCatalogType(DataQueries.PARTICLE_TYPE, ParticleType.class).get();
    Map<ParticleOption<?>, Object> options = new HashMap<>();
    container.getViewList(DataQueries.PARTICLE_OPTIONS).get().forEach(view -> {
        ParticleOption<?> option = view.getCatalogType(DataQueries.PARTICLE_OPTION_KEY, ParticleOption.class).get();
        Object value;
        if (option.getValueType().isAssignableFrom(DataSerializable.class)) {
            value = view.getSerializable(DataQueries.PARTICLE_OPTION_VALUE, (Class<? extends DataSerializable>) option.getValueType()).get();
        } else {
            value = view.getObject(DataQueries.PARTICLE_OPTION_VALUE, option.getValueType()).get();
        }
        options.put(option, value);
    });
    return Optional.of(new SpongeParticleEffect((SpongeParticleType) particleType, options));
}
Also used : ParticleOption(org.spongepowered.api.effect.particle.ParticleOption) HashMap(java.util.HashMap) ParticleType(org.spongepowered.api.effect.particle.ParticleType)

Example 2 with ParticleType

use of org.spongepowered.api.effect.particle.ParticleType in project SpongeCommon by SpongePowered.

the class AreaEffectCloudDataProcessor method set.

@SuppressWarnings("unchecked")
@Override
protected boolean set(EntityAreaEffectCloud dataHolder, Map<Key<?>, Object> keyValues) {
    final int age = (int) keyValues.get(Keys.AREA_EFFECT_CLOUD_AGE);
    final Color color = (Color) keyValues.get(Keys.AREA_EFFECT_CLOUD_COLOR);
    final double radius = (double) keyValues.get(Keys.AREA_EFFECT_CLOUD_RADIUS);
    final double radiusOnUse = (double) keyValues.get(Keys.AREA_EFFECT_CLOUD_RADIUS_ON_USE);
    final int duration = (int) keyValues.get(Keys.AREA_EFFECT_CLOUD_DURATION);
    final int durationOnUse = (int) keyValues.get(Keys.AREA_EFFECT_CLOUD_DURATION_ON_USE);
    final int waitTime = (int) keyValues.get(Keys.AREA_EFFECT_CLOUD_WAIT_TIME);
    final int reapplicationDelay = (int) keyValues.get(Keys.AREA_EFFECT_CLOUD_REAPPLICATION_DELAY);
    final List<PotionEffect> potionEffects = (List<PotionEffect>) keyValues.get(Keys.POTION_EFFECTS);
    final ParticleType particleType = (ParticleType) keyValues.get(Keys.AREA_EFFECT_CLOUD_PARTICLE_TYPE);
    dataHolder.ticksExisted = age;
    dataHolder.setColor(color.getRgb());
    dataHolder.setRadius((float) radius);
    dataHolder.setRadiusOnUse((float) radiusOnUse);
    dataHolder.setDuration(duration);
    ((IMixinAreaEffectCloud) dataHolder).setDurationOnUse(durationOnUse);
    dataHolder.setWaitTime(waitTime);
    final EnumParticleTypes internalType = ((SpongeParticleType) particleType).getInternalType();
    dataHolder.setParticle(internalType == null ? EnumParticleTypes.SPELL_MOB : internalType);
    final List<net.minecraft.potion.PotionEffect> effects = new ArrayList<>();
    for (PotionEffect effect : potionEffects) {
        effects.add((net.minecraft.potion.PotionEffect) effect);
    }
    ((IMixinAreaEffectCloud) dataHolder).setPotionEffects(effects);
    ((IMixinAreaEffectCloud) dataHolder).setReapplicationDelay(reapplicationDelay);
    return true;
}
Also used : SpongeParticleType(org.spongepowered.common.effect.particle.SpongeParticleType) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Color(org.spongepowered.api.util.Color) ArrayList(java.util.ArrayList) SpongeParticleType(org.spongepowered.common.effect.particle.SpongeParticleType) ParticleType(org.spongepowered.api.effect.particle.ParticleType) EnumParticleTypes(net.minecraft.util.EnumParticleTypes) ArrayList(java.util.ArrayList) List(java.util.List) IMixinAreaEffectCloud(org.spongepowered.common.mixin.core.entity.IMixinAreaEffectCloud)

Example 3 with ParticleType

use of org.spongepowered.api.effect.particle.ParticleType 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 4 with ParticleType

use of org.spongepowered.api.effect.particle.ParticleType in project LanternServer by LanternPowered.

the class LanternParticleEffectBuilder method buildContent.

@Override
protected Optional<ParticleEffect> buildContent(DataView container) throws InvalidDataException {
    if (!container.contains(DataQueries.PARTICLE_TYPE, DataQueries.PARTICLE_OPTIONS)) {
        return Optional.empty();
    }
    ParticleType particleType = container.getCatalogType(DataQueries.PARTICLE_TYPE, ParticleType.class).get();
    Map<ParticleOption<?>, Object> options = new HashMap<>();
    container.getViewList(DataQueries.PARTICLE_OPTIONS).get().forEach(view -> {
        ParticleOption option = view.getCatalogType(DataQueries.PARTICLE_OPTION_KEY, ParticleOption.class).get();
        Object value;
        if (option.getValueType().isAssignableFrom(DataSerializable.class)) {
            value = view.getSerializable(DataQueries.PARTICLE_OPTION_VALUE, option.getValueType()).get();
        } else {
            value = view.getObject(DataQueries.PARTICLE_OPTION_VALUE, option.getValueType()).get();
        }
        options.put(option, value);
    });
    return Optional.of(new LanternParticleEffect((LanternParticleType) particleType, options));
}
Also used : ParticleOption(org.spongepowered.api.effect.particle.ParticleOption) HashMap(java.util.HashMap) ParticleType(org.spongepowered.api.effect.particle.ParticleType)

Example 5 with ParticleType

use of org.spongepowered.api.effect.particle.ParticleType 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)

Aggregations

ParticleType (org.spongepowered.api.effect.particle.ParticleType)5 Vector3d (com.flowpowered.math.vector.Vector3d)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)2 Sponge (org.spongepowered.api.Sponge)2 CommandResult (org.spongepowered.api.command.CommandResult)2 GenericArguments (org.spongepowered.api.command.args.GenericArguments)2 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)2 ParticleOption (org.spongepowered.api.effect.particle.ParticleOption)2 PluginContainer (org.spongepowered.api.plugin.PluginContainer)2 Text (org.spongepowered.api.text.Text)2 Color (org.spongepowered.api.util.Color)2 Vector3f (com.flowpowered.math.vector.Vector3f)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1