package com.crossge.necessities;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.HashMap;
import java.util.UUID;

public class GetUUID {
    private static HashMap<String, UUID> uuids = new HashMap<>();
    private File configFileUUIDs = new File("plugins/Necessities/RankManager", "users.yml");

    public UUID getID(String name) {
        UUID partial = null;
        boolean startsWith = false;
        for (Player p : Bukkit.getServer().getOnlinePlayers()) {
            if (p.getName().equalsIgnoreCase(name))
                return p.getUniqueId();
            if (!startsWith && p.getName().toLowerCase().startsWith(name.toLowerCase())) {
                partial = p.getUniqueId();
                startsWith = true;
            }
            if (partial == null && (p.getName().toLowerCase().contains(name.toLowerCase()) || ChatColor.stripColor(p.getDisplayName()).toLowerCase().contains(name.toLowerCase())))
                partial = p.getUniqueId();
        }
        return partial;
    }

    public void addUUID(UUID uuid) {
        uuids.put(Bukkit.getPlayer(uuid).getName().toLowerCase(), uuid);
    }

    public UUID getOfflineID(String name) {
        if (uuids.containsKey(name.toLowerCase()))
            return uuids.get(name.toLowerCase());
        return null;
    }

    public void initiate() {
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Retrieving all stored UUIDs.");
        YamlConfiguration configUUIDs = YamlConfiguration.loadConfiguration(configFileUUIDs);
        for (String key : configUUIDs.getKeys(false))
            if (nameFromString(key) != null)
                uuids.put(nameFromString(key).toLowerCase(), UUID.fromString(key));
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "All stored UUIDs retrieved.");
    }

    public boolean hasJoined(UUID uuid) {
        return uuids.containsValue(uuid);
    }

    public String nameFromString(String message) {
        UUID uuid = UUID.fromString(message);
        if (Bukkit.getPlayer(uuid) == null) {
            if (Bukkit.getOfflinePlayer(uuid) == null)
                return null; //What did you do this time
            return Bukkit.getOfflinePlayer(uuid).getName();
        }
        return Bukkit.getPlayer(uuid).getName();
    }
}
@@ -193,16 +193,44 @@
 o.File;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1669,16 +1669,75 @@
 UUIDs);%0A
+        ArrayList%3CString%3E invalidKeys = new ArrayList%3C%3E();%0A
         
@@ -1822,24 +1822,26 @@
 ey) != null)
+ %7B
 %0A           
@@ -1849,75 +1849,566 @@
     
-uuids.put(nameFromString(key).toLowerCase(), UUID.fromString(key));
+UUID uuid = null;%0A                try %7B%0A                    uuid = UUID.fromString(key);%0A                %7D catch (Exception e) %7B%0A                    invalidKeys.add(key);%0A                %7D%0A                if (uuid != null)%0A                    uuids.put(nameFromString(key).toLowerCase(), UUID.fromString(key));%0A            %7D%0A        for (String key : invalidKeys)%0A            configUUIDs.set(key, null);%0A        if (!invalidKeys.isEmpty())%0A            try %7B%0A                configUUIDs.save(configFileUUIDs);%0A            %7D catch (Exception e) %7B%0A            %7D
 %0A   

Fix if somehow an invalid key gets into file