Tutorial Custom Particle Effects (creating a single particle in the world)

Discussion in 'Resources' started by doyoumath, Apr 27, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    doyoumath

    This tutorial will show how to create custom particle effects (arrangements of particles) using packets.


    Make sure that you have added CraftBukkit to your build path. Press Ctrl-O often to auto-import classes.

    Code:
    PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(EnumParticle.DRIP_WATER, false, x, y, z, 0, 0, 0, 0, 1, null);
    Let's analyze the important parameters. (Or, check out http://wiki.vg/Protocol#Particle for a table that describes every paramater).

    Code:
    EnumParticle.DRIP_WATER
    This is the type of particle that will be used - assuming you use Eclipse, type "EnumParticle." and press Ctrl-SPACE to show a list of all the possible particle types.

    Code:
    x, y, z
    You guessed it - these are the x, y, and z coordinates of the particle. They are floats, so you can use a few decimal places. You can use the coordinates off the F3 screen, or use the location of an entity.

    Code:
    0, 0, 0
    This is the x, y, and z Offsets of how far you want particles to be from the particle group's center. These parameters don't apply to create 1 single particle, so leave these at 0. (Check the wiki above if you're curious).

    Code:
    0, 1, null
    ParticleData, ParticleCount, and Data. ParticleData is a number that is used in some particle types. For example, when creating Splash Potion particles, use this to set what type of potion particles to create. ParticleCount is the number of particles to create - following this tutorial, you probably want 1. If you set this to a number greater than 1, make sure to put a positive value in for the Offsets, or all the particles will be in the same place. Data is an array that is relevant for setting the texture used in block-interact particles (jump, break, etc.) Unless you are using one of the mentioned block-interact particle types, you can leave this as null.

    You now have created a packet! Now you can send it off to players. This can be done as follows:

    Code:
    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    To send it to EVERY player, just put the above line into a for loop:

    Code:
    for (Player player : Bukkit.getServer().getOnlinePlayers()) {
        ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    }
    
    Remember, only the players who you send the packet too will see the particle effects.

    Here's an example where only players who are OP see the particles:

    Code:
    for (Player player : Bukkit.getServer().getOnlinePlayers()) {
        if (player.isOp()) {
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
        }
    }
    
    Once you can create 1 particle, try sending multiple packets, and packets of different types. Make large patterns of particles, and mass send them to players.

    Happy coding!

    2015-04-27_18.21.24.png
    Above, I have used a couple of for loops modifying the X- and Z- coordinates to create a grid of water particles around the player.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Apr 27, 2015
  2. Offline

    ChipDev

    This is going to help people alot, Some tutorials are not updated!
     
    gal0511Dev and GrandmaJam like this.
  3. Offline

    doyoumath

    SAMPLE: Add trig functions to create cool patterns!

    2015-05-07_19.16.26.png
     
  4. Offline

    Yannici

    How long the particles will stay?
     
  5. Offline

    Agentleader1

    @Yannici All particles are different and will not always be the same.
     
  6. Offline

    seanliam2000

    This is awesome! I was searching for a way to do this and I didn't know how since I am still pretty nooby with this stuff :) Thanks!
     
  7. Offline

    doyoumath

    Correct, every particle acts differently. Here are some examples:

    FLAME: Slowly shrinks in size
    DRIP_WATER: Hovers momentarily, then drops down until impact with a solid block, causing a WATER_SPLASH
    ENCHANTMENT_TABLE: Hovers momentarily, then sinks down and fades out
    FOOTSTEP: Remains for about 5 seconds, then fades out
    VILLAGER_HAPPY: Drifts randomly, then disappears with no transition (size of particle is not controllable)
    CRIT_MAGIC: Disappears immediately

    Some particles, like water, have variants (DRIP_WATER, WATER_BUBBLE, WATER_SPLASH, WATER_WAKE) that act in different ways. Unfortunately, the action of the particle is controlled client-side, and therefore cannot be controlled by a server plugin. However, if you send a CRIT_MAGIC packet every maybe 0.1 seconds, modifying the x, y, and z slightly, you can create the appearance of particles while controlling their movement.
     
Thread Status:
Not open for further replies.

Share This Page