package net.minecraft.client.renderer.blockentity;

import net.minecraft.world.level.block.state.StateHolder;
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 edw) {
        super(edw);
        this.blockRenderer = Minecraft.getInstance().getBlockRenderer();
    }
    
    @Override
    public void render(final PistonMovingBlockEntity cff, final float float2, final PoseStack dhl, final MultiBufferSource ebz, final int integer5, final int integer6) {
        final Level bqb8 = cff.getLevel();
        if (bqb8 == null) {
            return;
        }
        final BlockPos fu9 = cff.getBlockPos().relative(cff.getMovementDirection().getOpposite());
        BlockState cfj10 = cff.getMovedState();
        if (cfj10.isAir() || cff.getProgress(float2) >= 1.0f) {
            return;
        }
        ModelBlockRenderer.enableCaching();
        dhl.pushPose();
        dhl.translate(cff.getXOff(float2), cff.getYOff(float2), cff.getZOff(float2));
        if (cfj10.is(Blocks.PISTON_HEAD) && cff.getProgress(float2) <= 4.0f) {
            cfj10 = ((StateHolder<O, BlockState>)cfj10).<Comparable, Boolean>setValue((Property<Comparable>)PistonHeadBlock.SHORT, cff.getProgress(float2) <= 0.5f);
            this.renderBlock(fu9, cfj10, dhl, ebz, bqb8, false, integer6);
        }
        else if (cff.isSourcePiston() && !cff.isExtending()) {
            final PistonType cgk11 = cfj10.is(Blocks.STICKY_PISTON) ? PistonType.STICKY : PistonType.DEFAULT;
            BlockState cfj11 = (((StateHolder<O, BlockState>)Blocks.PISTON_HEAD.defaultBlockState()).setValue(PistonHeadBlock.TYPE, cgk11)).<Comparable, Comparable>setValue((Property<Comparable>)PistonHeadBlock.FACING, (Comparable)cfj10.<V>getValue((Property<V>)PistonBaseBlock.FACING));
            cfj11 = ((StateHolder<O, BlockState>)cfj11).<Comparable, Boolean>setValue((Property<Comparable>)PistonHeadBlock.SHORT, cff.getProgress(float2) >= 0.5f);
            this.renderBlock(fu9, cfj11, dhl, ebz, bqb8, false, integer6);
            final BlockPos fu10 = fu9.relative(cff.getMovementDirection());
            dhl.popPose();
            dhl.pushPose();
            cfj10 = ((StateHolder<O, BlockState>)cfj10).<Comparable, Boolean>setValue((Property<Comparable>)PistonBaseBlock.EXTENDED, true);
            this.renderBlock(fu10, cfj10, dhl, ebz, bqb8, true, integer6);
        }
        else {
            this.renderBlock(fu9, cfj10, dhl, ebz, bqb8, false, integer6);
        }
        dhl.popPose();
        ModelBlockRenderer.clearCache();
    }
    
    private void renderBlock(final BlockPos fu, final BlockState cfj, final PoseStack dhl, final MultiBufferSource ebz, final Level bqb, final boolean boolean6, final int integer) {
        final RenderType ech9 = ItemBlockRenderTypes.getMovingBlockRenderType(cfj);
        final VertexConsumer dhp10 = ebz.getBuffer(ech9);
        this.blockRenderer.getModelRenderer().tesselateBlock(bqb, this.blockRenderer.getBlockModel(cfj), cfj, fu, dhl, dhp10, boolean6, new Random(), cfj.getSeed(fu), integer);
    }
}
