Solved Java timer

Discussion in 'Plugin Development' started by Unica, Oct 13, 2014.

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

    Unica

    Hello,

    I need a 30 minute timer-ish for my minigame,
    and I need to know what the right way is.
    I understand that there are schedulars, but java comes with a timer and I was wondering if that is
    better than a schedular.

    If I am wrong, please give me a good alternative to count longer times (30 minute / 15 minutes etc.)

    I also need to be able to display the time like [14:35] (14 minute, 35 seconds)
    Any ideas?
     
  2. Offline

    Gater12

    Unica
    You can schedule a repeating task.
     
  3. Offline

    Unica

    Gater12

    Would that lagg the server? Or is it safe?
     
  4. Offline

    teej107

    It depends on a variety of things. Async, Main threaded, what your doing inside of it, how often it executes.....
     
  5. Offline

    mythbusterma

    Unica

    And using a Java Timer would not be the correct answer in this case. The server's 30 minutes may not be in sync with the real 30 minutes that passes, and you would ideally time it to the server's time (i.e. scale with TPS).
     
    Unica likes this.
  6. Offline

    Unica

    teej107 mythbusterma

    Forgot to mention the using of it,
    I have this minigame with multiple arena's at once, and every arena must have it's own timer, after those 30 minutes the game will end in that arena.

    I was think about storing the Timer and the arena in an HashMap, and so have arena-only timers.
    If you follow me :) Is it best to use a BukkitRunnable in this case?
     
  7. Offline

    coasterman10

    Bukkit has a scheduler for this. You can easily do this with BukkitRunnables. You don't need to store it in a Map unless you want early cancellation for any reason, just schedule it to run when your arena starts and have it end the game in the arena when the runnable is called.
     
  8. Offline

    Unica

    coasterman10

    I want to display the time on a scoreboard, therefor I created an HashMap, is this avoidable by using fields? (Avoiding the Map like you said)

    Done :)
    Code:java
    1. private int max_ticks = 1800; //Maximum amount of seconds
    2.  
    3. public HashMap<String, Integer> time = new HashMap<String, Integer>(); //HashMap containing the Arena + Time
    4.  
    5. private int getCounter(String arenaName){ //Return the count TODO make private
    6. if(!(time.containsKey(arenaName.toLowerCase()))){
    7. time.put(arenaName.toLowerCase(), max_ticks);
    8. }
    9. return time.get(arenaName.toLowerCase());
    10. }
    11.  
    12. private void updateCounter(String arenaName){ //Update the amount of ticks
    13. time.put(arenaName.toLowerCase(), (getCounter(arenaName) - 1));
    14. m.scoreboard.update(arenaName);
    15. }
    16.  
    17. public String getTime(String arenaName){ //Get the time in MM:SS format
    18. int seconds = getCounter(arenaName);
    19. TimeZone tz = TimeZone.getTimeZone("UTC");
    20. df.setTimeZone(tz);
    21. return df.format(new Date(seconds*1000));
    22. }
    23.  
    24. public void stopCounter(String arenaName){
    25. time.remove(arenaName.toLowerCase());
    26. }
    27.  
    28. public void startCounter(final String arenaName){ //Start counter for that Arena
    29. new BukkitRunnable(){
    30. @Override
    31. public void run() {
    32. if(time.containsKey(arenaName.toLowerCase())){
    33. if(getCounter(arenaName) > 0){
    34. updateCounter(arenaName);
    35. }else{
    36. m.arena_stop.stopArenaTiming(arenaName);
    37. this.cancel();
    38. }
    39. }else{
    40. this.cancel();
    41. }
    42. }
    43. }.runTaskTimer(m, 0, 20);
    44. }



    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page