Timer help

Discussion in 'Plugin Development' started by SeanyJo, Oct 12, 2013.

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

    SeanyJo

    Hi, I am wondering how you would make configurable timers, like lets say theres a countdown for a lobby, and in the config, the value is set to 120, so the countdown would be 2 minutes, because 120 is equal to seconds. I also want to know how you would send Messages to players, based on how many seconds have passed. So, if the counter is 4 minutes, it wouldn't say the 5 minute mark, but if it was 1 minute, it would show the 1 minute, 45 second, 30 second, 15 second, and 10-1 second count.
     
  2. Offline

    adam753

    Ultimate_n00b likes this.
  3. Offline

    SeanyJo

    I kind of need a little more info than that..
     
  4. Offline

    adam753

    SeanyJo
    Okay then, here's some example code.
    Code:java
    1.  
    2. //Untested, may not compile
    3. public class MyRunnable extends BukkitRunnable {
    4.  
    5. //Override the Runnable class's run method
    6. @Override
    7. public void run() {
    8. Bukkit.broadcastMessage("derp");
    9. }
    10. }
    11.  
    12. //Somewhere in your code:
    13. new MyRunnable().runTaskTimer(plugin, 0, 20);
    14.  

    The above will broadcast the message "derp" every second (delay of 20 ticks = 1 second). Really simple, right? And since the MyRunnable class I created there is just a regular instantiated class like any other, creating a countdown is as easy as giving it an integer variable and setting it when you create it:

    Code:java
    1.  
    2. //Untested, may not compile
    3. public class MyRunnable extends BukkitRunnable {
    4.  
    5. public int count = 0;
    6.  
    7. //Override the Runnable class's run method
    8. @Override
    9. public void run() {
    10. Bukkit.broadcastMessage(--count);
    11. }
    12. }
    13.  
    14. //Somewhere in your code:
    15. MyRunnable timer = new MyRunnable();
    16. timer.count = 120;
    17. timer.runTaskTimer(plugin, 0, 20);
    18.  

    That will make it broadcast 120, 119, 118 etc. every second.

    Does that make it any clearer how to do what you wanted?
     
  5. Offline

    SeanyJo

    I want it so that if it doesn't broadcast is every second, only the major times, like 5 mins, 4 mins, 3 mins, 2 mins, 1 min, 45 secs, 30 secs, 15 secs, 10-1 secs, 0 secs.
     
  6. Offline

    adam753

    SeanyJo
    You would do that simply by changing the code in run(). Check what the count is at, and broadcast only if it's at a number you want. Maybe use a switch statement if you're feeling adventurous.
     
  7. Offline

    SeanyJo

    Here's what I have, broadcastMessage wasn't compatible with the int, so I had to change a few things.
    Code:java
    1. public class Test extends BukkitRunnable {
    2. private void tellPlayersInMap(int i) {
    3. }
    4.  
    5. public int count = 0;
    6. @Override
    7. public void run() {
    8. tellPlayersInMap(--count);
    9. }
    10.  
    11. Test timer = new Test();
    12. timer.count = 120;
    13. timer.runTaskTimer(plugin, 0, 20);
    14. }

    I get errors with lines 12 and 13, saying that I should remove the '(' on line 13, and I should replace it with a ConstructorHeadername instead. For line 12, it says that I should add a VariableDeclaratorId after the 'count'. When I add something, it says that timer can't be resolved.
     
  8. Offline

    PatoTheBest

  9. Offline

    SeanyJo

    PatoTheBest adam753
    Heres the newer version of my code, though I have some problems:
    Code:java
    1. public void startLobbyCountdown() {
    2.  
    3. int lobbycountdown = plugin.getconfigfile().getInt("Countdown_Config.Lobby.Time-In-Seconds");
    4. {
    5. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    6. public void run() {
    7. lobbycountdown --;
    8. }
    9. }
    10. , 0L, 20L);
    11. }
    12. }
    13. }

    It says:

    Cannot refer to a non-final variable lobbycountdown inside an inner class defined in a different method.
    This is for line 7. I also have a warning for the int lobbycountdown line, but that's because it hasn't been utilized yet.
     
  10. Offline

    WauloK

    Try the code linked in my signature :)
     
  11. Offline

    PatoTheBest

    WauloK read my post above....
     
  12. Offline

    SeanyJo

    WauloK PatoTheBest
    Thanks Waulo, but I have a new problem, one that wasn't in your code. I want to replace %m with MinutesToCountdown, but it says that it's inapplicable for the type. heres the code I have:
    Code:java
    1. public Runnable startMinutesCountdown() {
    2. MinutesToCountdown = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    3. public void run() {
    4. MinutesToCountdown--;
    5. if (MinutesToCountdown!=0) {
    6. Bukkit.broadcastMessage(plugin.getmessagesfile().getInt("Messages.LobbyCount.Minutes").replace("%m", MinutesToCountdown));
    7. }
    8. if (MinutesToCountdown==0) {
    9. plugin.getServer().getScheduler().cancelTask(MinutesToCountdown);
    10. startSecondsCountdown(); //I will implement this later on
    11. }
    12.  
    13. }
    14. }, 20*60L, 20*60L); // 60 seconds in a minute
    15. return null;
    16. }
    17. }
     
  13. Offline

    WauloK

    What is Messages.LobbyCount.Minutes set to? Looks like you are getting an Integer and trying to use a string replace method?
     
  14. Offline

    SeanyJo


    Minutes: '&aThe game is starting in %m minutes!'
    I replaced the getInt with getString, but now it says that:

    The method replace(char, char) in the type String is not applicable for the arguments (String, int)
     
  15. Offline

    WauloK

    Try casting MinutesToCountdown to string with
    Code:java
    1. (String) MinutesToCountdown

    My Java is a little rusty at the moment so I hope it works ;)
     
  16. Offline

    SeanyJo

    Where would I put it? Wherever I think it would go, it's giving me errors.
     
  17. Offline

    WauloK

    Could you please repost all your new code? :)
     
  18. Offline

    SeanyJo

    sure, here it is:
    Code:java
    1. package me.SeanyJo.tigregames.events;
    2.  
    3. import me.SeanyJo.tigregames.TigreGames;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class LobbyCountdown {
    10. public static TigreGames plugin;
    11. public static int MinutesToCountdown=plugin.getconfigfile().getInt("Countdown_Config.Lobby.Time-In-Minutes");
    12. public static int SecondsToCountdown=plugin.getconfigfile().getInt("Countdown_Config.Lobby.Time-In-Seconds");
    13. public static void tellPlayersInMap(String message) {
    14. for (String playerName : TigreGames.lobby.keySet()) {
    15. Player player = Bukkit.getPlayerExact(playerName);
    16. if (player != null) {
    17. player.sendMessage(message);
    18. }
    19. }
    20. }
    21. public static void teleportPlayersInMap(Location location) {
    22. for (String playerName : TigreGames.lobby.keySet()) {
    23. Player player = Bukkit.getPlayerExact(playerName);
    24. if (player !=null) {
    25. player.teleport(location);
    26. }
    27. }
    28. }
    29. public Runnable startMinutesCountdown() {
    30. MinutesToCountdown = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    31. public void run() {
    32. MinutesToCountdown--;
    33. if (MinutesToCountdown!=0) {
    34. Bukkit.broadcastMessage(plugin.getmessagesfile().getString("Messages.LobbyCount.Minutes").replace("%m", MinutesToCountdown));
    35. }
    36. if (MinutesToCountdown==0) {
    37. plugin.getServer().getScheduler().cancelTask(MinutesToCountdown);
    38. startSecondsCountdown();
    39. }
    40.  
    41. }
    42. }, 20*60L, 20*60L);
    43. return null;
    44. }
    45. }
     
  19. Offline

    WauloK

    Code:java
    1. Bukkit.broadcastMessage(plugin.getmessagesfile().getString("Messages.LobbyCount.Minutes").replace("%m", Integer.toString(MinutesToCountdown)));
     
    SeanyJo likes this.
  20. Offline

    SeanyJo

    ok, the errors went away :D Thanks!

    WauloK
    Now this error is popping up on Enabling:
    It's an ExceptionInitializerError, and it worked before, but I think it;s messed up due to the new code. Heres what I'm using:
    Code:java
    1. LobbyCountdown.plugin = this;

    In the LobbyCountdown class, I have this code:
    Code:java
    1. public static TigreGames plugin;


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

    WauloK

    If you make some changes, it's best to post the full code each time.. even if it's a link to pastebin as the context really helps.
     
  22. Offline

    SeanyJo

  23. Offline

    WauloK

    I've never seen this done before, but I'm relatively new to Bukkit programming:
    Code:java
    1. Command_HG.plugin = this;
    2. LobbyCountdown.plugin = this;
    3. PlayerQuit.plugin = this;
    4. GlobalChatColor.plugin = this;
    5. InvincibilityCountdown.plugin = this;
    6. Invincible.plugin = this;
    7. GameCountdown.plugin = this;


    Is this line causing the error?
    Code:java
    1. LobbyCountdown.plugin = this;
     
  24. Offline

    SeanyJo

    That's the line it says. There is a NullPointerException on line 11 of the Countdown class, which is probably for plugin, because it isn't initializing. Is there another way to initialize a class/plugin? Heres the stacktrace if you need it:
    http://pastie.org/8400647
     
  25. Offline

    WauloK

    I've never had this problem before. It seems there's an issue with your static declaration in the Countdown code.

    Not sure. Try commenting out all the *.plugin = this;
    Or perhaps not declaring the variables as static in the Countdown class?

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

    SeanyJo

    I can't really figure it out, I mean, it worked before, but I don't know what messed it up right now..
     
  27. Offline

    WauloK

    Could you zip up the code somewhere?
     
  28. Offline

    SeanyJo

    Like.. put it in a zip file and upload it to mediafire? Like that, or..
     
  29. Offline

    WauloK

  30. Offline

    SeanyJo

    Ok, here it is:
    Click here for .zip file. Put this in your workspace (after unzipping)
     
Thread Status:
Not open for further replies.

Share This Page