package client.renderer;

import java.util.BitSet;
import java.util.List;
import java.util.Map;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.blockmodel.BakedModel;
import client.renderer.blockmodel.BakedQuad;
import client.renderer.blockmodel.IBakedModel;
import client.renderer.blockmodel.ModelManager;
import client.renderer.texture.TextureAtlasSprite;
import client.renderer.texture.TextureMap;
import client.renderer.tileentity.TileEntityItemStackRenderer;
import common.block.Block;
import common.block.Material;
import common.block.liquid.BlockDynamicLiquid;
import common.block.liquid.BlockLiquid;
import common.block.liquid.BlockStaticLiquid;
import common.collect.Maps;
import common.init.BlockRegistry;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.Facing;
import common.util.Pair;
import common.util.Vec3i;
import common.world.IBlockAccess;
import common.world.IWorldAccess;
import common.world.State;

public class BlockRenderer
{
    private ModelManager manager;
    private final Client gm;
	private final Map<BlockLiquid, TextureAtlasSprite[]> fluids = Maps.newHashMap();

    public BlockRenderer(ModelManager manager, Client gm)
    {
        this.manager = manager;
        this.gm = gm;
        this.initAtlasSprites();
    }

    public ModelManager getModelManager()
    {
        return this.manager;
    }

    public void renderBlockDamage(State state, BlockPos pos, TextureAtlasSprite texture, IWorldAccess blockAccess)
    {
        Block block = state.getBlock();
        int i = block.getRenderType();

        if (i == 3)
        {
            state = block.getActualState(state, blockAccess, pos);
            IBakedModel ibakedmodel = this.manager.getModelForState(state);
            IBakedModel ibakedmodel1 = (new BakedModel.Builder(ibakedmodel, texture)).makeBakedModel();
//            Tessellator.getInstance();
			this.renderModel(blockAccess, ibakedmodel1, state, pos, Tessellator.getBuffer());
        }
    }

    public boolean renderBlock(State state, BlockPos pos, IWorldAccess blockAccess, RenderBuffer worldRendererIn)
    {
    	if(this.gm.xrayActive && !state.getBlock().isXrayVisible())
    		return false;
        int i = state.getBlock().getRenderType();

        if (i == -1)
        {
            return false;
        }
        else
        {
            switch (i)
            {
                case 1:
                    return this.renderFluid(blockAccess, state, pos, worldRendererIn);

                case 2:
                    return false;

                case 3:
                    IBakedModel ibakedmodel = this.getModelFromBlockState(state, blockAccess, pos);
                    return this.renderBase(blockAccess, ibakedmodel, state, pos, worldRendererIn, !this.gm.xrayActive);

                default:
                    return false;
            }
        }
    }

    public IBakedModel getModelFromBlockState(State state, IWorldAccess worldIn, BlockPos pos)
    {
        Block block = state.getBlock();

        if (!this.gm.debugWorld)
        {
            try
            {
                state = block.getActualState(state, worldIn, pos);
            }
            catch (Exception var6)
            {
                ;
            }
        }

        return this.manager.getModelForState(state);

//        if (pos != null && this.gameSettings.allowBlockAlternatives && ibakedmodel instanceof WeightedBakedModel)
//        {
//            ibakedmodel = ((WeightedBakedModel)ibakedmodel).getAlternativeModel(MathHelper.getPositionRandom(pos));
//        }
//
//        return ibakedmodel;
    }

    public void renderBlockBrightness(State state, float brightness)
    {
        int i = state.getBlock().getRenderType();

        if (i != -1)
        {
            switch (i)
            {
                case 1:
                default:
                    break;

                case 2:
//                    this.chestRenderer.renderChestBrightness(state.getBlock(), brightness);
                    GlState.color(brightness, brightness, brightness, 1.0F);
				GL11.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
                    TileEntityItemStackRenderer.instance.renderByItem(new ItemStack(state.getBlock().getItem()));
                    break;

                case 3:
                    IBakedModel ibakedmodel = this.manager.getModelForState(state);
                    this.renderModelBrightness(ibakedmodel, state, brightness, true);
            }
        }
    }

//    public boolean isRenderTypeChest(Block p_175021_1_, int p_175021_2_)
//    {
//        if (p_175021_1_ == null)
//        {
//            return false;
//        }
//        else
//        {
//            int i = p_175021_1_.getRenderType();
//            return i == 3 ? false : i == 2;
//        }
//    }

