Efectos

Advertencia

These docs were written for SpongeAPI 7 and are likely out of date. If you feel like you can help update them, please submit a PR!

Utilizando la API de efectos en Sponge, podemos crear efectos especiales para ser utilizados en un servidor. Utilizando un Viewer, podemos reproducir sonidos o generar partículas en el servidor.

Reproducción de Sonidos

Con algún Viewer dado, podemos simplemente reproducir un sonido en una ubicación:

import org.spongepowered.api.effect.Viewer;
import org.spongepowered.api.effect.sound.SoundTypes;

import com.flowpowered.math.vector.Vector3d;

viewer.playSound(SoundTypes.ENTITY_CREEPER_PRIMED, new Vector3d(1, 65, 1), 1);

Now let’s break this down. First, the SoundType specifies the sound that will be played. Next, we have a Vector3d position. This position can be constructed, or it can be retrieved from a Location using the Location#getPosition() method. In the example above, the sound will be played at the coordinates 1, 65, 1. Lastly, we have the volume that the sound will be played at. The volume is a double that ranges from zero to two.

Ahora que podemos reproducir sonido básicos, podemos ir más profundo con nuestros sonidos. Digamos que queremos reproducir nuestro sonido en un tono específico. Podemos utilizar la clase PitchModulation para modular el tono a una nota específica. También podemos utilizar una SoundCategory para especificar que categoría de sonido estamos reproduciendo. Un ejemplo de estos se muestra a continuación:

import org.spongepowered.api.effect.sound.PitchModulation;
import org.spongepowered.api.effect.sound.SoundCategories;

viewer.playSound(SoundTypes.ENTITY_CREEPER_PRIMED, SoundCategories.HOSTILE,
    new Vector3d(1, 65, 1), 1, PitchModulation.AFLAT0);

Si una SoundCategory no está especificada cuando se reproduce un sonido, se utilizará SoundCategories#MASTER.

Generación de Partículas

Similarmente a los sonidos, podemos utilizar la clase Viewer para generar partículas dentro del mundo:

import org.spongepowered.api.effect.particle.ParticleEffect;
import org.spongepowered.api.effect.particle.ParticleTypes;

ParticleEffect effect = ParticleEffect.builder()
        .type(ParticleTypes.LAVA)
        .quantity(50)
        .build();
viewer.spawnParticles(effect, position);

Utilizando un ParticleEffect.Builder, podemos especificar el tipo de partícula que nos gustaría generar. Con esto, también especificamos que cincuenta partículas estarán en el efecto de partículas.

Now if we wanted to make a more specific particle, say the particle of a block, then we can use one of the several types found in the ParticleTypes class. For example, let’s say we wanted to spawn particles of a cracking block of sand, ParticleTypes#BLOCK_CRACK. We would need to use the ParticleOptions#BLOCK_STATE option and specify that we would like to use a sand block. This can be done like so:

import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.effect.particle.ParticleOptions;

ParticleEffect particle = ParticleEffect.builder()
        .type(ParticleTypes.BLOCK_CRACK)
        .option(ParticleOptions.BLOCK_STATE, BlockTypes.SAND.getDefaultState())
        .build();
viewer.spawnParticles(particle, position);

Potion Effects

Similarmente a partículas y sonidos, necesitamos utilizar un compilador para crear nuestro efecto de poción:

import org.spongepowered.api.effect.potion.PotionEffect;
import org.spongepowered.api.effect.potion.PotionEffectTypes;

PotionEffect potion = PotionEffect.builder()
        .potionType(PotionEffectTypes.HASTE)
        .duration(10)
        .amplifier(5)
        .build();

Utilizando esto, podemos crear un :javadoc:`PotionEffect`de precipitación que durará diez ticks y tendrá un amplificador de cinco. A diferencia de las partículas y los sonidos, las pociones no pueden ser aplicadas a un ``Viewer``. En cambio, necesitamos una entidad que soporte PotionEffectData, como un jugador.

import org.spongepowered.api.data.manipulator.mutable.PotionEffectData;
import org.spongepowered.api.entity.living.player.Player;

PotionEffectData effects = player.getOrCreate(PotionEffectData.class).get();
effects.addElement(potion);
player.offer(effects);

Esto obtendrá o creará un PotionEffectData de un jugador. Luego agregaremos nuestro anterior efecto de poción a la lista y se lo ofreceremos otra vez al jugador.