Countdown command

Discussion in 'Plugin Development' started by LimitedWard, Sep 16, 2012.

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

    LimitedWard

    I'm currently working on my very first bukkit plugin (though I know java, I've never worked with the bukkit library before). In my plugin, there are two teams that face off against each other in game. Once the game is finished, there is an intermission which waits 30 seconds before the next game starts.

    The command to start the game is "/startcountdown" which will start the intermission countdown. If, by the end of the countdown, there are not enough people that joined each team, the intermission countdown restarts by calling to the same function (recursion).

    Here is the function which is called by the /startcountdown command:
    Code:
    public void intermissionCountdown() {
            currentTime = System.currentTimeMillis();
            if (countdownEnabled) {
                Bukkit.broadcastMessage(ChatColor.DARK_RED + "Starting countdown...");
                while (timer > 0) {
                    if (System.currentTimeMillis() >= currentTime + interval) {
                        currentTime = System.currentTimeMillis();
                        timer -= interval;
                        Bukkit.broadcastMessage(ChatColor.YELLOW + ""
                                + (timer / 1000) + " second left.");
                    }
                }
                if (game.getNumSlender() < game.getMinSlender()
                        || game.getNumHuman() < game.getMinHuman()) {
                    if (countdownEnabled) {
                    Bukkit.getServer()
                            .broadcastMessage(
                                    "Not enough people have joined to play. Resetting countdown...");
                    timer = originalTime;
                    currentTime = System.currentTimeMillis();
                    intermissionCountdown();
                    }
                    else
                        Bukkit.getServer().broadcastMessage("Game cancelled");
                } else {
                    // ... start the game ...
                    if (countdownEnabled)
                        Bukkit.getServer().broadcastMessage("Starting game...");
                    else
                        Bukkit.getServer().broadcastMessage("Game cancelled");
                }
            }
        }
    This code works pretty well except for the fact that because the command executor class is waiting for the function "finish" to allow the executor to return true, no commands can be passed during the time of the countdown. If people wanted to join teams during the intermission, they would not be able to. Is there a way to fix this? I heard that an alternative would be to use bukkit's built in scheduler, but would this solve the problem, and, if so, how could I make the schduler display how much time is left every 5 seconds before the game starts?
     
  2. Offline

    gregthegeek

  3. Offline

    LimitedWard

  4. Offline

    Woobie

    Here is a code that counts from 3 to 0.. it has some issues though, use if you want.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("countdown")) {
                this.getServer().getScheduler()
                        .scheduleAsyncRepeatingTask(this, new Runnable() {
     
                            public void run() {
                                if (number != -1) {
                                    if (number != 0) {
                                        Bukkit.broadcastMessage("" + number);
                                        number--;
                                    } else {
                                        Bukkit.broadcastMessage(ChatColor.BLUE
                                                + "[Countdown] GO!");
                                    number--;
                                    }
                                }
                            }
                        }, 0L, 20L);
            }
            return false;
        }
    }
     
  5. Offline

    gregthegeek

    Sync is safer, especially if you're doing things with bukkit methods.
     
  6. Offline

    LimitedWard


    Wouldn't that cause the chat to go "3, 2, 1, go, go, go, go, go...." and keep using "go" forever?



    I'm confused. I thought Sync causes the task to be run through the main thread. Wouldn't that not be a good idea because then it would prevent other commands from being executed, or would it not interfere/stop the main thread?
     
  7. Offline

    Woobie

    No, it just does
    3
    2
    1
    [CountDown] GO!!

    And here is the issue, you would have to reload the server for the command to work again. As i said, use if you want.
     
  8. Offline

    LimitedWard

    What if I did a delayed task which calls to itself using recursion until it reaches a base case (the timer = 0)? That way the task would stop after there's no time left.
     
  9. Offline

    coolmonkeyguy

    How would i alter this code to allow the player to target another player sending just the target player the countdown. would i just replace bkkit.broadcastMessage with targetplayer?
     
Thread Status:
Not open for further replies.

Share This Page