/******************************************************************************* * Copyright (c) 2012 cpw. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html * * Contributors: * cpw - initial API and implementation ******************************************************************************/ package cpw.mods.ironchest.client; import static org.lwjgl.opengl.GL11.glColor4f; import static org.lwjgl.opengl.GL11.glDisable; import static org.lwjgl.opengl.GL11.glEnable; import static org.lwjgl.opengl.GL11.glPopMatrix; import static org.lwjgl.opengl.GL11.glPushMatrix; import static org.lwjgl.opengl.GL11.glRotatef; import static org.lwjgl.opengl.GL11.glScalef; import static org.lwjgl.opengl.GL11.glTranslatef; import java.util.HashMap; import java.util.Map; import java.util.Random; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.model.ModelChest; import net.minecraft.client.renderer.entity.RenderEntityItem; import net.minecraft.client.renderer.entity.RenderItem; import net.minecraft.client.renderer.entity.RenderManager; 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.util.ResourceLocation; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import cpw.mods.ironchest.BlockIronChest; import cpw.mods.ironchest.IronChest; import cpw.mods.ironchest.IronChestType; import cpw.mods.ironchest.MappableItemStackWrapper; import cpw.mods.ironchest.TileEntityIronChest; public class TileEntityIronChestRenderer extends TileEntitySpecialRenderer { private static Map renderList = new HashMap(); private static Map locations; static { Builder builder = ImmutableMap.builder(); for (IronChestType typ : IronChestType.values()) { builder.put(typ, new ResourceLocation("ironchest","textures/model/"+typ.getModelTexture())); } locations = builder.build(); } private Random random; private RenderEntityItem itemRenderer; private ModelChest model; private static float[][] shifts = { { 0.3F, 0.45F, 0.3F }, { 0.7F, 0.45F, 0.3F }, { 0.3F, 0.45F, 0.7F }, { 0.7F, 0.45F, 0.7F }, { 0.3F, 0.1F, 0.3F }, { 0.7F, 0.1F, 0.3F }, { 0.3F, 0.1F, 0.7F }, { 0.7F, 0.1F, 0.7F }, { 0.5F, 0.32F, 0.5F }, }; public TileEntityIronChestRenderer() { model = new ModelChest(); random = new Random(); itemRenderer = new RenderEntityItem(Minecraft.getMinecraft().getRenderManager(), Minecraft.getMinecraft().getRenderItem()); /*{ @Override public byte getMiniBlockCount(ItemStack stack, byte original) { return SignedBytes.saturatedCast(Math.min(stack.stackSize / 32, 15) + 1); } @Override public byte getMiniItemCount(ItemStack stack, byte original) { return SignedBytes.saturatedCast(Math.min(stack.stackSize / 32, 7) + 1); } @Override public boolean shouldBob() { return false; } @Override public boolean shouldSpreadItems() { return false; } };*/ } public void render(TileEntityIronChest tile, double x, double y, double z, float partialTick) { if (tile == null) { return; } int facing = 3; IronChestType type = tile.getType(); if (tile != null && tile.hasWorldObj() && tile.getWorld().getBlockState(tile.getPos()).getBlock() == IronChest.ironChestBlock) { facing = tile.getFacing(); type = tile.getType(); IBlockState state = tile.getWorld().getBlockState(tile.getPos()); type = (IronChestType)state.getValue(BlockIronChest.VARIANT_PROP); } bindTexture(locations.get(type)); glPushMatrix(); glEnable(32826 /* GL_RESCALE_NORMAL_EXT */); glColor4f(1.0F, 1.0F, 1.0F, 1.0F); glTranslatef((float) x, (float) y + 1.0F, (float) z + 1.0F); glScalef(1.0F, -1F, -1F); glTranslatef(0.5F, 0.5F, 0.5F); int k = 0; if (facing == 2) { k = 180; } if (facing == 3) { k = 0; } if (facing == 4) { k = 90; } if (facing == 5) { k = -90; } glRotatef(k, 0.0F, 1.0F, 0.0F); glTranslatef(-0.5F, -0.5F, -0.5F); float lidangle = tile.prevLidAngle + (tile.lidAngle - tile.prevLidAngle) * partialTick; lidangle = 1.0F - lidangle; lidangle = 1.0F - lidangle * lidangle * lidangle; model.chestLid.rotateAngleX = -((lidangle * 3.141593F) / 2.0F); // Render the chest itself model.renderAll(); glDisable(32826 /* GL_RESCALE_NORMAL_EXT */); glPopMatrix(); glColor4f(1.0F, 1.0F, 1.0F, 1.0F); if (type.isTransparent() && tile.getDistanceSq(this.rendererDispatcher.field_147560_j, this.rendererDispatcher.field_147561_k, this.rendererDispatcher.field_147558_l) < 128d) { random.setSeed(254L); float shiftX; float shiftY; float shiftZ; int shift = 0; float blockScale = 0.70F; float timeD = (float) (360.0 * (double) (System.currentTimeMillis() & 0x3FFFL) / (double) 0x3FFFL); if (tile.getTopItemStacks()[1] == null) { shift = 8; blockScale = 0.85F; } glPushMatrix(); glDisable(2896 /* GL_LIGHTING */); glTranslatef((float) x, (float) y, (float) z); EntityItem customitem = new EntityItem(this.getWorld()); customitem.hoverStart = 0f; for (ItemStack item : tile.getTopItemStacks()) { if (shift > shifts.length) { break; } if (item == null) { shift++; continue; } shiftX = shifts[shift][0]; shiftY = shifts[shift][1]; shiftZ = shifts[shift][2]; shift++; glPushMatrix(); glTranslatef(shiftX, shiftY, shiftZ); glRotatef(timeD, 0.0F, 1.0F, 0.0F); glScalef(blockScale, blockScale, blockScale); customitem.setEntityItemStack(item); itemRenderer.doRender(customitem, 0, 0, 0, 0, 0); glPopMatrix(); } glEnable(2896 /* GL_LIGHTING */); glPopMatrix(); glColor4f(1.0F, 1.0F, 1.0F, 1.0F); } } @Override public void renderTileEntityAt(TileEntity tileentity, double x, double y, double z, float partialTick, int p_180535_9_) { render((TileEntityIronChest) tileentity, x, y, z, partialTick); } }