package crazypants.enderio.machine;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import crazypants.enderio.machine.painter.IPaintableTileEntity;
import crazypants.enderio.machine.painter.PaintedBlockRenderer;
import crazypants.enderio.machine.painter.PainterUtil;
import crazypants.render.BoundingBox;
import crazypants.render.CubeRenderer;
import crazypants.render.CustomCubeRenderer;
import crazypants.render.IconUtil;
import crazypants.render.RenderUtil;

public class AbstractMachineRenderer implements ISimpleBlockRenderingHandler, IItemRenderer {

  private OverlayRenderer overlayRenderer = new OverlayRenderer();

  private CustomCubeRenderer ccr = new CustomCubeRenderer();

  private PaintedBlockRenderer paintedRenderer = new PaintedBlockRenderer(this.getRenderId(), null); // passthrough renderer for paintable machines

  @Override
  public boolean handleRenderType(ItemStack item, ItemRenderType type) {
    return true;
  }

  @Override
  public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper) {
    return true;
  }

  @Override
  public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
    Block block;
    if((block = PainterUtil.getSourceBlock(item)) != null) {
      paintedRenderer.renderInventoryBlock(block, PainterUtil.getSourceBlockMetadata(item), 0, (RenderBlocks) data[0]);
    } else {
      renderInventoryBlock(Block.getBlockFromItem(item.getItem()), item.getItemDamage(), 0, (RenderBlocks) data[0]);
    }
  }

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

    BoundingBox bb = BoundingBox.UNIT_CUBE;

    Tessellator.instance.startDrawingQuads();

    IIcon[] textures = RenderUtil.getBlockTextures(block, metadata);

    float[] brightnessPerSide = new float[6];
    for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
      brightnessPerSide[dir.ordinal()] = Math.max(RenderUtil.getColorMultiplierForFace(dir) + 0.1f, 1f);
    }
    CubeRenderer.render(bb, textures, null, brightnessPerSide);
    Tessellator.instance.draw();
  }

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

    TileEntity te = world.getTileEntity(x, y, z);
    AbstractMachineEntity machine = null;
    if(te instanceof AbstractMachineEntity) {
      machine = (AbstractMachineEntity) te;
    }

    overlayRenderer.setTile(machine);
    if(te instanceof IPaintableTileEntity && ((IPaintableTileEntity) te).getSourceBlock() != null) {
      ccr.setOverrideTexture(IconUtil.blankTexture);
      ccr.renderBlock(world, block, x, y, z, overlayRenderer);
      ccr.setOverrideTexture(renderer.overrideBlockTexture);
      paintedRenderer.renderWorldBlock(world, x, y, z, block, modelId, renderer);
    } else {
      ccr.setOverrideTexture(renderer.overrideBlockTexture);
      ccr.renderBlock(world, block, x, y, z, overlayRenderer);
    }
    ccr.setOverrideTexture(null);

    return true;
  }

  @Override
  public boolean shouldRender3DInInventory(int modelId) {
    return true;
  }

  @Override
  public int getRenderId() {
    return AbstractMachineBlock.renderId;
  }
}

@@ -30,16 +30,40 @@
 chine;%0A%0A
+import java.util.List;%0A%0A
 import n
@@ -809,32 +809,77 @@
 omCubeRenderer;%0A
+import crazypants.render.CustomRenderBlocks;%0A
 import crazypant
@@ -893,24 +893,24 @@
 r.IconUtil;%0A
-
 import crazy
@@ -933,16 +933,50 @@
 derUtil;
+%0Aimport crazypants.vecmath.Vertex;
 %0A%0Apublic
@@ -1064,16 +1064,16 @@
 erer %7B%0A%0A
-
   privat
@@ -1121,32 +1121,434 @@
 verlayRenderer()
+ %7B%0A    @Override%0A    public void renderFace(CustomRenderBlocks rb, ForgeDirection face, Block par1Block, double x, double y, double z, IIcon texture, List%3CVertex%3E refVertices,%0A        boolean translateToXyz) %7B%0A%0A      ccr.getCustomRenderBlocks().doDefaultRenderFace(face, par1Block, x, y, z, texture);%0A      super.renderFace(rb, face, par1Block, x, y, z, texture, refVertices, translateToXyz);%0A    %7D%0A  %7D
 ;%0A%0A  private Cus
@@ -3155,25 +3155,24 @@
 renderer) %7B%0A
-%0A
     TileEnti
@@ -3251,16 +3251,17 @@
 = null;%0A
+%0A
     if(t

Fix non-model machine rendering