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

203 lines
4.8 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-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 {
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-21 20:58:52 +11:00
public static BiMap<StringWrapper, UUID> getUuidMap() {
2014-10-12 01:18:47 +02:00
return uuidMap;
}
public static boolean uuidExists(UUID uuid) {
return uuidMap.containsValue(uuid);
}
2014-10-21 20:58:52 +11:00
public static boolean nameExists(StringWrapper name) {
return uuidMap.containsKey(name);
}
2014-10-21 20:58:52 +11:00
public static void add(StringWrapper name, UUID uuid) {
uuidMap.put(name, uuid);
}
/**
* @param name
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-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) {
2014-10-21 20:58:52 +11:00
if ((uuid = getUuidOnlinePlayer(nameWrap)) != null) {
return uuid;
}
try {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
uuid = fetcher.call().get(name);
2014-10-21 20:58:52 +11:00
add(nameWrap, uuid);
}
catch (Exception e) {
e.printStackTrace();
}
}
else {
2014-10-21 20:58:52 +11:00
return getUuidOfflineMode(nameWrap);
}
return null;
}
/**
* @param uuid
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);
}
/**
* @param uuid
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) {
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);
return name;
}
catch (Exception e) {
e.printStackTrace();
}
}
else {
return "unknown";
}
return "";
}
/**
* @param name
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;
}
/**
* @param uuid
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;
}
/**
* @param uuid
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;
}
/**
* @param name
2014-10-12 01:18:47 +02:00
* @return UUID
*/
2014-10-21 20:58:52 +11:00
private static UUID getUuidOnlinePlayer(StringWrapper name) {
Player player = Bukkit.getPlayer(name.value);
if (player == null) {
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-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;
}
/* 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
}