From 611832ad14ce7fb33b0140d7c4b83f449d744502 Mon Sep 17 00:00:00 2001 From: Apex Date: Sat, 23 Nov 2024 02:41:35 +0000 Subject: [PATCH] Allow setting custom properties onto model templates Allow setting render types for model templates --- .../models/model/ModelTemplate.java.patch | 25 ++-------- .../vanilla/ModelTemplateWithCustomData.java | 50 +++++++++++++++++++ .../common/data/vanilla/package-info.java | 13 +++++ .../extensions/IModelTemplateExtension.java | 17 +++---- .../resources/META-INF/accesstransformer.cfg | 1 + .../models/item/vanilla_model_gen_item.json | 1 + .../neoforge/debug/VanillaDataGenTest.java | 5 +- 7 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 src/main/java/net/neoforged/neoforge/common/data/vanilla/ModelTemplateWithCustomData.java create mode 100644 src/main/java/net/neoforged/neoforge/common/data/vanilla/package-info.java diff --git a/patches/net/minecraft/data/models/model/ModelTemplate.java.patch b/patches/net/minecraft/data/models/model/ModelTemplate.java.patch index 749fd1f7f5..0471356537 100644 --- a/patches/net/minecraft/data/models/model/ModelTemplate.java.patch +++ b/patches/net/minecraft/data/models/model/ModelTemplate.java.patch @@ -6,9 +6,9 @@ -public class ModelTemplate { +public class ModelTemplate implements net.neoforged.neoforge.common.extensions.IModelTemplateExtension { - private final Optional model; - private final Set requiredSlots; - private final Optional suffix; + public final Optional model; + public final Set requiredSlots; + public final Optional suffix; @@ -29,22 +_,30 @@ return ModelLocationUtils.getModelLocation(p_309103_, this.suffix.orElse("")); } @@ -49,22 +49,3 @@ public JsonObject createBaseTemplate(ResourceLocation p_266830_, Map p_266912_) { JsonObject jsonobject = new JsonObject(); this.model.ifPresent(p_176461_ -> jsonobject.addProperty("parent", p_176461_.toString())); -@@ -75,6 +_,18 @@ - private Map createMap(TextureMapping p_125609_) { - return Streams.concat(this.requiredSlots.stream(), p_125609_.getForced()).collect(ImmutableMap.toImmutableMap(Function.identity(), p_125609_::get)); - } -+ -+ // region: NeoForge Additions -+ @Override -+ public Optional model() { -+ return model; -+ } -+ -+ @Override -+ public Optional suffix() { -+ return suffix; -+ } -+ // endregion - - public interface JsonFactory { - JsonObject create(ResourceLocation p_266987_, Map p_266933_); diff --git a/src/main/java/net/neoforged/neoforge/common/data/vanilla/ModelTemplateWithCustomData.java b/src/main/java/net/neoforged/neoforge/common/data/vanilla/ModelTemplateWithCustomData.java new file mode 100644 index 0000000000..916fb23792 --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/common/data/vanilla/ModelTemplateWithCustomData.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.common.data.vanilla; + +import com.google.gson.JsonObject; +import java.util.Map; +import java.util.Optional; +import net.minecraft.data.models.model.ModelTemplate; +import net.minecraft.data.models.model.TextureSlot; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.common.data.ExistingFileHelper; +import org.jetbrains.annotations.Nullable; + +public class ModelTemplateWithCustomData extends ModelTemplate { + @Nullable + private ResourceLocation renderType; + + public ModelTemplateWithCustomData(Optional parent, Optional pathSuffix, TextureSlot... requiredSlots) { + super(parent, pathSuffix, requiredSlots); + } + + public ModelTemplateWithCustomData(ModelTemplate template) { + this(template.model, template.suffix, template.requiredSlots.toArray(TextureSlot[]::new)); + } + + @Override + public ModelTemplate withRenderType(ResourceLocation renderType) { + this.renderType = renderType; + return this; + } + + @Override + public JsonObject createBaseTemplate(ResourceLocation modelPath, Map textureMap, @Nullable ExistingFileHelper fileHelper) { + var json = super.createBaseTemplate(modelPath, textureMap, fileHelper); + + if (renderType != null) { + json.addProperty("render_type", renderType.toString()); + } + + return json; + } + + @Override + public JsonObject createBaseTemplate(ResourceLocation modelPath, Map textureMap) { + return createBaseTemplate(modelPath, textureMap, null); + } +} diff --git a/src/main/java/net/neoforged/neoforge/common/data/vanilla/package-info.java b/src/main/java/net/neoforged/neoforge/common/data/vanilla/package-info.java new file mode 100644 index 0000000000..723cbb9df0 --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/common/data/vanilla/package-info.java @@ -0,0 +1,13 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +@FieldsAreNonnullByDefault +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +package net.neoforged.neoforge.common.data.vanilla; + +import javax.annotation.ParametersAreNonnullByDefault; +import net.minecraft.FieldsAreNonnullByDefault; +import net.minecraft.MethodsReturnNonnullByDefault; diff --git a/src/main/java/net/neoforged/neoforge/common/extensions/IModelTemplateExtension.java b/src/main/java/net/neoforged/neoforge/common/extensions/IModelTemplateExtension.java index d78ff84ba0..7a38f0dddb 100644 --- a/src/main/java/net/neoforged/neoforge/common/extensions/IModelTemplateExtension.java +++ b/src/main/java/net/neoforged/neoforge/common/extensions/IModelTemplateExtension.java @@ -9,7 +9,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.util.Map; -import java.util.Optional; import java.util.function.BiConsumer; import java.util.function.Supplier; import net.minecraft.data.models.model.ModelLocationUtils; @@ -20,15 +19,16 @@ import net.minecraft.world.level.block.Block; import net.neoforged.neoforge.client.model.generators.ModelProvider; import net.neoforged.neoforge.common.data.ExistingFileHelper; +import net.neoforged.neoforge.common.data.vanilla.ModelTemplateWithCustomData; import org.jetbrains.annotations.Nullable; public interface IModelTemplateExtension { default ResourceLocation create(Block block, TextureMapping textures, BiConsumer> modelOutput, @Nullable ExistingFileHelper fileHelper) { - return create(ModelLocationUtils.getModelLocation(block, suffix().orElse("")), textures, modelOutput, fileHelper); + return create(ModelLocationUtils.getModelLocation(block, self().suffix.orElse("")), textures, modelOutput, fileHelper); } default ResourceLocation createWithSuffix(Block block, String suffix, TextureMapping textures, BiConsumer> modelOutput, @Nullable ExistingFileHelper fileHelper) { - return create(ModelLocationUtils.getModelLocation(block, suffix + suffix().orElse("")), textures, modelOutput, fileHelper); + return create(ModelLocationUtils.getModelLocation(block, suffix + self().suffix.orElse("")), textures, modelOutput, fileHelper); } default ResourceLocation createWithOverride(Block block, String suffix, TextureMapping textures, BiConsumer> modelOutput, @Nullable ExistingFileHelper fileHelper) { @@ -42,7 +42,7 @@ default ResourceLocation create(ResourceLocation modelPath, TextureMapping textu default JsonObject createBaseTemplate(ResourceLocation modelPath, Map textureMap, @Nullable ExistingFileHelper fileHelper) { var modelJson = new JsonObject(); - model().ifPresent(parentPath -> { + self().model.ifPresent(parentPath -> { modelJson.addProperty("parent", parentPath.toString()); if (fileHelper != null) { @@ -67,12 +67,11 @@ default JsonObject createBaseTemplate(ResourceLocation modelPath, Map model(); - - Optional suffix(); } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 978077be96..4f8e32d6cc 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -520,6 +520,7 @@ public net.minecraft.data.models.BlockModelGenerators$WoodProvider public net.minecraft.data.models.ItemModelGenerators *() public net.minecraft.data.models.ItemModelGenerators * public net.minecraft.data.models.model.ModelTemplates *() +public net.minecraft.data.models.model.ModelTemplate * public net.minecraft.data.models.model.TexturedModel createDefault(Ljava/util/function/Function;Lnet/minecraft/data/models/model/ModelTemplate;)Lnet/minecraft/data/models/model/TexturedModel$Provider; public net.minecraft.data.models.model.TextureSlot create(Ljava/lang/String;)Lnet/minecraft/data/models/model/TextureSlot; public net.minecraft.data.models.model.TextureSlot create(Ljava/lang/String;Lnet/minecraft/data/models/model/TextureSlot;)Lnet/minecraft/data/models/model/TextureSlot; \ No newline at end of file diff --git a/tests/src/generated/resources/assets/neotests_test_model_generators/models/item/vanilla_model_gen_item.json b/tests/src/generated/resources/assets/neotests_test_model_generators/models/item/vanilla_model_gen_item.json index c2d3d5274d..f7096f96c5 100644 --- a/tests/src/generated/resources/assets/neotests_test_model_generators/models/item/vanilla_model_gen_item.json +++ b/tests/src/generated/resources/assets/neotests_test_model_generators/models/item/vanilla_model_gen_item.json @@ -1,5 +1,6 @@ { "parent": "minecraft:item/generated", + "render_type": "minecraft:cutout", "textures": { "layer0": "neotests_test_model_generators:item/vanilla_model_gen_item" } diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/VanillaDataGenTest.java b/tests/src/main/java/net/neoforged/neoforge/debug/VanillaDataGenTest.java index 1c7c58e0fb..44e99f5e5e 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/VanillaDataGenTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/VanillaDataGenTest.java @@ -50,13 +50,14 @@ protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerat blockModels.createTrivialCube(block.value()); // generate simple flat model for our item - // itemModels.generateFlatItem(item.value(), ModelTemplates.FLAT_ITEM); + // itemModels.generateFlatItem(item.value(), ModelTemplates.FLAT_ITEM.withRenderType(ResourceLocation.withDefaultNamespace("cutout"))); // generates the same output as generateFlatItem // but running through custom model building instead of templates itemModels.generateCustom(item.value(), builder -> builder .parent(new ModelFile.ExistingModelFile(ModelLocationUtils.decorateItemModelLocation("generated"), itemModels.fileHelper)) - .texture(TextureSlot.LAYER0, TextureMapping.getItemTexture(item.value()))); + .texture(TextureSlot.LAYER0, TextureMapping.getItemTexture(item.value())) + .renderType("cutout")); // It is possible to tell system to not generate a BlockItem model for a matching Block // this allows generating your own custom Item model for your BlockItem