Add method to get a UUID from a playername and store it for future reference

This commit is contained in:
libraryaddict 2014-04-09 04:14:18 +12:00
parent 1b2c2c4690
commit dab0c5d3c5

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; 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.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -39,6 +41,7 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class DisguiseUtilities { public class DisguiseUtilities {
private static HashMap<Integer, HashSet<TargetedDisguise>> futureDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>(); private static HashMap<Integer, HashSet<TargetedDisguise>> futureDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>();
private static LibsDisguises libsDisguises; private static LibsDisguises libsDisguises;
private static HashMap<String, String> namesUuids = new HashMap<String, String>();
// A internal storage of fake entity ID's I can use. // 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 // Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID
private static HashMap<UUID, Integer> selfDisguisesIds = new HashMap<UUID, Integer>(); private static HashMap<UUID, Integer> selfDisguisesIds = new HashMap<UUID, Integer>();
@ -295,6 +298,57 @@ public class DisguiseUtilities {
return selfDisguisesIds; 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<String, UUID> 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) { public static void init(LibsDisguises disguises) {
libsDisguises = disguises; libsDisguises = disguises;
} }