From 12d6874b95f5f9d9c06bb947800aef45805cd1f9 Mon Sep 17 00:00:00 2001 From: Meredith Espinosa Date: Wed, 19 Jun 2019 15:07:14 -0700 Subject: [PATCH] remove hardcoding of variant provider --- .../github/cottonmc/slopetest/SlopeTest.java | 3 +++ .../cottonmc/slopetest/SlopeTestClient.java | 8 +++--- .../model/SlopeModelVariantProvider.java | 26 +++++++------------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/github/cottonmc/slopetest/SlopeTest.java b/src/main/java/io/github/cottonmc/slopetest/SlopeTest.java index 79fb26a..424ca85 100644 --- a/src/main/java/io/github/cottonmc/slopetest/SlopeTest.java +++ b/src/main/java/io/github/cottonmc/slopetest/SlopeTest.java @@ -2,6 +2,7 @@ package io.github.cottonmc.slopetest; import io.github.cottonmc.slopetest.block.SlopeTestBlock; import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity; +import io.github.cottonmc.slopetest.model.SlopeModelVariantProvider; import net.fabricmc.api.ModInitializer; import net.minecraft.block.Block; import net.minecraft.block.entity.BlockEntity; @@ -13,6 +14,8 @@ import java.util.function.Supplier; public class SlopeTest implements ModInitializer { public static final String MODID = "slopetest"; + //define/create here so that it always exists when called in a client initializer, regardless of load order + public static SlopeModelVariantProvider provider = new SlopeModelVariantProvider(); public static final Block SLOPE = register("slope", new SlopeTestBlock(), ItemGroup.DECORATIONS); @SuppressWarnings("unchecked") diff --git a/src/main/java/io/github/cottonmc/slopetest/SlopeTestClient.java b/src/main/java/io/github/cottonmc/slopetest/SlopeTestClient.java index df51c25..3983a07 100644 --- a/src/main/java/io/github/cottonmc/slopetest/SlopeTestClient.java +++ b/src/main/java/io/github/cottonmc/slopetest/SlopeTestClient.java @@ -1,13 +1,15 @@ package io.github.cottonmc.slopetest; -import io.github.cottonmc.slopetest.model.SlopeModelVariantProvider; +import io.github.cottonmc.slopetest.block.SlopeTestBlock; +import io.github.cottonmc.slopetest.model.SlopeTestModel; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry; +import net.minecraft.util.math.Direction; public class SlopeTestClient implements ClientModInitializer { @Override public void onInitializeClient() { - //BlockEntityRendererRegistry.INSTANCE.register(SlopeTestEntity.class, new SlopeTestRenderer()); - ModelLoadingRegistry.INSTANCE.registerVariantProvider(rm -> new SlopeModelVariantProvider()); + ModelLoadingRegistry.INSTANCE.registerVariantProvider(rm -> SlopeTest.provider); + SlopeTest.provider.registerTemplateBlock(SlopeTest.SLOPE, SlopeTest.SLOPE.getDefaultState().with(SlopeTestBlock.FACING, Direction.SOUTH), SlopeTestModel::new); } } diff --git a/src/main/java/io/github/cottonmc/slopetest/model/SlopeModelVariantProvider.java b/src/main/java/io/github/cottonmc/slopetest/model/SlopeModelVariantProvider.java index e13f600..d14c2a0 100644 --- a/src/main/java/io/github/cottonmc/slopetest/model/SlopeModelVariantProvider.java +++ b/src/main/java/io/github/cottonmc/slopetest/model/SlopeModelVariantProvider.java @@ -1,38 +1,32 @@ package io.github.cottonmc.slopetest.model; import java.util.HashMap; - -import io.github.cottonmc.slopetest.SlopeTest; -import io.github.cottonmc.slopetest.block.SlopeTestBlock; +import java.util.function.Function; import net.fabricmc.fabric.api.client.model.ModelProviderContext; import net.fabricmc.fabric.api.client.model.ModelProviderException; import net.fabricmc.fabric.api.client.model.ModelVariantProvider; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.client.render.block.BlockModels; import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.util.ModelIdentifier; -import net.minecraft.util.BlockRotation; -import net.minecraft.util.math.Direction; import net.minecraft.util.registry.Registry; public class SlopeModelVariantProvider implements ModelVariantProvider { private final HashMap variants = new HashMap<>(); - public SlopeModelVariantProvider() { - // A bit ugly to hard-code this in the constructor, but however it is done, - // best to have variants for all the mod's blocks in a single provider/map - // instance so that model loader doesn't have to query a large number of providers. - - for(BlockState state : SlopeTest.SLOPE.getStateFactory().getStates()) { - variants.put(BlockModels.getModelId(state), (SimpleUnbakedModel)() -> new SlopeTestModel(state)); - } - - variants.put(new ModelIdentifier(Registry.ITEM.getId(SlopeTest.SLOPE.asItem()), "inventory"), (SimpleUnbakedModel)() -> new SlopeTestModel(SlopeTest.SLOPE.getDefaultState().with(SlopeTestBlock.FACING, Direction.SOUTH))); - } + public SlopeModelVariantProvider() { } @Override public UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException { return variants.get(modelId); } + + public void registerTemplateBlock(Block block, BlockState itemState, Function model) { + for (BlockState state : block.getStateFactory().getStates()) { + variants.put(BlockModels.getModelId(state), (SimpleUnbakedModel)() -> model.apply(state)); + } + variants.put(new ModelIdentifier(Registry.ITEM.getId(block.asItem()), "inventory"), (SimpleUnbakedModel)() -> model.apply(itemState)); + } }