From 371295d9fefeadb3a730dd3ebb51bf9cea302f5d Mon Sep 17 00:00:00 2001 From: Burning_TNT <88144530+burningtnt@users.noreply.github.com> Date: Mon, 10 Jul 2023 15:01:09 +0800 Subject: [PATCH] Fix #2372 (#2373) --- .../hmcl/ui/account/CreateAccountPane.java | 86 ++++++++++--------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java index febddb826..08e12ea71 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java @@ -224,49 +224,51 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware { additionalData = null; } - if (factory instanceof OfflineAccountFactory && username != null) { - if (!USERNAME_CHECKER_PATTERN.matcher(username).matches()) { - Controllers.confirm( - i18n("account.methods.offline.name.invalid"), i18n("message.warning"), - MessageDialogPane.MessageType.WARNING, - () -> { - logging.set(true); - deviceCode.set(null); + Runnable doCreate = () -> { + logging.set(true); + deviceCode.set(null); - loginTask = Task.supplyAsync(() -> factory.create(new DialogCharacterSelector(), username, password, null, additionalData)) - .whenComplete(Schedulers.javafx(), account -> { - int oldIndex = Accounts.getAccounts().indexOf(account); - if (oldIndex == -1) { - Accounts.getAccounts().add(account); - } else { - // adding an already-added account - // instead of discarding the new account, we first remove the existing one then add the new one - Accounts.getAccounts().remove(oldIndex); - Accounts.getAccounts().add(oldIndex, account); - } - - // select the new account - Accounts.setSelectedAccount(account); - - spinner.hideSpinner(); - fireEvent(new DialogCloseEvent()); - }, exception -> { - if (exception instanceof NoSelectedCharacterException) { - fireEvent(new DialogCloseEvent()); - } else { - lblErrorMessage.setText(Accounts.localizeErrorMessage(exception)); - } - body.setDisable(false); - spinner.hideSpinner(); - }).executor(true); - }, - () -> { - lblErrorMessage.setText(i18n("account.methods.offline.name.invalid")); - body.setDisable(false); - spinner.hideSpinner(); + loginTask = Task.supplyAsync(() -> factory.create(new DialogCharacterSelector(), username, password, null, additionalData)) + .whenComplete(Schedulers.javafx(), account -> { + int oldIndex = Accounts.getAccounts().indexOf(account); + if (oldIndex == -1) { + Accounts.getAccounts().add(account); + } else { + // adding an already-added account + // instead of discarding the new account, we first remove the existing one then add the new one + Accounts.getAccounts().remove(oldIndex); + Accounts.getAccounts().add(oldIndex, account); } - ); - } + + // select the new account + Accounts.setSelectedAccount(account); + + spinner.hideSpinner(); + fireEvent(new DialogCloseEvent()); + }, exception -> { + if (exception instanceof NoSelectedCharacterException) { + fireEvent(new DialogCloseEvent()); + } else { + lblErrorMessage.setText(Accounts.localizeErrorMessage(exception)); + } + body.setDisable(false); + spinner.hideSpinner(); + }).executor(true); + }; + + if (factory instanceof OfflineAccountFactory && username != null && !USERNAME_CHECKER_PATTERN.matcher(username).matches()) { + Controllers.confirm( + i18n("account.methods.offline.name.invalid"), i18n("message.warning"), + MessageDialogPane.MessageType.WARNING, + doCreate, + () -> { + lblErrorMessage.setText(i18n("account.methods.offline.name.invalid")); + body.setDisable(false); + spinner.hideSpinner(); + } + ); + } else { + doCreate.run(); } } @@ -370,7 +372,7 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware { private static class AccountDetailsInputPane extends GridPane { // ==== authlib-injector hyperlinks ==== - private static final String[] ALLOWED_LINKS = { "homepage", "register" }; + private static final String[] ALLOWED_LINKS = {"homepage", "register"}; private static List createHyperlinks(AuthlibInjectorServer server) { if (server == null) {