77import net .md_5 .bungee .api .config .ServerInfo ;
88import net .md_5 .bungee .api .connection .ProxiedPlayer ;
99import net .md_5 .bungee .api .connection .Server ;
10+ import net .md_5 .bungee .api .scheduler .ScheduledTask ;
1011
1112import java .util .*;
13+ import java .util .concurrent .TimeUnit ;
1214import java .util .concurrent .atomic .AtomicBoolean ;
1315import java .util .regex .Matcher ;
1416import java .util .regex .Pattern ;
@@ -18,6 +20,7 @@ public class SectionManager {
1820 private final PlayerBalancer plugin ;
1921 private final BalancerProps props ;
2022 private ServerSection principal ;
23+ private ScheduledTask refreshTask ;
2124
2225 private final Map <String , ServerSection > sections = Collections .synchronizedMap (new HashMap <>());
2326 private final Map <ServerInfo , ServerSection > servers = Collections .synchronizedMap (new HashMap <>());
@@ -36,6 +39,19 @@ public void load() throws RuntimeException {
3639 stage .execute ();
3740 }
3841
42+ if (plugin .getSettings ().getServerRefreshProps ().isEnabled ()) {
43+ refreshTask = plugin .getProxy ().getScheduler ().schedule (plugin , () -> {
44+ props .getSectionProps ().forEach ((name , props ) -> {
45+ ServerSection section = sections .get (name );
46+ calculateServers (section );
47+ });
48+ },
49+ plugin .getSettings ().getServerRefreshProps ().getDelay (),
50+ plugin .getSettings ().getServerRefreshProps ().getInterval (),
51+ TimeUnit .MILLISECONDS
52+ );
53+ }
54+
3955 long ending = System .currentTimeMillis () - starting ;
4056 plugin .getLogger ().info (String .format ("A total of %s section(s) have been loaded in %sms" , sections .size (), ending ));
4157 }
@@ -56,6 +72,11 @@ public void flush() {
5672 }
5773 });
5874
75+ if (refreshTask != null ) {
76+ refreshTask .cancel ();
77+ refreshTask = null ;
78+ }
79+
5980 principal = null ;
6081 sections .clear ();
6182 servers .clear ();
@@ -195,7 +216,7 @@ public void execute(String sectionName, SectionProps sectionProps, ServerSection
195216 new SectionStage ("Resolving servers" ) {
196217 @ Override
197218 public void execute (String sectionName , SectionProps sectionProps , ServerSection section ) throws RuntimeException {
198- section . getServers (). addAll ( calculateServers (section ) );
219+ calculateServers (section );
199220 }
200221 },
201222 new SectionStage ("Section server processing" ) {
@@ -228,34 +249,43 @@ public void execute(String sectionName, SectionProps sectionProps, ServerSection
228249 },
229250 };
230251
231- public Set < ServerInfo > calculateServers (ServerSection section ) {
252+ public void calculateServers (ServerSection section ) {
232253 Set <ServerInfo > results = new HashSet <>();
233254
234255 section .getProps ().getServerEntries ().forEach (entry -> {
235256 Pattern pattern = Pattern .compile (entry );
236- AtomicBoolean matches = new AtomicBoolean (false );
237257 plugin .getProxy ().getServers ().forEach ((name , server ) -> {
238258 Matcher matcher = pattern .matcher (name );
239259 if (matcher .matches ()) {
240- plugin .getLogger ().info (String .format ("Found a match with \" %s\" for entry \" %s\" " , name , entry ));
241260 results .add (server );
242- register (server , section );
243- matches .set (true );
244261 }
245262 });
263+ });
246264
247- if (!matches .get ()) {
248- plugin .getLogger ().warning (String .format ("Could not match any servers with the entry \" %s\" " , entry ));
265+ section .getServers ().forEach (server -> {
266+ if (!results .contains (server )) {
267+ servers .remove (server );
268+ plugin .getLogger ().info (String .format ("Removed the server %s from %s as it does no longer exist" ,
269+ server .getName (), section .getName ()
270+ ));
249271 }
250272 });
251273
252- plugin .getLogger ().info (String .format ("Recognized %s server(s) out of %s in the section \" %s\" " ,
274+ results .forEach (server -> {
275+ if (!section .getServers ().contains (server )) {
276+ section .getServers ().add (server );
277+ register (server , section );
278+ plugin .getLogger ().info (String .format ("Added the server %s to %s" ,
279+ server .getName (), section .getName ()
280+ ));
281+ }
282+ });
283+
284+ plugin .getLogger ().info (String .format ("Recognized %s server%s in the section \" %s\" " ,
253285 results .size (),
254- section . getProps (). getServerEntries () ,
286+ results . size () != 1 ? "s" : "" ,
255287 section .getName ()
256288 ));
257-
258- return results ;
259289 }
260290
261291 public int calculatePosition (ServerSection section ) {
0 commit comments