remove hardcoding of variant provider

This commit is contained in:
Meredith Espinosa 2019-06-19 15:07:14 -07:00
parent 9b560e0af7
commit 12d6874b95
3 changed files with 18 additions and 19 deletions

View File

@ -2,6 +2,7 @@ package io.github.cottonmc.slopetest;
import io.github.cottonmc.slopetest.block.SlopeTestBlock; import io.github.cottonmc.slopetest.block.SlopeTestBlock;
import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity; import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity;
import io.github.cottonmc.slopetest.model.SlopeModelVariantProvider;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
@ -13,6 +14,8 @@ import java.util.function.Supplier;
public class SlopeTest implements ModInitializer { public class SlopeTest implements ModInitializer {
public static final String MODID = "slopetest"; 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); public static final Block SLOPE = register("slope", new SlopeTestBlock(), ItemGroup.DECORATIONS);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -1,13 +1,15 @@
package io.github.cottonmc.slopetest; 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.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry; import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry;
import net.minecraft.util.math.Direction;
public class SlopeTestClient implements ClientModInitializer { public class SlopeTestClient implements ClientModInitializer {
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
//BlockEntityRendererRegistry.INSTANCE.register(SlopeTestEntity.class, new SlopeTestRenderer()); ModelLoadingRegistry.INSTANCE.registerVariantProvider(rm -> SlopeTest.provider);
ModelLoadingRegistry.INSTANCE.registerVariantProvider(rm -> new SlopeModelVariantProvider()); SlopeTest.provider.registerTemplateBlock(SlopeTest.SLOPE, SlopeTest.SLOPE.getDefaultState().with(SlopeTestBlock.FACING, Direction.SOUTH), SlopeTestModel::new);
} }
} }

View File

@ -1,38 +1,32 @@
package io.github.cottonmc.slopetest.model; package io.github.cottonmc.slopetest.model;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Function;
import io.github.cottonmc.slopetest.SlopeTest;
import io.github.cottonmc.slopetest.block.SlopeTestBlock;
import net.fabricmc.fabric.api.client.model.ModelProviderContext; import net.fabricmc.fabric.api.client.model.ModelProviderContext;
import net.fabricmc.fabric.api.client.model.ModelProviderException; import net.fabricmc.fabric.api.client.model.ModelProviderException;
import net.fabricmc.fabric.api.client.model.ModelVariantProvider; import net.fabricmc.fabric.api.client.model.ModelVariantProvider;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.render.block.BlockModels; import net.minecraft.client.render.block.BlockModels;
import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.util.ModelIdentifier; import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
public class SlopeModelVariantProvider implements ModelVariantProvider { public class SlopeModelVariantProvider implements ModelVariantProvider {
private final HashMap<ModelIdentifier, UnbakedModel> variants = new HashMap<>(); private final HashMap<ModelIdentifier, UnbakedModel> variants = new HashMap<>();
public SlopeModelVariantProvider() { 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)));
}
@Override @Override
public UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException { public UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException {
return variants.get(modelId); return variants.get(modelId);
} }
public void registerTemplateBlock(Block block, BlockState itemState, Function<BlockState, SimpleModel> 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));
}
} }