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): Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler In the class called "Timer"... Code:java package me.rohan576.FreezeTag; import org.bukkit.Bukkit; public class Timer { public static void waitTicks(long ticks) { Bukkit.getServer().getScheduler().runTaskLater(FreezeTag.getInstance(), new Runnable() { @Override public void run() { // Twiddle your thumbs! :D } }, ticks); }}
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.
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 public static void waitTicks(Long ticks, Runnable runnable) { Bukkit.getServer().getScheduler().runTaskLater(FreezeTag.getInstance(), runnable, ticks); } Now you would call the method like this: Code:java Timer.waitTicks(20L, new Runnable() { @Override public void run() { //Code you want to run after the ticks are done }});
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. Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler Code:java System.out.println("TEST VERSION 8");calculatePlayerCount();System.out.println("GameManager: Calculated player count. " + playerCount + " players."); //Assign Players and ItSystem.out.println("GameManager: Testing for players...");pcheck = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(FreezeTag.getInstance(), new Runnable() { @Override public void run() { System.out.println("Running timer for 40 ticks..."); calculatePlayerCount(); System.out.println("GameManager: Calculated player count. " + playerCount + " players."); Bukkit.broadcastMessage(FT + ChatColor.GREEN + "Waiting for players... " + ChatColor.YELLOW + playerCount + ChatColor.AQUA + "/" + ChatColor.YELLOW + "10"); System.out.println("GameManager: Broadcasted start status."); System.out.println("Finished running timer for 40 ticks"); if (playerCount >= 10) { Bukkit.getServer().getScheduler().cancelTask(pcheck); } }}, 0, 40); 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...