lyonlancer5.halogen.BlockRenderHandler.java Source code

Java tutorial

Introduction

Here is the source code for lyonlancer5.halogen.BlockRenderHandler.java

Source

/***************************************************************************\
* Copyright 2016 [Lyonlancer5]                                              *
*                                                                           *
* Licensed under the Apache License, Version 2.0 (the "License");           *
* you may not use this file except in compliance with the License.          *
* You may obtain a copy of the License at                                   *
*                                                                           *
*     http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                           *
* Unless required by applicable law or agreed to in writing, software       *
* distributed under the License is distributed on an "AS IS" BASIS,         *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
* See the License for the specific language governing permissions and       *
*limitations under the License.                                             *
\***************************************************************************/
package lyonlancer5.halogen;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;

public class BlockRenderHandler implements ISimpleBlockRenderingHandler {

    private int renderId;
    private boolean shouldRender3D;

    public BlockRenderHandler(int par1, boolean par2) {
        this.renderId = par1;
        this.shouldRender3D = par2;
    }

    @Override
    public void renderInventoryBlock(Block _block, int metadata, int modelId, RenderBlocks renderer) {
        Block_HalogenLight block = (Block_HalogenLight) _block;
        boolean renderType = false;

        if (metadata >= 0 && metadata <= 15) {
            renderer.setRenderBounds(0.25D, 0.25D, 0.875D, 0.75D, 0.75D, 1.0D);
        } else if (metadata >= 16 && metadata <= 31) {
            renderer.setRenderBounds(0.25D, 0.0D, 0.25D, 0.75D, 0.7350000143051147D, 0.75D);
        } else if (metadata >= 32 && metadata <= 47) {
            renderer.setRenderBounds(0.0D, 0.4350000023841858D, 0.875D, 1.0D, 0.5649999976158142D, 1.0D);
        } else if (metadata >= 48 && metadata <= 63) {
            renderer.setRenderBounds(0.27000001072883606D, 0.40299999713897705D, 0.949999988079071D,
                    0.7300000190734863D, 0.597000002861023D, 1.0D);
        } else if (metadata >= 64 && metadata <= 79) {
            renderer.setRenderBounds(0.3199999928474426D, 0.3199999928474426D, 0.949999988079071D,
                    0.6800000071525574D, 0.6800000071525574D, 1.0D);
        } else if (metadata >= 80 && metadata <= 95) {
            renderer.setRenderBounds(0.3199999928474426D, 0.0D, 0.3199999928474426D, 0.6800000071525574D,
                    0.6600000262260437D, 0.6800000071525574D);
        } else if (metadata >= 96 && metadata <= 111) {
            renderer.setRenderBounds(0.3499999940395355D, 0.0D, 0.3499999940395355D, 0.6499999761581421D,
                    0.6000000238418579D, 0.6499999761581421D);
            renderType = true;
        } else if (metadata >= 112 && metadata <= 127) {
            renderer.setRenderBounds(0.0D, 0.375D, 0.0D, 1.0D, 0.625D, 1.0D);
        }

        Tessellator tessellator = Tessellator.instance;

        if (!renderType) {
            GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
            tessellator.startDrawingQuads();
            tessellator.setNormal(0.0F, -1.0F, 0.0F);
            renderer.renderFaceYNeg(block, 0.0D, 0.0D, 0.0D, block.getIcon(0, metadata));
            tessellator.draw();
            tessellator.startDrawingQuads();
            tessellator.setNormal(0.0F, 1.0F, 0.0F);
            renderer.renderFaceYPos(block, 0.0D, 0.0D, 0.0D, block.getIcon(1, metadata));
            tessellator.draw();
            tessellator.startDrawingQuads();
            tessellator.setNormal(0.0F, 0.0F, -1.0F);
            renderer.renderFaceZNeg(block, 0.0D, 0.0D, 0.0D, block.getIcon(2, metadata));
            tessellator.draw();
            tessellator.startDrawingQuads();
            tessellator.setNormal(0.0F, 0.0F, 1.0F);
            renderer.renderFaceZPos(block, 0.0D, 0.0D, 0.0D, block.getIcon(3, metadata));
            tessellator.draw();
            tessellator.startDrawingQuads();
            tessellator.setNormal(-1.0F, 0.0F, 0.0F);
            renderer.renderFaceXNeg(block, 0.0D, 0.0D, 0.0D, block.getIcon(4, metadata));
            tessellator.draw();
            tessellator.startDrawingQuads();
            tessellator.setNormal(1.0F, 0.0F, 0.0F);
            renderer.renderFaceXPos(block, 0.0D, 0.0D, 0.0D, block.getIcon(5, metadata));
            tessellator.draw();
            GL11.glTranslatef(0.5F, 0.5F, 0.5F);
        }

        if (renderType) {
            GL11.glDisable(GL11.GL_LIGHTING);
            GL11.glScalef(1.5F, 1.5F, 1.5F);
            tessellator.startDrawingQuads();
            tessellator.setNormal(0.0F, -1.0F, 0.0F);
            this.renderTorchAtAngle(renderer, block, -0.5D, -0.4D, -0.5D, 0.0D, 0.0D, metadata, false);
            tessellator.draw();
            GL11.glScalef(1.0F, 1.0F, 1.0F);
            GL11.glEnable(GL11.GL_LIGHTING);
        }
    }

