My current problem is: I'm trying to make a timer that will last 30 seconds! No more and no less! I would also like a countdown at the end! So the entire timer in chat would be like this: 1.) The game will begin in 30 seconds! 2.) 15 seconds until the game begins! 3.) 10 seconds until the game begins! 4.) 5 seconds 5.) 4 seconds 6.) 3 seconds 7.) 2 seconds 8.) 1 second *End Timer* I would appreciate if someone could either post some code that could help me, or point me to a proper place to find my answer! Thanks in advance!
MRPS I know that if this was in my main class then I would use: THIS.getServer().... But I want this timer in my "GameManagement" class, which is not my main class! What do I put in the plugin place?
PHP: public void startCountdown() { new BukkitRunnable() { int time = 30; // time in seconds public void run() { if (time > 0) { if (time % 10 == 0 || time < 11) { Bukkit.broadcastMessage(""); Bukkit.broadcastMessage("Game is starting in " + ChatColor.GOLD + time + ChatColor.GREEN + " seconds!"); Bukkit.broadcastMessage(""); } time--; } else { startGame(); this.cancel(); } } }.runTaskTimer(this, 20, 20); } This will broadcast a message every 10 seconds, when it gets to 0 you have to make a method which determines what happens in the game. Eg: PHP: public void startGame() { for(Player p: Bukkit.getOnlinePlayers()) { p.teleport(LOCATION); }} Something like that Also, when you want the countdown to start you simply do startCountDown(); You'd have to add boolean checks so it does not start multiple times, but I will leave that one to you. Mattkx4
To try and hijack this thread a little bit I need to make a timer for the plugin i'm working on and I need to it to keep track of time since I issue the original command. So unlike a countdown its just infinitely keeping track of time until my ondeathevent where it outputs the time they died since the game started. Could you lead me in the right direction?
paully104 When the original command is executed save the time by using something like this: Code:java Long time = Calendar.getInstance().getTimeInMillis(); Then in your onDeathEvent you get the current time again, substract the saved time and then you have the milliseconds of time between the two.
Thanks for the tip however i have 1 issue void methods cannot return a value, so how would you go about returning the value so that my ondeathevent can use my gameplaytime that is being generated in the public void run?
paully104 void methods cannot return a value, but you can pseudoreturn a value by doing something like this, buddy: Code: // ...in your runnable class run(){ PluginMainClass.static_field++; } in your main class (as a field, above your onDisable and onEnable) Code: public static int static_field; @Override public void onEnable(){ // ... do some stuff static_field = 0; // initialization at 0 [seconds], to be incremented only by a repeating timer task // ... more stuff } And finally in your event Code: @EventPriority... public void onWhatever(PlayerDeathEvent event){ int seconds; // some stuff here // call your runnable timer thing here seconds = PluginMainClass.static_field; // do some more stuff here with them seconds }
Mattkx4 Since you want to time the whole thing in seconds, you can use this class resource to create timers which count seconds.
GameplayJDK Mattkx4 http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html I've always found that non-bukkit objects are a lot more reliable than bukkit-specific objects
Might have to just message you im still getting an error trying this method im using it pretty much verbatim and im getting static_field cannot be resolved.
paully104 Jesus christ. Give me a minute to boot up Eclipse and i'll get the exact code for you. paully104 Code: package unit.test; import org.bukkit.plugin.java.JavaPlugin; public class MainClass extends JavaPlugin { public static int s_variable; public int ns_variable; @Override public void onDisable(){ } @Override public void onEnable(){ s_variable = 0; ns_variable = 99; this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){ @Override public void run() { // This is technically part of a different class than MainClass, so you have to access outside variables a different way MainClass.s_variable++; // increment up by 5? MainClass.s_variable+=5; // decrement down by 99 MainClass.s_variable-=99; // But what if i dont want to use static access!!!!1! MainClass.this.ns_variable++; // increment up by 5? MainClass.this.ns_variable+=5; // decrement down by 99 MainClass.this.ns_variable-=99; } }); } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.