diff --git a/gradle.properties b/gradle.properties index de06d498..bb0ee0ba 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ yarn_mappings=6 loader_version=0.11.3 # Mod Properties -mod_version = 0.1.13 +mod_version = 0.1.14 maven_group = ru.bclib archives_base_name = bclib diff --git a/src/main/java/ru/bclib/blockentities/DynamicBlockEntityType.java b/src/main/java/ru/bclib/blockentities/DynamicBlockEntityType.java new file mode 100644 index 00000000..a8ebdde3 --- /dev/null +++ b/src/main/java/ru/bclib/blockentities/DynamicBlockEntityType.java @@ -0,0 +1,28 @@ +package ru.bclib.blockentities; + +import com.google.common.collect.Sets; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; + +import java.util.Collections; +import java.util.Set; +import java.util.function.Supplier; + +public class DynamicBlockEntityType extends BlockEntityType { + + private final Set validBlocks = Sets.newHashSet(); + + public DynamicBlockEntityType(Supplier supplier) { + super(supplier, Collections.emptySet(), null); + } + + @Override + public boolean isValid(Block block) { + return validBlocks.contains(block); + } + + public void registerBlock(Block block) { + validBlocks.add(block); + } +} diff --git a/src/main/java/ru/bclib/client/BCLibClient.java b/src/main/java/ru/bclib/client/BCLibClient.java index c3f75011..ada4ab03 100644 --- a/src/main/java/ru/bclib/client/BCLibClient.java +++ b/src/main/java/ru/bclib/client/BCLibClient.java @@ -1,24 +1,11 @@ -package ru.bclib.client; - -import java.util.Arrays; - -import net.fabricmc.api.ClientModInitializer; -import ru.bclib.blocks.BaseChestBlock; -import ru.bclib.blocks.BaseSignBlock; -import ru.bclib.client.render.BaseChestBlockEntityRenderer; -import ru.bclib.client.render.BaseSignBlockEntityRenderer; -import ru.bclib.registry.BaseBlockEntities; -import ru.bclib.registry.BaseBlockEntityRenders; - -public class BCLibClient implements ClientModInitializer { - @Override - public void onInitializeClient() { - BaseBlockEntityRenders.register(); - Arrays.stream(BaseBlockEntities.getChests()).forEach(chest -> { - BaseChestBlockEntityRenderer.registerRenderLayer((BaseChestBlock) chest); - }); - Arrays.stream(BaseBlockEntities.getSigns()).forEach(sign -> { - BaseSignBlockEntityRenderer.registerRenderLayer((BaseSignBlock) sign); - }); - } -} +package ru.bclib.client; + +import net.fabricmc.api.ClientModInitializer; +import ru.bclib.registry.BaseBlockEntityRenders; + +public class BCLibClient implements ClientModInitializer { + @Override + public void onInitializeClient() { + BaseBlockEntityRenders.register(); + } +} diff --git a/src/main/java/ru/bclib/client/render/BaseChestBlockEntityRenderer.java b/src/main/java/ru/bclib/client/render/BaseChestBlockEntityRenderer.java index a1b987e6..caacf16f 100644 --- a/src/main/java/ru/bclib/client/render/BaseChestBlockEntityRenderer.java +++ b/src/main/java/ru/bclib/client/render/BaseChestBlockEntityRenderer.java @@ -1,176 +1,176 @@ -package ru.bclib.client.render; - -import java.util.HashMap; - -import com.google.common.collect.Maps; -import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.vertex.VertexConsumer; -import com.mojang.math.Vector3f; - -import it.unimi.dsi.fastutil.floats.Float2FloatFunction; -import it.unimi.dsi.fastutil.ints.Int2IntFunction; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.model.geom.ModelPart; -import net.minecraft.client.renderer.MultiBufferSource; -import net.minecraft.client.renderer.RenderType; -import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; -import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; -import net.minecraft.client.renderer.blockentity.BrightnessCombiner; -import net.minecraft.core.Direction; -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.AbstractChestBlock; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.ChestBlock; -import net.minecraft.world.level.block.DoubleBlockCombiner; -import net.minecraft.world.level.block.DoubleBlockCombiner.NeighborCombineResult; -import net.minecraft.world.level.block.entity.ChestBlockEntity; -import net.minecraft.world.level.block.entity.LidBlockEntity; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.block.state.properties.ChestType; -import ru.bclib.blockentities.BaseChestBlockEntity; -import ru.bclib.blocks.BaseChestBlock; - -@Environment(EnvType.CLIENT) -public class BaseChestBlockEntityRenderer extends BlockEntityRenderer { - private static final HashMap LAYERS = Maps.newHashMap(); - private static final RenderType[] defaultLayer; - - private static final int ID_NORMAL = 0; - private static final int ID_LEFT = 1; - private static final int ID_RIGHT = 2; - - private final ModelPart partA; - private final ModelPart partC; - private final ModelPart partB; - private final ModelPart partRightA; - private final ModelPart partRightC; - private final ModelPart partRightB; - private final ModelPart partLeftA; - private final ModelPart partLeftC; - private final ModelPart partLeftB; - - public BaseChestBlockEntityRenderer(BlockEntityRenderDispatcher blockEntityRenderDispatcher) { - super(blockEntityRenderDispatcher); - - this.partC = new ModelPart(64, 64, 0, 19); - this.partC.addBox(1.0F, 0.0F, 1.0F, 14.0F, 9.0F, 14.0F, 0.0F); - this.partA = new ModelPart(64, 64, 0, 0); - this.partA.addBox(1.0F, 0.0F, 0.0F, 14.0F, 5.0F, 14.0F, 0.0F); - this.partA.y = 9.0F; - this.partA.z = 1.0F; - this.partB = new ModelPart(64, 64, 0, 0); - this.partB.addBox(7.0F, -1.0F, 15.0F, 2.0F, 4.0F, 1.0F, 0.0F); - this.partB.y = 8.0F; - this.partRightC = new ModelPart(64, 64, 0, 19); - this.partRightC.addBox(1.0F, 0.0F, 1.0F, 15.0F, 9.0F, 14.0F, 0.0F); - this.partRightA = new ModelPart(64, 64, 0, 0); - this.partRightA.addBox(1.0F, 0.0F, 0.0F, 15.0F, 5.0F, 14.0F, 0.0F); - this.partRightA.y = 9.0F; - this.partRightA.z = 1.0F; - this.partRightB = new ModelPart(64, 64, 0, 0); - this.partRightB.addBox(15.0F, -1.0F, 15.0F, 1.0F, 4.0F, 1.0F, 0.0F); - this.partRightB.y = 8.0F; - this.partLeftC = new ModelPart(64, 64, 0, 19); - this.partLeftC.addBox(0.0F, 0.0F, 1.0F, 15.0F, 9.0F, 14.0F, 0.0F); - this.partLeftA = new ModelPart(64, 64, 0, 0); - this.partLeftA.addBox(0.0F, 0.0F, 0.0F, 15.0F, 5.0F, 14.0F, 0.0F); - this.partLeftA.y = 9.0F; - this.partLeftA.z = 1.0F; - this.partLeftB = new ModelPart(64, 64, 0, 0); - this.partLeftB.addBox(0.0F, -1.0F, 15.0F, 1.0F, 4.0F, 1.0F, 0.0F); - this.partLeftB.y = 8.0F; - } - - public void render(BaseChestBlockEntity entity, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light, int overlay) { - Level world = entity.getLevel(); - boolean worldExists = world != null; - BlockState blockState = worldExists ? entity.getBlockState() : (BlockState) Blocks.CHEST.defaultBlockState().setValue(ChestBlock.FACING, Direction.SOUTH); - ChestType chestType = blockState.hasProperty(ChestBlock.TYPE) ? (ChestType) blockState.getValue(ChestBlock.TYPE) : ChestType.SINGLE; - Block block = blockState.getBlock(); - if (block instanceof AbstractChestBlock) { - AbstractChestBlock abstractChestBlock = (AbstractChestBlock) block; - boolean isDouble = chestType != ChestType.SINGLE; - float f = ((Direction) blockState.getValue(ChestBlock.FACING)).toYRot(); - NeighborCombineResult propertySource; - - matrices.pushPose(); - matrices.translate(0.5D, 0.5D, 0.5D); - matrices.mulPose(Vector3f.YP.rotationDegrees(-f)); - matrices.translate(-0.5D, -0.5D, -0.5D); - - if (worldExists) { - propertySource = abstractChestBlock.combine(blockState, world, entity.getBlockPos(), true); - } else { - propertySource = DoubleBlockCombiner.Combiner::acceptNone; - } - - float pitch = ((Float2FloatFunction) propertySource.apply(ChestBlock.opennessCombiner((LidBlockEntity) entity))).get(tickDelta); - pitch = 1.0F - pitch; - pitch = 1.0F - pitch * pitch * pitch; - @SuppressWarnings({ "unchecked", "rawtypes" }) - int blockLight = ((Int2IntFunction) propertySource.apply(new BrightnessCombiner())).applyAsInt(light); - - VertexConsumer vertexConsumer = getConsumer(vertexConsumers, block, chestType); - - if (isDouble) { - if (chestType == ChestType.LEFT) { - renderParts(matrices, vertexConsumer, this.partLeftA, this.partLeftB, this.partLeftC, pitch, blockLight, overlay); - } else { - renderParts(matrices, vertexConsumer, this.partRightA, this.partRightB, this.partRightC, pitch, blockLight, overlay); - } - } else { - renderParts(matrices, vertexConsumer, this.partA, this.partB, this.partC, pitch, blockLight, overlay); - } - - matrices.popPose(); - } - } - - private void renderParts(PoseStack matrices, VertexConsumer vertices, ModelPart modelPart, ModelPart modelPart2, ModelPart modelPart3, float pitch, int light, int overlay) { - modelPart.xRot = -(pitch * 1.5707964F); - modelPart2.xRot = modelPart.xRot; - modelPart.render(matrices, vertices, light, overlay); - modelPart2.render(matrices, vertices, light, overlay); - modelPart3.render(matrices, vertices, light, overlay); - } - - private static RenderType getChestTexture(ChestType type, RenderType[] layers) { - switch (type) { - case LEFT: - return layers[ID_LEFT]; - case RIGHT: - return layers[ID_RIGHT]; - case SINGLE: - default: - return layers[ID_NORMAL]; - } - } - - public static VertexConsumer getConsumer(MultiBufferSource provider, Block block, ChestType chestType) { - RenderType[] layers = LAYERS.getOrDefault(block, defaultLayer); - return provider.getBuffer(getChestTexture(chestType, layers)); - } - - public static void registerRenderLayer(BaseChestBlock block) { - ResourceLocation blockId = Registry.BLOCK.getKey(block); - String modId = blockId.getNamespace(); - String path = blockId.getPath(); - LAYERS.put(block, new RenderType[] { - RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + ".png")), - RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_left.png")), - RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_right.png")) - }); - } - - static { - defaultLayer = new RenderType[] { - RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal.png")), - RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_left.png")), - RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_right.png")) - }; - } -} +package ru.bclib.client.render; + +import java.util.HashMap; + +import com.google.common.collect.Maps; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.math.Vector3f; + +import it.unimi.dsi.fastutil.floats.Float2FloatFunction; +import it.unimi.dsi.fastutil.ints.Int2IntFunction; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.model.geom.ModelPart; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.client.renderer.blockentity.BrightnessCombiner; +import net.minecraft.core.Direction; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.AbstractChestBlock; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.ChestBlock; +import net.minecraft.world.level.block.DoubleBlockCombiner; +import net.minecraft.world.level.block.DoubleBlockCombiner.NeighborCombineResult; +import net.minecraft.world.level.block.entity.ChestBlockEntity; +import net.minecraft.world.level.block.entity.LidBlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.ChestType; +import ru.bclib.blockentities.BaseChestBlockEntity; +import ru.bclib.blocks.BaseChestBlock; + +@Environment(EnvType.CLIENT) +public class BaseChestBlockEntityRenderer extends BlockEntityRenderer { + private static final HashMap LAYERS = Maps.newHashMap(); + private static final RenderType[] defaultLayer; + + private static final int ID_NORMAL = 0; + private static final int ID_LEFT = 1; + private static final int ID_RIGHT = 2; + + private final ModelPart partA; + private final ModelPart partC; + private final ModelPart partB; + private final ModelPart partRightA; + private final ModelPart partRightC; + private final ModelPart partRightB; + private final ModelPart partLeftA; + private final ModelPart partLeftC; + private final ModelPart partLeftB; + + public BaseChestBlockEntityRenderer(BlockEntityRenderDispatcher blockEntityRenderDispatcher) { + super(blockEntityRenderDispatcher); + + this.partC = new ModelPart(64, 64, 0, 19); + this.partC.addBox(1.0F, 0.0F, 1.0F, 14.0F, 9.0F, 14.0F, 0.0F); + this.partA = new ModelPart(64, 64, 0, 0); + this.partA.addBox(1.0F, 0.0F, 0.0F, 14.0F, 5.0F, 14.0F, 0.0F); + this.partA.y = 9.0F; + this.partA.z = 1.0F; + this.partB = new ModelPart(64, 64, 0, 0); + this.partB.addBox(7.0F, -1.0F, 15.0F, 2.0F, 4.0F, 1.0F, 0.0F); + this.partB.y = 8.0F; + this.partRightC = new ModelPart(64, 64, 0, 19); + this.partRightC.addBox(1.0F, 0.0F, 1.0F, 15.0F, 9.0F, 14.0F, 0.0F); + this.partRightA = new ModelPart(64, 64, 0, 0); + this.partRightA.addBox(1.0F, 0.0F, 0.0F, 15.0F, 5.0F, 14.0F, 0.0F); + this.partRightA.y = 9.0F; + this.partRightA.z = 1.0F; + this.partRightB = new ModelPart(64, 64, 0, 0); + this.partRightB.addBox(15.0F, -1.0F, 15.0F, 1.0F, 4.0F, 1.0F, 0.0F); + this.partRightB.y = 8.0F; + this.partLeftC = new ModelPart(64, 64, 0, 19); + this.partLeftC.addBox(0.0F, 0.0F, 1.0F, 15.0F, 9.0F, 14.0F, 0.0F); + this.partLeftA = new ModelPart(64, 64, 0, 0); + this.partLeftA.addBox(0.0F, 0.0F, 0.0F, 15.0F, 5.0F, 14.0F, 0.0F); + this.partLeftA.y = 9.0F; + this.partLeftA.z = 1.0F; + this.partLeftB = new ModelPart(64, 64, 0, 0); + this.partLeftB.addBox(0.0F, -1.0F, 15.0F, 1.0F, 4.0F, 1.0F, 0.0F); + this.partLeftB.y = 8.0F; + } + + public void render(BaseChestBlockEntity entity, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light, int overlay) { + Level world = entity.getLevel(); + boolean worldExists = world != null; + BlockState blockState = worldExists ? entity.getBlockState() : (BlockState) Blocks.CHEST.defaultBlockState().setValue(ChestBlock.FACING, Direction.SOUTH); + ChestType chestType = blockState.hasProperty(ChestBlock.TYPE) ? (ChestType) blockState.getValue(ChestBlock.TYPE) : ChestType.SINGLE; + Block block = blockState.getBlock(); + if (block instanceof AbstractChestBlock) { + AbstractChestBlock abstractChestBlock = (AbstractChestBlock) block; + boolean isDouble = chestType != ChestType.SINGLE; + float f = ((Direction) blockState.getValue(ChestBlock.FACING)).toYRot(); + NeighborCombineResult propertySource; + + matrices.pushPose(); + matrices.translate(0.5D, 0.5D, 0.5D); + matrices.mulPose(Vector3f.YP.rotationDegrees(-f)); + matrices.translate(-0.5D, -0.5D, -0.5D); + + if (worldExists) { + propertySource = abstractChestBlock.combine(blockState, world, entity.getBlockPos(), true); + } else { + propertySource = DoubleBlockCombiner.Combiner::acceptNone; + } + + float pitch = ((Float2FloatFunction) propertySource.apply(ChestBlock.opennessCombiner((LidBlockEntity) entity))).get(tickDelta); + pitch = 1.0F - pitch; + pitch = 1.0F - pitch * pitch * pitch; + @SuppressWarnings({ "unchecked", "rawtypes" }) + int blockLight = ((Int2IntFunction) propertySource.apply(new BrightnessCombiner())).applyAsInt(light); + + VertexConsumer vertexConsumer = getConsumer(vertexConsumers, block, chestType); + + if (isDouble) { + if (chestType == ChestType.LEFT) { + renderParts(matrices, vertexConsumer, this.partLeftA, this.partLeftB, this.partLeftC, pitch, blockLight, overlay); + } else { + renderParts(matrices, vertexConsumer, this.partRightA, this.partRightB, this.partRightC, pitch, blockLight, overlay); + } + } else { + renderParts(matrices, vertexConsumer, this.partA, this.partB, this.partC, pitch, blockLight, overlay); + } + + matrices.popPose(); + } + } + + private void renderParts(PoseStack matrices, VertexConsumer vertices, ModelPart modelPart, ModelPart modelPart2, ModelPart modelPart3, float pitch, int light, int overlay) { + modelPart.xRot = -(pitch * 1.5707964F); + modelPart2.xRot = modelPart.xRot; + modelPart.render(matrices, vertices, light, overlay); + modelPart2.render(matrices, vertices, light, overlay); + modelPart3.render(matrices, vertices, light, overlay); + } + + private static RenderType getChestTexture(ChestType type, RenderType[] layers) { + switch (type) { + case LEFT: + return layers[ID_LEFT]; + case RIGHT: + return layers[ID_RIGHT]; + case SINGLE: + default: + return layers[ID_NORMAL]; + } + } + + public static VertexConsumer getConsumer(MultiBufferSource provider, Block block, ChestType chestType) { + RenderType[] layers = LAYERS.getOrDefault(block, defaultLayer); + return provider.getBuffer(getChestTexture(chestType, layers)); + } + + public static void registerRenderLayer(Block block) { + ResourceLocation blockId = Registry.BLOCK.getKey(block); + String modId = blockId.getNamespace(); + String path = blockId.getPath(); + LAYERS.put(block, new RenderType[] { + RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + ".png")), + RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_left.png")), + RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_right.png")) + }); + } + + static { + defaultLayer = new RenderType[] { + RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal.png")), + RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_left.png")), + RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_right.png")) + }; + } +} diff --git a/src/main/java/ru/bclib/client/render/BaseSignBlockEntityRenderer.java b/src/main/java/ru/bclib/client/render/BaseSignBlockEntityRenderer.java index 2704a1e9..6ef7e419 100644 --- a/src/main/java/ru/bclib/client/render/BaseSignBlockEntityRenderer.java +++ b/src/main/java/ru/bclib/client/render/BaseSignBlockEntityRenderer.java @@ -1,113 +1,113 @@ -package ru.bclib.client.render; - -import java.util.HashMap; -import java.util.List; - -import com.google.common.collect.Maps; -import com.mojang.blaze3d.platform.NativeImage; -import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.vertex.VertexConsumer; -import com.mojang.math.Vector3f; - -import net.minecraft.client.gui.Font; -import net.minecraft.client.renderer.MultiBufferSource; -import net.minecraft.client.renderer.RenderType; -import net.minecraft.client.renderer.Sheets; -import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; -import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; -import net.minecraft.client.renderer.blockentity.SignRenderer; -import net.minecraft.client.renderer.blockentity.SignRenderer.SignModel; -import net.minecraft.client.resources.model.Material; -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.FormattedCharSequence; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.SignBlock; -import net.minecraft.world.level.block.StandingSignBlock; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.block.state.properties.WoodType; -import ru.bclib.blockentities.BaseSignBlockEntity; -import ru.bclib.blocks.BaseSignBlock; - -public class BaseSignBlockEntityRenderer extends BlockEntityRenderer { - private static final HashMap LAYERS = Maps.newHashMap(); - private static final RenderType defaultLayer; - private final SignModel model = new SignRenderer.SignModel(); - - public BaseSignBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) { - super(dispatcher); - } - - public void render(BaseSignBlockEntity signBlockEntity, float tickDelta, PoseStack matrixStack, - MultiBufferSource provider, int light, int overlay) { - BlockState state = signBlockEntity.getBlockState(); - matrixStack.pushPose(); - - matrixStack.translate(0.5D, 0.5D, 0.5D); - float angle = -((float) (state.getValue(StandingSignBlock.ROTATION) * 360) / 16.0F); - - BlockState blockState = signBlockEntity.getBlockState(); - if (blockState.getValue(BaseSignBlock.FLOOR)) { - matrixStack.mulPose(Vector3f.YP.rotationDegrees(angle)); - this.model.stick.visible = true; - } else { - matrixStack.mulPose(Vector3f.YP.rotationDegrees(angle + 180)); - matrixStack.translate(0.0D, -0.3125D, -0.4375D); - this.model.stick.visible = false; - } - - matrixStack.pushPose(); - matrixStack.scale(0.6666667F, -0.6666667F, -0.6666667F); - VertexConsumer vertexConsumer = getConsumer(provider, state.getBlock()); - model.sign.render(matrixStack, vertexConsumer, light, overlay); - model.stick.render(matrixStack, vertexConsumer, light, overlay); - matrixStack.popPose(); - Font textRenderer = renderer.getFont(); - matrixStack.translate(0.0D, 0.3333333432674408D, 0.046666666865348816D); - matrixStack.scale(0.010416667F, -0.010416667F, 0.010416667F); - int m = signBlockEntity.getColor().getTextColor(); - int n = (int) (NativeImage.getR(m) * 0.4D); - int o = (int) (NativeImage.getG(m) * 0.4D); - int p = (int) (NativeImage.getB(m) * 0.4D); - int q = NativeImage.combine(0, p, o, n); - - for (int s = 0; s < 4; ++s) { - FormattedCharSequence orderedText = signBlockEntity.getRenderMessage(s, (text) -> { - List list = textRenderer.split(text, 90); - return list.isEmpty() ? FormattedCharSequence.EMPTY : list.get(0); - }); - if (orderedText != null) { - float t = (float) (-textRenderer.width(orderedText) / 2); - textRenderer.drawInBatch(orderedText, t, (float) (s * 10 - 20), q, false, matrixStack.last().pose(), provider, false, 0, light); - } - } - - matrixStack.popPose(); - } - - public static Material getModelTexture(Block block) { - WoodType signType2; - if (block instanceof SignBlock) { - signType2 = ((SignBlock) block).type(); - } else { - signType2 = WoodType.OAK; - } - - return Sheets.signTexture(signType2); - } - - public static VertexConsumer getConsumer(MultiBufferSource provider, Block block) { - return provider.getBuffer(LAYERS.getOrDefault(block, defaultLayer)); - } - - public static void registerRenderLayer(BaseSignBlock block) { - ResourceLocation blockId = Registry.BLOCK.getKey(block); - RenderType layer = RenderType.entitySolid(new ResourceLocation(blockId.getNamespace(), - "textures/entity/sign/" + blockId.getPath() + ".png")); - LAYERS.put(block, layer); - } - - static { - defaultLayer = RenderType.entitySolid(new ResourceLocation("textures/entity/sign/oak.png")); - } -} +package ru.bclib.client.render; + +import java.util.HashMap; +import java.util.List; + +import com.google.common.collect.Maps; +import com.mojang.blaze3d.platform.NativeImage; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.math.Vector3f; + +import net.minecraft.client.gui.Font; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.Sheets; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.client.renderer.blockentity.SignRenderer; +import net.minecraft.client.renderer.blockentity.SignRenderer.SignModel; +import net.minecraft.client.resources.model.Material; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.FormattedCharSequence; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.SignBlock; +import net.minecraft.world.level.block.StandingSignBlock; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.WoodType; +import ru.bclib.blockentities.BaseSignBlockEntity; +import ru.bclib.blocks.BaseSignBlock; + +public class BaseSignBlockEntityRenderer extends BlockEntityRenderer { + private static final HashMap LAYERS = Maps.newHashMap(); + private static final RenderType defaultLayer; + private final SignModel model = new SignRenderer.SignModel(); + + public BaseSignBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) { + super(dispatcher); + } + + public void render(BaseSignBlockEntity signBlockEntity, float tickDelta, PoseStack matrixStack, + MultiBufferSource provider, int light, int overlay) { + BlockState state = signBlockEntity.getBlockState(); + matrixStack.pushPose(); + + matrixStack.translate(0.5D, 0.5D, 0.5D); + float angle = -((float) (state.getValue(StandingSignBlock.ROTATION) * 360) / 16.0F); + + BlockState blockState = signBlockEntity.getBlockState(); + if (blockState.getValue(BaseSignBlock.FLOOR)) { + matrixStack.mulPose(Vector3f.YP.rotationDegrees(angle)); + this.model.stick.visible = true; + } else { + matrixStack.mulPose(Vector3f.YP.rotationDegrees(angle + 180)); + matrixStack.translate(0.0D, -0.3125D, -0.4375D); + this.model.stick.visible = false; + } + + matrixStack.pushPose(); + matrixStack.scale(0.6666667F, -0.6666667F, -0.6666667F); + VertexConsumer vertexConsumer = getConsumer(provider, state.getBlock()); + model.sign.render(matrixStack, vertexConsumer, light, overlay); + model.stick.render(matrixStack, vertexConsumer, light, overlay); + matrixStack.popPose(); + Font textRenderer = renderer.getFont(); + matrixStack.translate(0.0D, 0.3333333432674408D, 0.046666666865348816D); + matrixStack.scale(0.010416667F, -0.010416667F, 0.010416667F); + int m = signBlockEntity.getColor().getTextColor(); + int n = (int) (NativeImage.getR(m) * 0.4D); + int o = (int) (NativeImage.getG(m) * 0.4D); + int p = (int) (NativeImage.getB(m) * 0.4D); + int q = NativeImage.combine(0, p, o, n); + + for (int s = 0; s < 4; ++s) { + FormattedCharSequence orderedText = signBlockEntity.getRenderMessage(s, (text) -> { + List list = textRenderer.split(text, 90); + return list.isEmpty() ? FormattedCharSequence.EMPTY : list.get(0); + }); + if (orderedText != null) { + float t = (float) (-textRenderer.width(orderedText) / 2); + textRenderer.drawInBatch(orderedText, t, (float) (s * 10 - 20), q, false, matrixStack.last().pose(), provider, false, 0, light); + } + } + + matrixStack.popPose(); + } + + public static Material getModelTexture(Block block) { + WoodType signType2; + if (block instanceof SignBlock) { + signType2 = ((SignBlock) block).type(); + } else { + signType2 = WoodType.OAK; + } + + return Sheets.signTexture(signType2); + } + + public static VertexConsumer getConsumer(MultiBufferSource provider, Block block) { + return provider.getBuffer(LAYERS.getOrDefault(block, defaultLayer)); + } + + public static void registerRenderLayer(Block block) { + ResourceLocation blockId = Registry.BLOCK.getKey(block); + RenderType layer = RenderType.entitySolid(new ResourceLocation(blockId.getNamespace(), + "textures/entity/sign/" + blockId.getPath() + ".png")); + LAYERS.put(block, layer); + } + + static { + defaultLayer = RenderType.entitySolid(new ResourceLocation("textures/entity/signs/oak.png")); + } +} diff --git a/src/main/java/ru/bclib/registry/BaseBlockEntities.java b/src/main/java/ru/bclib/registry/BaseBlockEntities.java index bbdcec44..183c3a1e 100644 --- a/src/main/java/ru/bclib/registry/BaseBlockEntities.java +++ b/src/main/java/ru/bclib/registry/BaseBlockEntities.java @@ -1,58 +1,54 @@ -package ru.bclib.registry; - -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraft.world.level.block.entity.BlockEntityType; -import ru.bclib.BCLib; -import ru.bclib.blockentities.BaseBarrelBlockEntity; -import ru.bclib.blockentities.BaseChestBlockEntity; -import ru.bclib.blockentities.BaseFurnaceBlockEntity; -import ru.bclib.blockentities.BaseSignBlockEntity; -import ru.bclib.blocks.BaseBarrelBlock; -import ru.bclib.blocks.BaseChestBlock; -import ru.bclib.blocks.BaseFurnaceBlock; -import ru.bclib.blocks.BaseSignBlock; - -public class BaseBlockEntities { - public static final BlockEntityType CHEST = registerBlockEntityType(BCLib.makeID("chest"), - BlockEntityType.Builder.of(BaseChestBlockEntity::new, getChests())); - public static final BlockEntityType BARREL = registerBlockEntityType(BCLib.makeID("barrel"), - BlockEntityType.Builder.of(BaseBarrelBlockEntity::new, getBarrels())); - public static final BlockEntityType SIGN = registerBlockEntityType(BCLib.makeID("sign"), - BlockEntityType.Builder.of(BaseSignBlockEntity::new, getSigns())); - public static final BlockEntityType FURNACE = registerBlockEntityType(BCLib.makeID("furnace"), - BlockEntityType.Builder.of(BaseFurnaceBlockEntity::new, getFurnaces())); - - public static BlockEntityType registerBlockEntityType(ResourceLocation blockId, BlockEntityType.Builder builder) { - return Registry.register(Registry.BLOCK_ENTITY_TYPE, blockId, builder.build(null)); - } - - public static void register() {} - - public static Block[] getChests() { - return BaseRegistry.getRegisteredBlocks().values().stream() - .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseChestBlock) - .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); - } - - private static Block[] getBarrels() { - return BaseRegistry.getRegisteredBlocks().values().stream() - .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseBarrelBlock) - .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); - } - - public static Block[] getSigns() { - return BaseRegistry.getRegisteredBlocks().values().stream() - .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseSignBlock) - .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); - } - - private static Block[] getFurnaces() { - return BaseRegistry.getRegisteredBlocks().values().stream() - .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseFurnaceBlock) - .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); - } -} +package ru.bclib.registry; + +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; +import ru.bclib.BCLib; +import ru.bclib.blockentities.*; +import ru.bclib.blocks.BaseBarrelBlock; +import ru.bclib.blocks.BaseChestBlock; +import ru.bclib.blocks.BaseFurnaceBlock; +import ru.bclib.blocks.BaseSignBlock; + +import java.util.Arrays; +import java.util.function.Supplier; + +public class BaseBlockEntities { + public static final DynamicBlockEntityType CHEST = registerBlockEntityType(BCLib.makeID("chest"), BaseChestBlockEntity::new); + public static final DynamicBlockEntityType BARREL = registerBlockEntityType(BCLib.makeID("barrel"), BaseBarrelBlockEntity::new); + public static final DynamicBlockEntityType SIGN = registerBlockEntityType(BCLib.makeID("sign"), BaseSignBlockEntity::new); + public static final DynamicBlockEntityType FURNACE = registerBlockEntityType(BCLib.makeID("furnace"), BaseFurnaceBlockEntity::new); + + public static DynamicBlockEntityType registerBlockEntityType(ResourceLocation typeId, Supplier supplier) { + return Registry.register(Registry.BLOCK_ENTITY_TYPE, typeId, new DynamicBlockEntityType<>(supplier)); + } + + public static void register() {} + + public static Block[] getChests() { + return BaseRegistry.getRegisteredBlocks().values().stream() + .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseChestBlock) + .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); + } + + private static Block[] getBarrels() { + return BaseRegistry.getRegisteredBlocks().values().stream() + .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseBarrelBlock) + .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); + } + + public static Block[] getSigns() { + return BaseRegistry.getRegisteredBlocks().values().stream() + .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseSignBlock) + .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); + } + + private static Block[] getFurnaces() { + return BaseRegistry.getRegisteredBlocks().values().stream() + .filter(item -> item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof BaseFurnaceBlock) + .map(item -> ((BlockItem) item).getBlock()).toArray(Block[]::new); + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 63f71d42..0cf6dd3d 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -22,6 +22,9 @@ "entrypoints": { "main": [ "ru.bclib.BCLib" + ], + "client": [ + "ru.bclib.client.BCLibClient" ] }, "mixins": [