Fix resource packs not loading at startup (#452)

Co-authored-by: boubou19 <[email protected]>
This commit is contained in:
Sunnyandboubou19 authored and GitHub committed 2026-08-21 19:36:57 +00:00
1 parent 676edd88a2
commit fc4257eb0d
1 file changed
+36 -21
@@ -4,24 +4,26 @@ import static com.gtnewhorizon.gtnhlib.core.GTNHLibCore.MODEL_LOGGER;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.AbstractResourcePack;
import net.minecraft.client.resources.FileResourcePack;
import net.minecraft.client.resources.data.IMetadataSection;
import net.minecraft.client.resources.data.PackMetadataSection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.gtnewhorizon.gtnhlib.client.model.loading.ModelResourcePack;
import com.gtnewhorizon.gtnhlib.client.model.loading.RPInfo;
import com.gtnewhorizon.gtnhlib.client.model.unbaked.JSONModel;
@@ -86,6 +88,9 @@ public abstract class MixinFileResourcePack extends AbstractResourcePack impleme
@Unique
private static final String METADATA_FILE = "pack.mcmeta";
@Unique
private int nhlib$packFormat = -1;
/// Block old resourcepacks from loading blockstates or models.
/// @return True if the resource SHOULDN'T load, false if it SHOULD.
@Definition(id = "zipentry", local = @Local(type = ZipEntry.class))
@@ -95,28 +100,38 @@ public abstract class MixinFileResourcePack extends AbstractResourcePack impleme
// Return the default for the metadata, or getting it below will infinitely recurse.
if (original || METADATA_FILE.equals(resource)) return original;
if (!resource.endsWith(".json")) return false; // not a JSON blockstate
var pathParts = resource.split("/");
if (pathParts.length < 4) return false; // too short to be a blockstate
if (!"assets".equals(pathParts[0])) return false;
if (!"blockstates".equals(pathParts[2])) return false;
// Not a JSON model or blockstate
if (!resource.endsWith(".json") || !resource.startsWith("assets/")) return false;
int firstSlash = 6; // Index of slash after "assets"
int secondSlash = resource.indexOf('/', firstSlash + 1);
if (secondSlash == -1 || !resource.startsWith("blockstates/", secondSlash + 1)) return false;
// This song and dance is needed because some calls to getInputStreamByName expect it to never fail,
// since they're referring to resources bundled in jars. So while the original has a checked exception, we can't
// rely on everyone checking it.
final int packFormat;
IMetadataSection packFormatSection = null;
try {
packFormatSection = getPackMetadata(
Minecraft.getMinecraft().getResourcePackRepository().rprMetadataSerializer,
"pack");
} catch (IOException e) {
// If the above fails, pack metadata is null and thus format is assumed to be 1 (i.e. old).
}
if (!(packFormatSection instanceof PackMetadataSection metadata)) packFormat = 1;
else packFormat = metadata.getPackFormat();
if (this.nhlib$packFormat == -1) {
try (InputStream is = getInputStreamByName(METADATA_FILE)) {
if (is != null) {
JsonObject json = new JsonParser().parse(new InputStreamReader(is, StandardCharsets.UTF_8))
.getAsJsonObject();
if (json.has("pack")) {
JsonObject packObj = json.getAsJsonObject("pack");
if (packObj.has("pack_format")) {
this.nhlib$packFormat = packObj.get("pack_format").getAsInt();
}
}
}
} catch (Exception e) {
// If the above fails, pack metadata is null and thus format is assumed to be 1 (i.e. old).
}
// This is a blockstate, reject it if it's too old.
return packFormat < PACK_FORMAT_MC_13_X;
if (this.nhlib$packFormat == -1) {
this.nhlib$packFormat = 1;
}
}
// Reject it if it's too old.
return this.nhlib$packFormat < PACK_FORMAT_MC_13_X;
}
}