    public void onReload()
    {
        this.initAtlasSprites();
    }
    
    private boolean renderBase(IWorldAccess blockAccessIn, IBakedModel modelIn, State blockStateIn, BlockPos blockPosIn, RenderBuffer worldRendererIn, boolean checkSides)
    {
        Block block = blockStateIn.getBlock();
        block.setBlockBoundsBasedOnState(blockAccessIn, blockPosIn);
        return this.renderModel(blockAccessIn, modelIn, blockStateIn, blockPosIn, worldRendererIn, checkSides);
    }
    
    private boolean renderModel(IWorldAccess blockAccessIn, IBakedModel modelIn, State blockStateIn, BlockPos blockPosIn, RenderBuffer worldRendererIn)
    {
        Block block = blockStateIn.getBlock();
        block.setBlockBoundsBasedOnState(blockAccessIn, blockPosIn);
        return this.renderModel(blockAccessIn, modelIn, blockStateIn, blockPosIn, worldRendererIn, true);
    }

    public boolean renderModel(IWorldAccess blockAccessIn, IBakedModel modelIn, State blockStateIn, BlockPos blockPosIn, RenderBuffer worldRendererIn, boolean checkSides)
    {
//        return this.renderModelStandard(blockAccessIn, modelIn, , blockPosIn, worldRendererIn, checkSides);
//    }
//
//    public boolean renderModelStandard(IWorldAccess blockAccessIn, IBakedModel modelIn, Block blockIn, BlockPos blockPosIn, RenderBuffer worldRendererIn, boolean checkSides)
//    {
    	Block blockIn = blockStateIn.getBlock();
        boolean flag = false;
        BitSet bitset = new BitSet(3);

        for (Facing enumfacing : Facing.values())
        {
            List<BakedQuad> list = modelIn.getFace(enumfacing);

            if (!list.isEmpty())
            {
                BlockPos blockpos = blockPosIn.offset(enumfacing);

                if (!checkSides || blockIn.shouldSideBeRendered(blockAccessIn, blockpos, enumfacing))
                {
                    int i = blockIn.getMixedBrightnessForBlock(blockAccessIn, blockpos);
                    this.renderModelStandardQuads(blockAccessIn, blockIn, blockPosIn, enumfacing, i, false, worldRendererIn, list, bitset);
                    flag = true;
                }
            }
        }

        List<BakedQuad> list1 = modelIn.getQuads();

        if (list1.size() > 0)
        {
            this.renderModelStandardQuads(blockAccessIn, blockIn, blockPosIn, (Facing)null, -1, true, worldRendererIn, list1, bitset);
            flag = true;
        }

        return flag;
    }

