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?
It depends on a variety of things. Async, Main threaded, what your doing inside of it, how often it executes.....
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).
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?
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.
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 private int max_ticks = 1800; //Maximum amount of seconds public HashMap<String, Integer> time = new HashMap<String, Integer>(); //HashMap containing the Arena + Time private int getCounter(String arenaName){ //Return the count TODO make private if(!(time.containsKey(arenaName.toLowerCase()))){ time.put(arenaName.toLowerCase(), max_ticks); } return time.get(arenaName.toLowerCase()); } private void updateCounter(String arenaName){ //Update the amount of ticks time.put(arenaName.toLowerCase(), (getCounter(arenaName) - 1)); m.scoreboard.update(arenaName); } public String getTime(String arenaName){ //Get the time in MM:SS format int seconds = getCounter(arenaName); TimeZone tz = TimeZone.getTimeZone("UTC"); SimpleDateFormat df = new SimpleDateFormat("mm:ss"); df.setTimeZone(tz); return df.format(new Date(seconds*1000)); } public void stopCounter(String arenaName){ time.remove(arenaName.toLowerCase()); } public void startCounter(final String arenaName){ //Start counter for that Arena new BukkitRunnable(){ @Override public void run() { if(time.containsKey(arenaName.toLowerCase())){ if(getCounter(arenaName) > 0){ updateCounter(arenaName); }else{ m.arena_stop.stopArenaTiming(arenaName); this.cancel(); } }else{ this.cancel(); } } }.runTaskTimer(m, 0, 20); } EDIT by Moderator: merged posts, please use the edit button instead of double posting.