Solved Help with preventing lag while scattering players around the map. (slowing down a loop)

Discussion in 'Plugin Development' started by krizzdawg, Aug 26, 2017.

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

    krizzdawg

    I've made a post similar to this a few weeks back. I thought it was solved but It was because I was testing with very low player counts.

    Code:
           public int amount = 1; // This is the amount I've set so I know how many players have been scattered.
    
    
    public void start() {
                for(UUID uuid : Game.players) {
    
                    if(Bukkit.getPlayer(uuid) !=null) {
                        Player player = Bukkit.getPlayer(uuid);
                        new BukkitRunnable() {
    
                            public void run() {
                                 
                                Scatter.scatterPlayer(player);
                                amount++; // This is increasing the Integer 'amount'.
                                preparePlayer(player);
                            }
                        }.runTaskLater(this, amount * 40);
    
                    }
                }
    }
    
    The issue is that when this is called, the task doesn't use the new amount after I increased it.
    (So task is created with amount * 40 instead of the amount++ * 40 );

    What I am looking for is a method to scatter 1 player every 2 seconds.

    I was told my @md_5 to try stay away from using 'Thread.sleep()' until I fully understand it but if its necessary I will use it.

    I need somthing like this:
    Code:
    for(Player player : getServer().getOnlinePlayers) {
        player.teleport(100, 100, 100);
        // Wait 2 seconds then go for the next player.
    }
     
  2. Offline

    Zombie_Striker

    @krizzdawg
    Instead of increment amount only in the task, increment the amount outside of the task. This should fix the problem.
     
  3. Offline

    krizzdawg

    I'll try that, thanks!

    It worked, that was such a blatant mistake I feel like a retard. But thanks a lot man

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 26, 2017
Thread Status:
Not open for further replies.

Share This Page