Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
03754d3
refactor: 重构 DataPack.Pack
fishdry1000 Oct 14, 2026
18cac93
refactor: 重构 DataPack.Pack
fishdry1000 Oct 14, 2026
e4aa23f
fix: 修复回归问题
fishdry1000 Oct 14, 2026
d4e8f48
refactor: 重构 DataPack
fishdry1000 Oct 15, 2026
42d7b37
chore: 修复变量名
fishdry1000 Oct 15, 2026
76ea022
refactor: 重构 DataPack
fishdry1000 Oct 15, 2026
ef8b49d
chore: 简化代码
fishdry1000 Oct 15, 2026
4c9f6de
Merge remote-tracking branch 'upstream/main' into work/refactor-datap…
fishdry1000 Oct 16, 2026
262bb71
refactor: 重构 DataPack 为 DataPackManager (风味 LocalAddonManager,但不是),移动…
fishdry1000 Oct 17, 2026
9914131
fix: 修复隔离安装后刷新产生的解析异常
fishdry1000 Oct 20, 2026
929c1cd
fix: 保留批量安装对非法路径异常的隔离
fishdry1000 Oct 26, 2026
41be5e2
Merge branch 'main' into work/refactor-datapack-from-main
fishdry1000 Oct 26, 2026
10ebf56
fix: 修复未使用的 import
fishdry1000 Oct 26, 2026
bc68bc4
fix: 修复版权注释
fishdry1000 Oct 26, 2026
e38b154
Merge branch 'main' into work/refactor-datapack-from-main
fishdry1000 Nov 9, 2026
c37e815
Merge branch 'main' into work/refactor-datapack-from-main
fishdry1000 Nov 9, 2026
9730d7d
chore: 简化代码
fishdry1000 Nov 9, 2026
a3d38a0
fix: 使用 toStringSingleLine
fishdry1000 Nov 9, 2026
75fdf69
Merge remote-tracking branch 'upstream/main' into work/refactor-datap…
fishdry1000 Nov 16, 2026
b858d0b
Merge remote-tracking branch 'upstream/main' into work/refactor-datap…
fishdry1000 Nov 18, 2026
8374eff
fix: 使用上游图标加载逻辑
fishdry1000 Nov 18, 2026
e774ba5
Merge remote-tracking branch 'upstream/main' into work/refactor-datap…
fishdry1000 Nov 22, 2026
1758e70
chore: 修正 merge 引入的问题
fishdry1000 Nov 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.image.Image;
import javafx.scene.control.ListView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.util.Duration;
import org.jackhuang.hmcl.addon.datapack.DataPack;
import org.jackhuang.hmcl.game.World;
import org.jackhuang.hmcl.addon.datapack.DataPackFile;
import org.jackhuang.hmcl.addon.datapack.DataPackManager;
import org.jackhuang.hmcl.image.ImageLoaders;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
Expand All @@ -42,11 +43,12 @@
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.ui.animation.ContainerAnimations;
import org.jackhuang.hmcl.ui.animation.TransitionPane;
import org.jackhuang.hmcl.ui.construct.*;
import org.jackhuang.hmcl.ui.construct.ImageContainer;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.TwoLineListItem;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.javafx.ItemPropertyAsyncCache;
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
import org.jetbrains.annotations.NotNullByDefault;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -78,25 +80,21 @@ public final class DataPackListPage extends ListPaneBase<DataPackListPage.DataPa
private final BooleanProperty isSearching = new SimpleBooleanProperty(false);
/// Timer for debouncing search input to avoid executing search on every keystroke.
private final PauseTransition searchPause = new PauseTransition(Duration.millis(100));
/// All loaded items before search filtering.
private final ObservableList<DataPackInfoObject> sourceItems = FXCollections.observableArrayList();

/// Source items matching the current search.
private final FilteredList<DataPackInfoObject> filteredItems;
private final FilteredList<DataPackInfoObject> filteredItems = new FilteredList<>(sourceItems);

private final World world;
private final DataPack dataPack;
final BooleanProperty readOnly;
private final DataPackManager dataPackManager;
private final BooleanProperty readOnly;

