Tutorial Make Your Own Particle Library

Discussion in 'Resources' started by Xtreme727, Dec 25, 2014.

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

    Xtreme727

    Hello everybody! =) People have requested and received help on particle-effects/effects, but I will give a short example of creating your OWN particle library instead of being confused with the methods or what the different ones are.

    If you are new to something like this, you may have been playing around with BukkitAPI, and did 'playEffect(location, Effect.CRI-' wait a minute! There is no critical effect? There is no enchantment effect? This will help you create your own custom way to get these effects! But how? Packets, bits of data, can help us create these effects. What do we need to know?
    • The effects. You don't need to memorize them, but the effects have 'names'. No, not the name 'Villager-Sparkles' or whatever, but id names. For example, Villager Sparkle thingies are known as 'happyVillager' particles.
    • The packet type. The packet type we will be using is known as 'PacketPlayOutWorldParticle'.
    So we are going to set up the effects in a neat ordered list. The list will be an enumerator, and will contain what you want to call it by, and the name-id of the particle.
    Code:
    package me.Xtreme727.example;
    public enum EffectType {
       EXPLOSION_HUGE("hugeexplosion");
    }
    
    So, the String parameter is the name-id of the particle. We will declare our constructor soon that will know it takes a String, but we need to finish our list first.
    Code:
    package me.Xtreme727.example;
    public enum ParticleEffect {
       EXPLOSION_HUGE("hugeexplosion"),
       EXPLOSION_LARGE("largeexplode"),
       FIREWORKS_SPARK("fireworksSpark"),
       BUBBLE("bubble"),
       SUSPEND("suspend"),
       SUSPEND_DEPTH("depthsuspend"),
       TOWNAURA("townaura"),
       CRIT("crit"),
       CRIT_MAGIC("magicCrit"),
       SMOKE("smoke"),
       SMOKE_LARGE("largesmoke"),
       MOB_SPELL("mobSpell"),
       MOB_SPELL_AMBIENT("mobSpellAmbient"),
       SPELL("spell"),
       INSTANT_SPELL("instantSpell"),
       WITHER_MAGIC("witchMagic"),
       NOTE("note"),
       PORTAL("portal"),
       ENCHANTMENT_TABLE("enchantmenttable"),
       EXPLODE("explode"),
       FLAME("flame"),
       LAVA("lava"),
       FOOTSTEP("footstep"),
       SPLASH("splash"),
       CLOUD("cloud"),
       REDDUST("reddust"),
       SNOWBALL_POOF("snowballpoof"),
       DRIP_WATER("dripWater"),
       DRIP_LAVA("dripLava"),
       SNOW_SHOVEL("snowshovel"),
       SLIME("slime"),
       HEART("heart"),
       VILLAGER_ANGRY("angryVillager"),
       VILLAGER_HAPPY("happyVillager");
    
       private String id;
       private ParticleEffect(String id) {
          this.id = id;
       }
    
       public String getID() {
          return id;
       }
    }
    
    Now that we have our enumerator, lets add a method at the end, so that we can actually play the effect.
    The parameters for the packet are:
    Code:
    PacketPlayOutWorldParticles(String id, float x, float y, float z, float radiusX, float radiusY, float radiusZ, float speed, int amount)
    So we need to declare our floats for X, Y, and Z and put them in the parameters. Along with those, we need to specify the String, which is going to be name-id.

    Code:
    public void playPE(Player p) {
       //Declare floats for X, Y, Z.
       float x = (float) loc.getX();
       float y = (float) loc.getY();
       float z = (float) loc.getZ();
       //Declare the packet
       PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(getName(), x, y, z, ...);
    }
    
    On a usual basis for me, all the radius' are set around 1 to 3. The speed I set to 1, and amount I set to 15. You can change these values, doing 'research' for what they do the effect, and how they can 'effect' the effect.

    Code:
    public void playPE(Player p) {
       //Declare floats for X, Y, Z.
       Location loc = p.getLocation();
       float x = (float) loc.getX();
       float y = (float) loc.getY();
       float z = (float) loc.getZ();
       //Declare the packet
       PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(getName(), x, y, z, 1, 1, 1, 1, 15);
    }
    
    Now that we have declared what the packet is, we need to actually send it to the somebody.
    Code:
    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    
    Together, it should look like:

    Code:
    public void playPE(Player p) {
       //Declare floats for X, Y, Z.
       float x = (float) loc.getX();
       float y = (float) loc.getY();
       float z = (float) loc.getZ();
       //Declare the packet
       PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(getName(), x, y, z, ...);
       ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    }
    
    You can also use locations instead of players, etc. because you don't always want the effect around the player, you could want it around an area or block.

    You have created your library!!! Play around with the effects and see what they do. Note, I don't exactly recommend causing the explosions to happen all the time, it can tend to lag...

    There are many more and other ways I haven't covered and discovered, but always look and explore, and you will find.

    ALSO: An example way to use this is -
    Code:
    for (Player player : Bukkit.getOnlinePlayers()) {
       ParticleEffect.VILLAGER_HAPPY.playPE(player.getLocation());
    }
    
    Thanks for reading,
    Hope this helped! :) -Xtreme727
     
    Last edited: Dec 25, 2014
    gal0511Dev, ChipDev and Concurrent like this.
  2. Offline

    ChipDev

    Nice :)
     
    Xtreme727 likes this.
  3. Offline

    Xtreme727

    @Assist

    It's intended for people who want to learn it so that way they can expand it their own way and find what they want to expand easily. ParticleLib is similar to an API(Yes, a library) that you copy in your own project. You can learn how it works from there, but you may not know also because you don't look over it.
     
    harry_potter4567 and TigerHix like this.
Thread Status:
Not open for further replies.

Share This Page