diff --git a/src/main/java/io/github/cottonmc/templates/TemplatesClient.java b/src/main/java/io/github/cottonmc/templates/TemplatesClient.java index e6a14d9..b08b57f 100644 --- a/src/main/java/io/github/cottonmc/templates/TemplatesClient.java +++ b/src/main/java/io/github/cottonmc/templates/TemplatesClient.java @@ -6,8 +6,13 @@ import io.github.cottonmc.templates.model.TemplateModelVariantProvider; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap; import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry; +import net.fabricmc.fabric.api.resource.ResourceManagerHelper; +import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.RenderLayer; +import net.minecraft.resource.ResourceManager; +import net.minecraft.resource.ResourceType; +import net.minecraft.util.Identifier; import net.minecraft.util.math.ChunkSectionPos; import net.minecraft.util.math.Direction; @@ -26,9 +31,29 @@ public class TemplatesClient implements ClientModInitializer { } }; - ModelLoadingRegistry.INSTANCE.registerVariantProvider(rm -> provider); - provider.registerTemplateModels2(Templates.SLOPE, Templates.SLOPE.getDefaultState().with(SlopeBlock.FACING, Direction.SOUTH), SlopeUnbakedModel::new); + ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() { + @Override + public Identifier getFabricId() { + return Templates.id("dump-caches"); + } + + @Override + public void reload(ResourceManager resourceManager) { + provider.dumpCache(); + } + }); + ModelLoadingRegistry.INSTANCE.registerVariantProvider(rm -> provider); BlockRenderLayerMap.INSTANCE.putBlock(Templates.SLOPE, RenderLayer.getCutout()); + + //ADDON DEVELOEPRS: do this! + provider.registerTemplateModels( + //block + Templates.SLOPE, + //the blockstate you'd like the item model to show + Templates.SLOPE.getDefaultState().with(SlopeBlock.FACING, Direction.SOUTH), + //Function that creates your model + SlopeUnbakedModel::new + ); } } diff --git a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java index 029bf8f..36f7d68 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java @@ -2,7 +2,6 @@ package io.github.cottonmc.templates.block; import io.github.cottonmc.templates.Templates; import io.github.cottonmc.templates.block.entity.TemplateEntity; -import io.github.cottonmc.templates.util.StateContainer; import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; @@ -25,7 +24,7 @@ import net.minecraft.util.math.Direction; import net.minecraft.world.BlockView; import net.minecraft.world.World; -public abstract class TemplateBlock extends Block implements BlockEntityProvider, StateContainer { +public abstract class TemplateBlock extends Block implements BlockEntityProvider { public static final IntProperty LIGHT = IntProperty.of("light", 0, 15); public static final BooleanProperty REDSTONE = BooleanProperty.of("redstone"); @@ -131,19 +130,6 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider return 0; } - @Override - public BlockState getContainedState(World world, BlockPos pos) { - BlockEntity be = world.getBlockEntity(pos); - if(be instanceof TemplateEntity) return ((TemplateEntity) be).getRenderedState(); - return Blocks.AIR.getDefaultState(); - } - - @Override - public void setContainedState(World world, BlockPos pos, BlockState state) { - BlockEntity be = world.getBlockEntity(pos); - if(be instanceof TemplateEntity) ((TemplateEntity) be).setRenderedState(state); - } - //TODO: pass to Block.Settings // "Cannot reference 'TemplateBlock.luminance' before supertype constructor has been called" public int luminance(BlockState state) { diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java b/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java index 6bb8bcd..a056f27 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java @@ -1,6 +1,5 @@ package io.github.cottonmc.templates.model; -import io.github.cottonmc.templates.util.SpriteSet; import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; import net.fabricmc.fabric.api.renderer.v1.RendererAccess; import net.fabricmc.fabric.api.renderer.v1.material.BlendMode; diff --git a/src/main/java/io/github/cottonmc/templates/util/SpriteSet.java b/src/main/java/io/github/cottonmc/templates/model/SpriteSet.java similarity index 98% rename from src/main/java/io/github/cottonmc/templates/util/SpriteSet.java rename to src/main/java/io/github/cottonmc/templates/model/SpriteSet.java index 1dfdbcf..37b99dc 100644 --- a/src/main/java/io/github/cottonmc/templates/util/SpriteSet.java +++ b/src/main/java/io/github/cottonmc/templates/model/SpriteSet.java @@ -1,4 +1,4 @@ -package io.github.cottonmc.templates.util; +package io.github.cottonmc.templates.model; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedQuad; diff --git a/src/main/java/io/github/cottonmc/templates/model/TemplateModelVariantProvider.java b/src/main/java/io/github/cottonmc/templates/model/TemplateModelVariantProvider.java index 2b5d5c2..3b4cc18 100644 --- a/src/main/java/io/github/cottonmc/templates/model/TemplateModelVariantProvider.java +++ b/src/main/java/io/github/cottonmc/templates/model/TemplateModelVariantProvider.java @@ -9,25 +9,40 @@ import net.minecraft.client.render.block.BlockModels; import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.util.ModelIdentifier; import net.minecraft.registry.Registries; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; +import java.util.Map; import java.util.function.Function; +import java.util.function.Supplier; public class TemplateModelVariantProvider implements ModelVariantProvider { - - private final HashMap variants = new HashMap<>(); - - public TemplateModelVariantProvider() {} + private final Map> factories = new HashMap<>(); + private final Map cache = new HashMap<>(); @Override public UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException { - return variants.get(modelId); + UnbakedModel cacheResult = cache.get(modelId); + if(cacheResult != null) return cacheResult; + + //Either we have a factory for this model (just haven't cached its output yet), + Supplier factory = factories.get(modelId); + if(factory != null) { + UnbakedModel freshModel = factory.get(); + cache.put(modelId, freshModel); + return freshModel; + } + + //or we don't cover this model at all. + return null; } - public void registerTemplateModels2(Block block, BlockState itemState, Function model) { - for(BlockState state : block.getStateManager().getStates()) { - variants.put(BlockModels.getModelId(state), model.apply(state)); - } - variants.put(new ModelIdentifier(Registries.ITEM.getId(block.asItem()), "inventory"), model.apply(itemState)); + public void registerTemplateModels(Block block, BlockState itemState, Function model) { + for(BlockState state : block.getStateManager().getStates()) factories.put(BlockModels.getModelId(state), () -> model.apply(state)); + factories.put(new ModelIdentifier(Registries.ITEM.getId(block.asItem()), "inventory"), () -> model.apply(itemState)); + } + + public void dumpCache() { + cache.clear(); } } diff --git a/src/main/java/io/github/cottonmc/templates/util/StateContainer.java b/src/main/java/io/github/cottonmc/templates/util/StateContainer.java deleted file mode 100644 index 8719409..0000000 --- a/src/main/java/io/github/cottonmc/templates/util/StateContainer.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.github.cottonmc.templates.util; - -import net.minecraft.block.BlockState; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; - -public interface StateContainer { - BlockState getContainedState(World world, BlockPos pos); - - void setContainedState(World world, BlockPos pos, BlockState state); -}