package JAKJ . RedstoneInMotion ;

import net . minecraft . tileentity . TileEntity ;
import net . minecraft . world . World ;
import net . minecraft . world . chunk . Chunk ;
import net . minecraft . world . chunk . storage . ExtendedBlockStorage ;

public abstract class WorldUtil
{
	public static void SetBlock ( World World , int X , int Y , int Z , int Id , int Meta )
	{
		World . setBlock ( X , Y , Z , Id , Meta , 0x3 ) ;
	}

	public static void NotifyBlocks ( World World , int X , int Y , int Z , int OldId , int NewId )
	{
		World . notifyBlockChange ( X , Y , Z , OldId ) ;

		if ( NewId == 0 )
		{
			return ;
		}

		if ( Block . Get ( NewId ) . hasComparatorInputOverride ( ) )
		{
			World . func_96440_m ( X , Y , Z , NewId ) ;
		}
	}

	public static void SneakySetBlock ( World World , int X , int Y , int Z , int Id , int Meta , TileEntity Entity )
	{
		Chunk Chunk = World . getChunkFromChunkCoords ( X >> 4 , Z >> 4 ) ;

		int ChunkX = X & 0xF ;
		int ChunkY = Y & 0xF ;
		int ChunkZ = Z & 0xF ;

		int HeightMapIndex = ChunkZ << 4 | ChunkX ;

		if ( Y >= Chunk . precipitationHeightMap [ HeightMapIndex ] - 1 )
		{
			Chunk . precipitationHeightMap [ HeightMapIndex ] = -999 ;
		}

		int HeightMapValue = Chunk . heightMap [ HeightMapIndex ] ;

		World . removeBlockTileEntity ( X , Y , Z ) ;

		ExtendedBlockStorage ExtendedBlockStorage = Chunk . getBlockStorageArray ( ) [ Y >> 4 ] ;

		if ( ExtendedBlockStorage == null )
		{
			ExtendedBlockStorage = new ExtendedBlockStorage ( ( Y >> 4 ) << 4 , ! World . provider . hasNoSky ) ;

			Chunk . getBlockStorageArray ( ) [ Y >> 4 ] = ExtendedBlockStorage ;
		}

		ExtendedBlockStorage . setExtBlockID ( ChunkX , ChunkY , ChunkZ , Id ) ;

		ExtendedBlockStorage . setExtBlockMetadata ( ChunkX , ChunkY , ChunkZ , Meta ) ;

		if ( Y >= HeightMapValue )
		{
			Chunk . generateSkylightMap ( ) ;
		}
		else
		{
			if ( Chunk . getBlockLightOpacity ( ChunkX , Y , ChunkZ ) > 0 )
			{
				if ( Y >= HeightMapValue )
				{
					Chunk . relightBlock ( ChunkX , Y + 1 , ChunkZ ) ;
				}
			}
			else if ( Y == HeightMapValue - 1 )
			{
				Chunk . relightBlock ( ChunkX , Y , ChunkZ ) ;
			}

			Chunk . propagateSkylightOcclusion ( ChunkX , ChunkZ ) ;
		}

		Chunk . isModified = true ;

		World . updateAllLightTypes ( X , Y , Z ) ;

		if ( Entity != null )
		{
			World . setBlockTileEntity ( X , Y , Z , Entity ) ;
		}
		else
		{
			World . removeBlockTileEntity ( X , Y , Z ) ;
		}

		World . markBlockForUpdate ( X , Y , Z ) ;
	}
}
