package musician101.luc.bukkit;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class UUIDFinder
{
	File file;
	JavaPlugin plugin;
	Map<String, String> uuids = new HashMap<String, String>();
	
	public UUIDFinder(JavaPlugin plugin) throws FileNotFoundException, IOException, InvalidConfigurationException
	{
		this.plugin = plugin;
		file = new File(plugin.getDataFolder(), "players.yml");
		loadUUIDs();
	}
	
	private void loadUUIDs() throws FileNotFoundException, IOException, InvalidConfigurationException
	{
		if (!file.exists())
		{
			file.createNewFile();
			BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
			bw.write("# This file keeps track of players' last known username.");
			bw.write("# Do not edit this file unless it is absolutely neccessary.");
			bw.close();
		}
		
		YamlConfiguration players = new YamlConfiguration();
		players.load(file);
		for (Entry<String, Object> entry : players.getValues(true).entrySet())
			uuids.put(entry.getKey(), entry.getValue().toString());
	}
	
	public void saveUUIDs() throws IOException
	{
		YamlConfiguration players = new YamlConfiguration();
		for (Entry<String, String> entry : uuids.entrySet())
			players.set(entry.getKey(), entry.getValue());
		
		players.save(file);
	}
	
	public void reloadUUIDs() throws FileNotFoundException, IOException, InvalidConfigurationException
	{
		saveUUIDs();
		loadUUIDs();
	}
	
	public Player getPlayer(String player) throws IllegalArgumentException, NullPointerException
	{
		if (!uuids.containsValue(player))
			throw new NullPointerException("Player not found.");
		
		for (Entry<String, String> entry : uuids.entrySet())
			if (entry.getValue().equals(player))
				return Bukkit.getPlayer(UUID.fromString(entry.getKey()));
		
		throw new NullPointerException("Player not found.");
	}
}

@@ -1133,24 +1133,41 @@
 sername.%22);%0A
+%09%09%09bw.newLine();%0A
 %09%09%09bw.write(
@@ -1884,156 +1884,62 @@
 yer)
- throws IllegalArgumentException, NullPointerException%0A%09%7B%0A%09%09if (!uuids.containsValue(player))%0A%09%09%09throw new NullPointerException(%22Player not found.%22)
+%0A%09%7B%0A%09%09if (!uuids.containsValue(player))%0A%09%09%09return null
 ;%0A%09%09
@@ -2105,58 +2105,226 @@
 %09%0A%09%09
-throw new NullPointerException(%22Player not found.%22
+return null;%0A%09%7D%0A%09%0A%09public void addPlayer(Player player)%0A%09%7B%0A%09%09uuids.put(player.getUniqueId().toString(), player.getName());%0A%09%7D%0A%09%0A%09public void removePlayer(Player player)%0A%09%7B%0A%09%09uuids.remove(player.getUniqueId().toString()
 );%0A%09

Add method for programmatically adding players and remove exception throwing in getPlayer.