Countdowns

Discussion in 'Plugin Development' started by crushh87, Jan 18, 2013.

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

    crushh87

    Okay bukkit/java geniuses need your help here.
    I have a countdown system I'm trying to get to work. Every time I join the game and we get the countdown to start the entire server freezes, nothing happens no console errors or nothing. You can't chat the entire thing shuts down.

    Here is my code:
    Code:
    }if(player1.size() > 0 && player2.size() > 0){
                                Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                                    public void run() {
                                      int time = 10;
                                        if(time > 0) {
                                            for(Bukkit.broadcastMessage(ChatColor.GREEN + "[Boxing] The Boxing match will start in" + time + "seconds");;)
                                                time--;
                                        }else if(time < 1){
                                            Bukkit.broadcastMessage("test");
                                            for(String player : player1)
                                            {
                                                Player p = Bukkit.getPlayer(player);
                                                if(p != null)
                                                {
                                                    p.teleport(spawn1);
                                                }
                                            }
                                            for(String player : player2)
                                            {
                                                Player p = Bukkit.getPlayer(player);
                                                if(p != null)
                                                {
                                                    p.teleport(spawn2);
                                                }
                                        }
                                        }
                                    }
                               
                                             
                          }, 20L, 200L);
                    }
            
    btw the countdown is suppose to be 10 seconds

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    KingKongC

    Please don't double post, Edit your existing thread.
     
  3. Offline

    ImTheFool

    Code:
    for(Bukkit.broadcastMessage(ChatColor.GREEN + "[Boxing] The Boxing match will start in" + time + "seconds");;)
        time--;
    
    I'm not really sure what this line does for you. It might be the problem, though.
     
  4. Offline

    Phinary

    Well, for starters your timer will never reach 0. Since you declare the variable time inside of the run code block, after it runs run(), time will be unset and set back to 10 again when run() is called again.

    And as stated before:
    Uh what?
    Code:
    for(Bukkit.broadcastMessage(ChatColor.GREEN + "[Boxing] The Boxing match will start in" + time + "seconds");;)
    time--;
    Bukkit.broadcastMessage() returns an int, not exactly sure what you are trying to do here...
    Anyways, pretty certain that for loop is what is freezing the server. I think that would just give you an endless loop.



    Try something like this:

    Ofcourse, you will need to adjust it a bit and you can probably figure out a better way to do some things
    Code:
    //make a variable called secondstostart and taskid inside your class
     
    //this will start the countdown timer
      private void startTimer() {
            taskid = getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {[/FONT][/FONT]
                //timer starts after 5 seconds and runs this every 1 second.
                public void run() {
                    secondstostart--;
                    if (secondstostart <= 0) {
                        startGame();
                    } else if (secondstostart % 10 == 0 || secondstostart <= 10) {
                        broadcastStartMessage(secondstostart);
                    }
                }
            }, 5 * 20L, 20L);
        }
     
    //called when the timer is finished
        private void startGame() {
            getServer().getScheduler().cancelTask(taskid);
            //run your start up code
     
            getServer().broadcastMessage(ChatColor.RED + "The game has begun! Good luck :)");
        }
     
    //used to broadcast starting messages to the server
        private void broadcastStartMessage(int time) {
            if (time%60 == 0) {
                getServer().broadcastMessage(ChatColor.RED + "The game will start in " + time/60 + " minutes.");
            } else {
                getServer().broadcastMessage(ChatColor.RED + "The game will start in " + time + " seconds.");
            }
        }
     
  5. Offline

    crushh87

    well, I guess I was way off, lol
    thanks let me try it out.

    Whats the variable taskid for?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  6. Offline

    Phinary

    When you register a task with Bukkit's task manager, it will return an integer. This is the taskid for that task. You need this if you want to cancel the task later on. Basically, it saves the task id inside an int called taskid and when the timer is over and startGame() is called, it cancels that task, otherwise it will keep calling the repeating task every second, even when the timer is over.
     
Thread Status:
Not open for further replies.

Share This Page