|
| 1 | +package generations.gg.generations.core.generationscore.common.client.render.block.entity |
| 2 | + |
| 3 | +import com.mojang.blaze3d.vertex.PoseStack |
| 4 | +import com.mojang.blaze3d.vertex.VertexConsumer |
| 5 | +import com.mojang.math.Axis |
| 6 | +import generations.gg.generations.core.generationscore.common.GenerationsCore |
| 7 | +import generations.gg.generations.core.generationscore.common.client.render.rarecandy.instanceOrNull |
| 8 | +import generations.gg.generations.core.generationscore.common.world.level.block.GenerationsBlocks |
| 9 | +import generations.gg.generations.core.generationscore.common.world.level.block.entities.generic.GenericChestBlockEntity |
| 10 | +import generations.gg.generations.core.generationscore.common.world.level.block.generic.GenericChestBlock |
| 11 | +import net.minecraft.Util |
| 12 | +import net.minecraft.client.model.geom.ModelLayers |
| 13 | +import net.minecraft.client.model.geom.ModelPart |
| 14 | +import net.minecraft.client.renderer.MultiBufferSource |
| 15 | +import net.minecraft.client.renderer.RenderType |
| 16 | +import net.minecraft.client.renderer.Sheets |
| 17 | +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer |
| 18 | +import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider |
| 19 | +import net.minecraft.client.resources.model.Material |
| 20 | +import net.minecraft.core.Direction |
| 21 | +import net.minecraft.resources.ResourceLocation |
| 22 | +import net.minecraft.world.level.block.ChestBlock |
| 23 | +import java.util.function.Consumer |
| 24 | + |
| 25 | +class GenericChestRenderer(arg: BlockEntityRendererProvider.Context) : |
| 26 | + BlockEntityRenderer<GenericChestBlockEntity> { |
| 27 | + private val lid: ModelPart |
| 28 | + private val bottom: ModelPart |
| 29 | + private val lock: ModelPart |
| 30 | + |
| 31 | + override fun render( |
| 32 | + blockEntity: GenericChestBlockEntity, |
| 33 | + partialTick: Float, |
| 34 | + poseStack: PoseStack, |
| 35 | + bufferSource: MultiBufferSource, |
| 36 | + packedLight: Int, |
| 37 | + packedOverlay: Int |
| 38 | + ) { |
| 39 | + val level = blockEntity.level |
| 40 | + val flag = level != null |
| 41 | + val blockstate = |
| 42 | + if (flag) blockEntity.blockState else GenerationsBlocks.POKEBALL_CHEST.get().defaultBlockState().setValue( |
| 43 | + ChestBlock.FACING, Direction.SOUTH |
| 44 | + ) |
| 45 | + |
| 46 | + poseStack.pushPose() |
| 47 | + val f = blockstate.getValue(ChestBlock.FACING).toYRot() |
| 48 | + poseStack.translate(0.5, 0.5, 0.5) |
| 49 | + poseStack.mulPose(Axis.YP.rotationDegrees(-f)) |
| 50 | + poseStack.translate(-0.5, -0.5, -0.5) |
| 51 | + |
| 52 | + var f1 = blockEntity.getOpenNess(partialTick) |
| 53 | + f1 = 1.0f - f1 |
| 54 | + f1 = 1.0f - f1 * f1 * f1 |
| 55 | + val material = getMaterial(blockEntity) |
| 56 | + val vertexconsumer = material.buffer(bufferSource) { location -> RenderType.entityCutout(location) } |
| 57 | + this.render(poseStack, vertexconsumer, this.lid, this.lock, this.bottom, f1, packedLight, packedOverlay) |
| 58 | + poseStack.popPose() |
| 59 | + } |
| 60 | + |
| 61 | + private fun render( |
| 62 | + poseStack: PoseStack, |
| 63 | + consumer: VertexConsumer, |
| 64 | + lidPart: ModelPart, |
| 65 | + lockPart: ModelPart, |
| 66 | + bottomPart: ModelPart, |
| 67 | + lidAngle: Float, |
| 68 | + packedLight: Int, |
| 69 | + packedOverlay: Int |
| 70 | + ) { |
| 71 | + lidPart.xRot = -(lidAngle * ((Math.PI / 2).toFloat())) |
| 72 | + lockPart.xRot = lidPart.xRot |
| 73 | + lidPart.render(poseStack, consumer, packedLight, packedOverlay) |
| 74 | + lockPart.render(poseStack, consumer, packedLight, packedOverlay) |
| 75 | + bottomPart.render(poseStack, consumer, packedLight, packedOverlay) |
| 76 | + } |
| 77 | + |
| 78 | + init { |
| 79 | + val modelpart = arg.bakeLayer(ModelLayers.CHEST) |
| 80 | + this.bottom = modelpart.getChild("bottom") |
| 81 | + this.lid = modelpart.getChild("lid") |
| 82 | + this.lock = modelpart.getChild("lock") |
| 83 | + } |
| 84 | + |
| 85 | + companion object { |
| 86 | + private const val BOTTOM = "bottom" |
| 87 | + private const val LID = "lid" |
| 88 | + private const val LOCK = "lock" |
| 89 | + private fun getMaterial(blockEntity: GenericChestBlockEntity): Material { |
| 90 | + return map[blockEntity.blockState.block.instanceOrNull<GenericChestBlock>()?.materialType ?: "pokeball_chest"]!! |
| 91 | + } |
| 92 | + |
| 93 | + private val map: HashMap<String, Material> = Util.make( |
| 94 | + HashMap() |
| 95 | + ) { map: HashMap<String, Material> -> |
| 96 | + val consumer = { id: String -> |
| 97 | + map[id] = Material( |
| 98 | + Sheets.CHEST_SHEET, GenerationsCore.id( |
| 99 | + "entity/chest/$id" |
| 100 | + ) |
| 101 | + ) |
| 102 | + } |
| 103 | + consumer.invoke("pokeball_chest") |
| 104 | + consumer.invoke("greatball_chest") |
| 105 | + consumer.invoke("ultraball_chest") |
| 106 | + consumer.invoke("masterball_chest") |
| 107 | + } |
| 108 | + |
| 109 | + val genericChestTextures: Collection<Material> |
| 110 | + get() = map.values |
| 111 | + } |
| 112 | +} |
0 commit comments