Timer Function

Discussion in 'Plugin Development' started by rohan576, Mar 10, 2014.

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

    rohan576

    I've skimmed through various plugins on github, and I've carefully observed the BukkitScheduler API. Whenever I run my plugin, the timer doesn't work, as it keeps repeating what I need it to do on a timer... as many times as it can a second.

    This is the code (that's not apparently working):
    Show Spoiler

    In the class called "Timer"...
    Code:java
    1. package me.rohan576.FreezeTag;
    2.  
    3. import org.bukkit.Bukkit;
    4.  
    5. public class Timer {
    6.  
    7. public static void waitTicks(long ticks) {
    8. Bukkit.getServer().getScheduler().runTaskLater(FreezeTag.getInstance(), new Runnable() {
    9. @Override
    10. public void run() {
    11. // Twiddle your thumbs! :D
    12. }
    13. }, ticks);
    14. }
    15. }
    16.  

     
  2. Offline

    ShadowLAX

    What's the problem exactly? Everything looks fine. Is there a stacktrace when you try to use it?
     
  3. Offline

    rohan576

    No, there is no apparent error. What's happening is that the timer simply isn't working. The event that it's supposed to run runs as many times as possible, as fast as possible, which tells me it isn't working.

    Also, I want to quickly note that the function is called from other classes when needed.
     
  4. Offline

    Bionicrm

    Whatever is calling it, could that be what's causing it?
     
  5. Offline

    rohan576

    I don't think so. It calls it very simply.
    Timer.waitTicks(20L)
     
  6. Offline

    mazentheamazin

    rohan576
    Well, I think you need to have some code to be run after the timer is done, so add a Runnable as a parameter
    Code:java
    1. public static void waitTicks(Long ticks, Runnable runnable) {
    2. Bukkit.getServer().getScheduler().runTaskLater(FreezeTag.getInstance(), runnable, ticks);
    3. }

    Now you would call the method like this:
    Code:java
    1. Timer.waitTicks(20L, new Runnable() {
    2. @Override
    3. public void run() {
    4. //Code you want to run after the ticks are done
    5. }
    6. });
     
  7. Offline

    rohan576

    Well, it seems that this problem has evolved into a somewhat similar problem after a few hours of me trying to figure out this annoyance.

    Show Spoiler

    Code:java
    1.  
    2. System.out.println("TEST VERSION 8");
    3. calculatePlayerCount();
    4. System.out.println("GameManager: Calculated player count. " + playerCount + " players.");
    5.  
    6. //Assign Players and It
    7. System.out.println("GameManager: Testing for players...");
    8. pcheck = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(FreezeTag.getInstance(), new Runnable() {
    9. @Override
    10. public void run() {
    11. System.out.println("Running timer for 40 ticks...");
    12. calculatePlayerCount();
    13. System.out.println("GameManager: Calculated player count. " + playerCount + " players.");
    14. Bukkit.broadcastMessage(FT + ChatColor.GREEN + "Waiting for players... " + ChatColor.YELLOW + playerCount + ChatColor.AQUA + "/" + ChatColor.YELLOW + "10");
    15. System.out.println("GameManager: Broadcasted start status.");
    16. System.out.println("Finished running timer for 40 ticks");
    17. if (playerCount >= 10) {
    18. Bukkit.getServer().getScheduler().cancelTask(pcheck);
    19. }
    20. }
    21. }, 0, 40);
    22.  



    This prints into the console:
    INFO: TEST VERSION 8
    INFO: GameManager: Calculated player count. 0 players.
    INFO: GameManager: Testing for players...
    And then it never seems to run the loop, and continues on with the rest of the plugin. Also, I'd like to note that I cannot log into the server, because of some kind of lag caused by my plugin...
     
Thread Status:
Not open for further replies.

Share This Page