package storagecraft.tile;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import storagecraft.block.BlockCable;

public class TileCable extends TileSC {
	public static boolean isCable(World world, int x, int y, int z, ForgeDirection dir) {
		Block block = world.getBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ);

		return block instanceof BlockCable;
	}

	public boolean hasConnection(ForgeDirection dir) {
		if (!isCable(worldObj, xCoord, yCoord, zCoord, dir)) {
			TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);

			return tile instanceof IMachine || tile instanceof TileController;
		}

		return true;
	}

	public List<IMachine> findMachines(TileController controller) {
		return findMachines(new ArrayList(), controller);
	}

	private List<IMachine> findMachines(List<Vec3> visited, TileController controller) {
		List<IMachine> machines = new ArrayList<IMachine>();

		for (Vec3 visitedCable : visited) {
			if (visitedCable.xCoord == xCoord && visitedCable.yCoord == yCoord && visitedCable.zCoord == zCoord) {
				return machines;
			}
		}

		visited.add(Vec3.createVectorHelper(xCoord, yCoord, zCoord));

		for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
			TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);

			if (tile instanceof IMachine) {
				machines.add((IMachine) tile);
			} else if (tile instanceof TileCable) {
				machines.addAll(((TileCable) tile).findMachines(visited, controller));
			} else if (tile instanceof TileController && (tile.xCoord != controller.xCoord || tile.yCoord != controller.yCoord || tile.zCoord != controller.zCoord)) {
				worldObj.createExplosion(null, tile.xCoord, tile.yCoord, tile.zCoord, 4.5f, true);
			}
		}

		return machines;
	}
}

@@ -1194,21 +1194,21 @@
  visited
-Cable
+Block
  : visit
@@ -1228,21 +1228,21 @@
 (visited
-Cable
+Block
 .xCoord 
@@ -1261,21 +1261,21 @@
  visited
-Cable
+Block
 .yCoord 
@@ -1294,21 +1294,21 @@
  visited
-Cable
+Block
 .zCoord 
@@ -1494,50 +1494,51 @@
 %0A%09%09%09
-TileEntity tile = worldObj.getTileEntity(x
+int x = xCoord + dir.offsetX;%0D%0A%09%09%09int y = y
 Coor
@@ -1543,36 +1543,48 @@
 ord + dir.offset
-X, y
+Y;%0D%0A%09%09%09int z = z
 Coord + dir.offs
@@ -1585,39 +1585,310 @@
 r.offset
-Y, zCoord + dir.offsetZ
+Z;%0D%0A%0D%0A%09%09%09boolean found = false;%0D%0A%0D%0A%09%09%09for (Vec3 visitedBlock : visited) %7B%0D%0A%09%09%09%09if (visitedBlock.xCoord == x && visitedBlock.yCoord == y && visitedBlock.zCoord == z) %7B%0D%0A%09%09%09%09%09found = true;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%09if (found) %7B%0D%0A%09%09%09%09continue;%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%09TileEntity tile = worldObj.getTileEntity(x, y, z
 );%0D%0A%0D%0A%09%09
@@ -1952,24 +1952,78 @@
 ine) tile);%0D
+%0A%0D%0A%09%09%09%09visited.add(Vec3.createVectorHelper(x, y, z));%0D
 %0A%09%09%09%7D else i
@@ -2180,27 +2180,17 @@
 ler && (
-tile.xCoord
+x
  != cont
@@ -2206,27 +2206,17 @@
 oord %7C%7C 
-tile.yCoord
+y
  != cont
@@ -2232,27 +2232,17 @@
 oord %7C%7C 
-tile.zCoord
+z
  != cont
@@ -2299,45 +2299,15 @@
 ll, 
-tile.xCoord, tile.yCoord, tile.zCoord
+x, y, z
 , 4.

check for duplicate machines (makes it even more slower)