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 fe6dbfc..1132e95 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java @@ -5,12 +5,15 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.state.StateManager; @@ -25,6 +28,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.BlockView; import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; public abstract class TemplateBlock extends Block implements BlockEntityProvider { public static final IntProperty LIGHT = IntProperty.of("light", 0, 15); @@ -111,6 +115,18 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider super.onStateReplaced(state, world, pos, newState, moved); } + @Override + public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { + //Load the BlockEntityTag clientside, which fixes the template briefly showing its default state when placing it. + //I'm surprised this doesn't happen by default; the BlockEntityTag stuff is only done serverside. + if(world.isClient && world.getBlockEntity(pos) instanceof TemplateEntity be) { + NbtCompound tag = BlockItem.getBlockEntityNbt(stack); + if(tag != null) be.readNbt(tag); + } + + super.onPlaced(world, pos, state, placer, stack); + } + @Override public boolean emitsRedstonePower(BlockState state) { return state.get(REDSTONE); diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeQuadTransformFactory.java b/src/main/java/io/github/cottonmc/templates/model/SlopeQuadTransformFactory.java index 91b84ff..fd3671d 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeQuadTransformFactory.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeQuadTransformFactory.java @@ -4,12 +4,15 @@ import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView; import net.fabricmc.fabric.api.renderer.v1.render.RenderContext; import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachedBlockView; -import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.client.color.block.BlockColorProvider; import net.minecraft.client.texture.Sprite; +import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtHelper; +import net.minecraft.registry.Registries; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.random.Random; @@ -28,28 +31,24 @@ public class SlopeQuadTransformFactory implements TemplateQuadTransformFactory { @Override public @NotNull RenderContext.QuadTransform blockTransformer(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier randomSupplier) { - Object renderAttach = ((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos); - BlockState template = (renderAttach instanceof BlockState s) ? s : Blocks.AIR.getDefaultState(); - Block block = template.getBlock(); + BlockState template = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; + if(template == null || template.isAir()) return new Transformer(tam.getDefaultAppearance(), 0xFFFFFFFF); - TemplateAppearance appearance; - int globalTint = 0xFFFFFF; - - if(block == Blocks.AIR) { - appearance = tam.getDefaultAppearance(); - } else { - appearance = tam.getAppearance(template); - - BlockColorProvider tint = ColorProviderRegistry.BLOCK.get(block); - if(tint != null) globalTint = 0xFF000000 | tint.getColor(template, blockView, pos, 1); - } - - return new Transformer(appearance, globalTint); + BlockColorProvider prov = ColorProviderRegistry.BLOCK.get(template.getBlock()); + int globalTint = prov != null ? prov.getColor(state, blockView, pos, 1) : 0xFFFFFFFF; + return new Transformer(tam.getAppearance(template), globalTint); } @Override public @NotNull RenderContext.QuadTransform itemTransformer(ItemStack stack, Supplier randomSupplier) { - return new Transformer(tam.getDefaultAppearance(), 0xFFFFFF); + //cheeky: if the item has NBT data, pluck out the blockstate from it + NbtCompound tag = BlockItem.getBlockEntityNbt(stack); + if(tag != null && tag.contains("BlockState")) { + BlockState state = NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), tag.getCompound("BlockState")); + if(!state.isAir()) return new Transformer(tam.getAppearance(state), 0xFFFFFFFF); + } + + return new Transformer(tam.getDefaultAppearance(), 0xFFFFFFFF); } public static record Transformer(TemplateAppearance appearance, int color) implements RenderContext.QuadTransform {