Refresh unbaked models on f3+t

This commit is contained in:
quat1024 2023-06-15 23:09:57 -04:00
parent f2a60f4e8e
commit aed37a2aaa
6 changed files with 54 additions and 40 deletions

View File

@ -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<BlockState, UnbakedModel> that creates your model
SlopeUnbakedModel::new
);
}
}

View File

@ -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) {

View File

@ -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;

View File

@ -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;

View File

@ -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<ModelIdentifier, UnbakedModel> variants = new HashMap<>();
public TemplateModelVariantProvider() {}
private final Map<ModelIdentifier, Supplier<UnbakedModel>> factories = new HashMap<>();
private final Map<ModelIdentifier, UnbakedModel> 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<UnbakedModel> 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<BlockState, UnbakedModel> 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<BlockState, UnbakedModel> 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();
}
}

View File

@ -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);
}