Skip to content

Commit

Permalink
Added option to export packs as folders
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 18, 2023
1 parent 7a681f3 commit 64f0441
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,8 +140,11 @@ public static void register(CommandDispatcher<CommandSourceStack> 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))
)
)
/*
Expand Down Expand Up @@ -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<ExportablePackResources>();

for (var pack : source.getServer().getResourceManager().listPacks().toList()) {
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 64f0441

Please sign in to comment.