2929import org .spongepowered .configurate .yaml .YamlConfigurationLoader ;
3030
3131import java .io .IOException ;
32+ import java .nio .charset .StandardCharsets ;
3233import java .nio .file .Files ;
3334import java .nio .file .Path ;
3435import java .time .Duration ;
35- import java .time .Instant ;
3636import java .time .temporal .ChronoUnit ;
3737import java .util .*;
38+ import java .util .concurrent .CompletableFuture ;
39+ import java .util .concurrent .CountDownLatch ;
3840import java .util .concurrent .TimeUnit ;
39- import java .util .concurrent .atomic .AtomicBoolean ;
4041import java .util .concurrent .atomic .AtomicInteger ;
4142import java .util .stream .Collectors ;
4243
@@ -66,21 +67,20 @@ public interface PistonQueuePlugin {
6667 default void scheduleTasks (QueueListenerShared queueListener ) {
6768 // Sends the position message and updates tab on an interval in chat
6869 schedule (() -> {
69- if (!queueListener .isMainOnline ())
70- return ;
71-
72- for (QueueType type : Config .QUEUE_TYPES ) {
73- sendMessage (type , Config .POSITION_MESSAGE_CHAT , MessageType .CHAT );
74- }
75- }, Config .POSITION_MESSAGE_DELAY , Config .POSITION_MESSAGE_DELAY , TimeUnit .MILLISECONDS );
76-
77- // Sends the position message and updates tab on an interval on hot bar
78- schedule (() -> {
79- if (!queueListener .isMainOnline ())
80- return ;
81-
82- for (QueueType type : Config .QUEUE_TYPES ) {
83- sendMessage (type , Config .POSITION_MESSAGE_HOT_BAR , MessageType .ACTION_BAR );
70+ if (queueListener .getOnlineServers ().contains (Config .TARGET_SERVER )) {
71+ for (QueueType type : Config .QUEUE_TYPES ) {
72+ if (Config .POSITION_MESSAGE_CHAT ) {
73+ sendMessage (type , MessageType .CHAT );
74+ }
75+ if (Config .POSITION_MESSAGE_HOT_BAR ) {
76+ sendMessage (type , MessageType .ACTION_BAR );
77+ }
78+ }
79+ } else if (Config .PAUSE_QUEUE_IF_TARGET_DOWN ) {
80+ for (QueueType type : Config .QUEUE_TYPES ) {
81+ type .getQueueMap ().forEach ((UUID id , String str ) ->
82+ getPlayer (id ).ifPresent (value -> value .sendMessage (Config .PAUSE_QUEUE_IF_TARGET_DOWN_MESSAGE )));
83+ }
8484 }
8585 }, Config .POSITION_MESSAGE_DELAY , Config .POSITION_MESSAGE_DELAY , TimeUnit .MILLISECONDS );
8686
@@ -91,91 +91,52 @@ default void scheduleTasks(QueueListenerShared queueListener) {
9191 }
9292 }, Config .QUEUE_MOVE_DELAY , Config .QUEUE_MOVE_DELAY , TimeUnit .MILLISECONDS );
9393
94- schedule (() -> {
95- if (!Config .PAUSE_QUEUE_IF_MAIN_DOWN || queueListener .isMainOnline ()) {
96- return ;
97- }
98-
99- for (QueueType type : Config .QUEUE_TYPES ) {
100- type .getQueueMap ().forEach ((UUID id , String str ) ->
101- getPlayer (id ).ifPresent (value -> value .sendMessage (Config .PAUSE_QUEUE_IF_MAIN_DOWN_MESSAGE )));
102- }
103- }, Config .POSITION_MESSAGE_DELAY , Config .POSITION_MESSAGE_DELAY , TimeUnit .MILLISECONDS );
104-
10594 // Send plugin message
10695 schedule (this ::sendCustomData , Config .QUEUE_MOVE_DELAY , Config .QUEUE_MOVE_DELAY , TimeUnit .MILLISECONDS );
10796
108- // Moves the queue when someone logs off the main server on an interval set in the config.yml
97+ // Moves the queue when someone logs off the target server on an interval set in the config.yml
10998 schedule (queueListener ::moveQueue , Config .QUEUE_MOVE_DELAY , Config .QUEUE_MOVE_DELAY , TimeUnit .MILLISECONDS );
11099
111- String message = "%s \" %s\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up" ;
112- AtomicBoolean isFirstRun = new AtomicBoolean (true );
113100 // Checks the status of all the servers
114101 schedule (() -> {
115- Optional <ServerInfoWrapper > serverInfoWrapper = getServer (Config .MAIN_SERVER );
116-
117- if (serverInfoWrapper .isPresent ()) {
118- if (serverInfoWrapper .get ().isOnline ()) {
119- if (!isFirstRun .get () && !queueListener .isMainOnline ()) {
120- queueListener .setOnlineSince (Instant .now ());
102+ List <String > servers = new ArrayList <>(Config .KICK_WHEN_DOWN_SERVERS );
103+ CountDownLatch latch = new CountDownLatch (servers .size ());
104+ for (String server : servers ) {
105+ CompletableFuture .runAsync (() -> {
106+ try {
107+ Optional <ServerInfoWrapper > serverInfoWrapper = getServer (server );
108+
109+ if (serverInfoWrapper .isPresent ()) {
110+ if (serverInfoWrapper .get ().isOnline ()) {
111+ queueListener .getOnlineServers ().add (server );
112+ } else {
113+ warning (String .format ("Server %s is down!!!" , server ));
114+ queueListener .getOnlineServers ().remove (server );
115+ }
116+ } else {
117+ warning (String .format ("Server \" %s\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up" , server ));
118+ }
119+ } finally {
120+ latch .countDown ();
121121 }
122-
123- queueListener .setMainOnline (true );
124- } else {
125- warning ("Main Server is down!!!" );
126- queueListener .setMainOnline (false );
127- }
128- isFirstRun .set (false );
129- } else {
130- warning (String .format (message , "Main Server" , Config .MAIN_SERVER ));
131- }
132- }, 500 , Config .SERVER_ONLINE_CHECK_DELAY , TimeUnit .MILLISECONDS );
133-
134- schedule (() -> {
135- Optional <ServerInfoWrapper > serverInfoWrapper = getServer (Config .QUEUE_SERVER );
136-
137- if (serverInfoWrapper .isPresent ()) {
138- if (serverInfoWrapper .get ().isOnline ()) {
139- queueListener .setQueueOnline (true );
140- } else {
141- warning ("Queue Server is down!!!" );
142- queueListener .setQueueOnline (false );
143- }
144- } else {
145- warning (String .format (message , "Queue Server" , Config .QUEUE_SERVER ));
122+ });
146123 }
147- }, 500 , Config .SERVER_ONLINE_CHECK_DELAY , TimeUnit .MILLISECONDS );
148-
149- schedule (() -> {
150- if (Config .ENABLE_AUTH_SERVER ) {
151- Optional <ServerInfoWrapper > serverInfoWrapper = getServer (Config .AUTH_SERVER );
152-
153- if (serverInfoWrapper .isPresent ()) {
154- if (serverInfoWrapper .get ().isOnline ()) {
155- queueListener .setAuthOnline (true );
156- } else {
157- warning ("Auth Server is down!!!" );
158- queueListener .setAuthOnline (false );
159- }
160- } else {
161- warning (String .format (message , "Auth Server" , Config .AUTH_SERVER ));
162- }
163- } else {
164- queueListener .setAuthOnline (true );
124+ try {
125+ latch .await ();
126+ } catch (InterruptedException e ) {
127+ e .printStackTrace ();
165128 }
166129 }, 500 , Config .SERVER_ONLINE_CHECK_DELAY , TimeUnit .MILLISECONDS );
167130 }
168131
169- default void sendMessage (QueueType queue , boolean bool , MessageType type ) {
170- if (bool ) {
171- AtomicInteger position = new AtomicInteger ();
132+ default void sendMessage (QueueType queue , MessageType type ) {
133+ AtomicInteger position = new AtomicInteger ();
172134
173- for (Map .Entry <UUID , String > entry : new LinkedHashMap <>(queue .getQueueMap ()).entrySet ()) {
174- getPlayer (entry .getKey ()).ifPresent (player ->
175- player .sendMessage (type , Config .QUEUE_POSITION
176- .replace ("%position%" , String .valueOf (position .incrementAndGet ()))
177- .replace ("%total%" , String .valueOf (queue .getQueueMap ().size ()))));
178- }
135+ for (Map .Entry <UUID , String > entry : new LinkedHashMap <>(queue .getQueueMap ()).entrySet ()) {
136+ getPlayer (entry .getKey ()).ifPresent (player ->
137+ player .sendMessage (type , Config .QUEUE_POSITION
138+ .replace ("%position%" , String .valueOf (position .incrementAndGet ()))
139+ .replace ("%total%" , String .valueOf (queue .getQueueMap ().size ()))));
179140 }
180141 }
181142
@@ -194,39 +155,36 @@ default void updateTab(QueueType queue) {
194155 }
195156
196157 default String replacePosition (String text , int position , QueueType type ) {
197- if (type .getDurationToPosition ().containsKey (position )) {
198- Duration duration = type .getDurationToPosition ().get (position );
158+ if (type .getDurationFromPosition ().containsKey (position )) {
159+ Duration duration = type .getDurationFromPosition ().get (position );
199160
200161 return SharedChatUtils .formatDuration (text , duration , position );
201162 } else {
202- int biggestPosition = 0 ;
203- Duration biggestDuration = Duration .ZERO ;
204-
205- for (Map .Entry <Integer , Duration > entry : type .getDurationToPosition ().entrySet ()) {
206- int positionOfDuration = entry .getKey ();
207- if (positionOfDuration > biggestPosition ) {
208- biggestPosition = positionOfDuration ;
209- biggestDuration = entry .getValue ();
163+ Map .Entry <Integer , Duration > biggestEntry = null ;
164+
165+ for (Map .Entry <Integer , Duration > entry : type .getDurationFromPosition ().entrySet ()) {
166+ if (biggestEntry == null || entry .getKey () > biggestEntry .getKey ()) {
167+ biggestEntry = entry ;
210168 }
211169 }
212170
213- int difference = position - biggestPosition ;
214-
215- Duration imaginaryDuration = biggestDuration . plus (difference , ChronoUnit .MINUTES );
171+ Duration predictedDuration = biggestEntry == null ?
172+ Duration . of ( position , ChronoUnit . MINUTES ) :
173+ biggestEntry . getValue (). plus (position - biggestEntry . getKey () , ChronoUnit .MINUTES );
216174
217- return SharedChatUtils .formatDuration (text , imaginaryDuration , position );
175+ return SharedChatUtils .formatDuration (text , predictedDuration , position );
218176 }
219177 }
220178
221179 default void initializeReservationSlots () {
222180 schedule (() -> {
223- Optional <ServerInfoWrapper > mainServer = getServer (Config .MAIN_SERVER );
224- if (!mainServer .isPresent ())
181+ Optional <ServerInfoWrapper > targetServer = getServer (Config .TARGET_SERVER );
182+ if (!targetServer .isPresent ())
225183 return ;
226184
227185 Map <QueueType , AtomicInteger > map = new HashMap <>();
228186
229- for (PlayerWrapper player : mainServer .get ().getConnectedPlayers ()) {
187+ for (PlayerWrapper player : targetServer .get ().getConnectedPlayers ()) {
230188 QueueType playerType = QueueType .getQueueType (player ::hasPermission );
231189
232190 if (map .containsKey (playerType )) {
@@ -236,7 +194,7 @@ default void initializeReservationSlots() {
236194 }
237195 }
238196
239- map .forEach ((type , count ) -> type .getPlayersWithTypeInMain ().set (count .get ()));
197+ map .forEach ((type , count ) -> type .getPlayersWithTypeInTarget ().set (count .get ()));
240198 }, 0 , 1 , TimeUnit .SECONDS );
241199 }
242200
@@ -257,12 +215,12 @@ default void sendCustomData() {
257215 outOnlineQueue .writeInt (queueType .getQueueMap ().size ());
258216 }
259217
260- ByteArrayDataOutput outOnlineMain = ByteStreams .newDataOutput ();
218+ ByteArrayDataOutput outOnlineTarget = ByteStreams .newDataOutput ();
261219
262- outOnlineMain .writeUTF ("onlineMain " );
220+ outOnlineTarget .writeUTF ("onlineTarget " );
263221 outOnlineQueue .writeInt (Config .QUEUE_TYPES .length );
264222 for (QueueType queueType : Config .QUEUE_TYPES ) {
265- outOnlineQueue .writeInt (queueType .getPlayersWithTypeInMain ().get ());
223+ outOnlineQueue .writeInt (queueType .getPlayersWithTypeInTarget ().get ());
266224 }
267225
268226 Set <String > servers = new HashSet <>();
@@ -272,7 +230,7 @@ default void sendCustomData() {
272230 getServer (server ).ifPresent (serverInfoWrapper ->
273231 serverInfoWrapper .sendPluginMessage ("piston:queue" , outOnlineQueue .toByteArray ()));
274232 getServer (server ).ifPresent (serverInfoWrapper ->
275- serverInfoWrapper .sendPluginMessage ("piston:queue" , outOnlineMain .toByteArray ()));
233+ serverInfoWrapper .sendPluginMessage ("piston:queue" , outOnlineTarget .toByteArray ()));
276234 }
277235 }
278236
0 commit comments