generated from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Start work on block entity renderer
1 parent
137f631
commit 1008135
Showing
6 changed files
with
178 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 26 additions & 3 deletions
29
src/main/java/com/leclowndu93150/invertedbed/InvertedBedBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
package com.leclowndu93150.invertedbed; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; | ||
import net.minecraft.world.item.DyeColor; | ||
import net.minecraft.world.level.block.BedBlock; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
|
||
import static com.leclowndu93150.invertedbed.Main.INVERTED_BED_E; | ||
import static com.leclowndu93150.invertedbed.Main.INVERTED_BED_BE; | ||
|
||
public class InvertedBedBlockEntity extends BlockEntity { | ||
public InvertedBedBlockEntity(BlockPos pos, BlockState state) { | ||
super(INVERTED_BED_E.get(), pos, state); | ||
private DyeColor color; | ||
|
||
public InvertedBedBlockEntity(BlockPos pPos, BlockState pBlockState) { | ||
super(INVERTED_BED_BE.get(), pPos, pBlockState); | ||
this.color = ((BedBlock)pBlockState.getBlock()).getColor(); | ||
} | ||
|
||
public InvertedBedBlockEntity(BlockPos pPos, BlockState pBlockState, DyeColor pColor) { | ||
super(INVERTED_BED_BE.get(), pPos, pBlockState); | ||
this.color = pColor; | ||
} | ||
|
||
public ClientboundBlockEntityDataPacket getUpdatePacket() { | ||
return ClientboundBlockEntityDataPacket.create(this); | ||
} | ||
|
||
public DyeColor getColor() { | ||
return this.color; | ||
} | ||
|
||
public void setColor(DyeColor pColor) { | ||
this.color = pColor; | ||
} | ||
} |
126 changes: 122 additions & 4 deletions
126
src/main/java/com/leclowndu93150/invertedbed/InvertedBedRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,136 @@ | ||
package com.leclowndu93150.invertedbed; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import com.mojang.blaze3d.vertex.VertexConsumer; | ||
import com.mojang.math.Axis; | ||
import it.unimi.dsi.fastutil.ints.Int2IntFunction; | ||
import net.minecraft.client.model.geom.ModelLayerLocation; | ||
import net.minecraft.client.model.geom.ModelLayers; | ||
import net.minecraft.client.model.geom.ModelPart; | ||
import net.minecraft.client.model.geom.PartPose; | ||
import net.minecraft.client.model.geom.builders.CubeListBuilder; | ||
import net.minecraft.client.model.geom.builders.LayerDefinition; | ||
import net.minecraft.client.model.geom.builders.MeshDefinition; | ||
import net.minecraft.client.model.geom.builders.PartDefinition; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers; | ||
import net.minecraft.client.renderer.RenderType; | ||
import net.minecraft.client.renderer.Sheets; | ||
import net.minecraft.client.renderer.blockentity.*; | ||
import net.minecraft.client.resources.model.Material; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.BedBlock; | ||
import net.minecraft.world.level.block.ChestBlock; | ||
import net.minecraft.world.level.block.DoubleBlockCombiner; | ||
import net.minecraft.world.level.block.entity.BedBlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.state.properties.BedPart; | ||
|
||
public class InvertedBedRenderer implements BlockEntityRenderer<InvertedBedBlockEntity> { | ||
public InvertedBedRenderer(BlockEntityRendererProvider.Context context) { | ||
private final ModelPart headRoot; | ||
private final ModelPart footRoot; | ||
public static final ModelLayerLocation BED_HEAD = new ModelLayerLocation(new ResourceLocation(Main.MODID, "bed_head"), "main"); | ||
public static final ModelLayerLocation BED_FOOT = new ModelLayerLocation(new ResourceLocation(Main.MODID, "bed_foot"), "main"); | ||
|
||
public InvertedBedRenderer(BlockEntityRendererProvider.Context pContext) { | ||
this.headRoot = pContext.bakeLayer(BED_HEAD); | ||
this.footRoot = pContext.bakeLayer(BED_FOOT); | ||
} | ||
|
||
public static LayerDefinition createHeadLayer() { | ||
MeshDefinition meshdefinition = new MeshDefinition(); | ||
PartDefinition partdefinition = meshdefinition.getRoot(); | ||
partdefinition.addOrReplaceChild("main", CubeListBuilder.create().texOffs(0, 0).addBox(0.0F, 0.0F, 0.0F, 16.0F, 16.0F, 6.0F), PartPose.ZERO); | ||
partdefinition.addOrReplaceChild( | ||
"left_leg", | ||
CubeListBuilder.create().texOffs(50, 6).addBox(0.0F, 6.0F, 0.0F, 3.0F, 3.0F, 3.0F), | ||
PartPose.rotation((float) (Math.PI / 2), 0.0F, (float) (Math.PI / 2)) | ||
); | ||
partdefinition.addOrReplaceChild( | ||
"right_leg", | ||
CubeListBuilder.create().texOffs(50, 18).addBox(-16.0F, 6.0F, 0.0F, 3.0F, 3.0F, 3.0F), | ||
PartPose.rotation((float) (Math.PI / 2), 0.0F, (float) Math.PI) | ||
); | ||
return LayerDefinition.create(meshdefinition, 64, 64); | ||
} | ||
|
||
public static LayerDefinition createFootLayer() { | ||
MeshDefinition meshdefinition = new MeshDefinition(); | ||
PartDefinition partdefinition = meshdefinition.getRoot(); | ||
partdefinition.addOrReplaceChild("main", CubeListBuilder.create().texOffs(0, 22).addBox(0.0F, 0.0F, 0.0F, 16.0F, 16.0F, 6.0F), PartPose.ZERO); | ||
partdefinition.addOrReplaceChild( | ||
"left_leg", | ||
CubeListBuilder.create().texOffs(50, 0).addBox(0.0F, 6.0F, -16.0F, 3.0F, 3.0F, 3.0F), | ||
PartPose.rotation((float) (Math.PI / 2), 0.0F, 0.0F) | ||
); | ||
partdefinition.addOrReplaceChild( | ||
"right_leg", | ||
CubeListBuilder.create().texOffs(50, 12).addBox(-16.0F, 6.0F, -16.0F, 3.0F, 3.0F, 3.0F), | ||
PartPose.rotation((float) (Math.PI / 2), 0.0F, (float) (Math.PI * 3.0 / 2.0)) | ||
); | ||
return LayerDefinition.create(meshdefinition, 64, 64); | ||
} | ||
|
||
@Override | ||
public void render(InvertedBedBlockEntity pBlockEntity, float pPartialTick, PoseStack pPoseStack, MultiBufferSource pBuffer, int pPackedLight, int pPackedOverlay) { | ||
Material material = Sheets.BED_TEXTURES[pBlockEntity.getColor().getId()]; | ||
Level level = pBlockEntity.getLevel(); | ||
if (level != null) { | ||
BlockState blockstate = pBlockEntity.getBlockState(); | ||
DoubleBlockCombiner.NeighborCombineResult<? extends InvertedBedBlockEntity> neighborcombineresult = DoubleBlockCombiner.combineWithNeigbour( | ||
Main.INVERTED_BED_BE.get(), | ||
InvertedBedBlock::getBlockType, | ||
InvertedBedBlock::getConnectedDirection, | ||
ChestBlock.FACING, | ||
blockstate, | ||
level, | ||
pBlockEntity.getBlockPos(), | ||
(p_112202_, p_112203_) -> false | ||
); | ||
int i = neighborcombineresult.<Int2IntFunction>apply(new BrightnessCombiner<>()).get(pPackedLight); | ||
this.renderPiece( | ||
pPoseStack, | ||
pBuffer, | ||
blockstate.getValue(InvertedBedBlock.PART) == BedPart.HEAD ? this.headRoot : this.footRoot, | ||
blockstate.getValue(InvertedBedBlock.FACING), | ||
material, | ||
i, | ||
pPackedOverlay, | ||
false | ||
); | ||
} else { | ||
this.renderPiece(pPoseStack, pBuffer, this.headRoot, Direction.SOUTH, material, pPackedLight, pPackedOverlay, false); | ||
this.renderPiece(pPoseStack, pBuffer, this.footRoot, Direction.SOUTH, material, pPackedLight, pPackedOverlay, true); | ||
} | ||
} | ||
|
||
/** | ||
* @param pFoot {@code true} if piece to render is the foot of the bed, {@code | ||
* false} otherwise or if being rendered by a {@link | ||
* net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer} | ||
*/ | ||
private void renderPiece( | ||
PoseStack pPoseStack, | ||
MultiBufferSource pBufferSource, | ||
ModelPart pModelPart, | ||
Direction pDirection, | ||
Material pMaterial, | ||
int pPackedLight, | ||
int pPackedOverlay, | ||
boolean pFoot | ||
) { | ||
pPoseStack.pushPose(); | ||
pPoseStack.translate(0.0F, 0.5625F, pFoot ? -1.0F : 0.0F); | ||
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F)); | ||
pPoseStack.mulPose(Axis.XN.rotationDegrees(180.0F)); | ||
pPoseStack.translate(0.5F, 0.5F, 0.5F); | ||
pPoseStack.mulPose(Axis.ZP.rotationDegrees(180.0F + pDirection.toYRot())); | ||
pPoseStack.translate(-0.5F, -0.5F, -0.5F); | ||
VertexConsumer vertexconsumer = pMaterial.buffer(pBufferSource, RenderType::entitySolid); | ||
pModelPart.render(pPoseStack, vertexconsumer, pPackedLight, pPackedOverlay); | ||
pPoseStack.popPose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/resources/assets/invertedbed/blockstates/inverted_bed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"variants": { | ||
"": { | ||
"model": "invertedbed:block/inverted_bed" | ||
} | ||
} | ||
} |