feat(multiplayer): auto rejection.

This commit is contained in:
huanghongxun
2021-10-10 02:02:44 +08:00
parent 32d557ee07
commit 6221f4b616
5 changed files with 23 additions and 17 deletions

View File

@@ -176,8 +176,11 @@ public final class MessageDialogPane extends StackPane {
@Override @Override
public void run() { public void run() {
if (timeout < 0) { if (timeout <= 0) {
cancel(); cancel();
runInFX(() -> {
cancelButton.fire();
});
return; return;
} }
timeout -= 1000; timeout -= 1000;

View File

@@ -46,6 +46,7 @@ import java.util.logging.Level;
import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig; import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig;
import static org.jackhuang.hmcl.ui.FXUtils.runInFX; import static org.jackhuang.hmcl.ui.FXUtils.runInFX;
import static org.jackhuang.hmcl.util.Lang.resolveException;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.Logging.LOG;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
@@ -215,7 +216,7 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
session.getServer().setOnClientAdding((client, resolveClient, rejectClient) -> { session.getServer().setOnClientAdding((client, resolveClient, rejectClient) -> {
runInFX(() -> { runInFX(() -> {
Controllers.dialog(new MessageDialogPane.Builder(i18n("multiplayer.session.create.join.prompt", client.getUsername()), i18n("multiplayer.session.create.join"), MessageDialogPane.MessageType.INFO) Controllers.dialog(new MessageDialogPane.Builder(i18n("multiplayer.session.create.join.prompt", client.getUsername()), i18n("multiplayer.session.create.join"), MessageDialogPane.MessageType.INFO)
.yesOrNo(resolveClient, () -> rejectClient.accept("")) .yesOrNo(resolveClient, () -> rejectClient.accept(i18n("multiplayer.session.join.wait_timeout")))
.cancelOnTimeout(30 * 1000) .cancelOnTimeout(30 * 1000)
.build()); .build());
}); });
@@ -314,20 +315,21 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
resolve.run(); resolve.run();
}, Platform::runLater) }, Platform::runLater)
.exceptionally(throwable -> { .exceptionally(throwable -> {
if (throwable instanceof CancellationException) { Throwable resolved = resolveException(throwable);
if (resolved instanceof CancellationException) {
LOG.info("Connection rejected by the server"); LOG.info("Connection rejected by the server");
reject.accept(i18n("multiplayer.session.join.rejected")); reject.accept(i18n("multiplayer.session.join.rejected"));
return null; return null;
} else if (throwable instanceof MultiplayerManager.CatoAlreadyStartedException) { } else if (resolved instanceof MultiplayerManager.CatoAlreadyStartedException) {
LOG.info("Cato already started"); LOG.info("Cato already started");
reject.accept(i18n("multiplayer.session.error.already_started")); reject.accept(i18n("multiplayer.session.error.already_started"));
return null; return null;
} else if (throwable instanceof MultiplayerManager.JoinRequestTimeoutException) { } else if (resolved instanceof MultiplayerManager.JoinRequestTimeoutException) {
LOG.info("Cato already started"); LOG.info("Cato already started");
reject.accept(i18n("multiplayer.session.join.wait_timeout")); reject.accept(i18n("multiplayer.session.join.wait_timeout"));
return null; return null;
} else { } else {
LOG.log(Level.WARNING, "Failed to join sessoin"); LOG.log(Level.WARNING, "Failed to join session", resolved);
reject.accept(i18n("multiplayer.session.join.error")); reject.accept(i18n("multiplayer.session.join.error"));
} }
return null; return null;
@@ -389,7 +391,9 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
} }
private void stopCatoSession() { private void stopCatoSession() {
getSession().stop(); if (getSession() != null) {
getSession().stop();
}
clearCatoSession(); clearCatoSession();
} }

View File

@@ -666,7 +666,7 @@ multiplayer.session.create.token.prompt=默认为临时 Token。你可以在 noi
multiplayer.session.error.already_started=本地已经开启 cato 服务,请检查是否有其他 HMCL 正在运行联机服务。或者你可以在任务管理器里杀掉 cato 进程以继续。 multiplayer.session.error.already_started=本地已经开启 cato 服务,请检查是否有其他 HMCL 正在运行联机服务。或者你可以在任务管理器里杀掉 cato 进程以继续。
multiplayer.session.expired=联机会话连续使用时间超过了 3 小时,你需要重新创建/加入房间以继续联机。 multiplayer.session.expired=联机会话连续使用时间超过了 3 小时,你需要重新创建/加入房间以继续联机。
multiplayer.session.join=加入房间 multiplayer.session.join=加入房间
multiplayer.session.join.error=加入房间失败 multiplayer.session.join.error=加入房间失败。如果你或对方的网络类型是差(对称型),可能无法使用联机功能。
multiplayer.session.join.hint=你需要向已经创建好房间的玩家索要邀请码以便加入多人联机房间 multiplayer.session.join.hint=你需要向已经创建好房间的玩家索要邀请码以便加入多人联机房间
multiplayer.session.join.invitation_code=邀请码 multiplayer.session.join.invitation_code=邀请码
multiplayer.session.join.invitation_code.error=邀请码不正确,请向开服玩家获取邀请码 multiplayer.session.join.invitation_code.error=邀请码不正确,请向开服玩家获取邀请码

View File

@@ -26,8 +26,7 @@ import java.util.Collections;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.Level; import java.util.logging.Level;
import static org.jackhuang.hmcl.util.Lang.rethrow; import static org.jackhuang.hmcl.util.Lang.*;
import static org.jackhuang.hmcl.util.Lang.wrap;
/** /**
* *
@@ -320,13 +319,6 @@ public final class AsyncTaskExecutor extends TaskExecutor {
} }
} }
private static Throwable resolveException(Throwable e) {
if (e instanceof ExecutionException || e instanceof CompletionException)
return resolveException(e.getCause());
else
return e;
}
private void checkCancellation() { private void checkCancellation() {
if (isCancelled()) { if (isCancelled()) {
throw new CancellationException("Cancelled by user"); throw new CancellationException("Cancelled by user");

View File

@@ -377,6 +377,13 @@ public final class Lang {
return task; return task;
} }
public static Throwable resolveException(Throwable e) {
if (e instanceof ExecutionException || e instanceof CompletionException)
return resolveException(e.getCause());
else
return e;
}
/** /**
* This is a useful function to prevent exceptions being eaten when using CompletableFuture. * This is a useful function to prevent exceptions being eaten when using CompletableFuture.
* You can write: * You can write: