| 1 | package net.encode.wurmesp.util; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | |
| 5 | public class CronoManager { |
| 6 | private long timelapse; |
| 7 | private long time; |
| 8 | private long remaining; |
| 9 | private long future; |
| 10 | private long last; |
| 11 | |
| 12 | public CronoManager(long timelapse) { |
| 13 | this.timelapse = timelapse; |
| 14 | this.time = System.currentTimeMillis(); |
| 15 | this.future = this.time + this.timelapse; |
| 16 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 17 | } |
| 18 | |
| 19 | public void restart(long timelapse) { |
| 20 | this.timelapse = timelapse; |
| 21 | this.time = System.currentTimeMillis(); |
| 22 | this.future = this.time + this.timelapse; |
| 23 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 24 | } |
| 25 | |
| 26 | public HashMap<CronoDataType, Long> getTime() { |
| 27 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 28 | long days = (long)Math.floor(this.remaining / 86400L); |
| 29 | long hours = (long)Math.floor(this.remaining % 86400L / 3600L); |
| 30 | long minutes = (long)Math.floor(this.remaining % 86400L % 3600L / 60L); |
| 31 | long seconds = (long)Math.floor(this.remaining % 86400L % 3600L % 60L); |
| 32 | HashMap<CronoDataType, Long> returnedTime = new HashMap<CronoDataType, Long>(); |
| 33 | returnedTime.put(CronoDataType.DAYS, days); |
| 34 | returnedTime.put(CronoDataType.HOURS, hours); |
| 35 | returnedTime.put(CronoDataType.MINUTES, minutes); |
| 36 | returnedTime.put(CronoDataType.SECONDS, seconds); |
| 37 | return returnedTime; |
| 38 | } |
| 39 | |
| 40 | public int getDays() { |
| 41 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 42 | long days = (long)Math.floor(this.remaining / 86400L); |
| 43 | return (int)days; |
| 44 | } |
| 45 | |
| 46 | public int getHours() { |
| 47 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 48 | long hours = (long)Math.floor(this.remaining % 86400L / 3600L); |
| 49 | return (int)hours; |
| 50 | } |
| 51 | |
| 52 | public int getMinutes() { |
| 53 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 54 | long minutes = (long)Math.floor(this.remaining % 86400L % 3600L / 60L); |
| 55 | return (int)minutes; |
| 56 | } |
| 57 | |
| 58 | public int getSeconds() { |
| 59 | this.last = this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 60 | long seconds = (long)Math.floor(this.remaining % 86400L % 3600L % 60L); |
| 61 | return (int)seconds; |
| 62 | } |
| 63 | |
| 64 | public boolean hasNext() { |
| 65 | this.remaining = (this.future - System.currentTimeMillis()) / 1000L; |
| 66 | return this.remaining < this.last; |
| 67 | } |
| 68 | |
| 69 | public boolean hasEnded() { |
| 70 | return System.currentTimeMillis() > this.future; |
| 71 | } |
| 72 | |
| 73 | public static enum CronoDataType { |
| 74 | DAYS, |
| 75 | HOURS, |
| 76 | MINUTES, |
| 77 | SECONDS; |
| 78 | |
| 79 | } |
| 80 | } |
| 81 | |