Except when naval ships, and their Captains, are carrying private freight for which they are paid for the service. Most of this treasure was not Spanish Royal Treasure, but was private treasure owned by Spanish entrepreneurs and officials. This treasure was subject to seizure by salvors, but the lazy American judges who couldn't be bothered to research 19th Century Admiralty law made an incorrect judgment.
Unless they’re captured in battle, and then they become prizes of war.
During much of the Napoleonic Wars, Britain got almost half of her Royal Navy tonnage from captured French and other combatant vessels.