diff --git a/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java b/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java index 6b017e6ca..4523e3fa2 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java @@ -59,12 +59,15 @@ import net.minecraft.world.item.ItemStack; import org.apache.commons.io.FileUtils; +import java.io.File; import java.io.IOException; import java.lang.reflect.Modifier; import java.nio.file.FileSystems; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Set; @@ -137,8 +140,11 @@ public static void register(CommandDispatcher dispatcher) { .then(Commands.literal("export") .requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2)) .executes(context -> export(context.getSource())) - .then(Commands.literal("packs") - .executes(context -> exportPacks(context.getSource())) + .then(Commands.literal("pack_zips") + .executes(context -> exportPacks(context.getSource(), true)) + ) + .then(Commands.literal("pack_folders") + .executes(context -> exportPacks(context.getSource(), false)) ) ) /* @@ -649,7 +655,7 @@ private static int export(CommandSourceStack source) { return 1; } - private static int exportPacks(CommandSourceStack source) { + private static int exportPacks(CommandSourceStack source, boolean exportZip) { var packs = new ArrayList(); for (var pack : source.getServer().getResourceManager().listPacks().toList()) { @@ -662,15 +668,29 @@ private static int exportPacks(CommandSourceStack source) { int success = 0; for (var pack : packs) { - var path = KubeJSPaths.EXPORTED_PACKS.resolve(pack.packId() + ".zip"); try { - Files.deleteIfExists(path); + if (exportZip) { + var path = KubeJSPaths.EXPORTED_PACKS.resolve(pack.packId() + ".zip"); + Files.deleteIfExists(path); + + try (var fs = FileSystems.newFileSystem(path, Map.of("create", true))) { + pack.export(fs.getPath(".")); + } + } else { + var path = KubeJSPaths.EXPORTED_PACKS.resolve(pack.packId()); + + if (Files.exists(path)) { + Files.walk(path) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } - try (var fs = FileSystems.newFileSystem(path, Map.of("create", true))) { - pack.export(fs); + Files.createDirectories(path); + pack.export(path); } - source.sendSuccess(() -> Component.literal("Successfully exported ").withStyle(ChatFormatting.GREEN).append(Component.literal(pack.packId()).withStyle(ChatFormatting.BLUE)), true); + source.sendSuccess(() -> Component.literal("Successfully exported ").withStyle(ChatFormatting.GREEN).append(Component.literal(pack.packId()).withStyle(ChatFormatting.BLUE)), false); success++; } catch (IOException e) { e.printStackTrace(); @@ -680,6 +700,14 @@ private static int exportPacks(CommandSourceStack source) { } } + int success1 = success; + + if (source.getServer().isSingleplayer() && !source.getServer().isPublished()) { + source.sendSuccess(() -> Component.literal("Exported " + success1 + " packs").kjs$clickOpenFile(KubeJSPaths.EXPORTED_PACKS.toAbsolutePath().toString()), false); + } else { + source.sendSuccess(() -> Component.literal("Exported " + success1 + " packs"), false); + } + return success; } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/script/data/ExportablePackResources.java b/common/src/main/java/dev/latvian/mods/kubejs/script/data/ExportablePackResources.java index 1ed93a3b1..110dd5fe3 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/script/data/ExportablePackResources.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/script/data/ExportablePackResources.java @@ -3,8 +3,8 @@ import net.minecraft.server.packs.PackResources; import java.io.IOException; -import java.nio.file.FileSystem; +import java.nio.file.Path; public interface ExportablePackResources extends PackResources { - void export(FileSystem fs) throws IOException; + void export(Path root) throws IOException; } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/script/data/GeneratedResourcePack.java b/common/src/main/java/dev/latvian/mods/kubejs/script/data/GeneratedResourcePack.java index 01c7dc1e7..3da966288 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/script/data/GeneratedResourcePack.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/script/data/GeneratedResourcePack.java @@ -14,8 +14,8 @@ import java.io.IOException; import java.io.InputStream; -import java.nio.file.FileSystem; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -179,14 +179,14 @@ public boolean isBuiltin() { } @Override - public void export(FileSystem fs) throws IOException { + public void export(Path root) throws IOException { for (var file : getGenerated().entrySet()) { - var path = fs.getPath(packType.getDirectory() + "/" + file.getKey().getNamespace() + "/" + file.getKey().getPath()); + var path = root.resolve(packType.getDirectory() + "/" + file.getKey().getNamespace() + "/" + file.getKey().getPath()); Files.createDirectories(path.getParent()); Files.write(path, file.getValue().data().get()); } - Files.write(fs.getPath(PACK_META), GeneratedData.PACK_META.data().get()); - Files.write(fs.getPath("pack.png"), GeneratedData.PACK_ICON.data().get()); + Files.write(root.resolve(PACK_META), GeneratedData.PACK_META.data().get()); + Files.write(root.resolve("pack.png"), GeneratedData.PACK_ICON.data().get()); } } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualKubeJSDataPack.java b/common/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualKubeJSDataPack.java index e733d002d..2ad5bd4a4 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualKubeJSDataPack.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualKubeJSDataPack.java @@ -14,8 +14,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.nio.file.FileSystem; import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -108,9 +108,9 @@ public String toString() { } @Override - public void export(FileSystem fs) throws IOException { + public void export(Path root) throws IOException { for (var file : pathToData.entrySet()) { - var path = fs.getPath(file.getKey()); + var path = root.resolve(file.getKey()); Files.createDirectories(path.getParent()); Files.writeString(path, file.getValue(), StandardCharsets.UTF_8); }