diff --git a/src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DataGenPackFinder.java b/src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DataGenPackFinder.java new file mode 100644 index 000000000..12e92eb2d --- /dev/null +++ b/src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DataGenPackFinder.java @@ -0,0 +1,33 @@ +package cy.jdkdigital.productivetrees.datagen.dynamic; + +import cy.jdkdigital.productivetrees.ProductiveTrees; +import cy.jdkdigital.productivetrees.registry.TreeFinder; +import net.minecraft.network.chat.Component; +import net.minecraft.server.packs.PackLocationInfo; +import net.minecraft.server.packs.PackSelectionConfig; +import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.repository.*; + +import java.util.Optional; +import java.util.function.Consumer; + +public class DataGenPackFinder implements RepositorySource +{ + private final PackType packType; + + public DataGenPackFinder(PackType packType) { + this.packType = packType; + } + + @Override + public void loadPacks(Consumer consumer) { + var packInfo = new PackLocationInfo("productivetrees_" + packType.getDirectory(), Component.literal("Productive tree dynamic resources"), PackSource.BUILT_IN, Optional.of(new KnownPack(ProductiveTrees.MODID, "productivetrees_" + packType.getDirectory(), "1.0"))); + Pack pack = Pack.readMetaAndCreate( + packInfo, + BuiltInPackSource.fromName((path) -> new DynamicDataPack(TreeFinder.DYNAMIC_RESOURCE_PATH, packType, packInfo)), + packType, + new PackSelectionConfig(true, Pack.Position.BOTTOM, false) + ); + consumer.accept(pack); + } +} diff --git a/src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DynamicDataPack.java b/src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DynamicDataPack.java new file mode 100644 index 000000000..74aeebcfc --- /dev/null +++ b/src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DynamicDataPack.java @@ -0,0 +1,48 @@ +package cy.jdkdigital.productivetrees.datagen.dynamic; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import cy.jdkdigital.productivetrees.ProductiveTrees; +import net.minecraft.server.packs.PackLocationInfo; +import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.PathPackResources; +import net.minecraft.server.packs.metadata.MetadataSectionSerializer; +import net.minecraft.util.GsonHelper; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.nio.file.Path; + +public class DynamicDataPack extends PathPackResources +{ + private final Path rootPath; + private final PackType packType; + private final PackLocationInfo packInfo; + + public DynamicDataPack(Path rootPath, PackType packType, PackLocationInfo packInfo) { + super(packInfo, rootPath); + ProductiveTrees.generateData(); + this.rootPath = rootPath; + this.packType = packType; + this.packInfo = packInfo; + } + + @Nullable + @Override + public T getMetadataSection(MetadataSectionSerializer metadataSectionSerializer) throws IOException { + JsonObject jsonobject = new JsonObject(); + JsonObject packObject = new JsonObject(); + packObject.addProperty("pack_format", packType.equals(PackType.SERVER_DATA) ? 48 : 34); + packObject.addProperty("description", ProductiveTrees.MODID); + jsonobject.add("pack", packObject); + if (!jsonobject.has(metadataSectionSerializer.getMetadataSectionName())) { + return null; + } else { + try { + return metadataSectionSerializer.fromJson(GsonHelper.getAsJsonObject(jsonobject, metadataSectionSerializer.getMetadataSectionName())); + } catch (JsonParseException jsonparseexception) { + return null; + } + } + } +}