Skip to content

Commit

Permalink
Use library to zip resource pack
Browse files Browse the repository at this point in the history
This fixes the resource pack not working. Previously, the resource pack was displaying in the resource pack menu, including the `pack.png`, but no resources would actually work in-game.

`zip4j` library: https://github.com/srikanth-lingala/zip4j
Someone else who was having a similar (if not the exact same) issue: https://www.spigotmc.org/threads/minecraft-does-not-accept-my-generated-zip-file-as-resource-pack.94596/
  • Loading branch information
EsotericEnderman committed Aug 12, 2024
1 parent 541e952 commit 54ee796
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ dependencies {
paperweight.paperDevBundle(paperApiVersion + "-R0.1-SNAPSHOT")

implementation("dev.jorel" , "commandapi-bukkit-shade-mojang-mapped" , "9.5.1")

implementation("net.lingala.zip4j", "zip4j", "2.11.5")
}

tasks {
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/net/slqmy/template_paper_plugin/file/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
import java.util.Collections;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.jar.JarEntry;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import net.lingala.zip4j.ZipFile;

public class FileUtil {

private static final String FILE_EXTENSION_SEPARATOR = ".";
Expand Down Expand Up @@ -66,18 +60,16 @@ public static List<String> getResourceFileFolderResourceFilePathsRecursively(Str
return paths;
}

public static void zipFolder(Path sourceFolderPath, Path zipPath) throws Exception {
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
zipOutputStream.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));
Files.copy(file, zipOutputStream);
zipOutputStream.closeEntry();
return FileVisitResult.CONTINUE;
public static void zipFolder(File sourceFolder, File zipFile) throws IOException {
try (ZipFile zipFileInstance = new ZipFile(zipFile)) {
for (File file : sourceFolder.listFiles()) {
if (file.isDirectory()) {
zipFileInstance.addFolder(file);
} else {
zipFileInstance.addFile(file);
}
}
});
zipOutputStream.close();
}
}

public static String getSha1HexString(File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void saveResourcepackZipFile() {

try {
resourcePackZipFile = new File(resourcePackZipFilePath);
FileUtil.zipFolder(resourcePackFolder.toPath(), resourcePackZipFile.toPath());
FileUtil.zipFolder(resourcePackFolder, resourcePackZipFile);

FileUtils.deleteDirectory(resourcePackFolder);
} catch (Exception exception) {
Expand Down

0 comments on commit 54ee796

Please sign in to comment.