Minecraft Modding: Spawning Vanilla Particles

Background


Particles are a mechanism in Minecraft that create effects like the hearts that float over a tamed animal, or even things like the rain. Basically they are simply a texture that can be drawn that keeps track of position and motion.

While sometimes you may want to make fully custom particles, there are other times when you might simply want to spawn some vanilla ones. This tutorial explains how to spawn vanilla particles.

For other general tips on particles, you can see Jabelar's Particle Modding Tips.

If you in fact want to make fully custom particles, check out Jabelar's Custom Particle Tutorial.

Spawning Vanilla Particles From Client Side


If the logic that is spawning the particles is executing on the client side (for example if you have a custom block emitting particles in the randomDisplayTickMethod()) you can directly call the ParticleManager.spawnEffectParticle() using the ID from the EnumParticleType for the vanilla particle.

Spawning Vanilla Particles From Server Side


There are also many cases where the logic for spawning might occur on the server side. For example, maybe during a LivingDamageEvent handler.

Spawning a vanilla particle from the server is pretty easy, just use the WorldServer#spawnParticle() method which takes the name of the particle (as a string) plus the initial position and the initial motion.

Note that the particle technically doesn't spawn on the server; instead a packet is sent to all players and the clients spawn the particles.

When spawning you also provide parameters for the position, the initial motion, and additional "particle arguments" that get processed according to the type. If you're interested in what additional particle arguments are available, you should look at the source for the vanilla particle.

Okay, so putting this all together here is an example where I wanted to continuously generate a modest number of happyVillager particles (I was making a custom villager).

if (!worldObj.isRemote && ticksExisted%40==0)
{
    double motionX = rand.nextGaussian() * 0.02D;
    double motionY = rand.nextGaussian() * 0.02D;
    double motionZ = rand.nextGaussian() * 0.02D;
    worldObj.spawnParticle(
          "happyVillager", 
          posX + rand.nextFloat() * width * 2.0F - width, 
          posY + 0.5D + rand.nextFloat() * height, 
          posZ + rand.nextFloat() * width * 2.0F - width, 
          motionX, 
          motionY, 
          motionZ);
}

This code was in the onUpdate() method of my custom villager.  You can see that first I check that we're on the server side.

Controlling The Particle's Position And Motion


You'll see in my code above that I added some randomness and also used the modulo operator to create a regular rate of creation.  I explain more about this in the related section in Jabelar's Particle Modding Tips.

No comments:

Post a Comment