Skip to content

Commit

Permalink
Allow setting custom properties onto model templates
Browse files Browse the repository at this point in the history
Allow setting render types for model templates
  • Loading branch information
ApexModder committed Nov 23, 2024
1 parent b4230f1 commit 611832a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 33 deletions.
25 changes: 3 additions & 22 deletions patches/net/minecraft/data/models/model/ModelTemplate.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

-public class ModelTemplate {
+public class ModelTemplate implements net.neoforged.neoforge.common.extensions.IModelTemplateExtension {
private final Optional<ResourceLocation> model;
private final Set<TextureSlot> requiredSlots;
private final Optional<String> suffix;
public final Optional<ResourceLocation> model;
public final Set<TextureSlot> requiredSlots;
public final Optional<String> suffix;
@@ -29,22 +_,30 @@
return ModelLocationUtils.getModelLocation(p_309103_, this.suffix.orElse(""));
}
Expand Down Expand Up @@ -49,22 +49,3 @@
public JsonObject createBaseTemplate(ResourceLocation p_266830_, Map<TextureSlot, ResourceLocation> p_266912_) {
JsonObject jsonobject = new JsonObject();
this.model.ifPresent(p_176461_ -> jsonobject.addProperty("parent", p_176461_.toString()));
@@ -75,6 +_,18 @@
private Map<TextureSlot, ResourceLocation> 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<ResourceLocation> model() {
+ return model;
+ }
+
+ @Override
+ public Optional<String> suffix() {
+ return suffix;
+ }
+ // endregion

public interface JsonFactory {
JsonObject create(ResourceLocation p_266987_, Map<TextureSlot, ResourceLocation> p_266933_);
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation> parent, Optional<String> 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<TextureSlot, ResourceLocation> 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<TextureSlot, ResourceLocation> textureMap) {
return createBaseTemplate(modelPath, textureMap, null);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ResourceLocation, Supplier<JsonElement>> 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<ResourceLocation, Supplier<JsonElement>> 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<ResourceLocation, Supplier<JsonElement>> modelOutput, @Nullable ExistingFileHelper fileHelper) {
Expand All @@ -42,7 +42,7 @@ default ResourceLocation create(ResourceLocation modelPath, TextureMapping textu
default JsonObject createBaseTemplate(ResourceLocation modelPath, Map<TextureSlot, ResourceLocation> textureMap, @Nullable ExistingFileHelper fileHelper) {
var modelJson = new JsonObject();

model().ifPresent(parentPath -> {
self().model.ifPresent(parentPath -> {
modelJson.addProperty("parent", parentPath.toString());

if (fileHelper != null) {
Expand All @@ -67,12 +67,11 @@ default JsonObject createBaseTemplate(ResourceLocation modelPath, Map<TextureSlo
return modelJson;
}

default ModelTemplate withRenderType(ResourceLocation renderType) {
return new ModelTemplateWithCustomData(self()).withRenderType(renderType);
}

private ModelTemplate self() {
return (ModelTemplate) this;
}

// TODO: Potentially replace these with AT
Optional<ResourceLocation> model();

Optional<String> suffix();
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"parent": "minecraft:item/generated",
"render_type": "minecraft:cutout",
"textures": {
"layer0": "neotests_test_model_generators:item/vanilla_model_gen_item"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 611832a

Please sign in to comment.