Skip to content
This repository was archived by the owner on Feb 15, 2022. It is now read-only.

Commit b0ea67b

Browse files
committed
Implemented time based timers
1 parent 1842e8c commit b0ea67b

File tree

6 files changed

+136
-25
lines changed

6 files changed

+136
-25
lines changed

src/main/java/net/badlion/timers/api/Timer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.inventory.ItemStack;
55

66
import java.util.Collection;
7+
import java.util.concurrent.TimeUnit;
78

89
public interface Timer {
910

@@ -78,6 +79,22 @@ public interface Timer {
7879
*/
7980
void setTime(long time);
8081

82+
/**
83+
* Get the timer countdown time
84+
*
85+
* @return Timer countdown time in milliseconds
86+
*/
87+
long getMillis();
88+
89+
/**
90+
* Set the timer countdown time
91+
* Note : This implies a call to {@link Timer#reset()}
92+
*
93+
* @param time Timer countdown time
94+
* @param timeUnit Timer countdown time unit
95+
*/
96+
void setTime(long time, TimeUnit timeUnit);
97+
8198
// Player functions
8299

83100
/**

src/main/java/net/badlion/timers/api/TimerApi.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.bukkit.entity.Player;
44
import org.bukkit.inventory.ItemStack;
55

6+
import java.util.concurrent.TimeUnit;
7+
68
public abstract class TimerApi {
79
static TimerApi instance;
810

@@ -27,7 +29,7 @@ public static TimerApi getInstance() {
2729
* @param time Countdown time, in ticks (20 per seconds)
2830
* @return The new timer instance
2931
*/
30-
public abstract Timer createTimer(ItemStack item, boolean repeating, long time);
32+
public abstract Timer createTickTimer(ItemStack item, boolean repeating, long time);
3133

3234
/**
3335
* Create a new timer and register it into the API.
@@ -42,7 +44,38 @@ public static TimerApi getInstance() {
4244
* @param time Countdown time, in ticks (20 per seconds)
4345
* @return The new timer instance
4446
*/
45-
public abstract Timer createTimer(String name, ItemStack item, boolean repeating, long time);
47+
public abstract Timer createTickTimer(String name, ItemStack item, boolean repeating, long time);
48+
49+
/**
50+
* Create a new timer and register it into the API.
51+
* <p>
52+
* A timer will automatically handle synchronizing with its receivers,
53+
* and will repeat itself if it's mark as repeating. If not, it'll be
54+
* automatically removed from the API.
55+
*
56+
* @param item Item to show in the client
57+
* @param repeating {@code true} if the timer is repeating, {@code false} otherwise
58+
* @param time Countdown time
59+
* @param timeUnit Countdown time unit
60+
* @return The new timer instance
61+
*/
62+
public abstract Timer createTimeTimer(ItemStack item, boolean repeating, long time, TimeUnit timeUnit);
63+
64+
/**
65+
* Create a new timer and register it into the API.
66+
* <p>
67+
* A timer will automatically handle synchronizing with its receivers,
68+
* and will repeat itself if it's mark as repeating. If not, it'll be
69+
* automatically removed from the API.
70+
*
71+
* @param name Name to show in the client
72+
* @param item Item to show in the client
73+
* @param repeating {@code true} if the timer is repeating, {@code false} otherwise
74+
* @param time Countdown time
75+
* @param timeUnit Countdown time unit
76+
* @return The new timer instance
77+
*/
78+
public abstract Timer createTimeTimer(String name, ItemStack item, boolean repeating, long time, TimeUnit timeUnit);
4679

4780
/**
4881
* Remove a timer from the API, disabling all API features about it.

src/main/java/net/badlion/timers/api/TimerApiImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Collections;
1010
import java.util.Set;
1111
import java.util.concurrent.ConcurrentHashMap;
12+
import java.util.concurrent.TimeUnit;
1213
import java.util.concurrent.atomic.AtomicInteger;
1314

1415
public class TimerApiImpl extends TimerApi {
@@ -26,19 +27,33 @@ public TimerApiImpl(TimerPlugin plugin) {
2627
}
2728

2829
@Override
29-
public Timer createTimer(ItemStack item, boolean repeating, long time) {
30-
return this.createTimer(null, item, repeating, time);
30+
public Timer createTickTimer(ItemStack item, boolean repeating, long time) {
31+
return this.createTickTimer(null, item, repeating, time);
3132
}
3233

3334
@Override
34-
public Timer createTimer(String name, ItemStack item, boolean repeating, long time) {
35+
public Timer createTickTimer(String name, ItemStack item, boolean repeating, long time) {
3536
TimerImpl timer = new TimerImpl(this.plugin, this.idGenerator.getAndIncrement(), name, item, repeating, time);
3637

3738
this.allTimers.add(timer);
3839

3940
return timer;
4041
}
4142

43+
@Override
44+
public Timer createTimeTimer(ItemStack item, boolean repeating, long time, TimeUnit timeUnit) {
45+
return this.createTimeTimer(null, item, repeating, time, timeUnit);
46+
}
47+
48+
@Override
49+
public Timer createTimeTimer(String name, ItemStack item, boolean repeating, long time, TimeUnit timeUnit) {
50+
TimerImpl timer = new TimerImpl(this.plugin, this.idGenerator.getAndIncrement(), name, item, repeating, time, timeUnit);
51+
52+
this.allTimers.add(timer);
53+
54+
return timer;
55+
}
56+
4257
@Override
4358
public void removeTimer(Timer timer) {
4459
// Failsafe

src/main/java/net/badlion/timers/impl/NmsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static void sendPluginMessage(Player player, String channel, byte[] messa
140140
Object playerConnection = NmsManager.playerConnectionField.get(nmsPlayer);
141141
NmsManager.sendPacketMethod.invoke(playerConnection, packet);
142142
} catch (Exception ex) {
143-
NmsManager.plugin.getLogger().severe("Failed to send BLC CPS packet");
143+
NmsManager.plugin.getLogger().severe("Failed to send BLC Timer packet");
144144
ex.printStackTrace();
145145
}
146146
}

src/main/java/net/badlion/timers/impl/TimerImpl.java

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.concurrent.TimeUnit;
2122
import java.util.concurrent.atomic.AtomicBoolean;
2223

2324
public 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;

src/main/java/net/badlion/timers/listeners/TimerListener.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import net.badlion.timers.TimerPlugin;
44
import net.badlion.timers.impl.NmsManager;
5-
import org.bukkit.entity.Player;
65
import org.bukkit.event.EventHandler;
76
import org.bukkit.event.EventPriority;
87
import org.bukkit.event.Listener;
98
import org.bukkit.event.player.PlayerJoinEvent;
10-
import org.bukkit.event.player.PlayerKickEvent;
119
import org.bukkit.event.player.PlayerQuitEvent;
1210
import org.bukkit.event.player.PlayerTeleportEvent;
1311

@@ -27,16 +25,7 @@ public void onJoin(PlayerJoinEvent event) {
2725

2826
@EventHandler
2927
public void onDisconnect(PlayerQuitEvent event) {
30-
this.onDisconnect(event.getPlayer());
31-
}
32-
33-
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
34-
public void onKick(PlayerKickEvent event) {
35-
this.onDisconnect(event.getPlayer());
36-
}
37-
38-
private void onDisconnect(Player player) {
39-
this.plugin.getTimerApi().clearTimers(player);
28+
this.plugin.getTimerApi().clearTimers(event.getPlayer());
4029
}
4130

4231
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)

0 commit comments

Comments
 (0)