//------------------------------------------------------
//
//   ArchitectureCraft - Shape rendering dispatcher
//
//------------------------------------------------------

package gcewing.architecture;

import net.minecraft.block.*;
import net.minecraft.block.state.*;
import net.minecraft.client.*;
import net.minecraft.client.renderer.texture.*;
import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraft.tileentity.*;
import net.minecraft.world.*;
import net.minecraft.util.*;

import gcewing.architecture.BaseModClient.*;

public class ShapeRenderDispatch implements ICustomRenderer {

	// Cannot have any per-render state, because it may be
	// called from more than one thread.

	@Override
	public void renderBlock(IBlockAccess world, BlockPos pos, IBlockState state, IRenderTarget target,
		EnumWorldBlockLayer layer, Trans3 t)
	{
		ShapeTE te = ShapeTE.get(world, pos);
		if (te != null) {
			Trans3 t2 = t.t(te.localToGlobalRotation());
			boolean renderBase = te.baseBlockState != null
				&& te.baseBlockState.getBlock().getBlockLayer() == layer;
			boolean renderSecondary = te.secondaryBlockState != null
				&& te.secondaryBlockState.getBlock().getBlockLayer() == layer;
			renderShapeTE(te, target, t2, renderBase, renderSecondary);
		}
	}

	@Override	
	public void renderItemStack(ItemStack stack, IRenderTarget target, Trans3 t) {
		ShapeTE te = new ShapeTE();
		te.readFromItemStack(stack);
		renderShapeTE(te, target, t,
			te.baseBlockState != null, te.secondaryBlockState != null);
	}

	protected void renderShapeTE(ShapeTE te, IRenderTarget target, Trans3 t,
		boolean renderBase, boolean renderSecondary)
	{
		if (te.shape != null && (renderBase || renderSecondary)) {
			IBlockState base = te.baseBlockState;
			if (base != null) {
				TextureAtlasSprite icon = Utils.getSpriteForBlockState(base);
				if (icon != null) {
					ITexture[] textures = new ITexture[4];
					textures[0] = BaseTexture.fromSprite(icon);
					textures[1] = textures[0].projected();
					if (renderSecondary) {
						TextureAtlasSprite icon2 = Utils.getSpriteForBlockState(te.secondaryBlockState);
						if (icon2 != null) {
							textures[2] = BaseTexture.fromSprite(icon2);
							textures[3] = textures[2].projected();
						}
						else
							renderSecondary = false;
					}
					if (renderBase && textures[2] == null && te.shape.kind.secondaryDefaultsToBase()) {
						textures[2] = textures[0];
						textures[3] = textures[1];
						renderSecondary = renderBase;
					}
					te.shape.kind.renderShape(te, textures, target, t, renderBase, renderSecondary);
				}
			}
		}
	}
	
}

@@ -494,16 +494,72 @@
 til.*;%0A%0A
+import net.minecraftforge.client.MinecraftForgeClient;%0A%0A
 import g
@@ -1832,67 +1832,350 @@
 %0A%09%09%09
-%09TextureAtlasSprite icon = Utils.getSpriteForBlockState(bas
+    //System.out.printf(%22ShapeRenderDispatch.renderShapeTE: in layer %25s renderBase = %25s renderSecondary = %25s%5Cn%22,%0A%09%09%09    //    MinecraftForgeClient.getRenderLayer(), renderBase, renderSecondary);%0A%09%09%09%09TextureAtlasSprite icon = Utils.getSpriteForBlockState(base);%0A%09%09%09%09TextureAtlasSprite icon2 = Utils.getSpriteForBlockState(te.secondaryBlockStat
 e);%0A
@@ -2243,24 +2243,51 @@
 re%5B4%5D;%0A%09%09%09%09%09
+if (renderBase) %7B%0A%09%09%09%09%09    
 textures%5B0%5D 
@@ -2319,24 +2319,28 @@
 icon);%0A%09%09%09%09%09
+    
 textures%5B1%5D 
@@ -2362,24 +2362,31 @@
 rojected();%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%09if (ren
@@ -2405,95 +2405,8 @@
 ) %7B%0A
-%09%09%09%09%09%09TextureAtlasSprite icon2 = Utils.getSpriteForBlockState(te.secondaryBlockState);%0A
 %09%09%09%09
@@ -2610,31 +2610,8 @@
 e &&
- textures%5B2%5D == null &&
  te.
@@ -2659,75 +2659,314 @@
 %09%09%09%09
-%09textures%5B2%5D = textures%5B0%5D;%0A%09%09%09%09%09%09textures%5B3%5D = textures%5B1%5D;%0A%09%09%09%09%09%09
+    if (icon2 == null %7C%7C (te.secondaryBlockState != null &&%0A%09%09%09%09%09        te.secondaryBlockState.getBlock().getBlockLayer() != EnumWorldBlockLayer.SOLID)) %7B%0A                                textures%5B2%5D = textures%5B0%5D;%0A                                textures%5B3%5D = textures%5B1%5D;%0A                                
 rend
@@ -2987,24 +2987,50 @@
 renderBase;%0A
+                        %7D%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%09

Fix rendering order of base and cladding with transparency