bau5.mods.projectbench.client.RenderCraftingFrame.java Source code

Java tutorial

Introduction

Here is the source code for bau5.mods.projectbench.client.RenderCraftingFrame.java

Source

package bau5.mods.projectbench.client;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.RenderItemFrame;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityItemFrame;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.Icon;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import bau5.mods.projectbench.common.EntityCraftingFrame;
import bau5.mods.projectbench.common.ProjectBench;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

/**
 * RenderCraftingFrame
 *
 * @author _bau5
 * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
 * 
 */

public class RenderCraftingFrame extends RenderItemFrame {
    private final RenderBlocks renderBlocks = new RenderBlocks();
    private final ResourceLocation resource = new ResourceLocation("projectbench",
            "textures/blocks/craftingFrame_back.png");
    private Icon icon;

    @Override
    public void updateIcons(IconRegister ir) {
        icon = ir.registerIcon("projectbench:craftingFrame_back");
    }

    private void render(EntityCraftingFrame entity, double x, double y, double z, float par8, float par9) {
        if (renderBlocks.blockAccess == null)
            renderBlocks.blockAccess = entity.worldObj;
        GL11.glPushMatrix();
        float f2 = (float) (entity.posX - x) - 0.5F;
        float f3 = (float) (entity.posY - y) - 0.5F;
        float f4 = (float) (entity.posZ - z) - 0.5F;
        int i = entity.xPosition + Direction.offsetX[entity.hangingDirection];
        int j = entity.yPosition;
        int k = entity.zPosition + Direction.offsetZ[entity.hangingDirection];
        float f5 = i - f2;
        float f6 = j - f3;
        float f7 = k - f4;
        GL11.glTranslatef(i - f2, j - f3, k - f4);
        renderFrameItemAsBlock(entity);
        renderItemInFrame(entity);
        GL11.glPopMatrix();
    }

    private void renderItemInFrame(EntityCraftingFrame entity) {
        ItemStack itemstack = entity.getDisplayedItem();

        if (itemstack != null) {
            EntityItem entityitem = new EntityItem(entity.worldObj, 0.0D, 0.0D, 0.0D, itemstack);
            entityitem.getEntityItem().stackSize = 1;
            entityitem.hoverStart = 0.0F;
            GL11.glPushMatrix();
            GL11.glTranslatef(-0.453125F * Direction.offsetX[entity.hangingDirection], -0.18F,
                    -0.453125F * Direction.offsetZ[entity.hangingDirection]);
            GL11.glRotatef(180.0F + entity.rotationYaw, 0.0F, 1.0F, 0.0F);
            GL11.glRotatef((-90 * entity.getRotation()), 0.0F, 0.0F, 1.0F);

            switch (entity.getRotation()) {
            case 1:
                GL11.glTranslatef(-0.16F, -0.16F, 0.0F);
                break;
            case 2:
                GL11.glTranslatef(0.0F, -0.32F, 0.0F);
                break;
            case 3:
                GL11.glTranslatef(0.16F, -0.16F, 0.0F);
            }

            RenderItem.renderInFrame = true;
            RenderManager.instance.renderEntityWithPosYaw(entityitem, 0.0D, 0.0D, 0.0D, 0.0F, 0.0F);
            RenderItem.renderInFrame = false;
            GL11.glPopMatrix();
        }
    }

    private void renderFrameItemAsBlock(EntityItemFrame eif) {
        GL11.glPushMatrix();
        this.renderManager.renderEngine.func_110577_a(TextureMap.field_110575_b);
        GL11.glRotatef(eif.rotationYaw, 0.0F, 1.0F, 0.0F);
        Block block = Block.planks;
        float f = 0.0625F;
        float f1 = 0.75F;
        float f2 = f1 / 2.0F;
        GL11.glPushMatrix();
        this.renderBlocks.overrideBlockBounds(0.0D, (0.5F - f2 + 0.0625F), (0.5F - f2 + 0.0625F), (f * 0.5F),
                (0.5F + f2 - 0.0625F), (0.5F + f2 - 0.0625F));
        this.renderBlocks.setOverrideBlockTexture(ProjectBench.instance.projectBench.getIcon(1, 0));
        this.renderBlocks.renderBlockAsItem(block, 0, 1.0F);
        this.renderBlocks.clearOverrideBlockTexture();
        this.renderBlocks.unlockBlockBounds();
        GL11.glPopMatrix();
        this.renderBlocks.setOverrideBlockTexture(Block.planks.getIcon(1, 1));
        GL11.glPushMatrix();
        this.renderBlocks.overrideBlockBounds(0.0D, (0.5F - f2), (0.5F - f2), (f + 1.0E-4F), (f + 0.5F - f2),
                (0.5F + f2));
        this.renderBlocks.renderBlockAsItem(block, 0, 1.0F);
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        this.renderBlocks.overrideBlockBounds(0.0D, (0.5F + f2 - f), (0.5F - f2), (f + 1.0E-4F), (0.5F + f2),
                (0.5F + f2));
        this.renderBlocks.renderBlockAsItem(block, 0, 1.0F);
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        this.renderBlocks.overrideBlockBounds(0.0D, (0.5F - f2), (0.5F - f2), f, (0.5F + f2), (f + 0.5F - f2));
        this.renderBlocks.renderBlockAsItem(block, 0, 1.0F);
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        this.renderBlocks.overrideBlockBounds(0.0D, (0.5F - f2), (0.5F + f2 - f), f, (0.5F + f2), (0.5F + f2));
        this.renderBlocks.renderBlockAsItem(block, 0, 1.0F);
        GL11.glPopMatrix();
        this.renderBlocks.unlockBlockBounds();
        this.renderBlocks.clearOverrideBlockTexture();
        GL11.glPopMatrix();
    }

    @Override
    protected ResourceLocation func_110775_a(Entity par1Entity) {
        return resource;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void doRender(Entity entity, double x, double y, double z, float par8, float par9) {
        render((EntityCraftingFrame) entity, x, y, z, par8, par9);
    }

}