    @Override
    public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block _block, int modelId,
            RenderBlocks renderer) {
        Block_HalogenLight block = (Block_HalogenLight) _block;
        int meta = 0;
        int type = 0;
        int direction = 1;
        boolean flag = false;

        try {
            TileEntity_HalogenLight te = (TileEntity_HalogenLight) world.getTileEntity(x, y, z);
            meta = te.getMeta();
            type = te.getType();
            direction = te.getDirection();
        } catch (Exception var24) {
            LL5_Halogen.logger.warn(
                    "Failed to load TileEntity. (Render #1) [x" + x + ", y" + y + 1 + ", z" + z + "]", 4, var24);
        }

        if ((meta < 0 || meta > 15) && (meta < 64 || meta > 79)) {
            if ((meta < 16 || meta > 31) && (meta < 80 || meta > 95)) {
                if (meta >= 32 && meta <= 47) {
                    if (type == 0) {
                        if (direction == 0 || direction == 1) {
                            renderer.uvRotateSouth = 1;
                            renderer.uvRotateNorth = 2;
                        }

                        if (direction == 4 || direction == 5) {
                            renderer.uvRotateTop = 2;
                            renderer.uvRotateBottom = 1;
                        }
                    }

                    if (type == 1) {
                        if (direction == 0 || direction == 1) {
                            renderer.uvRotateTop = 2;
                            renderer.uvRotateBottom = 1;
                            renderer.uvRotateEast = 1;
                            renderer.uvRotateWest = 2;
                        }

                        if (direction == 2 || direction == 3) {
                            renderer.uvRotateTop = 2;
                            renderer.uvRotateBottom = 1;
                            renderer.uvRotateEast = 1;
                            renderer.uvRotateWest = 2;
                            renderer.uvRotateSouth = 1;
                            renderer.uvRotateNorth = 2;
                        }

                        if (direction == 4 || direction == 5) {
                            renderer.uvRotateEast = 1;
                            renderer.uvRotateWest = 2;
                            renderer.uvRotateSouth = 1;
                            renderer.uvRotateNorth = 2;
                        }
                    }

                    renderer.renderStandardBlock(block, x, y, z);
                    this.resetUvRotate(renderer);
                    flag = true;
                } else if (meta >= 48 && meta <= 63) {
                    if (type == 1) {
                        if (direction == 0 || direction == 1) {
                            renderer.uvRotateTop = 2;
                            renderer.uvRotateBottom = 1;
                            renderer.uvRotateEast = 1;
                            renderer.uvRotateWest = 2;
                        }

                        if (direction == 2 || direction == 3) {
                            renderer.uvRotateTop = 2;
                            renderer.uvRotateBottom = 1;
                            renderer.uvRotateEast = 1;
                            renderer.uvRotateWest = 2;
                            renderer.uvRotateSouth = 1;
                            renderer.uvRotateNorth = 2;
                        }

                        if (direction == 4 || direction == 5) {
                            renderer.uvRotateEast = 1;
                            renderer.uvRotateWest = 2;
                            renderer.uvRotateSouth = 1;
                            renderer.uvRotateNorth = 2;
                        }
                    }

                    renderer.renderStandardBlock(block, x, y, z);
                    this.resetUvRotate(renderer);
                    flag = true;
                } else if (meta >= 96 && meta <= 111) {
                    Tessellator tessellator = Tessellator.instance;
                    tessellator.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z));
                    tessellator.setColorOpaque_F(1.0F, 1.0F, 1.0F);
                    double d1 = 0.4000000059604645D;
                    double d2 = 0.5D - d1;
                    double d3 = 0.20000000298023224D;

                    if (direction == 1) {
                        boolean longFlag = false;

                        if (world.getBlock(x, y + 1, z) == block) {
                            try {
                                TileEntity_HalogenLight e = (TileEntity_HalogenLight) world.getTileEntity(x, y + 1,
                                        z);

                                if (e.getDirection() == 1 && meta == e.getMeta()) {
                                    longFlag = true;
                                }
                            } catch (Exception var23) {
                                LL5_Halogen.logger.warn("Failed to load TileEntity. (Render #2) [x" + x + ", y" + y
                                        + 1 + ", z" + z + "]", 4, var23);
                            }
                        }

                        this.renderTorchAtAngle(renderer, block, (double) x, (double) y, (double) z, 0.0D, 0.0D,
                                meta, longFlag);
                    }

                    if (direction == 2) {
                        this.renderTorchAtAngle(renderer, block, (double) x, (double) y + d3, (double) z + d2, 0.0D,
                                d1, meta, false);
                    }

                    if (direction == 3) {
                        this.renderTorchAtAngle(renderer, block, (double) x, (double) y + d3, (double) z - d2, 0.0D,
                                -d1, meta, false);
                    }

                    if (direction == 4) {
                        this.renderTorchAtAngle(renderer, block, (double) x + d2, (double) y + d3, (double) z, d1,
                                0.0D, meta, false);
                    }

                    if (direction == 5) {
                        this.renderTorchAtAngle(renderer, block, (double) x - d2, (double) y + d3, (double) z, -d1,
                                0.0D, meta, false);
                    }

                    flag = true;
                } else if (meta >= 112 && meta <= 127) {
                    if (direction == 0 || direction == 1) {
                        renderer.uvRotateSouth = 2;
                        renderer.uvRotateNorth = 1;
                        renderer.uvRotateEast = 2;
                        renderer.uvRotateWest = 1;
                    }

                    if (direction == 2 || direction == 3) {
                        renderer.uvRotateSouth = 2;
                        renderer.uvRotateNorth = 1;
                    }

                    if (direction == 4 || direction == 5) {
                        renderer.uvRotateEast = 2;
                        renderer.uvRotateWest = 1;
                    }

                    renderer.renderStandardBlock(block, x, y, z);
                    this.resetUvRotate(renderer);
                    flag = true;
                }
            } else {
                if (direction == 2 || direction == 3) {
                    renderer.uvRotateSouth = 1;
                    renderer.uvRotateNorth = 2;
                }

                if (direction == 4 || direction == 5) {
                    renderer.uvRotateTop = 2;
                    renderer.uvRotateBottom = 1;
                    renderer.uvRotateEast = 1;
                    renderer.uvRotateWest = 2;
                }

                renderer.renderStandardBlock(block, x, y, z);
                this.resetUvRotate(renderer);
                flag = true;
            }
        } else {
            renderer.renderStandardBlock(block, x, y, z);
            flag = true;
        }

        return flag;
    }

    public void resetUvRotate(RenderBlocks renderer) {
        renderer.uvRotateEast = 0;
        renderer.uvRotateWest = 0;
        renderer.uvRotateNorth = 0;
        renderer.uvRotateSouth = 0;
        renderer.uvRotateTop = 0;
        renderer.uvRotateBottom = 0;
    }

    public void renderTorchAtAngle(RenderBlocks renderer, Block_HalogenLight par1Block, double par2, double par4,
            double par6, double par8, double par10, int par12, boolean longFlag) {
        Tessellator tessellator = Tessellator.instance;
        IIcon icon;

        if (longFlag) {
            icon = par1Block.getBlockTextureFromIndex(par12, "_torch2");
        } else {
            icon = renderer.getBlockIconFromSideAndMetadata(par1Block, 0, par12);
        }

        if (renderer.hasOverrideBlockTexture()) {
            icon = renderer.overrideBlockTexture;
        }

        double d5 = (double) icon.getMinU();
        double d6 = (double) icon.getMinV();
        double d7 = (double) icon.getMaxU();
        double d8 = (double) icon.getMaxV();
        double d9 = (double) icon.getInterpolatedU(7.0D);
        double d10 = (double) icon.getInterpolatedV(6.0D);
        double d11 = (double) icon.getInterpolatedU(9.0D);
        double d12 = (double) icon.getInterpolatedV(8.0D);
        double d13 = (double) icon.getInterpolatedU(7.0D);
        double d14 = (double) icon.getInterpolatedV(13.0D);
        double d15 = (double) icon.getInterpolatedU(9.0D);
        double d16 = (double) icon.getInterpolatedV(15.0D);
        par2 += 0.5D;
        par6 += 0.5D;
        double d17 = par2 - 0.5D;
        double d18 = par2 + 0.5D;
        double d19 = par6 - 0.5D;
        double d20 = par6 + 0.5D;
        double d21 = 0.0625D;
        double d22 = 0.625D;
        tessellator.addVertexWithUV(par2 + par8 * (1.0D - d22) - d21, par4 + d22, par6 + par10 * (1.0D - d22) - d21,
                d9, d10);
        tessellator.addVertexWithUV(par2 + par8 * (1.0D - d22) - d21, par4 + d22, par6 + par10 * (1.0D - d22) + d21,
                d9, d12);
        tessellator.addVertexWithUV(par2 + par8 * (1.0D - d22) + d21, par4 + d22, par6 + par10 * (1.0D - d22) + d21,
                d11, d12);
        tessellator.addVertexWithUV(par2 + par8 * (1.0D - d22) + d21, par4 + d22, par6 + par10 * (1.0D - d22) - d21,
                d11, d10);
        tessellator.addVertexWithUV(par2 + d21 + par8, par4, par6 - d21 + par10, d15, d14);
        tessellator.addVertexWithUV(par2 + d21 + par8, par4, par6 + d21 + par10, d15, d16);
        tessellator.addVertexWithUV(par2 - d21 + par8, par4, par6 + d21 + par10, d13, d16);
        tessellator.addVertexWithUV(par2 - d21 + par8, par4, par6 - d21 + par10, d13, d14);
        tessellator.addVertexWithUV(par2 - d21, par4 + 1.0D, d19, d5, d6);
        tessellator.addVertexWithUV(par2 - d21 + par8, par4 + 0.0D, d19 + par10, d5, d8);
        tessellator.addVertexWithUV(par2 - d21 + par8, par4 + 0.0D, d20 + par10, d7, d8);
        tessellator.addVertexWithUV(par2 - d21, par4 + 1.0D, d20, d7, d6);
        tessellator.addVertexWithUV(par2 + d21, par4 + 1.0D, d20, d5, d6);
        tessellator.addVertexWithUV(par2 + par8 + d21, par4 + 0.0D, d20 + par10, d5, d8);
        tessellator.addVertexWithUV(par2 + par8 + d21, par4 + 0.0D, d19 + par10, d7, d8);
        tessellator.addVertexWithUV(par2 + d21, par4 + 1.0D, d19, d7, d6);
        tessellator.addVertexWithUV(d17, par4 + 1.0D, par6 + d21, d5, d6);
        tessellator.addVertexWithUV(d17 + par8, par4 + 0.0D, par6 + d21 + par10, d5, d8);
        tessellator.addVertexWithUV(d18 + par8, par4 + 0.0D, par6 + d21 + par10, d7, d8);
        tessellator.addVertexWithUV(d18, par4 + 1.0D, par6 + d21, d7, d6);
        tessellator.addVertexWithUV(d18, par4 + 1.0D, par6 - d21, d5, d6);
        tessellator.addVertexWithUV(d18 + par8, par4 + 0.0D, par6 - d21 + par10, d5, d8);
        tessellator.addVertexWithUV(d17 + par8, par4 + 0.0D, par6 - d21 + par10, d7, d8);
        tessellator.addVertexWithUV(d17, par4 + 1.0D, par6 - d21, d7, d6);
    }

    @Override
    public int getRenderId() {
        return this.renderId;
    }

    @Override
    public boolean shouldRender3DInInventory(int modelId) {
        return this.shouldRender3D;
    }

}