Skip to content

Commit

Permalink
Make PackMcMeta into API, fix metadata not being overriden when it sh…
Browse files Browse the repository at this point in the history
…ould be
  • Loading branch information
Patbox committed Oct 18, 2024
1 parent fd4e765 commit 76c6593
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -218,6 +218,13 @@ public static boolean buildMain(Path output, Consumer<String> 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 {
Expand Down Expand Up @@ -258,6 +265,13 @@ public static boolean buildMain(Path output, Consumer<String> 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) {}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -94,4 +94,8 @@ default boolean copyResourcePackFromPath(Path root, String field) {
boolean addAssetsSource(String modId);

void addWriteConverter(BiFunction<String, byte[], @Nullable byte[]> converter);

default PackMcMeta.Builder getPackMcMetaBuilder() {
return new PackMcMeta.Builder();
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,7 +25,7 @@ public record PackMcMeta(PackResourceMetadata pack, Optional<ResourceFilter> 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();
}

Expand All @@ -43,13 +41,18 @@ public static class Builder {
);
private final List<BlockEntry> filter = new ArrayList<>();
private final List<PackOverlaysMetadata.Entry> overlay = new ArrayList<>();
private Map<String, LanguageDefinition> language = new HashMap<>();
private final Map<String, LanguageDefinition> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
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;

@ApiStatus.Internal
public interface InternalRPBuilder extends ResourcePackBuilder {
CompletableFuture<Boolean> buildResourcePack();


PackMcMeta.Builder getPackMcMetaBuilder();
}

0 comments on commit 76c6593

Please sign in to comment.