Help with Scheduler

Discussion in 'Plugin Development' started by Jbitters3, Nov 9, 2012.

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

    Jbitters3

    How do i keep repeating this after 5 minutes or after the game ends?
    Code:
    @Override
        public void onEnable() {
        getServer().getPluginManager().registerEvents(PresenceListener, this);
         
            getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                public void run() {
                    if(startTimeDelay != -1){
                        if(startTimeDelay == 10){
                            Bukkit.broadcastMessage(announcementPrefix + "Voting complete, the game starts in 10 seconds!");
                        }
                        if(startTimeDelay != 0){
                            startTimeDelay--;
                        }else{
                            startTimeDelay--;
                            Bukkit.broadcastMessage(announcementPrefix  + "The game is now starting!");
                            for(int i = 0; i < activePlayers.size(); i++){
                                String pName = activePlayers.get(i);
                                Player p = Bukkit.getPlayer(pName);
                                    if(teamRedPlayers.contains(pName)){
                                        //p.teleport(teamRedSpawnLoc);
                                    }
                                    if(teamBluePlayers.contains(p.getName())){
                                        //p.teleport(teamBlueSpawnLoc);
                                    }
                                }
                            }
                        }
                    }
            }, (60L * startTimeDelay), startTimeDelay);
        }
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    You should not use an Async task if you are going to be using the Bukkit API.
     
  3. Offline

    ZeusAllMighty11

    Not sure what all the math at the bottom is (60L * startTimeDelay), startTimeDelay


    What I do is every 20L, take my time int and -- it.
    Then, I do a switch(timeVariable) statement, and I use case.

    for example:

    Code:
    int countdownTime = 10;
     
    switch(countdownTime){
        case 9: Bukkit.broadcastMessage(ChatColor.GREEN + " 9 seconds left!");
        break;
     
        case 8: Bukkit.broadcastMessage(ChatColor.GREEN + " 8 seconds left!");
        break;
     
        case 7: Bukkit.broadcastMessage(ChatColor.GREEN + " 7 seconds left!");
        break;
     
        case 6: Bukkit.broadcastMessage(ChatColor.GREEN + " 6 seconds left!");
        break;
     
     
        case 5: Bukkit.broadcastMessage(ChatColor.GREEN + " 5 seconds left!");
        break;
     
     
        case 4: Bukkit.broadcastMessage(ChatColor.GREEN + " 4 seconds left!");
        break;
     
     
        case 3: Bukkit.broadcastMessage(ChatColor.GREEN + " 3 seconds left!");
        break;
     
     
        case 2: Bukkit.broadcastMessage(ChatColor.GREEN + " 2 seconds left!");
        break;
     
     
        case 1: Bukkit.broadcastMessage(ChatColor.GREEN + " 1 seconds left!");
        // also stop scheduler here and do what you have to
        break;
     
    }

    To make it repeat for 5 minutes, if you mean you want a timer that goes for 5 minutes, just do int timerCountdown = 300;

    ?
     
  4. Offline

    Jbitters3

    Could you edit it for me and how would I make it restart if everyone is dead and the 5 minutes is not up?
     
  5. Offline

    Loogeh

    This is what i've got.
    Code:
                            final int taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                                @Override
                                public void run() {
                                    int countDown = 60;
                                    countDown--;
                                    switch(countDown) {
                                    case 10:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 9:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 8:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 7:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 6:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 5:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 4:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 3:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 2:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    case 1:
                                        sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                                        break;
                                    }
                                }
                            }, 20L, 20L);
    Doesnt work..
    Also I can't figure out how to cancel it, plugin.getServer().getScheduler().cancelTask(id) doesnt work.
     
  6. Offline

    Jbitters3

    I mean like i have other things counting down and i need to add it the last 10 second of voting and before the game is starting
     
  7. Offline

    Loogeh

  8. Offline

    HouseMD

    try this:

    Code:
    final int taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
               
                int countDown = 10;
               
                @Override
                public void run() {
                   
                   
                    switch(countDown) {
                    case 10:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 9:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 8:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 7:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 6:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 5:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 4:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 3:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 2:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    case 1:
                        event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countDown);
                        break;
                    }
                   
                    countDown--;
                }
            }, 20L, 20L);
     
  9. Offline

    Loogeh

    I did that, but I still can't cancel it. I get an error under 'taskID' in here plugin.getServer().getScheduler().cancelTask(taskID); It says that the variable hasn't been initialized.
     
  10. Offline

    Wolvereness Bukkit Team Member

    Code:
    new BukkitRunnable() {
        int countdown = 10;
        @Override
        public void run() {
            event.getPlayer().sendMessage(ChatColor.WHITE + "Game starting - " + ChatColor.YELLOW + countdown--);
            if (countdown == 0) cancel();
        }
    }.runTaskTimer(this, 20l, 20l);
     
Thread Status:
Not open for further replies.

Share This Page