Skip to content

Commit a1ac5e1

Browse files
Add the scheduler to unified API and fix first run main down
1 parent 6b518db commit a1ac5e1

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

src/main/java/net/pistonmaster/pistonqueue/bungee/PistonQueueBungee.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.time.Instant;
4343
import java.util.*;
4444
import java.util.concurrent.TimeUnit;
45+
import java.util.concurrent.atomic.AtomicBoolean;
4546
import java.util.concurrent.atomic.AtomicInteger;
4647
import java.util.logging.Logger;
4748
import java.util.stream.Collectors;
@@ -95,7 +96,7 @@ public void onEnable() {
9596
logger.info(ChatColor.BLUE + "Scheduling tasks");
9697

9798
// Sends the position message and updates tab on an interval in chat
98-
getProxy().getScheduler().schedule(this, () -> {
99+
schedule(() -> {
99100
if (!queueListenerBungee.isMainOnline())
100101
return;
101102

@@ -105,7 +106,7 @@ public void onEnable() {
105106
}, Config.POSITIONMESSAGEDELAY, Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS);
106107

107108
// Sends the position message and updates tab on an interval on hot bar
108-
getProxy().getScheduler().schedule(this, () -> {
109+
schedule(() -> {
109110
if (!queueListenerBungee.isMainOnline())
110111
return;
111112

@@ -115,13 +116,13 @@ public void onEnable() {
115116
}, Config.POSITIONMESSAGEDELAY, Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS);
116117

117118
// Updates the tab
118-
getProxy().getScheduler().schedule(this, () -> {
119+
schedule(() -> {
119120
updateTab(QueueType.VETERAN, Config.HEADERVETERAN, Config.FOOTERVETERAN);
120121
updateTab(QueueType.PRIORITY, Config.HEADERPRIORITY, Config.FOOTERPRIORITY);
121122
updateTab(QueueType.REGULAR, Config.HEADER, Config.FOOTER);
122123
}, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
123124

124-
getProxy().getScheduler().schedule(this, () -> {
125+
schedule(() -> {
125126
if (Config.PAUSEQUEUEIFMAINDOWN && !queueListenerBungee.isMainOnline()) {
126127
QueueType.VETERAN.getQueueMap().forEach((UUID id, String str) -> {
127128
ProxiedPlayer player = getProxy().getPlayer(id);
@@ -147,13 +148,14 @@ public void onEnable() {
147148
}, Config.POSITIONMESSAGEDELAY, Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS);
148149

149150
// Send plugin message
150-
getProxy().getScheduler().schedule(this, this::sendCustomData, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
151+
schedule(this::sendCustomData, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
151152

152153
// Moves the queue when someone logs off the main server on an interval set in the config.yml
153-
getProxy().getScheduler().schedule(this, queueListenerBungee::moveQueue, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
154+
schedule(queueListenerBungee::moveQueue, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
154155

156+
AtomicBoolean isFirstRun = new AtomicBoolean(true);
155157
// Checks the status of all the servers
156-
getProxy().getScheduler().schedule(this, () -> {
158+
schedule(() -> {
157159
if (getProxy().getServers().containsKey(Config.MAINSERVER)) {
158160
try {
159161
Socket s = new Socket(
@@ -162,20 +164,22 @@ public void onEnable() {
162164

163165
s.close();
164166

165-
if (!queueListenerBungee.isMainOnline())
167+
if (!isFirstRun.get() && !queueListenerBungee.isMainOnline()) {
166168
queueListenerBungee.setOnlineSince(Instant.now());
169+
}
167170

168171
queueListenerBungee.setMainOnline(true);
169172
} catch (IOException e) {
170173
getLogger().warning("Main Server is down!!!");
171174
queueListenerBungee.setMainOnline(false);
172175
}
176+
isFirstRun.set(false);
173177
} else {
174178
getLogger().warning("Main Server \"" + Config.MAINSERVER + "\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up");
175179
}
176180
}, 500, Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS);
177181

178-
getProxy().getScheduler().schedule(this, () -> {
182+
schedule(() -> {
179183
if (getProxy().getServers().containsKey(Config.QUEUESERVER)) {
180184
try {
181185
Socket s = new Socket(
@@ -193,7 +197,7 @@ public void onEnable() {
193197
}
194198
}, 500, Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS);
195199

196-
getProxy().getScheduler().schedule(this, () -> {
200+
schedule(() -> {
197201
if (Config.ENABLEAUTHSERVER) {
198202
if (getProxy().getServers().containsKey(Config.AUTHSERVER)) {
199203
try {
@@ -268,6 +272,11 @@ public List<PlayerWrapper> getPlayers() {
268272
return getProxy().getPlayers().stream().map(this::wrapPlayer).collect(Collectors.toList());
269273
}
270274

275+
@Override
276+
public void schedule(Runnable runnable, long delay, long period, TimeUnit unit) {
277+
getProxy().getScheduler().schedule(this, runnable, delay, period, unit);
278+
}
279+
271280
public PlayerWrapper wrapPlayer(ProxiedPlayer player) {
272281
return new PlayerWrapper() {
273282
@Override

src/main/java/net/pistonmaster/pistonqueue/shared/PistonQueueProxy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.time.Duration;
3333
import java.time.temporal.ChronoUnit;
3434
import java.util.*;
35+
import java.util.concurrent.TimeUnit;
3536
import java.util.concurrent.atomic.AtomicInteger;
3637
import java.util.concurrent.atomic.AtomicReference;
3738
import java.util.stream.Collectors;
@@ -41,6 +42,8 @@ public interface PistonQueueProxy {
4142

4243
List<PlayerWrapper> getPlayers();
4344

45+
void schedule(Runnable runnable, long delay, long period, TimeUnit unit);
46+
4447
default void sendMessage(QueueType queue, boolean bool, MessageType type) {
4548
if (bool) {
4649
int position = 0;

src/main/java/net/pistonmaster/pistonqueue/velocity/PistonQueueVelocity.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.util.concurrent.CancellationException;
5353
import java.util.concurrent.CompletionException;
5454
import java.util.concurrent.TimeUnit;
55+
import java.util.concurrent.atomic.AtomicBoolean;
5556
import java.util.concurrent.atomic.AtomicInteger;
5657
import java.util.stream.Collectors;
5758

@@ -120,66 +121,69 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
120121
logger.info("Scheduling tasks");
121122

122123
// Sends the position message and updates tab on an interval in chat
123-
proxyServer.getScheduler().buildTask(this, () -> {
124+
schedule(() -> {
124125
if (!queueListenerVelocity.isMainOnline())
125126
return;
126127

127128
for (QueueType type : QueueType.values()) {
128129
sendMessage(type, Config.POSITIONMESSAGECHAT, MessageType.CHAT);
129130
}
130-
}).delay(Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS).repeat(Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS).schedule();
131+
}, Config.POSITIONMESSAGEDELAY, Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS);
131132

132133
// Sends the position message and updates tab on an interval on hot bar
133-
proxyServer.getScheduler().buildTask(this, () -> {
134+
schedule(() -> {
134135
if (!queueListenerVelocity.isMainOnline())
135136
return;
136137

137138
for (QueueType type : QueueType.values()) {
138139
sendMessage(type, Config.POSITIONMESSAGEHOTBAR, MessageType.ACTION_BAR);
139140
}
140-
}).delay(Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS).repeat(Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS).schedule();
141+
}, Config.POSITIONMESSAGEDELAY, Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS);
141142

142143
// Updates the tab
143-
proxyServer.getScheduler().buildTask(this, () -> {
144+
schedule(() -> {
144145
updateTab(QueueType.VETERAN, Config.HEADERVETERAN, Config.FOOTERVETERAN);
145146
updateTab(QueueType.PRIORITY, Config.HEADERPRIORITY, Config.FOOTERPRIORITY);
146147
updateTab(QueueType.REGULAR, Config.HEADER, Config.FOOTER);
147-
}).delay(Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS).repeat(Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS).schedule();
148+
}, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
148149

149-
proxyServer.getScheduler().buildTask(this, () -> {
150+
schedule(() -> {
150151
if (Config.PAUSEQUEUEIFMAINDOWN && !queueListenerVelocity.isMainOnline()) {
151152
QueueType.VETERAN.getQueueMap().forEach((UUID id, String str) -> proxyServer.getPlayer(id).ifPresent(value -> value.sendMessage(ChatUtils.parseToComponent(Config.PAUSEQUEUEIFMAINDOWNMESSAGE))));
152153
QueueType.PRIORITY.getQueueMap().forEach((UUID id, String str) -> proxyServer.getPlayer(id).ifPresent(value -> value.sendMessage(ChatUtils.parseToComponent(Config.PAUSEQUEUEIFMAINDOWNMESSAGE))));
153154
QueueType.REGULAR.getQueueMap().forEach((UUID id, String str) -> proxyServer.getPlayer(id).ifPresent(value -> value.sendMessage(ChatUtils.parseToComponent(Config.PAUSEQUEUEIFMAINDOWNMESSAGE))));
154155
}
155-
}).delay(Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS).repeat(Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS).schedule();
156+
}, Config.POSITIONMESSAGEDELAY, Config.POSITIONMESSAGEDELAY, TimeUnit.MILLISECONDS);
156157

157158
// Send plugin message
158-
proxyServer.getScheduler().buildTask(this, this::sendCustomData).delay(Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS).repeat(Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS).schedule();
159+
schedule(this::sendCustomData, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
159160

160161
// Moves the queue when someone logs off the main server on an interval set in the config.yml
161-
proxyServer.getScheduler().buildTask(this, queueListenerVelocity::moveQueue).delay(Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS).repeat(Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS).schedule();
162+
schedule(queueListenerVelocity::moveQueue, Config.QUEUEMOVEDELAY, Config.QUEUEMOVEDELAY, TimeUnit.MILLISECONDS);
162163

164+
AtomicBoolean isFirstRun = new AtomicBoolean(true);
163165
// Checks the status of all the servers
164-
proxyServer.getScheduler().buildTask(this, () -> {
166+
schedule(() -> {
165167
if (proxyServer.getServer(Config.MAINSERVER).isPresent()) {
166168
try {
167169
proxyServer.getServer(Config.MAINSERVER).get().ping().join();
168170

169-
if (!queueListenerVelocity.isMainOnline())
171+
if (!isFirstRun.get() && !queueListenerVelocity.isMainOnline()) {
170172
queueListenerVelocity.setOnlineSince(Instant.now());
173+
}
171174

172175
queueListenerVelocity.setMainOnline(true);
173176
} catch (CancellationException | CompletionException e) {
174177
logger.warn("Main Server is down!!!");
175178
queueListenerVelocity.setMainOnline(false);
176179
}
180+
isFirstRun.set(false);
177181
} else {
178182
logger.warn("Main Server \"" + Config.MAINSERVER + "\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up");
179183
}
180-
}).delay(500, TimeUnit.MILLISECONDS).repeat(Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS).schedule();
184+
}, 500, Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS);
181185

182-
proxyServer.getScheduler().buildTask(this, () -> {
186+
schedule(() -> {
183187
if (proxyServer.getServer(Config.QUEUESERVER).isPresent()) {
184188
try {
185189
proxyServer.getServer(Config.QUEUESERVER).get().ping().join();
@@ -191,9 +195,9 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
191195
} else {
192196
logger.warn("Queue Server \"" + Config.QUEUESERVER + "\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up");
193197
}
194-
}).delay(500, TimeUnit.MILLISECONDS).repeat(Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS).schedule();
198+
}, 500, Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS);
195199

196-
proxyServer.getScheduler().buildTask(this, () -> {
200+
schedule(() -> {
197201
if (Config.ENABLEAUTHSERVER) {
198202
if (proxyServer.getServer(Config.AUTHSERVER).isPresent()) {
199203
try {
@@ -209,7 +213,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
209213
} else {
210214
queueListenerVelocity.setAuthOnline(true);
211215
}
212-
}).delay(500, TimeUnit.MILLISECONDS).repeat(Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS).schedule();
216+
}, 500, Config.SERVERONLINECHECKDELAY, TimeUnit.MILLISECONDS);
213217
}
214218

215219
@SuppressWarnings({"UnstableApiUsage"})
@@ -264,6 +268,11 @@ public List<PlayerWrapper> getPlayers() {
264268
return proxyServer.getAllPlayers().stream().map(this::wrapPlayer).collect(Collectors.toList());
265269
}
266270

271+
@Override
272+
public void schedule(Runnable runnable, long delay, long period, TimeUnit unit) {
273+
proxyServer.getScheduler().buildTask(this, runnable).delay(delay, unit).repeat(period, unit).schedule();
274+
}
275+
267276
public PlayerWrapper wrapPlayer(Player player) {
268277
return new PlayerWrapper() {
269278
@Override

0 commit comments

Comments
 (0)