I'm working on developing a plugin that uses a timer and when a player types /stoptimer or leaves the game, the timer is stopped and all events that would take place after the timer are also prevented from happening. I've done a fair amount of research on runnables and they just don't make sense to me. I'd really appreciate if someone can try to teach me how runnables work in the context of this example.
@pale1 Let's say you have a runnable here to run some code: Code:java new BukkitRunnable() { @Override public void run() { // The code to be run at the scheduled time }}.runTaskLater(plugin /* (the plugin instance) */, 20L /* (the delay, in gameticks) */); Then save this runnable in a Map<UUID, BukkitRunnable> (uuid representing the player), and when said player leaves, just get his runnable from the map and call the "cancel()" method on it.
@AlvinB Thank you for the information to cancel it. I assume I would do the same thing with the /stoptimer command. I still don't know how I would make the timer though.
He just made the timer for you. If you'd like a timer which subtracts 1 from a variable every second, make a new runnable which will run every 20 ticks. Inside the run() method, subtract 1 from the variable. If the variable < 1 , then cancel it, or get it and cancel it whenever a player types /stoptimer