From 76c659307e3d9268abc76be0ee63ccd7fb9ec783 Mon Sep 17 00:00:00 2001 From: Patbox Date: Fri, 18 Oct 2024 10:50:41 +0200 Subject: [PATCH] Make PackMcMeta into API, fix metadata not being overriden when it should be --- .../api/PolymerResourcePackUtils.java | 16 +++++++++++++++- .../resourcepack/api/ResourcePackBuilder.java | 6 +++++- .../metadata/LanguageDefinition.java | 3 +-- .../metadata/LanguageResourceMetadata.java | 2 +- .../{impl => api}/metadata/PackMcMeta.java | 13 ++++++++----- .../impl/generation/DefaultRPBuilder.java | 4 +--- .../impl/generation/InternalRPBuilder.java | 4 ---- 7 files changed, 31 insertions(+), 17 deletions(-) rename polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/{impl => api}/metadata/LanguageDefinition.java (88%) rename polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/{impl => api}/metadata/LanguageResourceMetadata.java (92%) rename polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/{impl => api}/metadata/PackMcMeta.java (89%) diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/PolymerResourcePackUtils.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/PolymerResourcePackUtils.java index eaf3990c..4004b9eb 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/PolymerResourcePackUtils.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/PolymerResourcePackUtils.java @@ -6,6 +6,7 @@ import eu.pb4.polymer.resourcepack.impl.PolymerResourcePackImpl; import eu.pb4.polymer.resourcepack.impl.compat.polymc.PolyMcHelpers; import eu.pb4.polymer.resourcepack.impl.generation.DefaultRPBuilder; +import eu.pb4.polymer.resourcepack.api.metadata.PackMcMeta; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.item.ArmorMaterial; import net.minecraft.item.Item; @@ -18,7 +19,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import java.util.Locale; import java.util.UUID; import java.util.function.Consumer; @@ -218,6 +218,13 @@ public static boolean buildMain(Path output, Consumer status) { Path path = CommonImpl.getGameDir().resolve("polymer/source_assets"); if (Files.isDirectory(path)) { builder.copyFromPath(path); + try { + var metafile = path.resolve("pack.mcmeta"); + if (Files.exists(metafile)) { + var meta = PackMcMeta.fromString(Files.readString(metafile)); + builder.getPackMcMetaBuilder().metadata(meta.pack()); + } + } catch (Throwable ignored) {} } try { @@ -258,6 +265,13 @@ public static boolean buildMain(Path output, Consumer status) { Path path = CommonImpl.getGameDir().resolve("polymer/override_assets"); if (Files.isDirectory(path)) { builder.copyFromPath(path); + try { + var metafile = path.resolve("pack.mcmeta"); + if (Files.exists(metafile)) { + var meta = PackMcMeta.fromString(Files.readString(metafile)); + builder.getPackMcMetaBuilder().metadata(meta.pack()); + } + } catch (Throwable ignored) {} } }); } diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackBuilder.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackBuilder.java index 83a13662..ec826f01 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackBuilder.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackBuilder.java @@ -1,6 +1,6 @@ package eu.pb4.polymer.resourcepack.api; -import eu.pb4.polymer.resourcepack.impl.metadata.PackMcMeta; +import eu.pb4.polymer.resourcepack.api.metadata.PackMcMeta; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; @@ -94,4 +94,8 @@ default boolean copyResourcePackFromPath(Path root, String field) { boolean addAssetsSource(String modId); void addWriteConverter(BiFunction converter); + + default PackMcMeta.Builder getPackMcMetaBuilder() { + return new PackMcMeta.Builder(); + } } diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/LanguageDefinition.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/LanguageDefinition.java similarity index 88% rename from polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/LanguageDefinition.java rename to polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/LanguageDefinition.java index c473aabd..a7f25246 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/LanguageDefinition.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/LanguageDefinition.java @@ -1,8 +1,7 @@ -package eu.pb4.polymer.resourcepack.impl.metadata; +package eu.pb4.polymer.resourcepack.api.metadata; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; -import net.minecraft.text.Text; import net.minecraft.util.dynamic.Codecs; public record LanguageDefinition(String region, String name, boolean rightToLeft) { diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/LanguageResourceMetadata.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/LanguageResourceMetadata.java similarity index 92% rename from polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/LanguageResourceMetadata.java rename to polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/LanguageResourceMetadata.java index 1788fa35..dd65d7b4 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/LanguageResourceMetadata.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/LanguageResourceMetadata.java @@ -1,4 +1,4 @@ -package eu.pb4.polymer.resourcepack.impl.metadata; +package eu.pb4.polymer.resourcepack.api.metadata; import com.mojang.serialization.Codec; import net.minecraft.resource.metadata.ResourceMetadataSerializer; diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/PackMcMeta.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/PackMcMeta.java similarity index 89% rename from polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/PackMcMeta.java rename to polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/PackMcMeta.java index e2ee0502..9d6e6837 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/metadata/PackMcMeta.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/metadata/PackMcMeta.java @@ -1,13 +1,11 @@ -package eu.pb4.polymer.resourcepack.impl.metadata; +package eu.pb4.polymer.resourcepack.api.metadata; import com.google.gson.JsonParser; import com.mojang.serialization.Codec; import com.mojang.serialization.JsonOps; import com.mojang.serialization.codecs.RecordCodecBuilder; -import eu.pb4.polymer.common.impl.CommonImpl; import eu.pb4.polymer.resourcepack.mixin.accessors.PackOverlaysMetadataAccessor; import eu.pb4.polymer.resourcepack.mixin.accessors.ResourceFilterAccessor; -import io.netty.buffer.ByteBuf; import net.minecraft.SharedConstants; import net.minecraft.resource.ResourceType; import net.minecraft.resource.metadata.BlockEntry; @@ -27,7 +25,7 @@ public record PackMcMeta(PackResourceMetadata pack, Optional fil LanguageResourceMetadata.CODEC.optionalFieldOf("language").forGetter(PackMcMeta::language) ).apply(instaince, PackMcMeta::new)); - public static PackMcMeta fromString(String string) throws Exception { + public static PackMcMeta fromString(String string) { return CODEC.decode(JsonOps.INSTANCE, JsonParser.parseString(string)).getOrThrow().getFirst(); } @@ -43,13 +41,18 @@ public static class Builder { ); private final List filter = new ArrayList<>(); private final List overlay = new ArrayList<>(); - private Map language = new HashMap<>(); + private final Map language = new HashMap<>(); public Builder metadata(PackResourceMetadata metadata) { this.metadata = metadata; return this; } + public Builder description(Text description) { + this.metadata = new PackResourceMetadata(description, this.metadata.packFormat(), this.metadata.supportedFormats()); + return this; + } + public Builder addFilter(BlockEntry entry) { this.filter.add(entry); return this; diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/DefaultRPBuilder.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/DefaultRPBuilder.java index 34fae7d1..64658fe7 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/DefaultRPBuilder.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/DefaultRPBuilder.java @@ -6,16 +6,14 @@ import eu.pb4.polymer.common.api.events.SimpleEvent; import eu.pb4.polymer.common.impl.CommonImpl; import eu.pb4.polymer.common.impl.CommonImplUtils; -import eu.pb4.polymer.common.impl.FakeWorld; import eu.pb4.polymer.resourcepack.api.AssetPaths; import eu.pb4.polymer.resourcepack.api.PolymerArmorModel; import eu.pb4.polymer.resourcepack.api.PolymerModelData; import eu.pb4.polymer.resourcepack.impl.ArmorTextureMetadata; -import eu.pb4.polymer.resourcepack.impl.metadata.PackMcMeta; +import eu.pb4.polymer.resourcepack.api.metadata.PackMcMeta; import eu.pb4.polymer.resourcepack.mixin.accessors.ResourceFilterAccessor; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; -import net.minecraft.SharedConstants; import net.minecraft.item.Item; import net.minecraft.registry.Registries; import net.minecraft.util.Identifier; diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/InternalRPBuilder.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/InternalRPBuilder.java index 5a8841bf..ff7e3d27 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/InternalRPBuilder.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/generation/InternalRPBuilder.java @@ -1,7 +1,6 @@ package eu.pb4.polymer.resourcepack.impl.generation; import eu.pb4.polymer.resourcepack.api.ResourcePackBuilder; -import eu.pb4.polymer.resourcepack.impl.metadata.PackMcMeta; import org.jetbrains.annotations.ApiStatus; import java.util.concurrent.CompletableFuture; @@ -9,7 +8,4 @@ @ApiStatus.Internal public interface InternalRPBuilder extends ResourcePackBuilder { CompletableFuture buildResourcePack(); - - - PackMcMeta.Builder getPackMcMetaBuilder(); }