5252import java .util .concurrent .CancellationException ;
5353import java .util .concurrent .CompletionException ;
5454import java .util .concurrent .TimeUnit ;
55+ import java .util .concurrent .atomic .AtomicBoolean ;
5556import java .util .concurrent .atomic .AtomicInteger ;
5657import 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