Resource Build and create particle lines or trails.

Discussion in 'Spigot Plugin Development' started by Marido, Jul 16, 2018.

  1. Hello everyone.
    Here is my class I made for developers to easily create a particle line using object creation (it is really easy though).
    It did not take too much time, but it is useful for the current developers googling about how to create particle trails.
    Code (Text):
    package nl.marido.particles.handlers;

    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;

    import nl.marido.particles.Particles;

    public class ParticleLine {

        private static Location location;
        private static Effect effect;
        private static Sound sound;
        private static Vector vector;
        private static int speed;
        private static int distance;
        private static boolean collide;

        public ParticleLine(Location location) {
            ParticleLine.location = location;
        }

        public void setEffect(Effect effect) {
            ParticleLine.effect = effect;
        }

        public void setSound(Sound sound) {
            ParticleLine.sound = sound;
        }

        public void setVector(Vector vector) {
            ParticleLine.vector = vector;
        }

        public void setSpeed(int speed) {
            ParticleLine.speed = speed;
        }

        public void setDistance(int distance) {
            ParticleLine.distance = distance;
        }

        public void setCollide(boolean collide) {
            ParticleLine.collide = collide;
        }

        public void spawnParticle() {
            new BukkitRunnable() {
                public void run() {
                    distance--;
                    location.getWorld().playEffect(location, effect, 50);
                    location.getWorld().playSound(location, sound, 1, 1);
                    location = location.add(vector);
                    if ((location.getBlock().getType().isSolid() && collide) || distance <= 0) {
                        cancel();
                    }
                }
            }.runTaskTimer(Particles.getInstance(), 0, speed);
        }

    }

    Spigot: https://www.spigotmc.org/resources/58792/ (compiled version to use as a dependency).
    GitHub: https://github.com/CoderMarido/Particles (source-code and a useful usage README).
    Feel free to fork the repository or make a pull request or let me know if something could be improved.

    Note that currently only ParticleLine is supported, I am planning to make ParticleCuboid, ParticleSphere, and much more custom particle shapes in the future. Make sure to check out the GitHub for the latest development builds since they will be there first before I update it here.

    Have fun.
    - Marido
     

  2. Feedback/nitpicks from the most to least important:
    • Why static? Now there can only ever be a single ParticleLine at once, because creating a new one would override the properites on the old one and thus change the particles of the previous ongoing task.
    • Particle could be used instead of Effect with world.spawnParticle() method to allow more properties (offsets, speed, data).
    • 'handlers' is a bit odd package for the class, I'd move update checker to e.g. 'utils' package and ParticleLine to 'shapes'
    • Add @Override to the run() method because of java conventions (exists for a reason though)
    • Swap 'location.getBlock.getType().isSolid()' and 'collide', like this: '(collide && location.getBlock().isSolid())' because evaluating a boolean is faster than checking for the block to be solid... but honestly, it's such a minor thing it doesn't really matter.
    As for suggestions, you could make the gap between each particle configurable -- maybe you want to play a particle every 5 blocks, or make a very thick line with 3 particles per block, or just slightly reduce particle count to reduce lag, etc. Also, I'd definitely add support for configurable particle count per tick.

    Other than that; clean and readable, simple and aside from the static I don't see anything out of the place.
     
  3. Thank you for the friendly criticism, I will definitely take a look at these points and update it.