3131import java .util .Map ;
3232import java .util .Map .Entry ;
3333import java .util .Set ;
34+ import java .util .concurrent .ExecutionException ;
35+ import java .util .concurrent .FutureTask ;
3436
3537//TODO document this!
3638public class JoinServer extends VariableStepLoadProcess {
@@ -41,7 +43,7 @@ public class JoinServer extends VariableStepLoadProcess {
4143 private GameManifest gameManifest ;
4244 private JoinStatus joinStatus ;
4345
44- private Thread applyModuleThread ;
46+ private FutureTask < Context > applyModuleTask ;
4547 private ModuleEnvironment oldEnvironment ;
4648
4749 public JoinServer (Context context , GameManifest gameManifest , JoinStatus joinStatus ) {
@@ -53,7 +55,7 @@ public JoinServer(Context context, GameManifest gameManifest, JoinStatus joinSta
5355
5456 @ Override
5557 public String getMessage () {
56- if (applyModuleThread != null ) {
58+ if (applyModuleTask != null ) {
5759 return "${engine:menu#scanning-for-assets}" ;
5860 } else {
5961 return joinStatus .getCurrentActivity ();
@@ -62,8 +64,21 @@ public String getMessage() {
6264
6365 @ Override
6466 public boolean step () {
65- if (applyModuleThread != null ) {
66- if (!applyModuleThread .isAlive ()) {
67+ if (applyModuleTask != null ) {
68+ if (applyModuleTask .isDone ()) {
69+ try {
70+ Context gameContext = applyModuleTask .get ();
71+ CoreRegistry .setContext (gameContext );
72+ } catch (ExecutionException e ) {
73+ logger .error ("Failed to apply game environment" , e .getCause ());
74+ StateMainMenu mainMenu = new StateMainMenu ("Failed to apply game environment: " + e .getCause ().getMessage ());
75+ context .get (GameEngine .class ).changeState (mainMenu );
76+ networkSystem .shutdown ();
77+ return true ;
78+ } catch (InterruptedException e ) {
79+ Thread .currentThread ().interrupt ();
80+ return false ;
81+ }
6782 if (oldEnvironment != null ) {
6883 oldEnvironment .close ();
6984 }
@@ -120,15 +135,17 @@ public boolean step() {
120135 context .get (Game .class ).load (gameManifest );
121136
122137 EnvironmentSwitchHandler environmentSwitchHandler = context .get (EnvironmentSwitchHandler .class );
123- ContextImpl modulesContext = new ContextImpl (context ,
124- moduleManager .getEnvironment ().getBeans (BeanContext .class ).stream ().findFirst ().get ());
138+ BeanContext moduleBeanContext = moduleManager .getEnvironment ().getBeans (BeanContext .class ).stream ()
139+ .findFirst ()
140+ .orElseThrow (() -> new IllegalStateException (
141+ "Loaded module environment did not expose a BeanContext" ));
142+ ContextImpl modulesContext = new ContextImpl (context , moduleBeanContext );
125143 ServiceRegistry gameContextRegistry = new ServiceRegistry ();
126- applyModuleThread = new Thread (() -> {
144+ applyModuleTask = new FutureTask <> (() -> {
127145 environmentSwitchHandler .handleSwitchToGameEnvironment (modulesContext , gameContextRegistry );
128- Context gameContext = new ContextImpl (modulesContext , gameContextRegistry );
129- CoreRegistry .setContext (gameContext );
146+ return new ContextImpl (modulesContext , gameContextRegistry );
130147 });
131- applyModuleThread .start ();
148+ new Thread ( applyModuleTask , "apply-module-environment" ) .start ();
132149
133150 return false ;
134151 } else if (joinStatus .getStatus () == JoinStatus .Status .FAILED ) {
0 commit comments