TF-PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/UUIDHandler.java

278 lines
7.7 KiB
Java
Raw Normal View History

2014-10-06 14:51:28 +02:00
package com.intellectualcrafters.plot;
import com.google.common.base.Charsets;
2014-10-11 19:05:50 +11:00
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.intellectualcrafters.plot.uuid.NameFetcher;
import com.intellectualcrafters.plot.uuid.UUIDFetcher;
import com.intellectualcrafters.plot.uuid.UUIDSaver;
2014-10-11 18:16:08 +02:00
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.Arrays;
2014-10-21 20:58:52 +11:00
import java.util.HashMap;
2014-10-11 18:16:08 +02:00
import java.util.UUID;
2014-10-06 14:51:28 +02:00
2014-10-12 01:18:47 +02:00
/**
* This class can be used to efficiently translate UUIDs and names back and forth.
* It uses three primary methods of achieving this:
* - Read From Cache
* - Read from OfflinePlayer objects
* - Read from (if onlinemode: mojang api) (else: playername hashing)
* All UUIDs/Usernames will be stored in a map (cache) until the server is
* restarted.
*
* You can use getUuidMap() to save the uuids/names to a file (SQLite db for example).
* Primary methods: getUUID(String name) & getName(UUID uuid) <-- You should ONLY use these.
* Call startFetch(JavaPlugin plugin) in your onEnable().
*
* Originally created by:
* @author Citymonstret
* @author Empire92
* for PlotSquared.
*/
2014-10-06 14:51:28 +02:00
public class UUIDHandler {
2014-10-28 01:57:14 +01:00
/**
* Online mode
* @see org.bukkit.Server#getOnlineMode()
*/
private static boolean online = Bukkit.getServer().getOnlineMode();
2014-10-28 01:57:14 +01:00
/**
* Map containing names and UUID's
*/
2014-10-21 20:58:52 +11:00
private static BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
2014-10-28 01:57:14 +01:00
/**
* Get the map containing all names/uuids
* @return map with names + uuids
*/
2014-10-21 20:58:52 +11:00
public static BiMap<StringWrapper, UUID> getUuidMap() {
2014-10-12 01:18:47 +02:00
return uuidMap;
}
2014-10-28 01:57:14 +01:00
/**
* Check if a uuid is cached
* @param uuid to check
* @return true of the uuid is cached
*/
public static boolean uuidExists(UUID uuid) {
return uuidMap.containsValue(uuid);
}
2014-10-28 01:57:14 +01:00
/**
* Check if a name is cached
* @param name to check
* @return true of the name is cached
*/
2014-10-21 20:58:52 +11:00
public static boolean nameExists(StringWrapper name) {
return uuidMap.containsKey(name);
}
2014-10-28 01:57:14 +01:00
/**
* Add a set to the cache
* @param name to cache
* @param uuid to cache
*/
2014-10-21 20:58:52 +11:00
public static void add(StringWrapper name, UUID uuid) {
if (!uuidMap.containsKey(name) && !uuidMap.inverse().containsKey(uuid)) {
uuidMap.put(name, uuid);
}
}
/**
2014-10-28 01:57:14 +01:00
* @param name to use as key
2014-10-12 01:18:47 +02:00
* @return uuid
*/
public static UUID getUUID(String name) {
2014-10-21 20:58:52 +11:00
StringWrapper nameWrap = new StringWrapper(name);
if (uuidMap.containsKey(nameWrap)) {
return uuidMap.get(nameWrap);
}
2014-10-28 01:57:14 +01:00
@SuppressWarnings("deprecation")
2014-10-21 20:58:52 +11:00
Player player = Bukkit.getPlayer(name);
if (player!=null) {
UUID uuid = player.getUniqueId();
uuidMap.put(nameWrap, uuid);
return uuid;
}
2014-10-21 20:58:52 +11:00
UUID uuid;
2014-10-11 19:05:50 +11:00
if (online) {
if(Settings.CUSTOM_API) {
if ((uuid = getUuidOnlinePlayer(nameWrap)) != null) {
return uuid;
}
try {
return PlotMain.getUUIDSaver().mojangUUID(name);
}
catch(Exception e) {
try {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
uuid = fetcher.call().get(name);
add(nameWrap, uuid);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
} else {
try {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
uuid = fetcher.call().get(name);
add(nameWrap, uuid);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
else {
2014-10-21 20:58:52 +11:00
return getUuidOfflineMode(nameWrap);
}
return null;
}
/**
2014-10-28 01:57:14 +01:00
* @param uuid to use as key
2014-10-12 01:18:47 +02:00
* @return name (cache)
*/
2014-10-21 20:58:52 +11:00
private static StringWrapper loopSearch(UUID uuid) {
return uuidMap.inverse().get(uuid);
}
/**
2014-10-28 01:57:14 +01:00
* @param uuid to use as key
2014-10-12 01:18:47 +02:00
* @return Name
*/
public static String getName(UUID uuid) {
if (uuidExists(uuid)) {
2014-10-21 20:58:52 +11:00
return loopSearch(uuid).value;
}
String name;
if ((name = getNameOnlinePlayer(uuid)) != null) {
return name;
}
if ((name = getNameOfflinePlayer(uuid)) != null) {
return name;
}
2014-10-11 19:05:50 +11:00
if (online) {
if(!Settings.CUSTOM_API) {
try {
NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
name = fetcher.call().get(uuid);
add(new StringWrapper(name), uuid);
return name;
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
try {
return PlotMain.getUUIDSaver().mojangName(uuid);
} catch(Exception e) {
try {
NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
name = fetcher.call().get(uuid);
add(new StringWrapper(name), uuid);
return name;
} catch (Exception ex) {
e.printStackTrace();
}
}
}
try {
return PlotMain.getUUIDSaver().mojangName(uuid);
} catch(Exception e) {
try {
NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
name = fetcher.call().get(uuid);
add(new StringWrapper(name), uuid);
return name;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
else {
return "unknown";
}
return "";
}
/**
2014-10-28 01:57:14 +01:00
* @param name to use as key
2014-10-12 01:18:47 +02:00
* @return UUID (name hash)
*/
2014-10-21 20:58:52 +11:00
private static UUID getUuidOfflineMode(StringWrapper name) {
UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
add(name, uuid);
return uuid;
}
/**
2014-10-28 01:57:14 +01:00
* @param uuid to use as key
2014-10-12 01:18:47 +02:00
* @return String - name
*/
private static String getNameOnlinePlayer(UUID uuid) {
Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) {
return null;
}
String name = player.getName();
2014-10-21 20:58:52 +11:00
add(new StringWrapper(name), uuid);
return name;
}
/**
2014-10-28 01:57:14 +01:00
* @param uuid to use as key
2014-10-12 01:18:47 +02:00
* @return String - name
*/
private static String getNameOfflinePlayer(UUID uuid) {
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
if (player == null || !player.hasPlayedBefore()) {
return null;
}
String name = player.getName();
2014-10-21 20:58:52 +11:00
add(new StringWrapper(name), uuid);
return name;
}
/**
2014-10-28 01:57:14 +01:00
* @param name to use as key
2014-10-12 01:18:47 +02:00
* @return UUID
*/
2014-10-21 20:58:52 +11:00
private static UUID getUuidOnlinePlayer(StringWrapper name) {
2014-10-28 01:57:14 +01:00
@SuppressWarnings("deprecation")
2014-10-21 20:58:52 +11:00
Player player = Bukkit.getPlayer(name.value);
if (player == null) {
return null;
}
UUID uuid = player.getUniqueId();
add(name, uuid);
return uuid;
}
/**
2014-10-28 01:57:14 +01:00
* @param name to use as key
2014-10-12 01:18:47 +02:00
* @return UUID (username hash)
*/
2014-10-28 01:57:14 +01:00
@SuppressWarnings("unused")
2014-10-21 20:58:52 +11:00
private static UUID getUuidOfflinePlayer(StringWrapper name) {
UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
add(name, uuid);
return uuid;
}
2014-10-28 01:57:14 +01:00
/**
* Handle saving of uuids
* @see com.intellectualcrafters.plot.uuid.UUIDSaver#globalSave(com.google.common.collect.BiMap)
*/
@SuppressWarnings("unused")
public static void handleSaving() {
UUIDSaver saver = PlotMain.getUUIDSaver();
// Should it save per UUIDSet or all of them? TODO: Let Jesse decide xD
saver.globalSave(getUuidMap());
}
2014-10-06 14:51:28 +02:00
}