    private void fillQuadBounds(Block blockIn, int[] vertexData, Facing facingIn, float[] quadBounds, BitSet boundsFlags)
    {
        float f = 32.0F;
        float f1 = 32.0F;
        float f2 = 32.0F;
        float f3 = -32.0F;
        float f4 = -32.0F;
        float f5 = -32.0F;

        for (int i = 0; i < 4; ++i)
        {
            float f6 = Float.intBitsToFloat(vertexData[i * 7]);
            float f7 = Float.intBitsToFloat(vertexData[i * 7 + 1]);
            float f8 = Float.intBitsToFloat(vertexData[i * 7 + 2]);
            f = Math.min(f, f6);
            f1 = Math.min(f1, f7);
            f2 = Math.min(f2, f8);
            f3 = Math.max(f3, f6);
            f4 = Math.max(f4, f7);
            f5 = Math.max(f5, f8);
        }

        if (quadBounds != null)
        {
            quadBounds[Facing.WEST.getIndex()] = f;
            quadBounds[Facing.EAST.getIndex()] = f3;
            quadBounds[Facing.DOWN.getIndex()] = f1;
            quadBounds[Facing.UP.getIndex()] = f4;
            quadBounds[Facing.NORTH.getIndex()] = f2;
            quadBounds[Facing.SOUTH.getIndex()] = f5;
            quadBounds[Facing.WEST.getIndex() + Facing.values().length] = 1.0F - f;
            quadBounds[Facing.EAST.getIndex() + Facing.values().length] = 1.0F - f3;
            quadBounds[Facing.DOWN.getIndex() + Facing.values().length] = 1.0F - f1;
            quadBounds[Facing.UP.getIndex() + Facing.values().length] = 1.0F - f4;
            quadBounds[Facing.NORTH.getIndex() + Facing.values().length] = 1.0F - f2;
            quadBounds[Facing.SOUTH.getIndex() + Facing.values().length] = 1.0F - f5;
        }

        float f9 = 1.0E-4F;
        float f10 = 0.9999F;

        switch (facingIn)
        {
            case DOWN:
                boundsFlags.set(1, f >= 1.0E-4F || f2 >= 1.0E-4F || f3 <= 0.9999F || f5 <= 0.9999F);
                boundsFlags.set(0, (f1 < 1.0E-4F || blockIn.isFullCube()) && f1 == f4);
                break;

            case UP:
                boundsFlags.set(1, f >= 1.0E-4F || f2 >= 1.0E-4F || f3 <= 0.9999F || f5 <= 0.9999F);
                boundsFlags.set(0, (f4 > 0.9999F || blockIn.isFullCube()) && f1 == f4);
                break;

            case NORTH:
                boundsFlags.set(1, f >= 1.0E-4F || f1 >= 1.0E-4F || f3 <= 0.9999F || f4 <= 0.9999F);
                boundsFlags.set(0, (f2 < 1.0E-4F || blockIn.isFullCube()) && f2 == f5);
                break;

            case SOUTH:
                boundsFlags.set(1, f >= 1.0E-4F || f1 >= 1.0E-4F || f3 <= 0.9999F || f4 <= 0.9999F);
                boundsFlags.set(0, (f5 > 0.9999F || blockIn.isFullCube()) && f2 == f5);
                break;

            case WEST:
                boundsFlags.set(1, f1 >= 1.0E-4F || f2 >= 1.0E-4F || f4 <= 0.9999F || f5 <= 0.9999F);
                boundsFlags.set(0, (f < 1.0E-4F || blockIn.isFullCube()) && f == f3);
                break;

            case EAST:
                boundsFlags.set(1, f1 >= 1.0E-4F || f2 >= 1.0E-4F || f4 <= 0.9999F || f5 <= 0.9999F);
                boundsFlags.set(0, (f3 > 0.9999F || blockIn.isFullCube()) && f == f3);
        }
    }

    private void renderModelStandardQuads(IWorldAccess blockAccessIn, Block blockIn, BlockPos blockPosIn, Facing faceIn, int brightnessIn, boolean ownBrightness, RenderBuffer worldRendererIn, List<BakedQuad> listQuadsIn, BitSet boundsFlags)
    {
        double d0 = (double)blockPosIn.getX();
        double d1 = (double)blockPosIn.getY();
        double d2 = (double)blockPosIn.getZ();
//        EnumOffsetType block$enumoffsettype = blockIn.getOffsetType();
//
//        if (block$enumoffsettype != EnumOffsetType.NONE)
//        {
//            int i = blockPosIn.getX();
//            int j = blockPosIn.getZ();
//            long k = (long)(i * 3129871) ^ (long)j * 116129781L;
//            k = k * k * 42317861L + k * 11L;
//            d0 += ((double)((float)(k >> 16 & 15L) / 15.0F) - 0.5D) * 0.5D;
//            d2 += ((double)((float)(k >> 24 & 15L) / 15.0F) - 0.5D) * 0.5D;
//
//            if (block$enumoffsettype == EnumOffsetType.XYZ)
//            {
//                d1 += ((double)((float)(k >> 20 & 15L) / 15.0F) - 1.0D) * 0.2D;
//            }
//        }

        for (BakedQuad bakedquad : listQuadsIn)
        {
            if (ownBrightness)
            {
                this.fillQuadBounds(blockIn, bakedquad.getVertexData(), bakedquad.getFace(), (float[])null, boundsFlags);
                brightnessIn = boundsFlags.get(0) ? blockIn.getMixedBrightnessForBlock(blockAccessIn, blockPosIn.offset(bakedquad.getFace())) : blockIn.getMixedBrightnessForBlock(blockAccessIn, blockPosIn);
            }

            worldRendererIn.addVertexData(bakedquad.getVertexData());
            worldRendererIn.putBrightness4(brightnessIn, brightnessIn, brightnessIn, brightnessIn);

            if (bakedquad.hasTintIndex())
            {
                int l = blockIn.colorMultiplier(blockAccessIn, blockPosIn, bakedquad.getTintIndex());

//                if (EntityRenderer.anaglyphEnable)
//                {
//                    l = TextureUtil.anaglyphColor(l);
//                }

                float f = (float)(l >> 16 & 255) / 255.0F;
                float f1 = (float)(l >> 8 & 255) / 255.0F;
                float f2 = (float)(l & 255) / 255.0F;
                worldRendererIn.putColorMultiplier(f, f1, f2, 4);
                worldRendererIn.putColorMultiplier(f, f1, f2, 3);
                worldRendererIn.putColorMultiplier(f, f1, f2, 2);
                worldRendererIn.putColorMultiplier(f, f1, f2, 1);
            }

            worldRendererIn.putPosition(d0, d1, d2);
        }
    }

