package com.qmunity.lib.client.render;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;

import org.lwjgl.opengl.GL11;

import com.qmunity.lib.block.BlockMultipart;
import com.qmunity.lib.part.IPart;
import com.qmunity.lib.part.IPartRenderable;
import com.qmunity.lib.tile.TileMultipart;
import com.qmunity.lib.vec.Vec3d;
import com.qmunity.lib.vec.Vec3i;

import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import cpw.mods.fml.client.registry.RenderingRegistry;

public class RenderMultipart extends TileEntitySpecialRenderer implements ISimpleBlockRenderingHandler {

    public static int PASS = 0;
    public static final int RENDER_ID = RenderingRegistry.getNextAvailableRenderId();

    @Override
    public void renderTileEntityAt(TileEntity tile, double x, double y, double z, float delta) {

        TileMultipart te = (TileMultipart) tile;

        GL11.glPushMatrix();
        {
            GL11.glTranslated(x, y, z);
            for (IPart p : te.getParts()) {
                if (p.getParent() != null && p instanceof IPartRenderable) {
                    GL11.glPushMatrix();

                    ((IPartRenderable) p).renderDynamic(new Vec3d(0, 0, 0), delta, PASS);

                    GL11.glPopMatrix();
                }
            }
        }
        GL11.glPopMatrix();
    }

    @Override
    public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {

        TileMultipart te = BlockMultipart.get(world, x, y, z);
        if (te != null)
            for (IPart p : te.getParts())
                if (p.getParent() != null && p instanceof IPartRenderable)
                    if (((IPartRenderable) p).shouldRenderOnPass(PASS))
                        ((IPartRenderable) p).renderStatic(new Vec3i(0, 0, 0), renderer, PASS);

        return false;
    }

    @Override
    public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer) {

    }

    @Override
    public boolean shouldRender3DInInventory(int modelId) {

        return false;
    }

    @Override
    public int getRenderId() {

        return RENDER_ID;
    }

}

@@ -771,17 +771,16 @@
 gHandler
- 
 %7B%0A%0A    p
@@ -1001,17 +1001,16 @@
 t delta)
- 
 %7B%0A%0A     
@@ -1046,17 +1046,16 @@
 ltipart)
- 
 tile;%0A%0A 
@@ -1139,33 +1139,32 @@
 %0A            for
- 
 (IPart p : te.ge
@@ -1185,33 +1185,32 @@
               if
- 
 (p.getParent() !
@@ -1323,33 +1323,32 @@
 IPartRenderable)
- 
 p).renderDynamic
@@ -1635,25 +1635,24 @@
 ks renderer)
- 
 %7B%0A%0A        T
@@ -1715,17 +1715,16 @@
       if
- 
 (te != n
@@ -1731,25 +1731,12 @@
 ull)
-%0A           
  for
- 
 (IPa
@@ -1769,23 +1769,18 @@
         
-    
 if
- 
 (p.getPa
@@ -1830,32 +1830,11 @@
 ble)
-%0A                   
  if
- 
 (((I
@@ -1848,17 +1848,16 @@
 derable)
- 
 p).shoul
@@ -1880,32 +1880,8 @@
 SS))
-%0A                       
  ((I
@@ -1895,17 +1895,16 @@
 derable)
- 
 p).rende
@@ -1921,23 +1921,23 @@
 w Vec3i(
-0, 0, 0
+x, y, z
 ), rende
@@ -2091,17 +2091,16 @@
 enderer)
- 
 %7B%0A%0A    %7D
@@ -2172,17 +2172,16 @@
 modelId)
- 
 %7B%0A%0A     
@@ -2246,17 +2246,16 @@
 nderId()
- 
 %7B%0A%0A     

Fix passed locacion in renderStatic