Hi! I'm creating a plugin, but need to add a Timer. Code: Player player = event.getPlayer(); int delay = 10; Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { player.sendMessage("10 secounds delay!"); } }, delay); This do not work because TimerTask() is another class. How can i svole it? just need to run Code: player.sendMessage("10 secounds delay!"); In the timer
Don't use the Timer Class, use bukkits scheduler Take a look at my tutorial Code: int OneTime = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { public void run() { player.sendMessage("10 secounds delay!"); } }, 10 * 20L);
Nice, thanks But still have the problem that "public void run()" can't get variable player, since the function is in class Runnable() What do then? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Declare Player as a global variable Do this outside the method Code: Player player; Then on the method just do: Code: player = event.getPlayer();
Thanks, will try that. Don't know how i don't was thinking about this myself Sorry, seems like this is wrong, i have to just delay. Also: Code: if (delayTask) { // Delay 10 secounds } // Code EDIT by Moderator: merged posts, please use the edit button instead of double posting.
My goal is to check if player have done a task, if he has done, i'll prevent him to do it of other 30 secounds.
Then you don't need to schedule anything. Just check next time the player tries to do whatever you want to prevent if 30 seconds have passed already or not using System.currentTimeMillis() for ex.
You can store the time in a Map<Player, Long> so each player has its own time value and then calculate the difference between the current time and the time you stored in the map.
You could make another class to store both values Code: class X { Player pvp; long lastTimeOfTask; } and use this class for map values, but i would use 2 maps.