Skip to content

Commit

Permalink
dynamic datapack files
Browse files Browse the repository at this point in the history
  • Loading branch information
JaisDK committed Nov 15, 2024
1 parent 38e8d64 commit 3f58701
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Pack> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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> T getMetadataSection(MetadataSectionSerializer<T> 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;
}
}
}
}

0 comments on commit 3f58701

Please sign in to comment.