public DataPackListPage(WorldManagePage worldManagePage) {
world = worldManagePage.getWorld();
dataPack = new DataPack(world.getFile().resolve("datapacks"));
filteredItems = new FilteredList<>(MappedObservableList.create(dataPack.getPacks(), DataPackInfoObject::new));
dataPackManager = worldManagePage.getWorld().getDataPackManager();
readOnly = worldManagePage.readOnlyProperty();
FXUtils.applyDragListener(this, it -> Objects.equals("zip", FileUtils.getExtension(it)),
this::installMultiDataPack, this::refresh);

refresh();

FXUtils.applyDragListener(this, it -> Objects.equals("zip", FileUtils.getExtension(it)), this::addFiles);
initializeView();
refresh();
}

@Override
Expand Down Expand Up @@ -213,26 +211,46 @@ private void initializeView() {
}
}

private void installMultiDataPack(List<Path> dataPackPath) {
dataPackPath.forEach(this::installSingleDataPack);
if (readOnly.get()) {
Controllers.showToast(i18n("datapack.reload.toast"));
}
}

private void installSingleDataPack(Path dataPack) {
try {
this.dataPack.installPack(dataPack, world.getGameVersion());
} catch (IOException | IllegalArgumentException e) {
LOG.warning("Unable to parse datapack file " + dataPack, e);
}
private void addFiles(List<Path> files) {
List<Path> failures = new ArrayList<>();
Task.runAsync(() -> {
for (Path file : files) {
try {
dataPackManager.importDataPack(file);
} catch (Exception e) {
LOG.warning("Failed to add data pack", e);
failures.add(file);
}
}
}).thenRunAsync(Schedulers.javafx(), () -> {
if (!failures.isEmpty()) {
StringBuilder failure = new StringBuilder(i18n("datapack.add.failed"));
for (Path file : failures) {
failure.append("\n").append(file);
}
Controllers.dialog(failure.toString(), i18n("message.error"), MessageDialogPane.MessageType.ERROR);
}
if (readOnly.get())
Controllers.showToast(i18n("datapack.reload.toast"));
refresh();
}).start();
}

@Override
public void refresh() {
setLoading(true);
Task.runAsync(dataPack::loadFromDir)
.withRunAsync(Schedulers.javafx(), () -> setLoading(false))
.start();
Task.supplyAsync(Schedulers.io(), () -> {
dataPackManager.refresh();
return dataPackManager.getLocalFiles().stream().map(DataPackInfoObject::new).toList();
}).whenComplete(Schedulers.javafx(), (result, exception) -> {
if (exception == null) {
sourceItems.setAll(result);
} else {
LOG.warning("Error refreshing data pack list page", exception);
sourceItems.clear();
}
setLoading(false);
}).start();
}

public void add() {
Expand All @@ -241,48 +259,43 @@ public void add() {
chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter(i18n("extension.datapack"), "*.zip"));
List<Path> res = Controllers.showOpenMultipleDialog(chooser);
if (res != null) {
installMultiDataPack(res);
addFiles(res);
}

dataPack.loadFromDir();
}

void removeSelected(ObservableList<DataPackInfoObject> selectedItems) {
List<String> failed = new ArrayList<>();
selectedItems.stream()
.map(DataPackInfoObject::getPackInfo)
.forEach(pack -> {
try {
dataPack.deletePack(pack);
} catch (IOException e) {
failed.add(pack.getPath().toString());
LOG.warning("Failed to delete datapack \"" + pack.getId() + "\"", e);
}
});
selectedItems.stream().map(DataPackInfoObject::getFile).forEach(dataPack -> {
try {
dataPackManager.removePack(dataPack);
} catch (IOException e) {
failed.add(dataPack.getFile().toString());
LOG.warning("Failed to delete datapack \"" + dataPack.getFileName() + "\"", e);
}
});
refresh();
if (!failed.isEmpty())
Controllers.dialog(i18n("datapack.delete.failed") + "\n" + String.join(",\n", failed),
i18n("message.error"), MessageDialogPane.MessageType.ERROR);
}

void setSelectedEnabled(ObservableList<DataPackInfoObject> selectedItems, boolean enabled) {
List<String> failed = new ArrayList<>();
selectedItems.stream()
.map(DataPackInfoObject::getPackInfo)
.forEach(pack -> {
try {
pack.setActive(enabled);
} catch (IOException e) {
failed.add(pack.getPath().toString());
LOG.warning("Failed to enable or disable datapack \"" + pack.getId() + "\"", e);
}
});
selectedItems.stream().map(DataPackInfoObject::getFile).forEach(dataPack -> {
try {
dataPack.setActive(enabled);
} catch (IOException e) {
failed.add(dataPack.getFile().toString());
LOG.warning("Failed to enable or disable datapack \"" + dataPack.getFileName() + "\"", e);
}
});
if (!failed.isEmpty())
Controllers.dialog(i18n("datapack.toggle.failed") + "\n" + String.join(",\n", failed),
i18n("message.error"), MessageDialogPane.MessageType.ERROR);
}

void openDataPackFolder() {
FXUtils.openFolder(dataPack.getPath());
FXUtils.openFolder(dataPackManager.getDirectory());
}

/// Filters pack IDs and descriptions using the shared query syntax; invalid regular expressions leave the list empty.
Expand All @@ -304,7 +317,8 @@ private void search() {
}

filteredItems.setPredicate(item -> {
return predicate.test(item.getTitle()) || predicate.test(item.getSubtitle());
DataPackFile file = item.getFile();
return predicate.test(file.getFileName()) || predicate.test(file.getDescription().toString());
});
}
}
Expand All @@ -322,25 +336,16 @@ private void changeToolbar(HBox newToolbar) {
}
}

