Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
78db75d
feat: 现在复用WorldManagePage
Mine-diamond Mar 23, 2026
efdc263
feat: 优化只读模式实现
Mine-diamond Mar 23, 2026
0a2225e
feat: 修改单复数
Mine-diamond Mar 23, 2026
320a9f4
feat: 添加重命名文件功能(还有bug)
Mine-diamond Mar 24, 2026
ff993eb
feat: 修改世界时关闭锁
Mine-diamond Mar 24, 2026
72a1d76
feat: 修改世界锁管理机制
Mine-diamond Mar 24, 2026
e60d3ab
feat: 优化重命名世界功能
Mine-diamond Mar 24, 2026
ce3bb09
feat: 优化复制世界功能
Mine-diamond Mar 24, 2026
bedf2d9
feat: 现在快速启动不会退出世界管理窗口
Mine-diamond Mar 24, 2026
7b8ced5
feat: 优化锁机制
Mine-diamond Mar 24, 2026
5f80dc6
feat: 复制/重命名世界弹窗会将世界默认名称设为默认值
Mine-diamond Mar 24, 2026
bbbe526
feat: 复制世界弹窗当值为当前值时不再取消复制
Mine-diamond Mar 24, 2026
a7814fb
feat: 优化代码
Mine-diamond Mar 24, 2026
ed0eddf
feat: 优化代码
Mine-diamond Mar 24, 2026
00ddeb1
feat: 添加还原存档功能
Mine-diamond Mar 25, 2026
0a3fd64
fix: 修复还原世界位置错误的问题
Mine-diamond Mar 25, 2026
a3f5e2e
fix: 修复还原世界位置错误的问题
Mine-diamond Mar 25, 2026
5a128b6
feat: update
Mine-diamond Mar 25, 2026
5aed9d7
feat: 备份文件以倒序显示
Mine-diamond Mar 25, 2026
3e8a253
feat: 数据包不支持文本添加i18n
Mine-diamond Mar 25, 2026
792727b
fix: 修复世界解析错误后无法禁用的错误
Mine-diamond Mar 25, 2026
87e1970
feat: 优化代码
Mine-diamond Mar 25, 2026
db5704e
feat: 分离压缩包世界到ArchiveWorld中
Mine-diamond Mar 25, 2026
77e606c
feat: 优化恢复备份功能
Mine-diamond Mar 25, 2026
7f40758
fix: 修复复制世界逻辑的错误
Mine-diamond Mar 25, 2026
239eec0
fix: 修复复制/重命名世界时可能的错误
Mine-diamond Mar 25, 2026
b3631f0
fix: 世界锁错误/文件名修建错误
Mine-diamond Mar 25, 2026
91a5c7d
fix: 添加i18n
Mine-diamond Mar 25, 2026
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
8 changes: 8 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jackhuang.hmcl.ui.versions.GameListPage;
import org.jackhuang.hmcl.ui.versions.VersionPage;
import org.jackhuang.hmcl.ui.versions.Versions;
import org.jackhuang.hmcl.ui.versions.WorldManagePage;
import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.i18n.SupportedLocale;
Expand Down Expand Up @@ -123,6 +124,7 @@ public final class Controllers {
});
private static LauncherSettingsPage settingsPage;
private static Lazy<TerracottaPage> terracottaPage = new Lazy<>(TerracottaPage::new);
private static Lazy<WorldManagePage> worldManagePage = new Lazy<>(WorldManagePage::new);

private Controllers() {
}
Expand Down Expand Up @@ -203,6 +205,11 @@ public static Node getTerracottaPage() {
return terracottaPage.get();
}

// FXThread
public static WorldManagePage getWorldManagePage() {
return worldManagePage.get();
}

