diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerClient.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerClient.java index a2bade37a..c754b2ee9 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerClient.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerClient.java @@ -54,11 +54,11 @@ public class MultiplayerClient extends Thread { setDaemon(true); } - public void setGamePort(int gamePort) { + public synchronized void setGamePort(int gamePort) { this.gamePort = gamePort; } - public int getGamePort() { + public synchronized int getGamePort() { return gamePort; } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java index b4f14ddb1..8899d38f3 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java @@ -19,6 +19,7 @@ package org.jackhuang.hmcl.ui.multiplayer; import com.google.gson.JsonParseException; import com.google.gson.annotations.SerializedName; +import javafx.application.Platform; import org.jackhuang.hmcl.Metadata; import org.jackhuang.hmcl.event.Event; import org.jackhuang.hmcl.event.EventManager; @@ -175,7 +176,7 @@ public final class MultiplayerManager { session.setClient(client); TimerTask task = Lang.setTimeout(() -> { - future.completeExceptionally(new JoinRequestTimeoutException()); + Platform.runLater(() -> future.completeExceptionally(new JoinRequestTimeoutException())); session.stop(); }, 30 * 1000); @@ -186,24 +187,30 @@ public final class MultiplayerManager { session.addRelatedThread(Lang.thread(new LocalServerBroadcaster(port, session), "LocalServerBroadcaster", true)); session.setName(connectedEvent.getSessionName()); client.setGamePort(port); - session.onExit.unregister(onExit); - future.complete(session); + Platform.runLater(() -> { + session.onExit.unregister(onExit); + future.complete(session); + }); } catch (IOException e) { - future.completeExceptionally(e); session.stop(); + Platform.runLater(() -> future.completeExceptionally(e)); } task.cancel(); }); client.onKicked().register(kickedEvent -> { - future.completeExceptionally(new KickedException(kickedEvent.getReason())); session.stop(); task.cancel(); + Platform.runLater(() -> { + future.completeExceptionally(new KickedException(kickedEvent.getReason())); + }); }); client.onDisconnected().register(disconnectedEvent -> { - if (!client.isConnected()) { - // We fail to establish connection with server - future.completeExceptionally(new ConnectionErrorException()); - } + Platform.runLater(() -> { + if (!client.isConnected()) { + // We fail to establish connection with server + future.completeExceptionally(new ConnectionErrorException()); + } + }); }); client.onHandshake().register(handshakeEvent -> { if (handler != null) { @@ -253,9 +260,11 @@ public final class MultiplayerManager { }, 15 * 1000); session.onPeerConnected.register(event -> { - session.onExit.unregister(onExit); - future.complete(session); peerConnectionTimeoutTask.cancel(); + Platform.runLater(() -> { + session.onExit.unregister(onExit); + future.complete(session); + }); }); return future; @@ -353,11 +362,11 @@ public final class MultiplayerManager { writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)); } - public MultiplayerClient getClient() { + public synchronized MultiplayerClient getClient() { return client; } - public CatoSession setClient(MultiplayerClient client) { + public synchronized CatoSession setClient(MultiplayerClient client) { this.client = client; return this; } @@ -412,11 +421,11 @@ public final class MultiplayerManager { return id != null; } - public String getName() { + public synchronized String getName() { return name; } - public void setName(String name) { + public synchronized void setName(String name) { this.name = name; } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java index be32c19f6..71be8d5a0 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java @@ -39,7 +39,7 @@ public class ManagedProcess { /** * Constructor. * - * @param process the raw system process that this instance manages. + * @param process the raw system process that this instance manages. * @param commands the command line of {@code process}. */ public ManagedProcess(Process process, List commands) { @@ -51,8 +51,8 @@ public class ManagedProcess { /** * Constructor. * - * @param process the raw system process that this instance manages. - * @param commands the command line of {@code process}. + * @param process the raw system process that this instance manages. + * @param commands the command line of {@code process}. * @param classpath the classpath of java process */ public ManagedProcess(Process process, List commands, String classpath) { @@ -111,11 +111,11 @@ public class ManagedProcess { /** * Add related thread. - * + *

* If a thread is monitoring this raw process, * you are required to add the instance by this method. */ - public void addRelatedThread(Thread thread) { + public synchronized void addRelatedThread(Thread thread) { relatedThreads.add(thread); } @@ -146,7 +146,7 @@ public class ManagedProcess { destroyRelatedThreads(); } - public void destroyRelatedThreads() { + public synchronized void destroyRelatedThreads() { relatedThreads.forEach(Thread::interrupt); }