[Tutorial] Spawning particle effects in Sponge

First of all, thank you to @wingz for helping me test this :smile:

Secondly, please do not copy paste this code // this is a tutorial, read it and learn it.

Finally, if you would like to read more about the Particles API, I highly suggest skimming over the pull request made by @Cybermaxke , some really cool and interesting stuff he is doing for the community. :hankey:

EDIT: You don’t actually need to define ParticleType as a variable.

Thanks @TBotV63 !

! Cybermaxke’s Pull Request on Github !

Moving on.


Start with a new plugin template:

@Plugin(name = "particleExample", id = "ParticleExample", version = "0.1A")
public class ParticleExample {

    @Inject
    private Game game;

    @Inject
    private Logger logger;

    @Subscribe
    public void serverEvent(ServerStartingEvent event) {
        logger.info("Welcome to my tutorial!");
    }

}

This is a bare-bones example of what your plugin should look like.

We will proceed to @Subscribe to an event of your choosing, for the purposes of this tutorial, we will listen to the PlayerJoinEvent to spawn our fancy new particles.

First, we will add a GameRegistry variable below our Logger instance.

Then we will specify the type of particle we wish to display, in this case we will use Hearts. The ParticleType is represented as ParticleTypes.HEART, for now we will set this as a private field under our new GameRegistry variable.


##Our class will now look something like this:##

@Plugin(name = "particleExample", id = "ParticleExample", version = "0.1A")
public class ParticleExample {

    @Inject
    private Game game;

    @Inject
    private Logger logger;

    // New:
    private GameRegistry gameRegistry = game.getRegistry();

    // New:
    private ParticleType particleType = ParticleTypes.HEART;

    @Subscribe
    public void serverEvent(ServerStartingEvent event) {
        logger.info("Welcome to my tutorial!");
    }

}

Now we are ready to add these particles to a Player:

We will now add a PlayerJoinEvent to handle the spawning of our particles. Please check the github for info about how we register the particles through the game registry (such as the object types we use in the methods).

Here is what our class looks like now:

@Plugin(name = "particleExample", id = "ParticleExample", version = "0.1A")
public class ParticleExample {

    @Inject
    private Game game;

    @Inject
    private Logger logger;

    private GameRegistry gameRegistry = game.getRegistry();

    private ParticleType effectType = ParticleTypes.HEART;

    @Subscribe
    public void serverEvent(ServerStartingEvent event) {
        logger.info("Welcome to my tutorial!");
    }

    // New:
    @Subscribe
    public void playerEvent(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        Location location = player.getlocation();
        Position position = location.getposition();
        player.spawnParticle(registry.getParticleEffectBuilder(particleType).build(), position);
    }
}

and there you have it! When a player joins the server, the hearts particle effect will be applied to their person.

Let me know in the comments section if this helped you! PM me if you have issues or ideas forthe thread.

Thanks for reading! :smile:


8 Likes

You don’t have to define the ParticleType variable. I’d replace this:

.getParticleEffectBuilder(particleType)

with

.getParticleEffectBuilder(ParticleTypes.HEART)

then remove the variable.

Otherwise, nice tutorial :slight_smile:

1 Like

You can do in production code however you like. But in this example it serves the purpose of describing its contents, so it’s good to have.

@numbrs, you should however pay attention to the fact that you called the variable effectType and are then attempting to give particleType as an argument.

2 Likes

Wrote this on an iPhone lol, thanks for the edit! @TBotV63

1 Like

I don’t use the variable in my code, i was just trying to dissect the methods as much as i could. Ill add a sub title, thanks for the edit! @DotDash

Is there a way to change the visible-range of the particles, just like in Bukkit?

Necroposting to provide updated info since this page is an excellent find, and to answer the unanswered question:

The

registry.getParticleEffectBuilder(

part of the code sample above should really be gameRegistry. , referring to the game registry object created earlier.

And as of this time of writing until it may change later, the method is now
gameRegistry.createParticleEffectBuilder()

Also…there is a spawnParticle() method for both the player object and world-Extent object. Particles spawned for players appear to be visible only to the player spawned for, particles spawned for the extent are visible to all.

Two methods usable for both player and extent are:
.spawnParticles(ParticleEffect particleEffect, Vector3d position);
.spawnParticles(ParticleEffect particleEffect, Vector3d position, int radius);

where the radius is the radius around the position where particles can be seen by players

1 Like

@numbrs I’m not sure if this has changed in the newest build, but in trying to make a warp command with a nice particle effect, I can’t seem to get any particles to spawn.

using almost the exact code you stated,

            Player player = (Player) commandSource;
            Location oPos = player.getLocation();
            GameRegistry reg = QuestCmds.game.getRegistry();
            ParticleEffect myEff = reg.createParticleEffectBuilder(ParticleTypes.ENCHANTMENT_TABLE).count(2000).build();
            Vector3d origin = new Vector3d(oPos.getPosition().getX(), oPos.getPosition().getY() + 1, oPos.getPosition().getZ());
            player.getLocation().getExtent().spawnParticles(myEff, origin, 1);

Nothing happens… I don’t know what I could be doing wrong?

2000 count for enchant table effect is insane for being in one location - they will all drop at once. They are also very tiny particles, and to spawn at your location +1 puts them inside the center x/z of you, at belly height, a bit higher though cause the table particles rise a bit higher than 0 starting point, but they will definitely all spawn within your torso, and not be visible {{Also, make sure your client is set to render particles :slight_smile:

offset the x or z value as well and you should be able to see that, and can adjust things a bit from there.

Also a cooler, and more concise form is:
Vector3d origin = oPos.getPosition().add(0, 1,0);

1 Like

Someone should make a plugin for player particles :3

I thought of that thing, like water drops when you were in water and you are “wet”. Or other things I won’t name yet. :yum:

1 Like