[Unsolved] Creating loops with timers....

Discussion in 'Plugin Development' started by ASHninja1997, Aug 16, 2013.

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

    ASHninja1997

    Basically I am asking how you use the
    Code:
            Bukkit.getScheduler().runTask(arg0, arg1);
    and then use that to constantly create a loop for the server to run.
    There are two timers that I a running on my server one runs for 60 seconds then executes the other one to run for 4 minutes, than when the 4 minute timer is done is executes the 60 second timer. See the loop? If you need any more help with wondering about what I am doing just ask :).

    I will be gone for a bit so I won't be able to reply, sorry.

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

    hapm

  3. Offline

    ASHninja1997

    hapm
    Humm I did some research on this but I don't really understand it considering it is new to me...can you or anyone on reading this...tell/show me on how to do this?
     
  4. Offline

    artish1


    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
     
                        @Override
                        public void run() {
                            //Your code to Run every ticksForEveryRepeat ticks.
                           
                           
                        }
                       
                    }, ticksForFirstRunDelay, ticksForEveryRepeat);
    Usually i don't really use the ticksForFirstRunDelay so i just set that to 0, but if it is needed than you should use it.(But most of the time (Mostly for me) it's not needed).
    the plugin parameter is your plugin, if you are running this in your main class (The one with extends JavaPlugin), you might want to use "this".
     
  5. Offline

    ShadowLAX

    Here is how I would do it, hope it helps ^-^
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public Main plugin = this;
    4.  
    5. int counter;
    6. int counter2;
    7. int counter3;
    8. int number;
    9. int number2;
    10. int number3;
    11. String text = " seconds until game starts!";
    12. String text2 = " seconds until game ends!";
    13.  
    14. public void onEnable() {
    15. getLogger().info("has been enabled!");
    16. }
    17.  
    18. public void onDisable() {
    19. getLogger().info("has been disabled!");
    20. }
    21.  
    22. public void runnable1() {
    23. number = 60;
    24. counter = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
    25. public void run() {
    26. if (number == 60) {
    27. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    28. } else if (number == 30) {
    29. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    30. } else if (number == 15) {
    31. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    32. } else if (number == 10) {
    33. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    34. } else if (number == 5) {
    35. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    36. } else if (number == 4) {
    37. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    38. } else if (number == 3) {
    39. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    40. } else if (number == 2) {
    41. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    42. } else if (number == 1) {
    43. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number + text);
    44. } else if (number == 0) {
    45. Bukkit.getServer().getScheduler().cancelTask(counter);
    46. number2 = 60;
    47. counter2 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    48. public void run() {
    49. if (number2 == 60) {
    50. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    51. } else if (number2 == 30) {
    52. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    53. } else if (number2 == 10) {
    54. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    55. } else if (number2 == 5) {
    56. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    57. } else if (number2 == 4) {
    58. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    59. } else if (number2 == 3) {
    60. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    61. } else if (number2 == 2) {
    62. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    63. } else if (number2 == 1) {
    64. Bukkit.broadcastMessage(ChatColor.AQUA + "" + number2 + text2);
    65. } else if (number2 == 0) {
    66. Bukkit.getServer().getScheduler().cancelTask(counter);
    67. Bukkit.broadcastMessage(ChatColor.RED + "Game has ended!");
    68. }
    69. number2--;
    70. }
    71. }, 0L, 20L);
    72. }
    73. number--;
    74. }
    75. }, 0L, 20L);
    76. }
    77.  
    78. // Just a note, this loop is infinite because I did not call the cancel method, so it runs forever, or until server stop/reload.
    79. public void runnable2() {
    80. number3 = 90;
    81. counter3 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    82. public void run() {
    83. plugin.runnable1();
    84. }
    85. }, 0L, 2500L);// for calculating how long in between you want it to run, use this math: counter1 long + counter2 long + (20*(number of seconds you want it)) = number you put on this line for the long.
    86. }
    87.  
    88. // You can use anything to call it, just remember that it doesn't activate it more than once!
    89. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    90. if (label.equalsIgnoreCase("start")) {
    91. if (sender instanceof Player) {
    92. Player player = (Player) sender;
    93. if (player.isOp()) {
    94. player.sendMessage(ChatColor.GREEN + "Working!");
    95. plugin.runnable2();
    96. }
    97. }
    98. }
    99. return true;
    100. }
    101.  
    102. };
     
Thread Status:
Not open for further replies.

Share This Page