From 4261d17f71ff1763fa59ce62fb9b9a15b57eefed Mon Sep 17 00:00:00 2001 From: alexbegt Date: Wed, 26 Jun 2019 23:15:15 -0400 Subject: [PATCH] Add a way to generate Block/Item Model Jsons and BlockState Jsons by using the data pack generator. --- .../mantle/common/IGeneratedJson.java | 14 +++ .../mantle/util/BlockStateJsonGenerator.java | 60 ++++++++++++ .../mantle/util/ModelJsonGenerator.java | 98 +++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 src/main/java/slimeknights/mantle/common/IGeneratedJson.java create mode 100644 src/main/java/slimeknights/mantle/util/BlockStateJsonGenerator.java create mode 100644 src/main/java/slimeknights/mantle/util/ModelJsonGenerator.java diff --git a/src/main/java/slimeknights/mantle/common/IGeneratedJson.java b/src/main/java/slimeknights/mantle/common/IGeneratedJson.java new file mode 100644 index 0000000..8a82882 --- /dev/null +++ b/src/main/java/slimeknights/mantle/common/IGeneratedJson.java @@ -0,0 +1,14 @@ +package slimeknights.mantle.common; + +import com.google.gson.JsonObject; + +public interface IGeneratedJson { + + String getParentToUse(); + + JsonObject getTexturesToUse(); + + default JsonObject getVariants() { + return null; + } +} diff --git a/src/main/java/slimeknights/mantle/util/BlockStateJsonGenerator.java b/src/main/java/slimeknights/mantle/util/BlockStateJsonGenerator.java new file mode 100644 index 0000000..1e216c0 --- /dev/null +++ b/src/main/java/slimeknights/mantle/util/BlockStateJsonGenerator.java @@ -0,0 +1,60 @@ +package slimeknights.mantle.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import net.minecraft.block.Block; +import net.minecraft.data.DataGenerator; +import net.minecraft.data.DirectoryCache; +import net.minecraft.data.IDataProvider; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.registry.Registry; +import slimeknights.mantle.common.IGeneratedJson; + +import java.io.IOException; +import java.nio.file.Path; + +public class BlockStateJsonGenerator implements IDataProvider { + + private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().create(); + private final DataGenerator generator; + private final String modId; + + public BlockStateJsonGenerator(DataGenerator generatorIn, String modId) { + this.generator = generatorIn; + this.modId = modId; + } + + @Override + public void act(DirectoryCache cache) throws IOException { + JsonObject blockState = new JsonObject(); + + for (Block block : Registry.BLOCK) { + blockState = new JsonObject(); + ResourceLocation resourcelocation = Registry.BLOCK.getKey(block); + + if (!resourcelocation.getNamespace().equals(this.modId)) { + continue; + } + + if (!(block instanceof IGeneratedJson)) { + continue; + } + + IGeneratedJson block1 = (IGeneratedJson) block; + + blockState.add("variants", block1.getVariants()); + + Path path = this.generator.getOutputFolder().resolve("assets/" + this.modId + "/blockstates/" + resourcelocation.getPath() + ".json"); + IDataProvider.func_218426_a(GSON, cache, blockState, path); + } + } + + /** + * Gets a name for this provider, to use in logging. + */ + @Override + public String getName() { + return "Blockstate Generator"; + } +} diff --git a/src/main/java/slimeknights/mantle/util/ModelJsonGenerator.java b/src/main/java/slimeknights/mantle/util/ModelJsonGenerator.java new file mode 100644 index 0000000..a083bea --- /dev/null +++ b/src/main/java/slimeknights/mantle/util/ModelJsonGenerator.java @@ -0,0 +1,98 @@ +package slimeknights.mantle.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import net.minecraft.block.Block; +import net.minecraft.data.DataGenerator; +import net.minecraft.data.DirectoryCache; +import net.minecraft.data.IDataProvider; +import net.minecraft.item.Item; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.registry.Registry; +import slimeknights.mantle.common.IGeneratedJson; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +public class ModelJsonGenerator implements IDataProvider { + + private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().create(); + private final DataGenerator generator; + private final String modId; + + public ModelJsonGenerator(DataGenerator generatorIn, String modId) { + this.generator = generatorIn; + this.modId = modId; + } + + @Override + public void act(DirectoryCache cache) throws IOException { + JsonObject blockObject = new JsonObject(); + JsonObject itemObject = new JsonObject(); + List resourceLocations = new ArrayList(); + + for (Block block : Registry.BLOCK) { + blockObject = new JsonObject(); + ResourceLocation resourcelocation = Registry.BLOCK.getKey(block); + + if (!resourcelocation.getNamespace().equals(this.modId)) { + continue; + } + if (!(block instanceof IGeneratedJson)) { + continue; + } + + IGeneratedJson block1 = (IGeneratedJson) block; + + resourceLocations.add(resourcelocation); + + blockObject.addProperty("parent", block1.getParentToUse()); + + blockObject.add("textures", block1.getTexturesToUse()); + + Path path = this.generator.getOutputFolder().resolve("assets/" + this.modId + "/models/block/" + resourcelocation.getPath() + ".json"); + IDataProvider.func_218426_a(GSON, cache, blockObject, path); + } + + for (Item item : Registry.ITEM) { + itemObject = new JsonObject(); + ResourceLocation resourcelocation = Registry.ITEM.getKey(item); + + if (!resourcelocation.getNamespace().equals(this.modId)) { + continue; + } + + if (resourceLocations.contains(resourcelocation)) { + itemObject.addProperty("parent", resourcelocation.getNamespace() + ":block/" + resourcelocation.getPath()); + + Path path = this.generator.getOutputFolder().resolve("assets/" + this.modId + "/models/item/" + resourcelocation.getPath() + ".json"); + IDataProvider.func_218426_a(GSON, cache, itemObject, path); + } + else { + if (!(item instanceof IGeneratedJson)) { + continue; + } + + IGeneratedJson item1 = (IGeneratedJson) item; + + itemObject.addProperty("parent", item1.getParentToUse()); + + itemObject.add("textures", item1.getTexturesToUse()); + + Path path = this.generator.getOutputFolder().resolve("assets/" + this.modId + "/models/item/" + resourcelocation.getPath() + ".json"); + IDataProvider.func_218426_a(GSON, cache, itemObject, path); + } + } + } + + /** + * Gets a name for this provider, to use in logging. + */ + @Override + public String getName() { + return "Model Generator"; + } +}