package JAKJ . RedstoneInMotion ;

public class MotiveSpectreRendererWorld extends net . minecraft . world . World
{
	public BlockRecordSet Ghosts ;

	public MotiveSpectreRendererWorld ( net . minecraft . world . World World , BlockRecordSet Ghosts )
	{
		super ( null , "" , null , new net . minecraft . world . WorldSettings ( 0 , null , false , false , null ) , null , null ) ;

		try
		{
			for ( java . lang . reflect . Field Field : net . minecraft . world . World . class . getDeclaredFields ( ) )
			{
				Field . setAccessible ( true ) ;

				java . lang . reflect . Field Modifiers = java . lang . reflect . Field . class . getDeclaredField ( "modifiers" ) ;

				Modifiers . setAccessible ( true ) ;

				Modifiers . setInt ( Field , Field . getModifiers ( ) & ( ~ java . lang . reflect . Modifier . FINAL ) ) ;

				Field . set ( this , Field . get ( World ) ) ;
			}
		}
		catch ( Throwable Throwable )
		{
			throw ( new RuntimeException ( Throwable ) ) ;
		}

		this . Ghosts = Ghosts ;

		for ( BlockRecord Record : Ghosts )
		{
			if ( Record . RenderingRecord != null )
			{
				try
				{
					if ( Record . Entity == null )
					{
						Record . Entity = Block . Get ( Record . Id ) . createTileEntity ( this , Record . Meta ) ;

						Record . Entity . xCoord = Record . X ;
						Record . Entity . yCoord = Record . Y ;
						Record . Entity . zCoord = Record . Z ;

						Record . Entity . worldObj = this ;
					}

					net . minecraft . network . packet . Packet132TileEntityData Packet = new net . minecraft . network . packet . Packet132TileEntityData
					(
						Record . X , Record . Y , Record . Z , Record . RenderingRecord . getInteger ( "actionType" ) , Record . RenderingRecord
					) ;

					Record . Entity . onDataPacket ( null , Packet ) ;
				}
				catch ( Throwable Throwable )
				{
					Throwable . printStackTrace ( ) ;

					Record . Entity = null ;
				}
			}
			else if ( Record . EntityRecord != null )
			{
				try
				{
					Record . Entity = net . minecraft . tileentity . TileEntity . createAndLoadEntity ( Record . EntityRecord ) ;

					Record . Entity . worldObj = this ;
				}
				catch ( Throwable Throwable )
				{
					Throwable . printStackTrace ( ) ;
				}
			}

			if ( Record . Entity != null )
			{
				try
				{
					if ( Class . forName ( "buildcraft.transport.TileGenericPipe" ) . isInstance ( Record . Entity ) )
					{
						try
						{
							Class . forName ( "buildcraft.transport.Pipe" ) . getDeclaredField ( "worldObj" ) . set
							(
								Class . forName ( "buildcraft.transport.TileGenericPipe" ) . getDeclaredField ( "pipe" ) . get ( Record . Entity ) , this
							) ;
						}
						catch ( Throwable Throwable )
						{
							Throwable . printStackTrace ( ) ;

							Record . Entity = null ;
						}
					}
				}
				catch ( Throwable Throwable )
				{
				}
			}
		}
	}

	@Override
	public net . minecraft . world . chunk . IChunkProvider createChunkProvider ( )
	{
		return ( null ) ;
	}

	@Override
	public net . minecraft . entity . Entity getEntityByID ( int Id )
	{
		return ( null ) ;
	}

	@Override
	public void markBlockForUpdate ( int X , int Y , int Z )
	{
	}

	@Override
	public void markBlockForRenderUpdate ( int X , int Y , int Z )
	{
	}

	public BlockRecord Lookup ( int X , int Y , int Z )
	{
		BlockRecord TargetRecord = new BlockRecord ( X , Y , Z ) ;

		BlockRecord FoundRecord = Ghosts . ceiling ( TargetRecord ) ;

		if ( FoundRecord == null )
		{
			return ( null ) ;
		}

		if ( TargetRecord . compareTo ( FoundRecord ) == 0 )
		{
			return ( FoundRecord ) ;
		}

		return ( null ) ;
	}

	@Override
	public int getBlockId ( int X , int Y , int Z )
	{
		BlockRecord Record = Lookup ( X , Y , Z ) ;

		if ( Record != null )
		{
			return ( Record . Id ) ;
		}

		return ( super . getBlockId ( X , Y , Z ) ) ;
	}

	@Override
	public int getBlockMetadata ( int X , int Y , int Z )
	{
		BlockRecord Record = Lookup ( X , Y , Z ) ;

		if ( Record != null )
		{
			return ( Record . Meta ) ;
		}

		return ( super . getBlockMetadata ( X , Y , Z ) ) ;
	}

	@Override
	public net . minecraft . tileentity . TileEntity getBlockTileEntity ( int X , int Y , int Z )
	{
		BlockRecord Record = Lookup ( X , Y , Z ) ;

		if ( Record != null )
		{
			if ( Record . Entity == null )
			{
				Record . Entity = Block . Get ( Record . Id ) . createTileEntity ( this , Record . Meta ) ;

				Record . Entity . xCoord = Record . X ;
				Record . Entity . yCoord = Record . Y ;
				Record . Entity . zCoord = Record . Z ;

				Record . Entity . worldObj = this ;
			}

			return ( Record . Entity ) ;
		}

		return ( super . getBlockTileEntity ( X , Y , Z ) ) ;
	}
}
