1818import java .util .Collections ;
1919import java .util .Set ;
2020import java .util .concurrent .ConcurrentHashMap ;
21+ import java .util .concurrent .TimeUnit ;
2122import java .util .concurrent .atomic .AtomicBoolean ;
2223
2324public class TimerImpl implements Timer {
@@ -35,9 +36,11 @@ public class TimerImpl implements Timer {
3536 private ItemStack item ;
3637 private boolean repeating ;
3738 private long time ;
39+ private long millis ;
3840 private AtomicBoolean updated ;
3941 private Set <Player > receivers ;
4042 private long currentTime ;
43+ private long lastTick ;
4144
4245 public TimerImpl (TimerPlugin plugin , long id , String name , ItemStack item , boolean repeating , long time ) {
4346 this .plugin = plugin ;
@@ -46,7 +49,27 @@ public TimerImpl(TimerPlugin plugin, long id, String name, ItemStack item, boole
4649 this .item = item ;
4750 this .repeating = repeating ;
4851 this .time = time ;
52+ this .millis = -1 ;
4953 this .currentTime = time ;
54+ this .lastTick = System .currentTimeMillis ();
55+
56+ this .updated = new AtomicBoolean (false );
57+ this .receivers = Collections .newSetFromMap (new ConcurrentHashMap <Player , Boolean >());
58+
59+ this .removeReceiverRequest = new RemoveReceiverRequest ();
60+ this .removeReceiverRequest .id = id ;
61+ }
62+
63+ public TimerImpl (TimerPlugin plugin , int id , String name , ItemStack item , boolean repeating , long time , TimeUnit timeUnit ) {
64+ this .plugin = plugin ;
65+ this .id = id ;
66+ this .name = name ;
67+ this .item = item ;
68+ this .repeating = repeating ;
69+ this .time = -1 ;
70+ this .millis = timeUnit .toMillis (time );
71+ this .currentTime = this .millis ;
72+ this .lastTick = System .currentTimeMillis ();
5073
5174 this .updated = new AtomicBoolean (false );
5275 this .receivers = Collections .newSetFromMap (new ConcurrentHashMap <Player , Boolean >());
@@ -116,6 +139,21 @@ public long getTime() {
116139 @ Override
117140 public void setTime (long time ) {
118141 this .time = time ;
142+ this .millis = -1L ;
143+ this .updated .set (true );
144+ this .reset ();
145+ }
146+
147+ @ Override
148+ public long getMillis () {
149+ return this .millis ;
150+ }
151+
152+ @ Override
153+ public void setTime (long time , TimeUnit timeUnit ) {
154+ this .time = -1L ;
155+ this .millis = timeUnit .toMillis (time );
156+ this .updated .set (true );
119157 this .reset ();
120158 }
121159
@@ -147,21 +185,39 @@ public Collection<Player> getReceivers() {
147185
148186 @ Override
149187 public void reset () {
150- this .currentTime = this .time ;
188+ this .currentTime = this .time != - 1L ? this . time : this . millis ;
151189
152190 this .syncTimer ();
153191 }
154192
155193 public void tick () {
156- if (--this .currentTime <= 0 ) {
157- if (!this .repeating ) {
158- this .plugin .getTimerApi ().removeTimer (this );
159- return ;
160- } else {
161- this .currentTime = this .time ;
194+
195+ long currentMillis = System .currentTimeMillis ();
196+
197+ if (this .time != -1L ) {
198+ if (--this .currentTime <= 0 ) {
199+ if (!this .repeating ) {
200+ this .plugin .getTimerApi ().removeTimer (this );
201+ return ;
202+ } else {
203+ this .currentTime = this .time ;
204+ }
205+ }
206+ } else {
207+ long diff = currentMillis - this .lastTick ;
208+
209+ if ((this .currentTime -= diff ) <= 0 ) {
210+ if (!this .repeating ) {
211+ this .plugin .getTimerApi ().removeTimer (this );
212+ return ;
213+ } else {
214+ this .currentTime += this .millis ;
215+ }
162216 }
163217 }
164218
219+ this .lastTick = currentMillis ;
220+
165221 if (this .updated .compareAndSet (true , false )) {
166222 this .send (this .receivers , "UPDATE_TIMER" , this );
167223 }
@@ -197,6 +253,7 @@ public JsonElement serialize(TimerImpl timer, Type type, JsonSerializationContex
197253 jsonObject .add ("item" , TimerImpl .GSON .toJsonTree (timer .item .serialize ()));
198254 jsonObject .add ("repeating" , new JsonPrimitive (timer .repeating ));
199255 jsonObject .add ("time" , new JsonPrimitive (timer .time ));
256+ jsonObject .add ("millis" , new JsonPrimitive (timer .millis ));
200257 jsonObject .add ("currentTime" , new JsonPrimitive (timer .currentTime ));
201258
202259 return jsonObject ;
0 commit comments