    public void renderModelBrightnessColor(IBakedModel bakedModel, float p_178262_2_, float red, float green, float blue)
    {
        for (Facing enumfacing : Facing.values())
        {
            this.renderModelBrightnessColorQuads(p_178262_2_, red, green, blue, bakedModel.getFace(enumfacing));
        }

        this.renderModelBrightnessColorQuads(p_178262_2_, red, green, blue, bakedModel.getQuads());
    }

    private void renderModelBrightness(IBakedModel model, State p_178266_2_, float brightness, boolean p_178266_4_)
    {
        Block block = p_178266_2_.getBlock();
        block.setBlockBoundsForItemRender();
        GL11.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
        int i = block.getRenderColor(block.getStateForEntityRender(p_178266_2_));

//        if (EntityRenderer.anaglyphEnable)
//        {
//            i = TextureUtil.anaglyphColor(i);
//        }

        float f = (float)(i >> 16 & 255) / 255.0F;
        float f1 = (float)(i >> 8 & 255) / 255.0F;
        float f2 = (float)(i & 255) / 255.0F;

        if (!p_178266_4_)
        {
            GlState.color(brightness, brightness, brightness, 1.0F);
        }

        this.renderModelBrightnessColor(model, brightness, f, f1, f2);
    }

    private void renderModelBrightnessColorQuads(float brightness, float red, float green, float blue, List<BakedQuad> listQuads)
    {
//        Tessellator tessellator = Tessellator.getInstance();
        RenderBuffer worldrenderer = Tessellator.getBuffer();

        for (BakedQuad bakedquad : listQuads)
        {
            worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM);
            worldrenderer.addVertexData(bakedquad.getVertexData());

            if (bakedquad.hasTintIndex())
            {
                worldrenderer.putColorRGB_F4(red * brightness, green * brightness, blue * brightness);
            }
            else
            {
                worldrenderer.putColorRGB_F4(brightness, brightness, brightness);
            }

            Vec3i vec3i = bakedquad.getFace().getDirectionVec();
            worldrenderer.putNormal((float)vec3i.getX(), (float)vec3i.getY(), (float)vec3i.getZ());
            Tessellator.draw();
        }
    }

    private void initAtlasSprites()
    {
        TextureMap texturemap = Client.CLIENT.getTextureMapBlocks();
        for(Pair<BlockStaticLiquid, BlockDynamicLiquid> liquid : BlockLiquid.LIQUIDS) {
        	String name = BlockRegistry.getName(liquid.first());
        	TextureAtlasSprite[] sprites = new TextureAtlasSprite[] {texturemap.getAtlasSprite("blocks/" + name + "_still"), texturemap.getAtlasSprite("blocks/" + name + "_flow")};
        	this.fluids.put(liquid.second(), sprites);
        	this.fluids.put(liquid.first(), sprites);
        }
    }

    private boolean renderFluid(IWorldAccess blockAccess, State blockStateIn, BlockPos blockPosIn, RenderBuffer worldRendererIn)
    {
        BlockLiquid blockliquid = (BlockLiquid)blockStateIn.getBlock();
        blockliquid.setBlockBoundsBasedOnState(blockAccess, blockPosIn);
        TextureAtlasSprite[] atextureatlassprite = this.fluids.get(blockliquid);
        int i = blockliquid.colorMultiplier(blockAccess, blockPosIn);
        float f = (float)(i >> 16 & 255) / 255.0F;
        float f1 = (float)(i >> 8 & 255) / 255.0F;
        float f2 = (float)(i & 255) / 255.0F;
        boolean flag = blockliquid.shouldSideBeRendered(blockAccess, blockPosIn.up(), Facing.UP);
        boolean flag1 = blockliquid.shouldSideBeRendered(blockAccess, blockPosIn.down(), Facing.DOWN);
        boolean[] aboolean = new boolean[] {blockliquid.shouldSideBeRendered(blockAccess, blockPosIn.north(), Facing.NORTH), blockliquid.shouldSideBeRendered(blockAccess, blockPosIn.south(), Facing.SOUTH), blockliquid.shouldSideBeRendered(blockAccess, blockPosIn.west(), Facing.WEST), blockliquid.shouldSideBeRendered(blockAccess, blockPosIn.east(), Facing.EAST)};

        if (!flag && !flag1 && !aboolean[0] && !aboolean[1] && !aboolean[2] && !aboolean[3])
        {
            return false;
        }
        else
        {
            boolean flag2 = false;
            float f3 = 0.5F;
            float f4 = 1.0F;
            float f5 = 0.8F;
            float f6 = 0.6F;
            Material material = blockliquid.getMaterial();
            float f7 = this.getFluidHeight(blockAccess, blockPosIn, material);
            float f8 = this.getFluidHeight(blockAccess, blockPosIn.south(), material);
            float f9 = this.getFluidHeight(blockAccess, blockPosIn.east().south(), material);
            float f10 = this.getFluidHeight(blockAccess, blockPosIn.east(), material);
            double d0 = (double)blockPosIn.getX();
            double d1 = (double)blockPosIn.getY();
            double d2 = (double)blockPosIn.getZ();
            float f11 = 0.001F;

            if (flag)
            {
                flag2 = true;
                TextureAtlasSprite textureatlassprite = atextureatlassprite[0];
                float f12 = (float)BlockLiquid.getFlowDirection(blockAccess, blockPosIn, blockliquid);

                if (f12 > -999.0F)
                {
                    textureatlassprite = atextureatlassprite[1];
                }

                f7 -= f11;
                f8 -= f11;
                f9 -= f11;
                f10 -= f11;
                float f13;
                float f14;
                float f15;
                float f16;
                float f17;
                float f18;
                float f19;
                float f20;

                if (f12 < -999.0F)
                {
                    f13 = textureatlassprite.getInterpolatedU(0.0D);
                    f17 = textureatlassprite.getInterpolatedV(0.0D);
                    f14 = f13;
                    f18 = textureatlassprite.getInterpolatedV(16.0D);
                    f15 = textureatlassprite.getInterpolatedU(16.0D);
                    f19 = f18;
                    f16 = f15;
                    f20 = f17;
                }
                else
                {
                    float f21 = ExtMath.sin(f12) * 0.25F;
                    float f22 = ExtMath.cos(f12) * 0.25F;
                    float f23 = 8.0F;
                    f13 = textureatlassprite.getInterpolatedU((double)(8.0F + (-f22 - f21) * 16.0F));
                    f17 = textureatlassprite.getInterpolatedV((double)(8.0F + (-f22 + f21) * 16.0F));
                    f14 = textureatlassprite.getInterpolatedU((double)(8.0F + (-f22 + f21) * 16.0F));
                    f18 = textureatlassprite.getInterpolatedV((double)(8.0F + (f22 + f21) * 16.0F));
                    f15 = textureatlassprite.getInterpolatedU((double)(8.0F + (f22 + f21) * 16.0F));
                    f19 = textureatlassprite.getInterpolatedV((double)(8.0F + (f22 - f21) * 16.0F));
                    f16 = textureatlassprite.getInterpolatedU((double)(8.0F + (f22 - f21) * 16.0F));
                    f20 = textureatlassprite.getInterpolatedV((double)(8.0F + (-f22 - f21) * 16.0F));
                }

                int k2 = blockliquid.getMixedBrightnessForBlock(blockAccess, blockPosIn);
                int l2 = k2 >> 16 & 65535;
                int i3 = k2 & 65535;
                float f24 = f4 * f;
                float f25 = f4 * f1;
                float f26 = f4 * f2;
                worldRendererIn.pos(d0 + 0.0D, d1 + (double)f7, d2 + 0.0D).color(f24, f25, f26, 1.0F).tex((double)f13, (double)f17).lightmap(l2, i3).endVertex();
                worldRendererIn.pos(d0 + 0.0D, d1 + (double)f8, d2 + 1.0D).color(f24, f25, f26, 1.0F).tex((double)f14, (double)f18).lightmap(l2, i3).endVertex();
                worldRendererIn.pos(d0 + 1.0D, d1 + (double)f9, d2 + 1.0D).color(f24, f25, f26, 1.0F).tex((double)f15, (double)f19).lightmap(l2, i3).endVertex();
                worldRendererIn.pos(d0 + 1.0D, d1 + (double)f10, d2 + 0.0D).color(f24, f25, f26, 1.0F).tex((double)f16, (double)f20).lightmap(l2, i3).endVertex();

                if (blockliquid.shouldRenderSides(blockAccess, blockPosIn.up()))
                {
                    worldRendererIn.pos(d0 + 0.0D, d1 + (double)f7, d2 + 0.0D).color(f24, f25, f26, 1.0F).tex((double)f13, (double)f17).lightmap(l2, i3).endVertex();
                    worldRendererIn.pos(d0 + 1.0D, d1 + (double)f10, d2 + 0.0D).color(f24, f25, f26, 1.0F).tex((double)f16, (double)f20).lightmap(l2, i3).endVertex();
                    worldRendererIn.pos(d0 + 1.0D, d1 + (double)f9, d2 + 1.0D).color(f24, f25, f26, 1.0F).tex((double)f15, (double)f19).lightmap(l2, i3).endVertex();
                    worldRendererIn.pos(d0 + 0.0D, d1 + (double)f8, d2 + 1.0D).color(f24, f25, f26, 1.0F).tex((double)f14, (double)f18).lightmap(l2, i3).endVertex();
                }
            }

            if (flag1)
            {
                float f35 = atextureatlassprite[0].getMinU();
                float f36 = atextureatlassprite[0].getMaxU();
                float f37 = atextureatlassprite[0].getMinV();
                float f38 = atextureatlassprite[0].getMaxV();
                int l1 = blockliquid.getMixedBrightnessForBlock(blockAccess, blockPosIn.down());
                int i2 = l1 >> 16 & 65535;
                int j2 = l1 & 65535;
                worldRendererIn.pos(d0, d1, d2 + 1.0D).color(f3, f3, f3, 1.0F).tex((double)f35, (double)f38).lightmap(i2, j2).endVertex();
                worldRendererIn.pos(d0, d1, d2).color(f3, f3, f3, 1.0F).tex((double)f35, (double)f37).lightmap(i2, j2).endVertex();
                worldRendererIn.pos(d0 + 1.0D, d1, d2).color(f3, f3, f3, 1.0F).tex((double)f36, (double)f37).lightmap(i2, j2).endVertex();
                worldRendererIn.pos(d0 + 1.0D, d1, d2 + 1.0D).color(f3, f3, f3, 1.0F).tex((double)f36, (double)f38).lightmap(i2, j2).endVertex();
                flag2 = true;
            }

            for (int i1 = 0; i1 < 4; ++i1)
            {
                int j1 = 0;
                int k1 = 0;

                if (i1 == 0)
                {
                    --k1;
                }

                if (i1 == 1)
                {
                    ++k1;
                }

                if (i1 == 2)
                {
                    --j1;
                }

                if (i1 == 3)
                {
                    ++j1;
                }

                BlockPos blockpos = blockPosIn.add(j1, 0, k1);
                TextureAtlasSprite textureatlassprite1 = atextureatlassprite[1];

                if (aboolean[i1])
                {
                    float f39;
                    float f40;
                    double d3;
                    double d4;
                    double d5;
                    double d6;

                    if (i1 == 0)
                    {
                        f39 = f7;
                        f40 = f10;
                        d3 = d0;
                        d5 = d0 + 1.0D;
                        d4 = d2 + (double)f11;
                        d6 = d2 + (double)f11;
                    }
                    else if (i1 == 1)
                    {
                        f39 = f9;
                        f40 = f8;
                        d3 = d0 + 1.0D;
                        d5 = d0;
                        d4 = d2 + 1.0D - (double)f11;
                        d6 = d2 + 1.0D - (double)f11;
                    }
                    else if (i1 == 2)
                    {
                        f39 = f8;
                        f40 = f7;
                        d3 = d0 + (double)f11;
                        d5 = d0 + (double)f11;
                        d4 = d2 + 1.0D;
                        d6 = d2;
                    }
                    else
                    {
                        f39 = f10;
                        f40 = f9;
                        d3 = d0 + 1.0D - (double)f11;
                        d5 = d0 + 1.0D - (double)f11;
                        d4 = d2;
                        d6 = d2 + 1.0D;
                    }

                    flag2 = true;
                    float f41 = textureatlassprite1.getInterpolatedU(0.0D);
                    float f27 = textureatlassprite1.getInterpolatedU(8.0D);
                    float f28 = textureatlassprite1.getInterpolatedV((double)((1.0F - f39) * 16.0F * 0.5F));
                    float f29 = textureatlassprite1.getInterpolatedV((double)((1.0F - f40) * 16.0F * 0.5F));
                    float f30 = textureatlassprite1.getInterpolatedV(8.0D);
                    int j = blockliquid.getMixedBrightnessForBlock(blockAccess, blockpos);
                    int k = j >> 16 & 65535;
                    int l = j & 65535;
                    float f31 = i1 < 2 ? f5 : f6;
                    float f32 = f4 * f31 * f;
                    float f33 = f4 * f31 * f1;
                    float f34 = f4 * f31 * f2;
                    worldRendererIn.pos(d3, d1 + (double)f39, d4).color(f32, f33, f34, 1.0F).tex((double)f41, (double)f28).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d5, d1 + (double)f40, d6).color(f32, f33, f34, 1.0F).tex((double)f27, (double)f29).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d5, d1 + 0.0D, d6).color(f32, f33, f34, 1.0F).tex((double)f27, (double)f30).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d3, d1 + 0.0D, d4).color(f32, f33, f34, 1.0F).tex((double)f41, (double)f30).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d3, d1 + 0.0D, d4).color(f32, f33, f34, 1.0F).tex((double)f41, (double)f30).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d5, d1 + 0.0D, d6).color(f32, f33, f34, 1.0F).tex((double)f27, (double)f30).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d5, d1 + (double)f40, d6).color(f32, f33, f34, 1.0F).tex((double)f27, (double)f29).lightmap(k, l).endVertex();
                    worldRendererIn.pos(d3, d1 + (double)f39, d4).color(f32, f33, f34, 1.0F).tex((double)f41, (double)f28).lightmap(k, l).endVertex();
                }
            }

            return flag2;
        }
    }

    private float getFluidHeight(IBlockAccess blockAccess, BlockPos blockPosIn, Material blockMaterial)
    {
        int i = 0;
        float f = 0.0F;

        for (int j = 0; j < 4; ++j)
        {
            BlockPos blockpos = blockPosIn.add(-(j & 1), 0, -(j >> 1 & 1));

            if (blockAccess.getState(blockpos.up()).getBlock().getMaterial() == blockMaterial)
            {
                return 1.0F;
            }

            State iblockstate = blockAccess.getState(blockpos);
            Material material = iblockstate.getBlock().getMaterial();

            if (material != blockMaterial)
            {
                if (!material.isSolid())
                {
                    ++f;
                    ++i;
                }
            }
            else
            {
                int k = ((Integer)iblockstate.getValue(BlockLiquid.LEVEL)).intValue();

                if (k >= 8 || k == 0)
                {
                    f += BlockLiquid.getLiquidHeightPercent(k) * 10.0F;
                    i += 10;
                }

                f += BlockLiquid.getLiquidHeightPercent(k);
                ++i;
            }
        }

        return 1.0F - f / (float)i;
    }
}
