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 a161188 commit 04d5fb9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,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 @@ -136,21 +139,13 @@ 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))
)
)
/*
.then(Commands.literal("output_recipes")
.executes(context -> outputRecipes(context.getSource().getPlayerOrException()))
)
.then(Commands.literal("input_recipes")
.executes(context -> inputRecipes(context.getSource().getPlayerOrException()))
)
.then(Commands.literal("check_recipe_conflicts")
.executes(context -> checkRecipeConflicts(context.getSource().getPlayerOrException()))
)
*/
.then(Commands.literal("list_tag")
.then(Commands.argument("registry", ResourceLocationArgument.id())
.suggests((ctx, builder) -> SharedSuggestionProvider.suggest(
Expand Down Expand Up @@ -645,7 +640,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 @@ -658,15 +653,29 @@ private static int exportPacks(CommandSourceStack source) {
int success = 0;

for (var pack : packs) {
var path = KubeJSPaths.EXPORTED_PACKS.resolve(pack.getName() + ".zip");
try {
Files.deleteIfExists(path);
if (exportZip) {
var path = KubeJSPaths.EXPORTED_PACKS.resolve(pack.getName() + ".zip");
Files.deleteIfExists(path);

try (var fs = FileSystems.newFileSystem(path, Map.of("create", true))) {
pack.export(fs);
try (var fs = FileSystems.newFileSystem(path, Map.of("create", true))) {
pack.export(fs.getPath("."));
}
} else {
var path = KubeJSPaths.EXPORTED_PACKS.resolve(pack.getName());

if (Files.exists(path)) {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}

Files.createDirectories(path);
pack.export(path);
}

source.sendSuccess(Component.literal("Successfully exported ").withStyle(ChatFormatting.GREEN).append(Component.literal(pack.getName()).withStyle(ChatFormatting.BLUE)), true);
source.sendSuccess(Component.literal("Successfully exported ").withStyle(ChatFormatting.GREEN).append(Component.literal(pack.getName()).withStyle(ChatFormatting.BLUE)), false);
success++;
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -676,22 +685,13 @@ private static int exportPacks(CommandSourceStack source) {
}
}

return success;
}

private static int outputRecipes(ServerPlayer player) {
player.sendSystemMessage(Component.literal("WIP!"));
return Command.SINGLE_SUCCESS;
}

private static int inputRecipes(ServerPlayer player) {
player.sendSystemMessage(Component.literal("WIP!"));
return Command.SINGLE_SUCCESS;
}
if (source.getServer().isSingleplayer() && !source.getServer().isPublished()) {
source.sendSuccess(Component.literal("Exported " + success + " packs").kjs$clickOpenFile(KubeJSPaths.EXPORTED_PACKS.toAbsolutePath().toString()), false);
} else {
source.sendSuccess(Component.literal("Exported " + success + " packs"), false);
}

private static int checkRecipeConflicts(ServerPlayer player) {
player.sendSystemMessage(Component.literal("WIP!"));
return Command.SINGLE_SUCCESS;
return success;
}

private static <T> int listTagsFor(CommandSourceStack source, ResourceKey<Registry<T>> registry) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

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 @@ -13,8 +13,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.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -187,14 +187,14 @@ public void close() {
}

@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.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -119,9 +119,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 04d5fb9

Please sign in to comment.