Mekanism-tilera-Edition/common/mekanism/client/render/tileentity/RenderBin.java
2013-11-11 19:27:36 -05:00

206 lines
7.1 KiB
Java

package mekanism.client.render.tileentity;
import mekanism.api.Object3D;
import mekanism.common.tileentity.TileEntityBin;
import mekanism.common.util.MekanismUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.OpenGlHelper;
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.TextureManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.common.ForgeDirection;
import org.lwjgl.opengl.GL11;
import universalelectricity.core.vector.Vector3;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/*
* Courtesy of MFFS & Assembly Line
*/
@SideOnly(Side.CLIENT)
public class RenderBin extends TileEntitySpecialRenderer
{
private final RenderBlocks renderBlocks = new RenderBlocks();
private final RenderItem renderItem = (RenderItem)RenderManager.instance.getEntityClassRenderObject(EntityItem.class);
@Override
public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float partialTick)
{
renderAModelAt((TileEntityBin)tileEntity, x, y, z, partialTick);
}
@SuppressWarnings("incomplete-switch")
private void renderAModelAt(TileEntityBin tileEntity, double x, double y, double z, float partialTick)
{
if(tileEntity instanceof TileEntityBin)
{
String itemName = "None";
String amount = "";
ItemStack itemStack = tileEntity.getStack();
if(itemStack != null)
{
itemName = itemStack.getDisplayName();
amount = Integer.toString(itemStack.stackSize);
}
for(ForgeDirection side : MekanismUtils.SIDE_DIRS)
{
Object3D obj = Object3D.get(tileEntity).getFromSide(side);
if(tileEntity.worldObj.isBlockSolidOnSide(obj.xCoord, obj.yCoord, obj.zCoord, side.getOpposite()))
{
continue;
}
doLight(tileEntity.worldObj, obj);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
if(itemStack != null)
{
GL11.glPushMatrix();
switch (side)
{
case NORTH:
GL11.glTranslated(x + 0.65, y + 0.9, z - 0.01);
break;
case SOUTH:
GL11.glTranslated(x + 0.35, y + 0.9, z + 1.01);
GL11.glRotatef(180, 0, 1, 0);
break;
case WEST:
GL11.glTranslated(x - 0.01, y + 0.9, z + 0.35);
GL11.glRotatef(90, 0, 1, 0);
break;
case EAST:
GL11.glTranslated(x + 1.01, y + 0.9, z + 0.65);
GL11.glRotatef(-90, 0, 1, 0);
break;
}
float scale = 0.03125F;
GL11.glScalef(0.6f * scale, 0.6f * scale, 0);
GL11.glRotatef(180, 0, 0, 1);
TextureManager renderEngine = Minecraft.getMinecraft().renderEngine;
GL11.glDisable(2896);
if(!ForgeHooksClient.renderInventoryItem(renderBlocks, renderEngine, itemStack, true, 0.0F, 0.0F, 0.0F))
{
renderItem.renderItemIntoGUI(getFontRenderer(), renderEngine, itemStack, 0, 0);
}
GL11.glEnable(2896);
GL11.glPopMatrix();
}
renderText(itemName, side, 0.02f, x, y - 0.35f, z);
if(amount != "")
{
renderText(amount, side, 0.02f, x, y - 0.15f, z);
}
}
}
}
private void doLight(World world, Object3D obj)
{
if(world.isBlockOpaqueCube(obj.xCoord, obj.yCoord, obj.zCoord))
{
return;
}
int br = world.getLightBrightnessForSkyBlocks(obj.xCoord, obj.yCoord, obj.zCoord, 0);
int var11 = br % 65536;
int var12 = br / 65536;
float scale = 0.6F;
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, var11 * scale, var12 * scale);
}
@SuppressWarnings("incomplete-switch")
private void renderText(String text, ForgeDirection side, float maxScale, double x, double y, double z)
{
GL11.glPushMatrix();
GL11.glPolygonOffset(-10, -10);
GL11.glEnable(GL11.GL_POLYGON_OFFSET_FILL);
float displayWidth = 1 - (2 / 16);
float displayHeight = 1 - (2 / 16);
GL11.glTranslated(x, y, z);
switch(side)
{
case SOUTH:
GL11.glTranslatef(0, 1, 0);
GL11.glRotatef(0, 0, 1, 0);
GL11.glRotatef(90, 1, 0, 0);
break;
case NORTH:
GL11.glTranslatef(1, 1, 1);
GL11.glRotatef(180, 0, 1, 0);
GL11.glRotatef(90, 1, 0, 0);
break;
case EAST:
GL11.glTranslatef(0, 1, 1);
GL11.glRotatef(90, 0, 1, 0);
GL11.glRotatef(90, 1, 0, 0);
break;
case WEST:
GL11.glTranslatef(1, 1, 0);
GL11.glRotatef(-90, 0, 1, 0);
GL11.glRotatef(90, 1, 0, 0);
break;
}
GL11.glTranslatef(displayWidth / 2, 1F, displayHeight / 2);
GL11.glRotatef(-90, 1, 0, 0);
FontRenderer fontRenderer = getFontRenderer();
int requiredWidth = Math.max(fontRenderer.getStringWidth(text), 1);
int lineHeight = fontRenderer.FONT_HEIGHT + 2;
int requiredHeight = lineHeight * 1;
float scaler = 0.8f;
float scaleX = (displayWidth / requiredWidth);
float scale = scaleX * scaler;
if(maxScale > 0)
{
scale = Math.min(scale, maxScale);
}
GL11.glScalef(scale, -scale, scale);
GL11.glDepthMask(false);
int offsetX;
int offsetY;
int realHeight = (int) Math.floor(displayHeight / scale);
int realWidth = (int) Math.floor(displayWidth / scale);
offsetX = (realWidth - requiredWidth) / 2;
offsetY = (realHeight - requiredHeight) / 2;
GL11.glDisable(GL11.GL_LIGHTING);
fontRenderer.drawString("\u00a7f" + text, offsetX - (realWidth / 2), 1 + offsetY - (realHeight / 2), 1);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glDepthMask(true);
GL11.glDisable(GL11.GL_POLYGON_OFFSET_FILL);
GL11.glPopMatrix();
}
}