11package com .jaimemartz .playerbalancer .section ;
22
3- import com .google .common .base .Preconditions ;
43import com .jaimemartz .playerbalancer .PlayerBalancer ;
54import com .jaimemartz .playerbalancer .settings .props .features .BalancerProps ;
65import com .jaimemartz .playerbalancer .settings .props .shared .SectionProps ;
@@ -20,9 +19,10 @@ public class SectionManager {
2019 private final PlayerBalancer plugin ;
2120 private final BalancerProps props ;
2221 private ScheduledTask updateTask ;
22+ private ServerSection principal ;
2323
24- private final Map <String , ServerSection > sections = new HashMap <>();
25- private final Map <ServerInfo , ServerSection > servers = new HashMap <>();
24+ private final Map <String , ServerSection > sections = Collections . synchronizedMap ( new HashMap <>() );
25+ private final Map <ServerInfo , ServerSection > servers = Collections . synchronizedMap ( new HashMap <>() );
2626
2727 public SectionManager (PlayerBalancer plugin ) {
2828 this .props = plugin .getSettings ().getBalancerProps ();
@@ -33,18 +33,16 @@ public void load() throws RuntimeException {
3333 plugin .getLogger ().info ("Executing section initialization stages, this may take a while..." );
3434 long starting = System .currentTimeMillis ();
3535
36- props .getSectionProps ().forEach ((name , prop ) -> {
37- plugin .getLogger ().info (String .format ("Construction of section with name \" %s\" " , name ));
38- ServerSection object = new ServerSection (name , prop );
39- sections .put (name , object );
40- });
41-
42- Preconditions .checkNotNull (this .getPrincipal (),
43- "Could not set principal section, there is no section named \" %s\" " ,
44- props .getPrincipalSectionName ()
45- );
46-
47- sections .forEach (this ::processSection );
36+ for (Stage stage : stages ) {
37+ plugin .getLogger ().info ("Executing stage \" " + stage .title + "\" " );
38+ if (stage instanceof SectionStage ) {
39+ props .getSectionProps ().forEach ((name , props ) -> {
40+ ((SectionStage ) stage ).execute (name , props , sections .get (name ));
41+ });
42+ } else {
43+ ((GlobalStage ) stage ).execute ();
44+ }
45+ }
4846
4947 long ending = System .currentTimeMillis () - starting ;
5048 plugin .getLogger ().info (String .format ("A total of %s section(s) have been loaded in %sms" , sections .size (), ending ));
@@ -71,6 +69,7 @@ public void flush() {
7169 updateTask = null ;
7270 }
7371
72+ principal = null ;
7473 sections .clear ();
7574 servers .clear ();
7675 }
@@ -121,67 +120,119 @@ public ServerSection getByPlayer(ProxiedPlayer player) {
121120 }
122121
123122 private final Stage [] stages = {
124- new Stage ( "Creation " ) {
123+ new SectionStage ( "Constructing sections " ) {
125124 @ Override
126- public void execute (SectionProps props , ServerSection section ) throws RuntimeException {
127-
125+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
126+ ServerSection object = new ServerSection (name , props );
127+ sections .put (name , object );
128128 }
129129 },
130- new Stage ( " " ) {
130+ new GlobalStage ( "Processing principal section " ) {
131131 @ Override
132- public void execute (SectionProps props , ServerSection section ) throws RuntimeException {
133-
132+ public void execute () {
133+ principal = sections .get (props .getPrincipalSectionName ());
134+ if (principal == null ) {
135+ throw new IllegalArgumentException (String .format (
136+ "Could not set principal section, there is no section called \" %s\" " ,
137+ props .getPrincipalSectionName ()
138+ ));
139+ }
140+ }
141+ },
142+ new SectionStage ("Processing parents" ) {
143+ @ Override
144+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
145+ if (props .getParentName () != null ) {
146+ ServerSection parent = getByName (props .getParentName ());
147+ if (principal .equals (section ) && parent == null ) {
148+ throw new IllegalArgumentException (String .format (
149+ "The section \" %s\" has an invalid parent set" ,
150+ section .getName ()
151+ ));
152+ } else {
153+ section .setParent (parent );
154+ }
155+ }
156+ }
157+ },
158+ new SectionStage ("Validating parents" ) {
159+ @ Override
160+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
161+ ServerSection parent = section .getParent ();
162+ if (parent != null && parent .getParent () == section ) {
163+ throw new IllegalStateException (String .format (
164+ "The sections \" %s\" and \" %s\" are parents of each other" ,
165+ section .getName (),
166+ parent .getName ()
167+ ));
168+ }
169+ }
170+ },
171+ new SectionStage ("Validating providers" ) {
172+ @ Override
173+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
174+ if (props .getProvider () == null ) {
175+ ServerSection current = section .getParent ();
176+ if (current != null ) {
177+ while (current .getExplicitProvider () == null ) {
178+ current = current .getParent ();
179+ }
180+
181+ plugin .getLogger ().info (String .format (
182+ "The section \" %s\" inherits the provider from the section \" %s\" " ,
183+ section .getName (),
184+ current .getName ()
185+ ));
186+
187+ section .setInherited (true );
188+ }
189+ } else {
190+ section .setInherited (false );
191+ }
192+ }
193+ },
194+ new SectionStage ("Calculating positions" ) {
195+ @ Override
196+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
197+ section .setPosition (calculatePosition (section ));
198+ }
199+ },
200+ new SectionStage ("Resolving servers" ) {
201+ @ Override
202+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
203+ section .setServers (calculateServers (section ));
204+ }
205+ },
206+ new SectionStage ("Section server processing" ) {
207+ @ Override
208+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
209+ if (props .getServerName () != null ) {
210+ FakeServer server = new FakeServer (section );
211+ section .setServer (server );
212+ plugin .getSectionManager ().register (server , section );
213+ FixedAdapter .getFakeServers ().put (server .getName (), server );
214+ plugin .getProxy ().getServers ().put (server .getName (), server );
215+ }
216+ }
217+ },
218+ new SectionStage ("Section command processing" ) {
219+ @ Override
220+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
221+ if (props .getCommand () != null ) {
222+ SectionCommand command = new SectionCommand (plugin , props .getCommand (), section );
223+ section .setCommand (command );
224+ plugin .getProxy ().getPluginManager ().registerCommand (plugin , command );
225+ }
226+ }
227+ },
228+ new SectionStage ("Finishing loading sections" ) {
229+ @ Override
230+ public void execute (String name , SectionProps props , ServerSection section ) throws RuntimeException {
231+ section .setValid (true );
134232 }
135233 }
136234 };
137235
138- public void processSection (String sectionName , ServerSection section ) throws RuntimeException {
139- plugin .getLogger ().info (String .format ("Loading section with name \" %s\" " , sectionName ));
140-
141- Optional .ofNullable (section .getProps ().getParentName ()).ifPresent (parentName -> {
142- ServerSection parent = getByName (parentName );
143- if (parent == null ) {
144- throw new IllegalArgumentException (String .format ("The section \" %s\" has an invalid parent set" , sectionName ));
145- } else {
146- section .setParent (parent );
147- }
148- });
149-
150- Optional .ofNullable (section .getParent ()).ifPresent (parent -> {
151- if (parent .getProps ().getParentName ().equals (sectionName )) {
152- throw new IllegalStateException (String .format ("The sections \" %s\" and \" %s\" are parents of each other" ,
153- sectionName ,
154- section .getParent ().getName ()
155- ));
156- }
157- });
158-
159- Set <ServerInfo > servers = calculateServers (section );
160- section .getServers ().addAll (servers );
161-
162- //TODO Load sections
163-
164- section .setPosition (calculatePosition (section ));
165-
166- Optional .ofNullable (section .getProps ().getServerName ()).ifPresent (serverName -> {
167- FakeServer server = new FakeServer (section );
168- section .setServer (server );
169-
170- plugin .getSectionManager ().register (server , section );
171- FixedAdapter .getFakeServers ().put (server .getName (), server );
172- plugin .getProxy ().getServers ().put (server .getName (), server );
173- });
174-
175- Optional .ofNullable (section .getProps ().getCommand ()).ifPresent (props -> {
176- SectionCommand command = new SectionCommand (plugin , props , section );
177- section .setCommand (command );
178-
179- plugin .getProxy ().getPluginManager ().registerCommand (plugin , command );
180- });
181-
182- section .setValid (true );
183- }
184-
185236 public Set <ServerInfo > calculateServers (ServerSection section ) {
186237 Set <ServerInfo > results = new HashSet <>();
187238
@@ -203,7 +254,7 @@ public Set<ServerInfo> calculateServers(ServerSection section) {
203254 }
204255 });
205256
206- plugin .getLogger ().info (String .format ("Recognized %s server(s) out of %s entries on the section \" %s\" " ,
257+ plugin .getLogger ().info (String .format ("Recognized %s server(s) out of %s in the section \" %s\" " ,
207258 servers .size (),
208259 section .getProps ().getServerEntries (),
209260 section .getName ()
@@ -213,7 +264,6 @@ public Set<ServerInfo> calculateServers(ServerSection section) {
213264 }
214265
215266 public int calculatePosition (ServerSection section ) {
216- ServerSection principal = this .getPrincipal ();
217267 ServerSection current = section ;
218268
219269 //Calculate above principal
@@ -244,6 +294,14 @@ public int calculatePosition(ServerSection section) {
244294 return iterations ;
245295 }
246296
297+ public ServerSection getPrincipal () {
298+ return principal ;
299+ }
300+
301+ public boolean isPrincipal (ServerSection section ) {
302+ return principal .equals (section );
303+ }
304+
247305 public boolean isDummy (ServerSection section ) {
248306 List <String > dummySections = props .getDummySectionNames ();
249307 return dummySections .contains (section .getName ());
@@ -260,31 +318,34 @@ public Optional<ServerSection> getBind(Map<String, String> rules, ServerSection
260318 return Optional .ofNullable (res );
261319 }
262320
263- //todo maybe store this as a variable?
264- public ServerSection getPrincipal () {
265- return getByName (props .getPrincipalSectionName ());
266- }
267-
268- public boolean isPrincipal (ServerSection section ) {
269- return getPrincipal ().equals (section );
270- }
271-
272321 public Map <String , ServerSection > getSections () {
273322 return sections ;
274323 }
275324
276- private abstract static class Stage {
325+ private static class Stage {
277326 private final String title ;
278327
279- public Stage (String title ) {
328+ private Stage (String title ) {
280329 this .title = title ;
281330 }
331+ }
332+
333+ private static abstract class GlobalStage extends Stage {
334+ private GlobalStage (String title ) {
335+ super (title );
336+ }
337+
338+ public abstract void execute (
339+ ) throws RuntimeException ;
340+ }
282341
283- public String getTitle () {
284- return title ;a
342+ private static abstract class SectionStage extends Stage {
343+ private SectionStage (String title ) {
344+ super (title );
285345 }
286346
287347 public abstract void execute (
348+ String name ,
288349 SectionProps props ,
289350 ServerSection section
290351 ) throws RuntimeException ;
0 commit comments