-
Notifications
You must be signed in to change notification settings - Fork 818
添加服务器管理界面 #4962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CaaMoe
wants to merge
16
commits into
HMCL-dev:main
Choose a base branch
from
CaaMoe:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
添加服务器管理界面 #4962
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a8fb362
添加服务器管理界面
CaaMoe c9150e4
添加服务器管理相关的国际化支持
CaaMoe bd2dfee
更新版权年份至2025
CaaMoe a812c50
调整SERVERS、SERVERS_FILL在SVG列表中的顺序
CaaMoe 2e1ceb8
为服务器列表项添加右键点击弹出菜单的功能
CaaMoe cc47863
调整服务器管理界面标
CaaMoe 75cf527
参参考 FXUtils 以替代直接使用 ImageIO 加载图片
CaaMoe e11a9cf
调整服务器管理界面标
CaaMoe 93f38ef
调整服务器管理界面中的一些翻译
CaaMoe 0d0be67
调整 servers.show_hide 翻译
CaaMoe a08e194
调整 servers.manage.copy.to.instance 繁体翻译内容
CaaMoe d98c8da
补充遗漏的翻译 server.tag.hide
CaaMoe 8d139d4
修复多实例运行路径一致会导致列表服务器项重复的问题
CaaMoe 95e8905
style
CaaMoe b7a8644
添加对服务器数据的持有实例支持,更新相关翻译
CaaMoe d021e92
额外显示其他持有实例 tag
CaaMoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/server/ServerData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2021 huangyuhui <huanghongxun2008@126.com> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.jackhuang.hmcl.ui.versions.server; | ||
|
|
||
| import com.github.steveice10.opennbt.tag.builtin.ByteTag; | ||
| import com.github.steveice10.opennbt.tag.builtin.CompoundTag; | ||
| import com.github.steveice10.opennbt.tag.builtin.StringTag; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
| import java.awt.*; | ||
| import java.awt.image.BufferedImage; | ||
CaaMoe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.jackhuang.hmcl.util.logging.Logger.LOG; | ||
|
|
||
| public record ServerData( | ||
| boolean acceptTextures, | ||
| boolean hidden, | ||
| BufferedImage icon, | ||
| String ip, | ||
| String name | ||
| ) { | ||
|
|
||
| public static ServerData fromStorage(Map<Object, Object> storage) { | ||
| return new ServerData( | ||
| Boolean.TRUE.equals(storage.get("acceptTextures")), | ||
| Boolean.TRUE.equals(storage.get("hidden")), | ||
| storage.get("icon") instanceof String s ? parseImage(s) : null, | ||
| storage.get("ip") instanceof String s ? s : null, | ||
| storage.get("name") instanceof String s ? s : null | ||
| ); | ||
| } | ||
|
|
||
| public static ServerData fromCompoundTag(CompoundTag tag) { | ||
| return new ServerData( | ||
| tag.get("acceptTextures") instanceof ByteTag bt && bt.getValue() != 0, | ||
| tag.get("hidden") instanceof ByteTag bt && bt.getValue() != 0, | ||
| tag.get("icon") instanceof StringTag st ? parseImage(st.getValue()) : null, | ||
| tag.get("ip") instanceof StringTag st ? st.getValue() : null, | ||
| tag.get("name") instanceof StringTag st ? st.getValue() : null | ||
| ); | ||
| } | ||
|
|
||
| public static BufferedImage parseImage(String imageBase64String) { | ||
| if (imageBase64String == null || imageBase64String.isEmpty()) { | ||
| return null; | ||
| } | ||
| try (ByteArrayInputStream bais = new ByteArrayInputStream(Base64.getDecoder().decode(imageBase64String))) { | ||
| return ImageIO.read(bais); | ||
| } catch (IOException e) { | ||
| LOG.warning("Failed to decode server icon", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static String encodeImage(Image image) { | ||
| if (image == null) { | ||
| return null; | ||
| } | ||
| try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
| ImageIO.write((BufferedImage) image, "PNG", baos); | ||
| return Base64.getEncoder().encodeToString(baos.toByteArray()); | ||
| } catch (IOException e) { | ||
| LOG.warning("Failed to encode server icon", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static ServerData createServerData(String name, String ip) { | ||
| return new ServerData(false, false, null, ip, name); | ||
| } | ||
|
|
||
| public void writeToCompoundTag(CompoundTag tag) { | ||
| tag.put(new ByteTag("acceptTextures", (byte) (acceptTextures ? 1 : 0))); | ||
| tag.put(new ByteTag("hidden", (byte) (hidden ? 1 : 0))); | ||
| if (icon != null) tag.put(new StringTag("icon", encodeImage(icon))); | ||
| if (ip != null) tag.put(new StringTag("ip", ip)); | ||
| if (name != null) tag.put(new StringTag("name", name)); | ||
| } | ||
|
|
||
| public Map<Object, Object> toSerializeMap() { | ||
| Map<Object, Object> map = new HashMap<>(); | ||
| map.put("acceptTextures", acceptTextures); | ||
| if (icon != null) map.put("icon", encodeImage(icon)); | ||
| if (ip != null) map.put("ip", ip); | ||
| if (name != null) map.put("name", name); | ||
| return map; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| ServerData that = (ServerData) o; | ||
| return Objects.equals(ip, that.ip) && Objects.equals(name, that.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(ip, name); | ||
| } | ||
| } | ||
160 changes: 160 additions & 0 deletions
160
HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/server/ServerListItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2021 huangyuhui <huanghongxun2008@126.com> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.jackhuang.hmcl.ui.versions.server; | ||
|
|
||
| import com.jfoenix.controls.JFXButton; | ||
| import com.jfoenix.controls.JFXPopup; | ||
| import javafx.geometry.Insets; | ||
| import javafx.geometry.Pos; | ||
| import javafx.scene.control.Control; | ||
| import javafx.scene.control.Skin; | ||
| import javafx.scene.control.SkinBase; | ||
| import javafx.scene.image.ImageView; | ||
| import javafx.scene.layout.BorderPane; | ||
| import javafx.scene.layout.HBox; | ||
| import javafx.scene.layout.StackPane; | ||
| import org.jackhuang.hmcl.setting.Profile; | ||
| import org.jackhuang.hmcl.task.Schedulers; | ||
| import org.jackhuang.hmcl.task.Task; | ||
| import org.jackhuang.hmcl.ui.Controllers; | ||
| import org.jackhuang.hmcl.ui.FXUtils; | ||
| import org.jackhuang.hmcl.ui.SVG; | ||
| import org.jackhuang.hmcl.ui.construct.*; | ||
| import org.jackhuang.hmcl.util.SwingFXUtils; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| import static org.jackhuang.hmcl.ui.FXUtils.determineOptimalPopupPosition; | ||
| import static org.jackhuang.hmcl.ui.versions.server.ServerListPage.readServersFromDat; | ||
| import static org.jackhuang.hmcl.ui.versions.server.ServerListPage.saveServerToDat; | ||
| import static org.jackhuang.hmcl.util.StringUtils.parseColorEscapes; | ||
| import static org.jackhuang.hmcl.util.i18n.I18n.i18n; | ||
|
|
||
| public class ServerListItem extends Control { | ||
| final Profile ownerProfile; | ||
| final String ownerProfileID; | ||
| final ServerData serverData; | ||
| private final ServerListPage parent; | ||
|
|
||
| public ServerListItem(ServerListPage parent, Profile ownerProfile, String ownerProfileID, ServerData serverData) { | ||
| this.ownerProfile = ownerProfile; | ||
| this.ownerProfileID = ownerProfileID; | ||
| this.serverData = serverData; | ||
| this.parent = parent; | ||
| } | ||
|
|
||
| @Override | ||
| protected Skin<?> createDefaultSkin() { | ||
| return new ServerListItemSkin(); | ||
| } | ||
|
|
||
| public void showPopupMenu(JFXPopup.PopupHPosition hPosition, double initOffsetX, double initOffsetY) { | ||
| PopupMenu popupMenu = new PopupMenu(); | ||
| JFXPopup popup = new JFXPopup(popupMenu); | ||
|
|
||
| IconedMenuItem copyToInstance = new IconedMenuItem(SVG.CONTENT_COPY, i18n("servers.manage.copy.to.instance"), () -> { | ||
| Task.runAsync(() -> { | ||
| Path datFilePath = parent.profile.getRepository().getServersDatFilePath(parent.version); | ||
| List<ServerData> dataList = readServersFromDat(datFilePath); | ||
| dataList.add(serverData); | ||
| saveServerToDat(datFilePath, dataList); | ||
| Task.runAsync(Schedulers.javafx(), parent::refresh).start(); | ||
| }).start(); | ||
| }, popup); | ||
|
|
||
| copyToInstance.setDisable(ownerProfile.equals(parent.profile) && ownerProfileID.equals(parent.version)); | ||
| popupMenu.getContent().addAll( | ||
| new IconedMenuItem(SVG.CONTENT_COPY, i18n("servers.manage.copy.server.ip"), () -> | ||
| FXUtils.copyText(serverData.ip(), i18n("servers.manage.copy.server.ip.ok.toast")), popup), | ||
| copyToInstance, | ||
| new MenuSeparator(), | ||
| new IconedMenuItem(SVG.DELETE, i18n("servers.manage.delete"), this::delete, popup) | ||
| ); | ||
|
|
||
|
|
||
| JFXPopup.PopupVPosition vPosition = determineOptimalPopupPosition(this, popup); | ||
| popup.show(this, vPosition, hPosition, initOffsetX, vPosition == JFXPopup.PopupVPosition.TOP ? initOffsetY : -initOffsetY); | ||
| } | ||
|
|
||
| public void delete() { | ||
| Controllers.confirm( | ||
| i18n("button.remove.confirm"), | ||
| i18n("server.delete"), | ||
| () -> Task.runAsync(() -> { | ||
| Path datFilePath = ownerProfile.getRepository().getServersDatFilePath(ownerProfileID); | ||
| List<ServerData> dataList = readServersFromDat(datFilePath); | ||
| dataList.remove(serverData); | ||
| saveServerToDat(datFilePath, dataList); | ||
| Task.runAsync(Schedulers.javafx(), parent::refresh).start(); | ||
| }).start(), | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| private class ServerListItemSkin extends SkinBase<ServerListItem> { | ||
| protected ServerListItemSkin() { | ||
| super(ServerListItem.this); | ||
|
|
||
| BorderPane root = new BorderPane(); | ||
| root.getStyleClass().add("md-list-cell"); | ||
| root.setPadding(new Insets(8)); | ||
|
|
||
| { | ||
| StackPane left = new StackPane(); | ||
| root.setLeft(left); | ||
| left.setPadding(new Insets(0, 8, 0, 0)); | ||
|
|
||
| ImageView imageView = new ImageView(); | ||
| left.getChildren().add(imageView); | ||
| FXUtils.limitSize(imageView, 32, 32); | ||
| imageView.setImage(serverData.icon() == null ? FXUtils.newBuiltinImage("/assets/img/unknown_server.png") : SwingFXUtils.toFXImage(serverData.icon(), null)); | ||
| } | ||
|
|
||
| { | ||
| TwoLineListItem item = new TwoLineListItem(); | ||
| root.setCenter(item); | ||
| item.setMouseTransparent(true); | ||
| if (serverData.name() != null) | ||
| item.setTitle(parseColorEscapes(serverData.name())); | ||
| item.setSubtitle(serverData.ip()); | ||
| item.addTag(ownerProfileID); | ||
| if (serverData.hidden()) { | ||
| item.addTag(i18n("server.tag.hide")); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| { | ||
| HBox right = new HBox(8); | ||
| root.setRight(right); | ||
| right.setAlignment(Pos.CENTER_RIGHT); | ||
|
|
||
| JFXButton btnMore = new JFXButton(); | ||
| right.getChildren().add(btnMore); | ||
| btnMore.getStyleClass().add("toggle-icon4"); | ||
| btnMore.setGraphic(SVG.MORE_VERT.createIcon()); | ||
| btnMore.setOnAction(event -> showPopupMenu(JFXPopup.PopupHPosition.RIGHT, 0, root.getHeight())); | ||
CaaMoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| RipplerContainer container = new RipplerContainer(root); | ||
| getChildren().setAll(container); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.