Timers and Runnables

Discussion in 'Plugin Development' started by pale1, Jan 9, 2017.

Thread Status:
Not open for further replies.
  1. Offline


    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.
  2. @pale1
    Let's say you have a runnable here to run some code:
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4. // The code to be run at the scheduled time
    5. }
    6. }.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.
    DinosaurKappa likes this.
  3. Offline



    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.
  4. Offline


    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
Thread Status:
Not open for further replies.

Share This Page