Solved Setting timer that triggers stuff after countdown.

Discussion in 'Plugin Development' started by GeekyCompz, Dec 31, 2013.

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

    GeekyCompz

    I need to start a timer FROM the seconds that the player gives. "/timer start 10" would result in a 10 second delay before executing a message.

    I know that 20L is 1 second, so to get the correct countdown time you would do:
    Code:
    final int Countdown = Integer.parseInt(args[1]) * 20;
     
    Countdown + L
    
    I think...

    ~BytesCode
     
  2. Offline

    adam753

    Adding L to the end of a literal number (e.g. 20L) makes it a long rather than an int. The only way to do this with a variable is to cast it:
    Code:
    (long)Countdown
    This is technically not the same because the first one doesn't do any casting and is just different at compile time, but you don't need to concern yourself with that if you don't want to.

    If you were asking how to actually do the countdown, then here's the code:
    Code:java
    1.  
    2. new BukkitRunnable() {
    3. //Code
    4.  
    5. }.runTaskLater(plugin, 20L);
    6.  
     
  3. Offline

    GeekyCompz

    It works, thanks

    ~BytesCode

    Also how would I make count down the final 5 seconds?

    ~BytesCode

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    BillyGalbreath

    Make a self canceling task.
    Code:java
    1.  
    2. // MyPlugin.java
    3.  
    4. public class MyPlugin extends JavaPlugin {
    5. public void onEnable() {
    6. int startCount = 10; // get this from the user input "/timer start 10" or whatever
    7.  
    8. BukkitTask task = new Countdown(this, startCount).runTaskTimer(this, 0, 20); // "this" is a reference to MyPlugin, in case you want to use this in a different class
    9. }
    10.  
    11. public void onDisable() {
    12. //
    13. }
    14. }
    15.  


    Code:java
    1.  
    2. //Countdown.java
    3.  
    4. public class Countdown extends BukkitRunnable {
    5. private final MyPlugin plugin;
    6. private int counter;
    7.  
    8. public Countdown(MyPlugin plugin, int counter) {
    9. this.plugin = plugin;
    10. if (counter < 1) {
    11. throw new illegalArgumentException("counter must be greater than 1");
    12. } else {
    13. this.counter = counter;
    14. }
    15. }
    16.  
    17. public void run() {
    18. if (counter > 0) {
    19. plugin.getServer().broadcastMessage("Commence greeting in " + counter--);
    20. } else {
    21. plugin.getServer().broadcastMessage("Welcome to Bukkit! Remember to read the documentation!")
    22. this.cancel();
    23. }
    24. }
    25. }
    26.  

    Source: http://wiki.bukkit.org/Scheduler_Programming#Self-Canceling_Example
     
Thread Status:
Not open for further replies.

Share This Page