From dab0c5d3c58532a3cd20f126da2afc3e298ac746 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 9 Apr 2014 04:14:18 +1200 Subject: [PATCH] Add method to get a UUID from a playername and store it for future reference --- .../disguise/utilities/DisguiseUtilities.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index c9923ca8..3e53c74a 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -4,6 +4,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -18,6 +19,7 @@ import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; +import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -39,6 +41,7 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher; public class DisguiseUtilities { private static HashMap> futureDisguises = new HashMap>(); private static LibsDisguises libsDisguises; + private static HashMap namesUuids = new HashMap(); // A internal storage of fake entity ID's I can use. // Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID private static HashMap selfDisguisesIds = new HashMap(); @@ -295,6 +298,57 @@ public class DisguiseUtilities { return selfDisguisesIds; } + public static UUID getUUID(final Disguise disguise, final String playerName) { + if (LibVersion.getGameVersion() == LibVersion.V1_7) { + Player p = Bukkit.getPlayerExact(playerName); + if (p != null) { + return p.getUniqueId(); + } else if (disguise != null) { + if (namesUuids.containsKey(playerName)) { + if (namesUuids.get(playerName) != null) { + return UUID.fromString(namesUuids.get(playerName)); + } + } else { + // Add null so that if this is called again. I already know I'm doing something about it + namesUuids.put(playerName, null); + Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() { + public void run() { + UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(playerName)); + try { + final HashMap map = fetcher.call(); + Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() { + public void run() { + if (!map.containsKey(playerName)) { + if (namesUuids.containsKey(playerName) && namesUuids.get(playerName) == null) { + namesUuids.remove(playerName); + } + } else { + for (String name : map.keySet()) { + if (namesUuids.containsKey(name) && namesUuids.get(name) == null) { + namesUuids.put(name, map.get(name).toString()); + } + } + if (DisguiseUtilities.isDisguiseInUse(disguise)) { + DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise); + } + } + } + }); + } catch (Exception e) { + if (namesUuids.containsKey(playerName) && namesUuids.get(playerName) == null) { + namesUuids.remove(playerName); + } + System.out.print("[LibsDisguises] Error when fetching " + playerName + "'s uuid from mojang: " + + e.getMessage()); + } + } + }); + } + } + } + return null; + } + public static void init(LibsDisguises disguises) { libsDisguises = disguises; }