package zmaster587.advancedRocketry.event;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import zmaster587.advancedRocketry.cable.NetworkRegistry;
import zmaster587.advancedRocketry.tile.cables.TilePipe;

public class CableTickHandler {

	@SubscribeEvent
	public void onTick(TickEvent.ServerTickEvent tick) {
		if(tick.phase == Phase.END) {
			NetworkRegistry.dataNetwork.tickAllNetworks();
			NetworkRegistry.energyNetwork.tickAllNetworks();
			NetworkRegistry.liquidNetwork.tickAllNetworks();
		}
	}

	@SubscribeEvent
	public void chunkLoadedEvent(ChunkEvent.Load event) {

		Map map = event.getChunk().getTileEntityMap();
		Iterator<Entry> iter = map.entrySet().iterator();

		while(iter.hasNext()) {
			Object obj = iter.next().getValue();

			if(obj instanceof TilePipe) {
				((TilePipe)obj).markDirty();
			}
		}
	}

	@SubscribeEvent
	public void onBlockBroken(BreakEvent event) {

		if(event.getState().getBlock().hasTileEntity(event.getState())) {

			TileEntity homeTile = event.getWorld().getTileEntity(event.getPos());

			if(homeTile instanceof TilePipe) {

				//removed in favor of pipecount
				//boolean lastInNetwork =true;

				((TilePipe)homeTile).setDestroyed();
				((TilePipe)homeTile).setInvalid();

				int pipecount=0;

				for(EnumFacing dir : EnumFacing.values()) {
					TileEntity tile = event.getWorld().getTileEntity(event.getPos().offset(dir));
					if(tile instanceof TilePipe) 
						pipecount++;
				}
				//TODO: delete check if sinks/sources need removal
				if(pipecount > 1) {
					for(EnumFacing dir : EnumFacing.VALUES) {
						TileEntity tile = event.getWorld().getTileEntity(event.getPos().offset(dir));

						if(tile instanceof TilePipe) {
							((TilePipe) tile).getNetworkHandler().removeNetworkByID(((TilePipe) tile).getNetworkID());
							((TilePipe) tile).setInvalid();
							//lastInNetwork = false;
						}
						//HandlerCableNetwork.removeFromAllTypes((TilePipe)tile,event.world.getTileEntity(event.x, event.y, event.z));
					}
				}
				if(pipecount == 0) //lastInNetwork
					((TilePipe)homeTile).getNetworkHandler().removeNetworkByID(((TilePipe)homeTile).getNetworkID());
				((TilePipe)homeTile).markDirty();
			}
			else if(homeTile != null) {
				for(EnumFacing dir : EnumFacing.VALUES) {
					TileEntity tile = event.getWorld().getTileEntity(event.getPos().offset(dir));

					if(tile instanceof TilePipe) {
						((TilePipe)tile).getNetworkHandler().removeFromAllTypes((TilePipe)tile, homeTile);
					}
				}
			}
		}
	}
}

@@ -37,16 +37,66 @@
 event;%0A%0A
+import java.util.ConcurrentModificationException;%0A
 import j
@@ -647,16 +647,69 @@
 .Phase;%0A
+import zmaster587.advancedRocketry.AdvancedRocketry;%0A
 import z
@@ -1289,16 +1289,25 @@
 tor();%0A%0A
+%09%09try %7B%0A%09
 %09%09while(
@@ -1327,16 +1327,17 @@
 )) %7B%0A%09%09%09
+%09
 Object o
@@ -1369,16 +1369,17 @@
 ();%0A%0A%09%09%09
+%09
 if(obj i
@@ -1396,24 +1396,25 @@
 TilePipe) %7B%0A
+%09
 %09%09%09%09((TilePi
@@ -1433,25 +1433,291 @@
 Dirty();%0A%09%09%09
-%7D
+%09%7D%0A%09%09%09%7D%0A%09%09%7D catch ( ConcurrentModificationException e) %7B%0A%09%09%09AdvancedRocketry.logger.warn(%22You have been visited by the rare pepe.. I mean error of pipes not loading, this is not good, some pipe systems may not work right away.  But it's better than a corrupt world%22);
 %0A%09%09%7D%0A%09%7D%0A%0A%09@S

add error handling for a rare but potentially world destroying bug