diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java index c35783ed6..9dd6b67eb 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java @@ -21,7 +21,10 @@ import com.jfoenix.concurrency.JFXUtilities; import com.jfoenix.controls.*; import javafx.beans.binding.Bindings; +import javafx.beans.property.ListProperty; import javafx.beans.property.ReadOnlyObjectProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.geometry.Pos; import javafx.scene.control.Hyperlink; @@ -45,11 +48,15 @@ import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.construct.*; import org.jackhuang.hmcl.util.Logging; +import org.jackhuang.hmcl.util.javafx.MultiStepBinding; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; +import static java.util.Collections.unmodifiableList; import static java.util.Objects.requireNonNull; import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.ui.FXUtils.*; @@ -64,12 +71,13 @@ public class AddAccountPane extends StackPane { @FXML private JFXComboBox> cboType; @FXML private JFXComboBox cboServers; @FXML private Label lblInjectorServer; - @FXML private Hyperlink linkManageInjectorServers; - @FXML private JFXDialogLayout layout; @FXML private JFXButton btnAccept; @FXML private JFXButton btnAddServer; @FXML private JFXButton btnManageServer; @FXML private SpinnerPane acceptPane; + @FXML private HBox linksContainer; + + private ListProperty links = new SimpleListProperty<>();; public AddAccountPane() { FXUtils.loadFXML(this, "/assets/fxml/account-add.fxml"); @@ -118,6 +126,30 @@ public class AddAccountPane extends StackPane { txtUsername.textProperty(), txtPassword.textProperty(), txtPassword.visibleProperty(), cboServers.getSelectionModel().selectedItemProperty(), cboServers.visibleProperty())); + + // authlib-injector links + links.bind(MultiStepBinding.of(cboServers.getSelectionModel().selectedItemProperty()) + .map(AddAccountPane::createHyperlinks) + .map(FXCollections::observableList)); + Bindings.bindContent(linksContainer.getChildren(), links); + linksContainer.visibleProperty().bind(cboServers.visibleProperty()); + } + + private static final String[] ALLOWED_LINKS = { "register" }; + + public static List createHyperlinks(AuthlibInjectorServer server) { + Map links = server.getLinks(); + List result = new ArrayList<>(); + for (String key : ALLOWED_LINKS) { + String value = links.get(key); + if (value != null) { + Hyperlink link = new Hyperlink(i18n("account.injector.link." + key)); + FXUtils.installSlowTooltip(link, value); + link.setOnAction(e -> FXUtils.openLink(value)); + result.add(link); + } + } + return unmodifiableList(result); } /** diff --git a/HMCL/src/main/resources/assets/fxml/account-add.fxml b/HMCL/src/main/resources/assets/fxml/account-add.fxml index 857a4c846..ccc5290b3 100644 --- a/HMCL/src/main/resources/assets/fxml/account-add.fxml +++ b/HMCL/src/main/resources/assets/fxml/account-add.fxml @@ -32,6 +32,7 @@ + diff --git a/HMCL/src/main/resources/assets/lang/I18N.properties b/HMCL/src/main/resources/assets/lang/I18N.properties index 5c2d448fe..6caad45cf 100644 --- a/HMCL/src/main/resources/assets/lang/I18N.properties +++ b/HMCL/src/main/resources/assets/lang/I18N.properties @@ -49,6 +49,7 @@ account.injector.empty=Empty (Click the plus button right to add) account.injector.manage=Manage authentication servers account.injector.manage.title=Authentication servers account.injector.http=Warning: This server is using HTTP, which will cause your password be transmitted in clear text. +account.injector.link.register=Register account.injector.server=Auth Server account.injector.server_url=Server URL account.injector.server_name=Server Name diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh.properties b/HMCL/src/main/resources/assets/lang/I18N_zh.properties index e448ffbb7..070e6a3ca 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh.properties @@ -48,6 +48,7 @@ account.injector.empty=無(點擊右側加號添加) account.injector.manage=管理認證伺服器 account.injector.manage.title=認證伺服器 account.injector.http=警告:此伺服器使用不安全的 HTTP 協議,您的密碼在登入時會被明文傳輸。 +account.injector.link.register=註冊 account.injector.server=認證伺服器 account.injector.server_url=伺服器位址 account.injector.server_name=伺服器名稱 diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties index 5c72e9066..864169851 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties @@ -48,6 +48,7 @@ account.injector.empty=无(点击右侧加号添加) account.injector.manage=管理认证服务器 account.injector.manage.title=认证服务器 account.injector.http=警告:此服务器使用不安全的 HTTP 协议,您的密码在登录时会被明文传输。 +account.injector.link.register=注册 account.injector.server=认证服务器 account.injector.server_url=服务器地址 account.injector.server_name=服务器名称 diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java index 8c48639eb..e3d9aff9b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java @@ -18,6 +18,7 @@ package org.jackhuang.hmcl.auth.authlibinjector; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyMap; import static org.jackhuang.hmcl.util.Lang.tryCast; import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.io.IOUtils.readFullyAsByteArray; @@ -29,6 +30,8 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Optional; import java.util.logging.Level; @@ -137,6 +140,7 @@ public class AuthlibInjectorServer implements Observable { @Nullable private transient String name; + private transient Map links = emptyMap(); private transient boolean metadataRefreshed; private transient ObservableHelper helper = new ObservableHelper(this); @@ -162,6 +166,10 @@ public class AuthlibInjectorServer implements Observable { .orElse(url); } + public Map getLinks() { + return links; + } + public String fetchMetadataResponse() throws IOException { if (metadataResponse == null || !metadataRefreshed) { refreshMetadata(); @@ -201,6 +209,16 @@ public class AuthlibInjectorServer implements Observable { this.name = metaObject.flatMap(meta -> tryCast(meta.get("serverName"), JsonPrimitive.class).map(JsonPrimitive::getAsString)) .orElse(null); + this.links = metaObject.flatMap(meta -> tryCast(meta.get("links"), JsonObject.class)) + .map(linksObject -> { + Map converted = new LinkedHashMap<>(); + linksObject.entrySet().forEach( + entry -> tryCast(entry.getValue(), JsonPrimitive.class).ifPresent(element -> { + converted.put(entry.getKey(), element.getAsString()); + })); + return converted; + }) + .orElse(emptyMap()); } }