Im making a groundclear plugin I want to have a ten minutes timer which will stop using /stopcleartimer or after ten minutes. Halftime there must be a message and a call to a function. I searched for solutions, but i have no idea how to start this, I hope some one can help me, Thanks
Create a runnable that will repeat every X ticks and then compare System.currentTimeMillis() to see if the specified amount of time has actually passed on the machine. If so, do whatever you wish- otherwise do nothing.
thanks but I didn't see your reply before. so, after some more searching I found a code. the timer code I put in my Clearcountdown.java and the command /startcleartimer I put in my Clearcommands.java Clearcountdown.java: Code:java public void clearTimer(){Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){@Overridepublic void run(){Bukkit.broadcastMessage("test");}}, 20, 20);} Clearcommand.java Code:java if(cmd.getLabel().equalsIgnoreCase("startcleartimer")){Bukkit.broadcastMessage("Clearing started");Clearcountdown.clearTimer();} but then I get the error: Code: non-static method clearTimer() cannot be referenced from a static context anyone knows how to fix it
the method "Timer()" is not static. The method that is calling "Timer()" is static. This can't work. Both must be static or both must be not static to be able to call another. So set the other one non-static or set "Timer()" static. Also, where is your method "Timer()" and where are you calling it? can you show me the code pls?
I didn't copy the error, I typed it in. I made a mistake with typing so Timer(); = clearTimer(); making clearTimer(); static won't work because getServer(); isn't static, how do I do that then
moomann the code you showed to us, is it the whole? just whithout the class bodys? because i would like to see all of it. i cant even see somewhere a "static"
Code:java import org.bukkit.Bukkit; public class Clearcountdown {public void clearTimer(){Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){@Overridepublic void run(){Bukkit.broadcastMessage("test");}}, 20, 20);}} and commands, Code:java import org.bukkit.command.Command;import org.bukkit.command.CommandExecutor;import org.bukkit.command.CommandSender; public class Commands implements CommandExecutor{@Overridepublic boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args) {if(cmd.getLabel().equalsIgnoreCase("startcleartimer")){Bukkit.broadcastMessage("Clearing started");Clearcountdown.clearTimer();}}
okay here is your error: you try to access clearTimer(); from the class without having an instance. you must make clearTimer(); static or you make an instance of the class Clearcountdown. I would rather do the second. looks like this: Code:java import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; public class Commands implements CommandExecutor{ private Clearcountdown clearCountdown = new Clearcountdown(); @Override public boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args) { if(cmd.getLabel().equalsIgnoreCase("startcleartimer")){ Bukkit.broadcastMessage("Clearing started"); clearCountdown.clearTimer(); } }
that gives me an error on "this" Code: incompatible types: clearTimer cannot be converted to Plugin I mean this "this" Code:java Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() Bump EDIT by Moderator: merged posts, please use the edit button instead of double posting.
moomann with this you point at the object you are working with at the moment. in this case you are pointing at the instance of the class you are calling "this" in. this class does not extend Plugin and so it can't be used as the first Parameter, because the method want's scheduleAsyncRepeatingTask(Plugin, Runnable). you must use the instance of a class which is a Plugin!!
Here's my first piece of advice: do NOT create any other classes unless you actually are making a new object! In Java, code is broken into files by objects they represent, NOT by functionality. A good tutorial on scheduling is here: wiki.bukkit.org/Scheduler_Programming. I would recommend using BukkitRunnable, by either defining a new class to extend it, or using an anonymous declaration inline with the calling code.