Solved Runnable Repeat

Discussion in 'Plugin Development' started by Techno, Apr 15, 2014.

Thread Status:
Not open for further replies.
  1. As the title says I have a runnable and it keeps on repeating, what it is, is that when I execute the command it works fine for a moment.. then spams.. ALOT!

    Code:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    3. {
    4. if (commandLabel.equalsIgnoreCase("countdown"))
    5. {
    6. Player player = (Player) sender;
    7. if(player instanceof Player){
    8. Bukkit.broadcastMessage(ChatColor.GRAY + "[" + ChatColor.AQUA + "+" + ChatColor.GRAY + "] " + ChatColor.RED + SecondsToCountDown);
    9. startSecondsCountdown();
    10. }
    11. }
    12. return true;
    13. }
    14.  
    15. public Runnable startSecondsCountdown() {
    16. // REPEATING TASKS COUNTDOWN DEMO
    17. taskID2 = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    18. public void run() {
    19. SecondsToCountDown--;
    20. if (SecondsToCountDown!=0) {
    21. Bukkit.broadcastMessage(ChatColor.GRAY + "[" + ChatColor.AQUA + "+" + ChatColor.GRAY + "] " + ChatColor.RED + SecondsToCountDown);
    22. }
    23. if (SecondsToCountDown==0) {
    24. SecondsToCountDown=5;
    25. Bukkit.broadcastMessage(ChatColor.GRAY + "[" + ChatColor.AQUA + "+" + ChatColor.GRAY + "] " + ChatColor.GREEN + "GAME STARTED!");
    26.  
    27. Bukkit.getScheduler().cancelTask(taskID1);
    28. Bukkit.getScheduler().cancelTask(taskID2);
    29. }
    30.  
    31. }
    32. }, 20L, 20L); // once each second
    33. return null;
    34. }
    35. }
    36.  
     
  2. Offline

    Konkz

    PHP:
        public void myCountDown() {
                new 
    BukkitRunnable() {
                    
    int time 20//time in seconds
     
                    
    public void run() {
                        if (
    time >0) {
                             
    Bukkit.broadcastMessage("Time is: " time//This will say "Time is: 500" if time = 500...
                        
    if (time == 0) {
                            
    Bukkit.broadcastMessage("Times over!");
                            
    this.cancel(); //cancelling the event
                        
    }
                        
    time--;
                    }
                }.
    runTaskTimer(this2020);
        }
    Techno
     
    Techno likes this.
  3. Konkz
    Do I put that into the command?
     
  4. Offline

    Konkz

    You can, I made it into a method itself as I find it cleaner then I simply call the method inside my command using myCommand();
     
    Techno likes this.
  5. Konkz likes this.
Thread Status:
Not open for further replies.

Share This Page