/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.render;
import org.lwjgl.opengl.GL11;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraftforge.client.IItemRenderer;
import buildcraft.BuildCraftTransport;
import buildcraft.api.facades.FacadeType;
import buildcraft.api.facades.IFacadeItem;
import buildcraft.core.CoreConstants;
import buildcraft.core.render.RenderUtils;
import buildcraft.transport.ItemFacade;
import buildcraft.transport.ItemFacade.FacadeState;
import buildcraft.transport.PipeIconProvider;
public class FacadeItemRenderer implements IItemRenderer {
private long lastTime = 0L;
private int renderState = 0;
private void renderFacadeItem(RenderBlocks render, ItemStack item, float translateX, float translateY, float translateZ) {
if (lastTime < System.currentTimeMillis()) {
// 12 = LCM(1, 2, 3, 4)
renderState = (renderState + 1) % 12;
lastTime = System.currentTimeMillis() + 1000L;
}
FacadeType type = ((IFacadeItem) item.getItem()).getFacadeType(item);
FacadeState[] states = ItemFacade.getFacadeStates(item);
FacadeState activeState = null;
if (states.length > 0) {
// TODO: Figure out why NEI causes states[] to be of length 0
if (type == FacadeType.Basic) {
activeState = states[0];
} else if (type == FacadeType.Phased) {
activeState = states[renderState % states.length];
}
}
Block block = activeState != null ? activeState.block : null;
int decodedMeta = activeState != null ? activeState.metadata : 0;
try {
int color = item.getItem().getColorFromItemStack(new ItemStack(block, 1, decodedMeta), 0);
RenderUtils.setGLColorFromInt(color);
} catch (Throwable error) {
}
Tessellator tessellator = Tessellator.instance;
if (tryGetBlockIcon(block, 0, decodedMeta) == null) {
return;
}
// Render Facade
GL11.glPushMatrix();
// Enable glBlending for transparency
if (block != null && block.getRenderBlockPass() > 0) {
GL11.glAlphaFunc(GL11.GL_GREATER, 0.1F);
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(770, 771, 1, 0);
}
render.setRenderBounds(0F, 0F, 1F - 1F / 16F, 1F, 1F, 1F);
GL11.glTranslatef(translateX, translateY, translateZ);
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, -1F, 0.0F);
render.renderFaceYNeg(block, 0.0D, 0.0D, 0.0D, tryGetBlockIcon(block, 0, decodedMeta));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 1.0F, 0.0F);
render.renderFaceYPos(block, 0.0D, 0.0D, 0.0D, tryGetBlockIcon(block, 1, decodedMeta));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 0.0F, -1F);
render.renderFaceZNeg(block, 0.0D, 0.0D, 0.0D, tryGetBlockIcon(block, 2, decodedMeta));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 0.0F, 1.0F);
render.renderFaceZPos(block, 0.0D, 0.0D, 0.0D, tryGetBlockIcon(block, 3, decodedMeta));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(-1F, 0.0F, 0.0F);
render.renderFaceXNeg(block, 0.0D, 0.0D, 0.0D, tryGetBlockIcon(block, 4, decodedMeta));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(1.0F, 0.0F, 0.0F);
render.renderFaceXPos(block, 0.0D, 0.0D, 0.0D, tryGetBlockIcon(block, 5, decodedMeta));
tessellator.draw();
// Disable blending
if (block != null && block.getRenderBlockPass() > 0) {
GL11.glDisable(GL11.GL_BLEND);
}
GL11.glPopMatrix();
// Render StructurePipe
block = BuildCraftTransport.genericPipeBlock;
IIcon textureID = BuildCraftTransport.instance.pipeIconProvider.getIcon(PipeIconProvider.TYPE.PipeStructureCobblestone.ordinal()); // Structure pipe
block.setBlockBounds(CoreConstants.PIPE_MIN_POS, CoreConstants.PIPE_MIN_POS, CoreConstants.PIPE_MIN_POS, CoreConstants.PIPE_MAX_POS, CoreConstants.PIPE_MAX_POS, CoreConstants.PIPE_MAX_POS - 1F / 16F);
block.setBlockBoundsForItemRender();
render.setRenderBoundsFromBlock(block);
GL11.glTranslatef(translateX, translateY, translateZ + 0.25F);
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, -0F, 0.0F);
render.renderFaceYNeg(block, 0.0D, 0.0D, 0.0D, textureID);
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 1.0F, 0.0F);
render.renderFaceYPos(block, 0.0D, 0.0D, 0.0D, textureID);
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 0.0F, -1F);
render.renderFaceZNeg(block, 0.0D, 0.0D, 0.0D, textureID);
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 0.0F, 1.0F);
render.renderFaceZPos(block, 0.0D, 0.0D, 0.0D, textureID);
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(-1F, 0.0F, 0.0F);
render.renderFaceXNeg(block, 0.0D, 0.0D, 0.0D, textureID);
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(1.0F, 0.0F, 0.0F);
render.renderFaceXPos(block, 0.0D, 0.0D, 0.0D, textureID);
tessellator.draw();
GL11.glTranslatef(0.5F, 0.5F, 0.5F);
block.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
}
private IIcon tryGetBlockIcon(Block block, int side, int decodedMeta) {
IIcon icon = null;
try {
icon = block.getIcon(side, decodedMeta);
} catch (Throwable t) {
try {
icon = block.getBlockTextureFromSide(side);
} catch (Throwable ignored) {
}
} finally {
if (icon == null) {
icon = PipeIconProvider.TYPE.TransparentFacade.getIcon();
}
}
return icon;
}
@Override
public boolean handleRenderType(ItemStack item, ItemRenderType type) {
switch (type) {
case ENTITY:
return true;
case EQUIPPED:
return true;
case EQUIPPED_FIRST_PERSON:
return true;
case INVENTORY:
return true;
default:
return false;
}
}
@Override
public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper) {
return true;
}
@Override
public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
switch (type) {
case ENTITY:
GL11.glScalef(0.50F, 0.50F, 0.50F);
renderFacadeItem((RenderBlocks) data[0], item, -0.6F, 0f, -0.6F);
break;
case EQUIPPED:
case EQUIPPED_FIRST_PERSON:
renderFacadeItem((RenderBlocks) data[0], item, 0F, 0F, 0f);
break;
case INVENTORY:
GL11.glScalef(1.1F, 1.1F, 1.1F);
renderFacadeItem((RenderBlocks) data[0], item, -0.3f, -0.35f, -0.7f);
break;
default:
}
}
}