-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DataGenPackFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/cy/jdkdigital/productivetrees/datagen/dynamic/DynamicDataPack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |