2014-10-06 14:51:28 +02:00
|
|
|
package com.intellectualcrafters.plot;
|
|
|
|
|
2014-10-11 00:33:10 -07:00
|
|
|
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;
|
2014-10-11 00:33:10 -07:00
|
|
|
import com.intellectualcrafters.plot.uuid.NameFetcher;
|
|
|
|
import com.intellectualcrafters.plot.uuid.UUIDFetcher;
|
2014-10-13 21:22:19 +02:00
|
|
|
import com.intellectualcrafters.plot.uuid.UUIDSaver;
|
2014-10-21 20:58:52 +11:00
|
|
|
|
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-12 00:37:36 -07:00
|
|
|
private static boolean online = Bukkit.getServer().getOnlineMode();
|
|
|
|
|
2014-10-21 20:58:52 +11:00
|
|
|
private static BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
|
2014-10-11 00:33:10 -07:00
|
|
|
|
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-11 00:33:10 -07:00
|
|
|
public static boolean uuidExists(UUID uuid) {
|
|
|
|
return uuidMap.containsValue(uuid);
|
|
|
|
}
|
|
|
|
|
2014-10-21 20:58:52 +11:00
|
|
|
public static boolean nameExists(StringWrapper name) {
|
2014-10-11 00:33:10 -07:00
|
|
|
return uuidMap.containsKey(name);
|
|
|
|
}
|
|
|
|
|
2014-10-21 20:58:52 +11:00
|
|
|
public static void add(StringWrapper name, UUID uuid) {
|
2014-10-11 00:33:10 -07:00
|
|
|
uuidMap.put(name, uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return uuid
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
|
|
|
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-11 00:33:10 -07:00
|
|
|
}
|
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-11 00:33:10 -07:00
|
|
|
}
|
2014-10-21 20:58:52 +11:00
|
|
|
UUID uuid;
|
2014-10-11 19:05:50 +11:00
|
|
|
if (online) {
|
2014-10-21 20:58:52 +11:00
|
|
|
if ((uuid = getUuidOnlinePlayer(nameWrap)) != null) {
|
|
|
|
return uuid;
|
|
|
|
}
|
2014-10-11 00:33:10 -07:00
|
|
|
try {
|
|
|
|
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
|
|
|
|
uuid = fetcher.call().get(name);
|
2014-10-21 20:58:52 +11:00
|
|
|
add(nameWrap, uuid);
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-10-11 00:33:10 -07:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-10-21 20:58:52 +11:00
|
|
|
return getUuidOfflineMode(nameWrap);
|
2014-10-11 00:33:10 -07:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param uuid
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return name (cache)
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
2014-10-21 20:58:52 +11:00
|
|
|
private static StringWrapper loopSearch(UUID uuid) {
|
2014-10-12 00:37:36 -07:00
|
|
|
return uuidMap.inverse().get(uuid);
|
2014-10-11 00:33:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param uuid
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return Name
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
|
|
|
public static String getName(UUID uuid) {
|
2014-10-12 00:37:36 -07:00
|
|
|
if (uuidExists(uuid)) {
|
2014-10-21 20:58:52 +11:00
|
|
|
return loopSearch(uuid).value;
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
2014-10-11 00:33:10 -07:00
|
|
|
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) {
|
2014-10-11 00:33:10 -07:00
|
|
|
try {
|
|
|
|
NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
|
|
|
|
name = fetcher.call().get(uuid);
|
2014-10-21 20:58:52 +11:00
|
|
|
add(new StringWrapper(name), uuid);
|
2014-10-11 00:33:10 -07:00
|
|
|
return name;
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-10-11 00:33:10 -07:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-10-11 00:33:10 -07:00
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return UUID (name hash)
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
2014-10-21 20:58:52 +11:00
|
|
|
private static UUID getUuidOfflineMode(StringWrapper name) {
|
2014-10-12 00:37:36 -07:00
|
|
|
UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
|
2014-10-11 00:33:10 -07:00
|
|
|
add(name, uuid);
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param uuid
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return String - name
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
|
|
|
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);
|
2014-10-11 00:33:10 -07:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param uuid
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return String - name
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
|
|
|
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);
|
2014-10-11 00:33:10 -07:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return UUID
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
2014-10-21 20:58:52 +11:00
|
|
|
private static UUID getUuidOnlinePlayer(StringWrapper name) {
|
|
|
|
Player player = Bukkit.getPlayer(name.value);
|
|
|
|
if (player == null) {
|
2014-10-11 00:33:10 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
|
add(name, uuid);
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name
|
2014-10-12 01:18:47 +02:00
|
|
|
* @return UUID (username hash)
|
2014-10-11 00:33:10 -07:00
|
|
|
*/
|
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));
|
2014-10-11 00:33:10 -07:00
|
|
|
add(name, uuid);
|
|
|
|
return uuid;
|
|
|
|
}
|
2014-10-13 21:22:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Save UUIDS */
|
|
|
|
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
|
|
|
}
|