package JAKJ . RedstoneInMotion ;

public class PacketHandler implements cpw . mods . fml . common . network . IPacketHandler
{
	public static final String Channel = "JAKJ_RIM" ;

	public static void DispatchPacket ( CarriagePackage Package )
	{
		try
		{
			java . io . ByteArrayOutputStream DataStore = new java . io . ByteArrayOutputStream ( ) ;
			java . io . DataOutputStream Data  = new java . io . DataOutputStream ( DataStore ) ;

			Data . writeInt ( Package . DriveRecord . X ) ;
			Data . writeInt ( Package . DriveRecord . Y ) ;
			Data . writeInt ( Package . DriveRecord . Z ) ;
			Data . writeInt ( Package . RenderCacheKey . Dimension ) ;

			Data . writeBoolean ( Package . DriveIsAnchored ) ;

			Data . writeInt ( Package . Blocks . size ( ) ) ;

			for ( BlockRecord Record : Package . Blocks )
			{
				Data . writeInt ( Record . X ) ;
				Data . writeInt ( Record . Y ) ;
				Data . writeInt ( Record . Z ) ;
			}

			if ( Package . MotionDirection == null )
			{
				cpw . mods . fml . common . network . PacketDispatcher . sendPacketToAllAround
				(
					Package . AnchorRecord . X + 0.5 ,
					Package . AnchorRecord . Y + 0.5 ,
					Package . AnchorRecord . Z + 0.5 ,
					64 ,
					Package . World . provider . dimensionId ,
					cpw . mods . fml . common . network . PacketDispatcher . getPacket ( Channel , DataStore . toByteArray ( ) )
				) ;

				cpw . mods . fml . common . network . PacketDispatcher . sendPacketToAllAround
				(
					Package . AnchorRecord . X - Package . DriveRecord . X + Package . Translocator . xCoord + 0.5 ,
					Package . AnchorRecord . Y - Package . DriveRecord . Y + Package . Translocator . yCoord + 0.5 ,
					Package . AnchorRecord . Z - Package . DriveRecord . Z + Package . Translocator . zCoord + 0.5 ,
					64 ,
					Package . Translocator . worldObj . provider . dimensionId ,
					cpw . mods . fml . common . network . PacketDispatcher . getPacket ( Channel , DataStore . toByteArray ( ) )
				) ;
			}
			else
			{
				cpw . mods . fml . common . network . PacketDispatcher . sendPacketToAllAround
				(
					Package . AnchorRecord . X + Package . MotionDirection . DeltaX + 0.5 ,
					Package . AnchorRecord . Y + Package . MotionDirection . DeltaY + 0.5 ,
					Package . AnchorRecord . Z + Package . MotionDirection . DeltaZ + 0.5 ,
					64 ,
					Package . World . provider . dimensionId ,
					cpw . mods . fml . common . network . PacketDispatcher . getPacket ( Channel , DataStore . toByteArray ( ) )
				) ;
			}
		}
		catch ( Throwable Throwable )
		{
			Throwable . printStackTrace ( ) ;
		}
	}

	@Override
	public void onPacketData ( net . minecraft . network . INetworkManager NetworkManager , net . minecraft . network . packet . Packet250CustomPayload Packet , cpw . mods . fml . common . network . Player PlayerObject )
	{
		try
		{
			java . io . DataInputStream Data = new java . io . DataInputStream ( new java . io . ByteArrayInputStream ( Packet . data ) ) ;

			int DriveX = Data . readInt ( ) ;
			int DriveY = Data . readInt ( ) ;
			int DriveZ = Data . readInt ( ) ;
			int Dimension = Data . readInt ( ) ;

			boolean DriveIsAnchored = Data . readBoolean ( ) ;

			int BlockCount = Data . readInt ( ) ;

			BlockRecordSet Blocks = new BlockRecordSet ( ) ;

			BlockRecordSet TileEntities = new BlockRecordSet ( ) ;

			net . minecraft . entity . player . EntityPlayer Player = ( net . minecraft . entity . player . EntityPlayer ) PlayerObject ;

			for ( int Index = 0 ; Index < BlockCount ; Index ++ )
			{
				BlockRecord Record = new BlockRecord ( Data . readInt ( ) , Data . readInt ( ) , Data . readInt ( ) ) ;

				Record . Identify ( Player . worldObj ) ;

				Blocks . add ( Record ) ;

				if ( Record . Entity != null )
				{
					TileEntities . add ( Record ) ;
				}

				if ( ! DriveIsAnchored )
				{
					if ( Record . X == DriveX )
					{
						if ( Record . Y == DriveY )
						{
							if ( Record . Z == DriveZ )
							{
								( ( CarriageDriveEntity ) Record . Entity ) . Active = true ;
							}
						}
					}
				}
			}

			try
			{
				CarriageRenderCache . Assemble ( Blocks , TileEntities , Player . worldObj , new BlockPosition ( DriveX , DriveY , DriveZ , Dimension , null ) ) ;
			}
			catch ( Throwable Throwable )
			{
				Throwable . printStackTrace ( ) ;
			}
		}
		catch ( Throwable Throwable )
		{
			Throwable . printStackTrace ( ) ;
		}
	}
}
