package net.minecraft.client.renderer.blockentity;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.entity.BlockEntity;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.level.BlockAndTintGetter;
import java.util.Random;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.piston.PistonBaseBlock;
import net.minecraft.world.level.block.state.properties.PistonType;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.piston.PistonHeadBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.client.renderer.block.ModelBlockRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.world.level.block.piston.PistonMovingBlockEntity;

public class PistonHeadRenderer extends BlockEntityRenderer<PistonMovingBlockEntity> {
    private final BlockRenderDispatcher blockRenderer;
    
    public PistonHeadRenderer(final BlockEntityRenderDispatcher dsq) {
        super(dsq);
        this.blockRenderer = Minecraft.getInstance().getBlockRenderer();
    }
    
    @Override
    public void render(final PistonMovingBlockEntity byf, final float float2, final PoseStack cyl, final MultiBufferSource dqt, final int integer5, final int integer6) {
        final Level bjw8 = byf.getLevel();
        if (bjw8 == null) {
            return;
        }
        final BlockPos fk9 = byf.getBlockPos().relative(byf.getMovementDirection().getOpposite());
        BlockState byj10 = byf.getMovedState();
        if (byj10.isAir() || byf.getProgress(float2) >= 1.0f) {
            return;
        }
        ModelBlockRenderer.enableCaching();
        cyl.pushPose();
        cyl.translate(byf.getXOff(float2), byf.getYOff(float2), byf.getZOff(float2));
        if (byj10.getBlock() == Blocks.PISTON_HEAD && byf.getProgress(float2) <= 4.0f) {
            byj10 = ((AbstractStateHolder<O, BlockState>)byj10).<Comparable, Boolean>setValue((Property<Comparable>)PistonHeadBlock.SHORT, true);
            this.renderBlock(fk9, byj10, cyl, dqt, bjw8, false, integer6);
        }
        else if (byf.isSourcePiston() && !byf.isExtending()) {
            final PistonType bzl11 = (byj10.getBlock() == Blocks.STICKY_PISTON) ? PistonType.STICKY : PistonType.DEFAULT;
            BlockState byj11 = (((AbstractStateHolder<O, BlockState>)Blocks.PISTON_HEAD.defaultBlockState()).setValue(PistonHeadBlock.TYPE, bzl11)).<Comparable, Comparable>setValue((Property<Comparable>)PistonHeadBlock.FACING, (Comparable)byj10.<V>getValue((Property<V>)PistonBaseBlock.FACING));
            byj11 = ((AbstractStateHolder<O, BlockState>)byj11).<Comparable, Boolean>setValue((Property<Comparable>)PistonHeadBlock.SHORT, byf.getProgress(float2) >= 0.5f);
            this.renderBlock(fk9, byj11, cyl, dqt, bjw8, false, integer6);
            final BlockPos fk10 = fk9.relative(byf.getMovementDirection());
            cyl.popPose();
            cyl.pushPose();
            byj10 = ((AbstractStateHolder<O, BlockState>)byj10).<Comparable, Boolean>setValue((Property<Comparable>)PistonBaseBlock.EXTENDED, true);
            this.renderBlock(fk10, byj10, cyl, dqt, bjw8, true, integer6);
        }
        else {
            this.renderBlock(fk9, byj10, cyl, dqt, bjw8, false, integer6);
        }
        cyl.popPose();
        ModelBlockRenderer.clearCache();
    }
    
    private void renderBlock(final BlockPos fk, final BlockState byj, final PoseStack cyl, final MultiBufferSource dqt, final Level bjw, final boolean boolean6, final int integer) {
        final RenderType drb9 = ItemBlockRenderTypes.getChunkRenderType(byj);
        final VertexConsumer cyo10 = dqt.getBuffer(drb9);
        this.blockRenderer.getModelRenderer().tesselateBlock(bjw, this.blockRenderer.getBlockModel(byj), byj, fk, cyl, cyo10, boolean6, new Random(), byj.getSeed(fk), integer);
    }
}
