Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static org.jackhuang.hmcl.setting.ConfigHolder.*;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;

public final class Controllers {
public static final String JAVA_VERSION_TIP = "javaVersion";
Expand Down
2 changes: 2 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/SVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public enum SVG {
WARNING("M1 21 12 2 23 21H1ZM4.45 19H19.55L12 6 4.45 19ZM12 18Q12.425 18 12.7125 17.7125T13 17Q13 16.575 12.7125 16.2875T12 16Q11.575 16 11.2875 16.2875T11 17Q11 17.425 11.2875 17.7125T12 18ZM11 15H13V10H11V15ZM12 12.5Z"),
WB_SUNNY("M11 4V1H13V4H11ZM11 23V20H13V23H11ZM20 13V11H23V13H20ZM1 13V11H4V13H1ZM18.7 6.7 17.3 5.3 19.05 3.5 20.5 4.95 18.7 6.7ZM4.95 20.5 3.5 19.05 5.3 17.3 6.7 18.7 4.95 20.5ZM19.05 20.5 17.3 18.7 18.7 17.3 20.5 19.05 19.05 20.5ZM5.3 6.7 3.5 4.95 4.95 3.5 6.7 5.3 5.3 6.7ZM12 18Q9.5 18 7.75 16.25T6 12Q6 9.5 7.75 7.75T12 6Q14.5 6 16.25 7.75T18 12Q18 14.5 16.25 16.25T12 18ZM12 16Q13.675 16 14.8375 14.8375T16 12Q16 10.325 14.8375 9.1625T12 8Q10.325 8 9.1625 9.1625T8 12Q8 13.675 9.1625 14.8375T12 16ZM12 12Z"),
WB_SUNNY_FILL("M11 4V1h2V4H11Zm0 19V20h2v3H11Zm9-10V11h3v2H20ZM1 13V11H4v2H1ZM18.7 6.7 17.3 5.3l1.75-1.8L20.5 4.95 18.7 6.7ZM4.95 20.5 3.5 19.05 5.3 17.3l1.4 1.4-1.75 1.8Zm14.1 0-1.75-1.8 1.4-1.4 1.8 1.75-1.45 1.45ZM5.3 6.7 3.5 4.95 4.95 3.5 6.7 5.3 5.3 6.7ZM12 18q-2.5 0-4.25-1.75T6 12 7.75 7.75 12 6t4.25 1.75T18 12t-1.75 4.25T12 18Z"),
SERVERS("M11 4V1H13V4H11ZM11 23V20H13V23H11ZM20 13V11H23V13H20ZM1 13V11H4V13H1ZM18.7 6.7 17.3 5.3 19.05 3.5 20.5 4.95 18.7 6.7ZM4.95 20.5 3.5 19.05 5.3 17.3 6.7 18.7 4.95 20.5ZM19.05 20.5 17.3 18.7 18.7 17.3 20.5 19.05 19.05 20.5ZM5.3 6.7 3.5 4.95 4.95 3.5 6.7 5.3 5.3 6.7ZM12 18Q9.5 18 7.75 16.25T6 12Q6 9.5 7.75 7.75T12 6Q14.5 6 16.25 7.75T18 12Q18 14.5 16.25 16.25T12 18ZM12 16Q13.675 16 14.8375 14.8375T16 12Q16 10.325 14.8375 9.1625T12 8Q10.325 8 9.1625 9.1625T8 12Q8 13.675 9.1625 14.8375T12 16ZM12 12Z"),
SERVERS_FILL("M11 4V1H13V4H11ZM11 23V20H13V23H11ZM20 13V11H23V13H20ZM1 13V11H4V13H1ZM18.7 6.7 17.3 5.3 19.05 3.5 20.5 4.95 18.7 6.7ZM4.95 20.5 3.5 19.05 5.3 17.3 6.7 18.7 4.95 20.5ZM19.05 20.5 17.3 18.7 18.7 17.3 20.5 19.05 19.05 20.5ZM5.3 6.7 3.5 4.95 4.95 3.5 6.7 5.3 5.3 6.7ZM12 18Q9.5 18 7.75 16.25T6 12Q6 9.5 7.75 7.75T12 6Q14.5 6 16.25 7.75T18 12Q18 14.5 16.25 16.25T12 18ZM12 16Q13.675 16 14.8375 14.8375T16 12Q16 10.325 14.8375 9.1625T12 8Q10.325 8 9.1625 9.1625T8 12Q8 13.675 9.1625 14.8375T12 16ZM12 12Z"),
;

public static final double DEFAULT_SIZE = 24;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.jackhuang.hmcl.ui.construct.*;
import org.jackhuang.hmcl.ui.decorator.DecoratorAnimatedPage;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.ui.versions.server.ServerListPage;
import org.jackhuang.hmcl.util.io.FileUtils;

import java.util.Optional;
Expand All @@ -56,6 +57,7 @@ public class VersionPage extends DecoratorAnimatedPage implements DecoratorPage
private final TabHeader.Tab<ModListPage> modListTab = new TabHeader.Tab<>("modListTab");
private final TabHeader.Tab<WorldListPage> worldListTab = new TabHeader.Tab<>("worldList");
private final TabHeader.Tab<SchematicsPage> schematicsTab = new TabHeader.Tab<>("schematicsTab");
private final TabHeader.Tab<ServerListPage> serverListTab = new TabHeader.Tab<>("serverListTab");
private final TabHeader.Tab<ResourcepackListPage> resourcePackTab = new TabHeader.Tab<>("resourcePackTab");
private final TransitionPane transitionPane = new TransitionPane();
private final BooleanProperty currentVersionUpgradable = new SimpleBooleanProperty();
Expand All @@ -71,8 +73,9 @@ public VersionPage() {
resourcePackTab.setNodeSupplier(loadVersionFor(ResourcepackListPage::new));
worldListTab.setNodeSupplier(loadVersionFor(WorldListPage::new));
schematicsTab.setNodeSupplier(loadVersionFor(SchematicsPage::new));
serverListTab.setNodeSupplier(loadVersionFor(ServerListPage::new));

tab = new TabHeader(transitionPane, versionSettingsTab, installerListTab, modListTab, resourcePackTab, worldListTab, schematicsTab);
tab = new TabHeader(transitionPane, versionSettingsTab, installerListTab, modListTab, resourcePackTab, worldListTab, schematicsTab, serverListTab);
tab.select(versionSettingsTab);

addEventHandler(Navigator.NavigationEvent.NAVIGATED, this::onNavigated);
Expand Down Expand Up @@ -138,6 +141,8 @@ public void loadVersion(String version, Profile profile) {
worldListTab.getNode().loadVersion(profile, version);
if (schematicsTab.isInitialized())
schematicsTab.getNode().loadVersion(profile, version);
if (serverListTab.isInitialized())
serverListTab.getNode().loadVersion(profile, version);
currentVersionUpgradable.set(profile.getRepository().isModpack(version));
}

Expand Down Expand Up @@ -244,7 +249,8 @@ protected Skin(VersionPage control) {
.addNavigationDrawerTab(control.tab, control.modListTab, i18n("mods.manage"), SVG.EXTENSION, SVG.EXTENSION_FILL)
.addNavigationDrawerTab(control.tab, control.resourcePackTab, i18n("resourcepack.manage"), SVG.TEXTURE)
.addNavigationDrawerTab(control.tab, control.worldListTab, i18n("world.manage"), SVG.PUBLIC)
.addNavigationDrawerTab(control.tab, control.schematicsTab, i18n("schematics.manage"), SVG.SCHEMA, SVG.SCHEMA_FILL);
.addNavigationDrawerTab(control.tab, control.schematicsTab, i18n("schematics.manage"), SVG.SCHEMA, SVG.SCHEMA_FILL)
.addNavigationDrawerTab(control.tab, control.serverListTab, i18n("servers.manage"), SVG.SERVERS, SVG.SERVERS_FILL);
VBox.setVgrow(sideBar, Priority.ALWAYS);

PopupMenu browseList = new PopupMenu();
Expand Down
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;
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);
}
}
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()));
}

RipplerContainer container = new RipplerContainer(root);
getChildren().setAll(container);
}
}

}
Loading
Loading