First of all, thank you to @wingz for helping me test this
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.
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.