mal.carbonization.render.StructureItemRenderer.java Source code

Java tutorial

Introduction

Here is the source code for mal.carbonization.render.StructureItemRenderer.java

Source

package mal.carbonization.render;

import org.lwjgl.opengl.GL11;

import mal.carbonization.item.ItemStructureBlock;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraftforge.client.IItemRenderer;

public class StructureItemRenderer implements IItemRenderer {

    @Override
    public boolean handleRenderType(ItemStack item, ItemRenderType type) {
        switch (type) {
        case ENTITY:
        case EQUIPPED:
        case EQUIPPED_FIRST_PERSON:
        case INVENTORY:
            return true;
        default:
            return false;
        }
    }

    @Override
    public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper) {
        switch (type) {
        case ENTITY: {
            return (helper == ItemRendererHelper.ENTITY_BOBBING || helper == ItemRendererHelper.ENTITY_ROTATION
                    || helper == ItemRendererHelper.BLOCK_3D);
        }
        case EQUIPPED: {
            return (helper == ItemRendererHelper.BLOCK_3D || helper == ItemRendererHelper.EQUIPPED_BLOCK);
        }
        case EQUIPPED_FIRST_PERSON: {
            return (helper == ItemRendererHelper.EQUIPPED_BLOCK);
        }
        case INVENTORY: {
            return (helper == ItemRendererHelper.INVENTORY_BLOCK);
        }
        default: {
            return false;
        }
        }
    }

    @Override
    public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
        Tessellator tessellator = Tessellator.instance;

        //verify that the item is a ItemStructureBlock
        if (!(item.getItem() instanceof ItemStructureBlock))
            return;
        GL11.glPushMatrix();
        //      GL11.glDisable(GL11.GL_LIGHTING);
        GL11.glEnable(GL11.GL_ALPHA_TEST);
        //        GL11.glEnable(GL11.GL_BLEND);
        tessellator.startDrawingQuads();

        // adjust rendering space to match what caller expects
        boolean mustundotranslate = false;
        switch (type) {
        case EQUIPPED:
            break;
        case INVENTORY:
            GL11.glTranslatef(0.0f, -0.08f, 0.0f);
            break;
        case EQUIPPED_FIRST_PERSON: {
            break;
        }
        case ENTITY: {
            GL11.glScalef(0.5f, 0.5f, 0.5f);
            GL11.glTranslatef(-0.5f, -0.5f, -0.5f);
            break;
        }
        default:
            break; // never here
        }

        GL11.glPushMatrix();
        for (int i = 0; i < 3; i++) {

            IIcon icon;
            // xpos face textures
            icon = item.getItem().getIconFromDamageForRenderPass(item.getItemDamage(), i);
            if (icon != null) {
                tessellator.setNormal(1.0F, 0.0F, 0.0F);
                tessellator.addVertexWithUV(1.0, 0.0, 0.0, (double) icon.getMaxU(), (double) icon.getMaxV());
                tessellator.addVertexWithUV(1.0, 1.0, 0.0, (double) icon.getMaxU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(1.0, 1.0, 1.0, (double) icon.getMinU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(1.0, 0.0, 1.0, (double) icon.getMinU(), (double) icon.getMaxV());
            }

            // xneg face textures
            icon = item.getItem().getIconFromDamageForRenderPass(item.getItemDamage(), i);
            if (icon != null) {
                tessellator.setNormal(-1.0F, 0.0F, 0.0F);
                tessellator.addVertexWithUV(0.0, 0.0, 1.0, (double) icon.getMaxU(), (double) icon.getMaxV());
                tessellator.addVertexWithUV(0.0, 1.0, 1.0, (double) icon.getMaxU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(0.0, 1.0, 0.0, (double) icon.getMinU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(0.0, 0.0, 0.0, (double) icon.getMinU(), (double) icon.getMaxV());
            }

            // zneg face textures
            icon = item.getItem().getIconFromDamageForRenderPass(item.getItemDamage(), i);
            if (icon != null) {
                tessellator.setNormal(0.0F, 0.0F, -1.0F);
                tessellator.addVertexWithUV(0.0, 0.0, 0.0, (double) icon.getMaxU(), (double) icon.getMaxV());
                tessellator.addVertexWithUV(0.0, 1.0, 0.0, (double) icon.getMaxU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(1.0, 1.0, 0.0, (double) icon.getMinU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(1.0, 0.0, 0.0, (double) icon.getMinU(), (double) icon.getMaxV());
            }

            // zpos face textures
            icon = item.getItem().getIconFromDamageForRenderPass(item.getItemDamage(), i);
            if (icon != null) {
                tessellator.setNormal(0.0F, 0.0F, -1.0F);
                tessellator.addVertexWithUV(1.0, 0.0, 1.0, (double) icon.getMaxU(), (double) icon.getMaxV());
                tessellator.addVertexWithUV(1.0, 1.0, 1.0, (double) icon.getMaxU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(0.0, 1.0, 1.0, (double) icon.getMinU(), (double) icon.getMinV());
                tessellator.addVertexWithUV(0.0, 0.0, 1.0, (double) icon.getMinU(), (double) icon.getMaxV());
            }

            if (i < 2) {
                // ypos face textures
                icon = item.getItem().getIconFromDamageForRenderPass(item.getItemDamage(), i);
                if (icon != null) {
                    tessellator.setNormal(0.0F, 1.0F, 0.0F);
                    tessellator.addVertexWithUV(1.0, 1.0, 1.0, (double) icon.getMaxU(), (double) icon.getMaxV());
                    tessellator.addVertexWithUV(1.0, 1.0, 0.0, (double) icon.getMaxU(), (double) icon.getMinV());
                    tessellator.addVertexWithUV(0.0, 1.0, 0.0, (double) icon.getMinU(), (double) icon.getMinV());
                    tessellator.addVertexWithUV(0.0, 1.0, 1.0, (double) icon.getMinU(), (double) icon.getMaxV());
                }

                // yneg face textures
                icon = item.getItem().getIconFromDamageForRenderPass(item.getItemDamage(), i);
                if (icon != null) {
                    tessellator.setNormal(0.0F, -1.0F, 0.0F);
                    tessellator.addVertexWithUV(0.0, 0.0, 1.0, (double) icon.getMaxU(), (double) icon.getMaxV());
                    tessellator.addVertexWithUV(0.0, 0.0, 0.0, (double) icon.getMaxU(), (double) icon.getMinV());
                    tessellator.addVertexWithUV(1.0, 0.0, 0.0, (double) icon.getMinU(), (double) icon.getMinV());
                    tessellator.addVertexWithUV(1.0, 0.0, 1.0, (double) icon.getMinU(), (double) icon.getMaxV());
                }
            }

        }
        tessellator.draw();
        GL11.glPopMatrix();
        /*      GL11.glEnable(GL11.GL_LIGHTING);
                GL11.glDisable(GL11.GL_ALPHA_TEST);
                GL11.glDisable(GL11.GL_BLEND);*/

        GL11.glPopMatrix();

    }

}

/*******************************************************************************
* Copyright (c) 2014 Malorolam.
* 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the included license, which is also
* available at http://carbonization.wikispaces.com/License
* 
*********************************************************************************/