Solved Countdown Skipping Numbers?

Discussion in 'Plugin Development' started by TheBlueBlaster, Dec 18, 2015.

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

    TheBlueBlaster

    So every time i try to make a countdown that runs on player join it skips the number by 2.

    Here is my code:
    Code:
        int playerAmount = this.getServer().getOnlinePlayers().size();
            if (playerAmount == 2) {
    
                for (Player p : Bukkit.getOnlinePlayers()) {
    
                    p.playSound(p.getLocation(), Sound.LEVEL_UP, 3, 1);
                   
                    startCountdown();
                    WorldBorder b = Bukkit.getWorld("UHC").getWorldBorder();
    
                    world.setTime(0);
    
                    b.setSize(1000);
                    
                }
            }
    Start Countdown Code:
    Code:
    public void  startCountdown(){
               
            this.getServer().getScheduler()
            .scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                for(Player p : Bukkit.getOnlinePlayers()){
                   
                   
                if(lobbyTime > 0){
                    ActionBarAPI.sendActionBar(p, "§cStarting in §a" + lobbyTime + "s");
                }
                if(lobbyTime == 0){
                    ActionBarAPI.sendActionBar(p,
                            "§a§lTeleporting...");
    
                    startGame();
                }
               
               
               
                    if(lobbyTime > 0)
                        lobbyTime--;
                }
                }
            }, 0L, 20L);
        }
     
  2. Offline

    teej107

    @TheBlueBlaster Because for each player, you are running the countdown for each player again.
     
  3. Offline

    TheBlueBlaster

    So you mean that when they join it executes the scheduler to all players? if thats so, thats what I thought. And if it is how would i accomplish that?(Like how would i make it so it only runs for one player)
     
    Last edited: Dec 18, 2015
  4. Offline

    teej107

    Rubberduck debugging can help.
     
  5. Offline

    TheBlueBlaster

    Yeah idk, if it helps you that it is in there onJoin event?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Dec 20, 2015
  6. Offline

    TheBlueBlaster

    I really need help, thanks :) (sorry mods idk how to delete this message so I had to put something)
     
  7. You decrease lobby time by Bukkit.getOnlinePlayers().size() in your runnable. You want that if-statement outside your for loop.
     
  8. Offline

    TheBlueBlaster

    What do you mean by that? If i decrease it goes faster?
     
  9. @TheBlueBlaster
    Remember to Tahg me or I won't get a notification :)

    If you look in your Runnable in your startCountdown method, you have your --lobbytime inside your for(Player p : Bukkit.getOnlinePlayers()) block. Which means that it gets decreased TWICE if Bukkit.getOnlinePlayers() is 2. Thrice if it's 3 and so on.

    I think you want to move that last if statement beneath the bracket on line 23 in your block.
     
  10. Offline

    TheBlueBlaster

  11. It happens! Glad I could help :)
     
Thread Status:
Not open for further replies.

Share This Page