static class DataPackInfoObject {
private final DataPack.Pack packInfo;
private final ItemPropertyAsyncCache<Image, DataPackInfoObject> iconCache;

DataPackInfoObject(DataPack.Pack packInfo) {
this.packInfo = packInfo;
this.iconCache = new ItemPropertyAsyncCache.Soft<>(this, this::loadIcon, this::getDefaultIcon);
}

String getTitle() {
return packInfo.getId();
}
public static class DataPackInfoObject {
private final DataPackFile file;
private final ItemPropertyAsyncCache<Image, DataPackInfoObject> iconCache = new ItemPropertyAsyncCache.Soft<>(this, this::loadIcon, this::getDefaultIcon);

String getSubtitle() {
return packInfo.getDescription().toStringSingleLine();
public DataPackInfoObject(DataPackFile file) {
this.file = file;
}

DataPack.Pack getPackInfo() {
return packInfo;
public DataPackFile getFile() {
return file;
}

private Image getDefaultIcon() {
Expand All @@ -349,7 +354,7 @@ private Image getDefaultIcon() {

/// Loads the icon, using the default icon if it is absent or invalid.
private Image loadIcon() {
Image icon = packInfo.loadIcon(ImageLoaders.DEFAULT);
Image icon = file.loadIcon(ImageLoaders.DEFAULT);
return FXUtils.isValidIcon(icon) ? icon : getDefaultIcon();
}
}
Expand All @@ -359,7 +364,7 @@ private static final class DataPackCell extends ItemCell<DataPackInfoObject> {
ImageContainer imageContainer = new ImageContainer(32);
final TwoLineListItem content = new TwoLineListItem();

private final InvalidationListener activeListener = obs -> activeSwitch.setSelected(getItem().getPackInfo().isActive());
private final InvalidationListener activeListener = obs -> activeSwitch.setSelected(((BooleanProperty) obs).get());
private final WeakInvalidationListener weakActiveListener = new WeakInvalidationListener(activeListener);
private @Nullable ReadOnlyBooleanProperty observedActiveProperty;

Expand Down Expand Up @@ -393,18 +398,18 @@ protected void updateListItem(@Nullable DataPackInfoObject dataItem) {
if (observedActiveProperty != null)
observedActiveProperty.removeListener(weakActiveListener);
if (dataItem == null) return;
content.setTitle(dataItem.getTitle());
content.setSubtitle(dataItem.getSubtitle());

DataPack.Pack packInfo = dataItem.getPackInfo();
DataPackFile packInfo = dataItem.getFile();
content.setTitle(packInfo.getFileName());
content.setSubtitle(packInfo.getDescription().toStringSingleLine());
(observedActiveProperty = packInfo.activeProperty()).addListener(weakActiveListener);
activeSwitch.setDisableAnimation(true);
activeSwitch.setSelected(packInfo.isActive());
activeSwitch.setOnAction(event -> {
try {
packInfo.setActive(activeSwitch.isSelected());
} catch (IOException e) {
LOG.warning("Unable to change state of datapack " + packInfo.getPath(), e);
LOG.warning("Unable to change state of datapack " + packInfo.getFile(), e);
Controllers.showToast(i18n("datapack.toggle.failed"));
} finally {
activeSwitch.setSelected(packInfo.isActive());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static org.jackhuang.hmcl.ui.FXUtils.determineOptimalPopupPosition;
import static org.jackhuang.hmcl.util.StringUtils.parseColorEscapes;
Expand Down Expand Up @@ -130,7 +132,24 @@ public void refresh() {
Task.supplyAsync(Schedulers.io(), () -> {
// Ensure the game version number is parsed
gameInstance.getVersion();
return World.getWorlds(savesDir);
if (!Files.exists(savesDir))
return List.<World>of();

try (Stream<Path> stream = Files.list(savesDir)) {
return stream.filter(Files::isDirectory)
.flatMap(world -> {
try {
return Stream.of(new World(world.toAbsolutePath().normalize()));
} catch (IOException e) {
LOG.warning("Failed to read world " + world, e);
return Stream.empty();
}
})
.toList();
} catch (IOException e) {
LOG.warning("Failed to read saves", e);
return List.<World>of();
}
}).whenComplete(Schedulers.javafx(), (result, exception) -> {
if (refreshCount != currentRefresh) {
// A newer refresh task is running, discard this result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public World getWorld() {
return world;
}

public HMCLGameInstance getGameInstance() {
return gameInstance;
}

public Path getBackupsDir() {
return backupsDir;
}
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ nbt.title=View File - %s
datapack=Data Packs
datapack.add=Add
datapack.add.title=Choose data pack archive you want to add
datapack.add.failed=Failed to add data packs:
datapack.delete.failed=Failed to delete data pack(s). The data pack file(s) may be in use by another program.
datapack.empty=No Data Packs
datapack.reload.toast=Minecraft is running. Use the /reload command to reload data packs
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_ar.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@ nbt.title=عرض الملف - %s
datapack=حزم البيانات
datapack.add=إضافة
datapack.add.title=اختر أرشيف حزمة البيانات التي تريد إضافتها
# datapack.add.failed=
# datapack.delete.failed=
# datapack.empty=
datapack.reload.toast=اللعبة تعمل حالياً، يُرجى استخدام الأمر /reload لإعادة تحميل حزمة البيانات
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ nbt.title=Datei ansehen - %s
datapack=Datenpakete
datapack.add=Hinzufügen
datapack.add.title=Wählen Sie das Datenpaket-Archiv, das Sie hinzufügen möchten
# datapack.add.failed=
# datapack.delete.failed=
datapack.empty=Keine Datenpakete
datapack.reload.toast=Minecraft läuft. Verwenden Sie den Befehl /reload, um Datenpakete neu zu laden
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ nbt.title=Ver archivo - %s
datapack=Paquetes de datos
# datapack.add=
# datapack.add.title=
# datapack.add.failed=
# datapack.delete.failed=
# datapack.empty=
# datapack.reload.toast=
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ mods.update_modpack_mod.warning=modpack 内の mod を更新すると、修復
datapack=Datapacks
# datapack.add=
# datapack.add.title=
# datapack.add.failed=
# datapack.delete.failed=
# datapack.empty=
# datapack.reload.toast=
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_lzh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ nbt.title=覽案 - %s
datapack=錄囊
datapack.add=增錄囊
datapack.add.title=擇須導入之錄囊緊囊
# datapack.add.failed=
# datapack.delete.failed=
# datapack.empty=
# datapack.reload.toast=
Expand Down
Loading
Loading