// FXThread
public static DecoratorController getDecorator() {
return decorator;
Expand Down Expand Up @@ -630,6 +637,7 @@ public static void shutdown() {
accountListPage = null;
settingsPage = null;
terracottaPage = null;
worldManagePage = null;
decorator = null;
stage = null;
scene = null;
Expand Down
158 changes: 158 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ArchiveWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2026 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;

import javafx.scene.image.Image;
import org.glavo.nbt.io.NBTCodec;
import org.glavo.nbt.tag.CompoundTag;
import org.glavo.nbt.tag.LongTag;
import org.glavo.nbt.tag.StringTag;
import org.glavo.nbt.tag.TagType;
import org.jackhuang.hmcl.game.World;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.Unzipper;
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.util.List;

import static org.jackhuang.hmcl.util.logging.Logger.LOG;

/// @author mineDiamond
public final class ArchiveWorld {
private final Path file;
private final String fileName;
private final boolean hasSubDir;
private String worldName;
private @Nullable GameVersionNumber gameVersion;
private @Nullable Image icon;

public ArchiveWorld(Path file) throws IOException {
if (Files.isRegularFile(file)) {
this.file = file;

try (FileSystem fs = CompressingUtils.readonly(this.file).setAutoDetectEncoding(true).build()) {
Path root;
if (Files.isRegularFile(fs.getPath("/level.dat"))) {
root = fs.getPath("/");
hasSubDir = false;
fileName = FileUtils.getName(this.file);
} else {
List<Path> files = Files.list(fs.getPath("/")).toList();
if (files.size() != 1 || !Files.isDirectory(files.get(0))) {
throw new IOException("Not a valid world zip file");
}

root = files.get(0);
hasSubDir = true;
fileName = FileUtils.getName(root);
}

Path levelDat = root.resolve("level.dat");
if (!Files.exists(levelDat)) { //version 20w14infinite
levelDat = root.resolve("special_level.dat");
}
if (!Files.exists(levelDat)) {
throw new IOException("Not a valid world zip file since level.dat or special_level.dat cannot be found.");
}
checkAndLoadLevelData(levelDat);

Path iconFile = root.resolve("icon.png");
if (Files.isRegularFile(iconFile)) {
try (InputStream inputStream = Files.newInputStream(iconFile)) {
icon = new Image(inputStream, 64, 64, true, false);
if (icon.isError())
throw icon.getException();
} catch (Exception e) {
LOG.warning("Failed to load world icon", e);
}
}
}
} else {
throw new IOException("Path " + file + " cannot be recognized as a archive Minecraft world");
}
}

private void checkAndLoadLevelData(Path levelDatPath) throws IOException {
CompoundTag levelData = NBTCodec.of().readTag(levelDatPath, TagType.COMPOUND);
if (!(levelData.get("Data") instanceof CompoundTag data))
throw new IOException("level.dat missing Data");

if (data.get("LevelName") instanceof StringTag levelNameTag) {
this.worldName = levelNameTag.getValue();
} else {
throw new IOException("level.dat missing LevelName");
}

if (data.get("Version") instanceof CompoundTag versionTag &&
versionTag.get("Name") instanceof StringTag nameTag) {
this.gameVersion = GameVersionNumber.asGameVersion(nameTag.getValue());
}

if (!(data.get("LastPlayed") instanceof LongTag))
throw new IOException("level.dat missing LastPlayed");
}

public Path getFile() {
return file;
}

public String getFileName() {
return fileName;
}

public boolean hasSubDir() {
return hasSubDir;
}

public String getWorldName() {
return worldName;
}

public @Nullable GameVersionNumber getGameVersion() {
return gameVersion;
}

public @Nullable Image getIcon() {
return icon;
}

public void install(Path savesDir, String name) throws IOException {
Path worldDir;
try {
worldDir = savesDir.resolve(name);
Comment on lines +140 to +142
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArchiveWorld.install resolves the destination directory directly from the user-provided name (savesDir.resolve(name)), which can still throw InvalidPathException or create illegal folder names on Windows. Given the PR goal of matching Minecraft’s behavior for invalid file names, consider using FileUtils.getSafeWorldFolderName(name) (and handling conflicts similarly to World.rename/copy) for the folder name while separately setting LevelName to the intended display name.

Suggested change
Path worldDir;
try {
worldDir = savesDir.resolve(name);
String folderName = FileUtils.getSafeWorldFolderName(name);
Path worldDir;
try {
worldDir = savesDir.resolve(folderName);

Copilot uses AI. Check for mistakes.
} catch (InvalidPathException e) {
throw new IOException(e);
}

if (Files.isDirectory(worldDir)) {
throw new FileAlreadyExistsException("World already exists");
}

if (hasSubDir) {
new Unzipper(file, worldDir).setSubDirectory("/" + fileName + "/").unzip();
} else {
new Unzipper(file, worldDir).unzip();
}
new World(worldDir).rename(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,12 @@
import static org.jackhuang.hmcl.util.logging.Logger.LOG;

public final class DatapackListPage extends ListPageBase<DatapackListPageSkin.DatapackInfoObject> implements WorldManagePage.WorldRefreshable {
private final World world;
private final Datapack datapack;
final BooleanProperty readOnly;
private final WorldManagePage worldManagePage;
private World world;
private Datapack datapack;

public DatapackListPage(WorldManagePage worldManagePage) {
world = worldManagePage.getWorld();
datapack = new Datapack(world.getFile().resolve("datapacks"));
setItems(MappedObservableList.create(datapack.getPacks(), DatapackListPageSkin.DatapackInfoObject::new));
readOnly = worldManagePage.readOnlyProperty();
this.worldManagePage = worldManagePage;
FXUtils.applyDragListener(this, it -> Objects.equals("zip", FileUtils.getExtension(it)),
this::installMultiDatapack, this::refresh);

Expand All @@ -62,7 +59,7 @@ public DatapackListPage(WorldManagePage worldManagePage) {

private void installMultiDatapack(List<Path> datapackPath) {
datapackPath.forEach(this::installSingleDatapack);
if (readOnly.get()) {
if (readOnlyProperty().get()) {
Controllers.showToast(i18n("datapack.reload.toast"));
}
}
Expand All @@ -80,13 +77,27 @@ protected Skin<?> createDefaultSkin() {
return new DatapackListPageSkin(this);
}

@Override
public void refresh() {
setLoading(true);
setFailedReason(null);
world = worldManagePage.getWorld();
if (!world.supportsDatapacks()) {
setFailedReason(i18n("datapack.not_support.info"));
setLoading(false);
return;
}
datapack = new Datapack(world.getFile().resolve("datapacks"));
setItems(MappedObservableList.create(datapack.getPacks(), DatapackListPageSkin.DatapackInfoObject::new));
Task.runAsync(datapack::loadFromDir)
.withRunAsync(Schedulers.javafx(), () -> setLoading(false))
.start();
}

public BooleanProperty readOnlyProperty() {
return worldManagePage.readOnlyProperty();
}

public void add() {
FileChooser chooser = new FileChooser();
chooser.setTitle(i18n("datapack.add.title"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ final class DatapackListPageSkin extends SkinBase<DatapackListPage> {
ComponentList root = new ComponentList();
root.getStyleClass().add("no-padding");
listView = new JFXListView<>();
filteredList = new FilteredList<>(skinnable.getItems());
filteredList = new FilteredList<>(skinnable.itemsProperty());

{
toolbarPane = new TransitionPane();
Expand All @@ -119,9 +119,9 @@ final class DatapackListPageSkin extends SkinBase<DatapackListPage> {
skinnable.enableSelected(listView.getSelectionModel().getSelectedItems()));
JFXButton disableButton = createToolbarButton2(i18n("mods.disable"), SVG.CLOSE, () ->
skinnable.disableSelected(listView.getSelectionModel().getSelectedItems()));
removeButton.disableProperty().bind(getSkinnable().readOnly);
enableButton.disableProperty().bind(getSkinnable().readOnly);
disableButton.disableProperty().bind(getSkinnable().readOnly);
removeButton.disableProperty().bind(getSkinnable().readOnlyProperty());
enableButton.disableProperty().bind(getSkinnable().readOnlyProperty());
disableButton.disableProperty().bind(getSkinnable().readOnlyProperty());

selectingToolbar.getChildren().addAll(
removeButton,
Expand Down Expand Up @@ -163,6 +163,7 @@ final class DatapackListPageSkin extends SkinBase<DatapackListPage> {

FXUtils.onChangeAndOperate(listView.getSelectionModel().selectedItemProperty(),
selectedItem -> isSelecting.set(selectedItem != null));
toolbarPane.disableProperty().bind(skinnable.loadingProperty().or(skinnable.failedReasonProperty().isNotNull()));
root.getContent().add(toolbarPane);

updateBarByStateWeakListener = FXUtils.observeWeak(() -> {
Expand All @@ -180,8 +181,9 @@ final class DatapackListPageSkin extends SkinBase<DatapackListPage> {
SpinnerPane center = new SpinnerPane();
ComponentList.setVgrow(center, Priority.ALWAYS);
center.loadingProperty().bind(skinnable.loadingProperty());
center.failedReasonProperty().bind(skinnable.failedReasonProperty());

listView.setCellFactory(x -> new DatapackInfoListCell(listView, getSkinnable().readOnly));
listView.setCellFactory(x -> new DatapackInfoListCell(listView, getSkinnable().readOnlyProperty()));
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
this.listView.setItems(filteredList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
Expand All @@ -37,17 +36,15 @@ public final class WorldBackupTask extends Task<Path> {

private final World world;
private final Path backupsDir;
private final boolean needLock;

public WorldBackupTask(World world, Path backupsDir, boolean needLock) {
public WorldBackupTask(World world, Path backupsDir) {
this.world = world;
this.backupsDir = backupsDir;
this.needLock = needLock;
}

@Override
public void execute() throws Exception {
try (FileChannel lockChannel = needLock ? world.lock() : null) {
try (World.WorldLock.Guard guard = world.getWorldLock().guard()) {
Files.createDirectories(backupsDir);
String time = LocalDateTime.now().format(WorldBackupsPage.TIME_FORMATTER);
String baseName = time + "_" + world.getFileName();
Expand Down
Loading