package joshie.harvestmoon.blocks.render;

import joshie.harvestmoon.blocks.BlockPreview;
import joshie.harvestmoon.blocks.tiles.TileMarker;
import joshie.harvestmoon.buildings.Building;
import joshie.harvestmoon.buildings.placeable.Placeable;
import joshie.harvestmoon.buildings.placeable.blocks.PlaceableBlock;
import joshie.harvestmoon.util.RenderBase;
import net.minecraft.client.renderer.RenderBlocks;

public class RenderBlockPreview extends RenderBase {
    public static RenderBlocks renderer = new RenderBlocks();

    public static Building preview;
    
    public static int[] getAdjustedCoordinates(PlaceableBlock block, boolean n1, boolean n2, boolean swap) {
        int yCoord = block.getY();
        int xCoord = n1 ? -block.getX() : block.getX();
        int zCoord = n2 ? -block.getZ() : block.getZ();
        if (swap) {
            int xClone = xCoord; //Create a copy of X
            xCoord = zCoord; //Set x to z
            zCoord = xClone; //Set z to the old value of x
        }
        
        return new int[] { xCoord, yCoord, zCoord };
    }

    @Override
    public void renderBlock() {
        if (!isItem()) {
            int meta = world.getBlockMetadata(x, y, z);
            boolean n1 = BlockPreview.getN1FromMeta(meta);
            boolean n2 = BlockPreview.getN2FromMeta(meta);
            boolean swap = BlockPreview.getSwapFromMeta(meta);
            TileMarker marker = (TileMarker) world.getTileEntity(x, y, z);
            preview = marker.getBuilding();
            if (preview == null) return;
            
            renderer.blockAccess = preview.getBlockAccess(x, y, z, n1, n2, swap);
            
            for (Placeable placeable : preview.getList()) {
                if (placeable instanceof PlaceableBlock) {
                    PlaceableBlock block = (PlaceableBlock) placeable;
                    int xCoord = n1? -block.getX(): block.getX();
                    int yCoord = block.getY();
                    int zCoord = n2? -block.getZ(): block.getZ();
                    
                    if (swap) {
                        int xClone = xCoord; //Create a copy of X
                        xCoord = zCoord; //Set x to z
                        zCoord = xClone; //Set z to the old value of x
                    }
                    
                    renderer.renderBlockAllFaces(block.getBlock(), x + xCoord, y + yCoord, z + zCoord);
                }
            }
        }
    }
}

@@ -521,559 +521,8 @@
 );%0A%0A
-    public static Building preview;%0A    %0A    public static int%5B%5D getAdjustedCoordinates(PlaceableBlock block, boolean n1, boolean n2, boolean swap) %7B%0A        int yCoord = block.getY();%0A        int xCoord = n1 ? -block.getX() : block.getX();%0A        int zCoord = n2 ? -block.getZ() : block.getZ();%0A        if (swap) %7B%0A            int xClone = xCoord; //Create a copy of X%0A            xCoord = zCoord; //Set x to z%0A            zCoord = xClone; //Set z to the old value of x%0A        %7D%0A        %0A        return new int%5B%5D %7B xCoord, yCoord, zCoord %7D;%0A    %7D%0A%0A
     
@@ -912,16 +912,25 @@
         
+Building 
 preview 
@@ -994,28 +994,16 @@
 return;%0A
-            
 %0A       
@@ -1077,28 +1077,16 @@
  swap);%0A
-            
 %0A       
@@ -1303,16 +1303,17 @@
 ord = n1
+ 
 ? -block
@@ -1319,16 +1319,17 @@
 k.getX()
+ 
 : block.
@@ -1418,16 +1418,17 @@
 ord = n2
+ 
 ? -block
@@ -1434,16 +1434,17 @@
 k.getZ()
+ 
 : block.
@@ -1451,36 +1451,16 @@
 getZ();%0A
-                    
 %0A       
@@ -1697,36 +1697,16 @@
       %7D%0A
-                    
 %0A       

Remove redundant code.