Best way to show many particle at different location

Discussion in 'Spigot Plugin Development' started by Parozzz, May 8, 2017.

  1. Hey,

    I am making a plugin in which I need to display particles to many locations. I am using a runnable (obviously) and this is its class
    Code (Text):

    public class ParticleRunnable
            extends BukkitRunnable
    {

        private final JavaPlugin pl;
        private final Map<Location,Particle> map;
        public ParticleRunnable(JavaPlugin pl)
        {
            this.pl=pl;
           
            map=new ConcurrentHashMap<>();
           
        }
       
        public void addParticle(Location l,Particle particle)
        {
            map.putIfAbsent(l, particle);
        }
       
        public void removeParticle(Location l)
        {
            map.remove(l);
        }

        @Override
        public void run()
        {
            run=true;          
            SplittableRandom random=new SplittableRandom(System.currentTimeMillis());
            for(Location l:map.keySet())
            {
                World w=l.getWorld();
                Particle particle=(Particle)map.get(l);
                 
                Location newLoc = l.clone().add(0.5,0.5,0.5);
                for(Integer j=0;j<5;j++)
                {
                    w.spawnParticle(particle, newLoc, 1,random.nextDouble(0.5),random.nextDouble(0.5),random.nextDouble(0.5),0);
                }
            }
        }
       
        private Boolean run=false;
        public Boolean isRunning()
        {
            return run;
        }
    }
     

    I'm quite new to the SpigotAPI and I am not sure if I can run this code using an async task (I googled some but didn't find anything specific). I am worried that thousands of block could block the main thread too much, but don't want either any problem with unsafe thread operations and the damage the could do

    Is there any other way I could do it? If so, any suggestion is appreciated
     
  2. You shouldn't run the task async since it depends on the spigot API.
    Other than that, I don't think there's a better way to continually generate particles in one place...
     
  3. Check if the chunk the location is in is loaded.