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

331 lines
11 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-05 14:42:08 +11:00
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.util;
2014-11-05 14:42:08 +11: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-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.uuid.NameFetcher;
import com.intellectualcrafters.plot.uuid.UUIDFetcher;
import com.intellectualcrafters.plot.uuid.UUIDSaver;
2014-11-08 20:27:09 +01:00
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
2014-10-06 14:51:28 +02:00
2014-10-12 01:18:47 +02:00
/**
2014-11-05 14:42:08 +11:00
* This class can be used to efficiently translate UUIDs and names back and
* forth.
2014-10-12 01:18:47 +02:00
* 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.
2014-11-08 20:27:09 +01:00
* <p/>
2014-11-05 14:42:08 +11:00
* 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.
2014-10-12 01:18:47 +02:00
* Call startFetch(JavaPlugin plugin) in your onEnable().
2014-11-08 20:27:09 +01:00
* <p/>
2014-10-12 01:18:47 +02:00
* Originally created by:
2014-11-05 14:42:08 +11:00
*
2014-10-12 01:18:47 +02:00
* @author Citymonstret
* @author Empire92
2014-11-05 14:42:08 +11:00
* for PlotSquared.
2014-10-12 01:18:47 +02:00
*/
2014-11-08 21:02:52 +01:00
@SuppressWarnings("unused")
2014-10-06 14:51:28 +02:00
public class UUIDHandler {
2014-12-13 22:59:43 +11:00
public static UUIDWrapper uuidWrapper = null;
2014-10-06 14:51:28 +02:00
2014-10-28 01:57:14 +01:00
/**
* Online mode
2014-11-05 14:42:08 +11:00
*
2014-10-28 01:57:14 +01:00
* @see org.bukkit.Server#getOnlineMode()
*/
2014-12-14 01:32:42 +11:00
private final static boolean online = Bukkit.getServer().getOnlineMode() && !Settings.OFFLINE_MODE;
2014-10-28 01:57:14 +01:00
/**
2014-11-20 00:00:38 +01:00
* Map containing names and UUIDs
2014-11-21 23:45:46 +01:00
*
2014-11-20 00:00:38 +01:00
* @see com.google.common.collect.BiMap
2014-10-28 01:57:14 +01:00
*/
2014-11-21 23:45:46 +01:00
private final 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
2014-11-05 14:42:08 +11:00
*
2014-10-28 01:57:14 +01:00
* @return map with names + uuids
2014-11-20 00:00:38 +01:00
* @see com.google.common.collect.BiMap
2014-10-28 01:57:14 +01: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-28 01:57:14 +01:00
/**
* Check if a uuid is cached
2014-11-05 14:42:08 +11:00
*
2014-11-08 20:27:09 +01:00
* @param uuid to check
2014-10-28 01:57:14 +01:00
* @return true of the uuid is cached
2014-11-20 00:00:38 +01:00
* @see com.google.common.collect.BiMap#containsValue(Object)
2014-10-28 01:57:14 +01:00
*/
2014-11-05 14:42:08 +11:00
public static boolean uuidExists(final UUID uuid) {
return uuidMap.containsValue(uuid);
}
2014-10-28 01:57:14 +01:00
/**
* Check if a name is cached
2014-11-05 14:42:08 +11:00
*
2014-11-08 20:27:09 +01:00
* @param name to check
2014-10-28 01:57:14 +01:00
* @return true of the name is cached
2014-11-20 00:00:38 +01:00
* @see com.google.common.collect.BiMap#containsKey(Object)
2014-10-28 01:57:14 +01:00
*/
2014-11-05 14:42:08 +11:00
public static boolean nameExists(final StringWrapper name) {
return uuidMap.containsKey(name);
}
2014-10-28 01:57:14 +01:00
/**
* Add a set to the cache
2014-11-05 14:42:08 +11:00
*
2014-11-08 20:27:09 +01:00
* @param name to cache
* @param uuid to cache
2014-10-28 01:57:14 +01:00
*/
2014-11-05 14:42:08 +11:00
public static void add(final StringWrapper name, final UUID uuid) {
if (!uuidMap.containsKey(name) && !uuidMap.inverse().containsKey(uuid)) {
uuidMap.put(name, uuid);
}
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param name to use as key
2014-11-05 14:42:08 +11:00
* @return uuid
*/
public static UUID getUUID(final String name) {
final StringWrapper nameWrap = new StringWrapper(name);
if (uuidMap.containsKey(nameWrap)) {
return uuidMap.get(nameWrap);
}
2014-10-28 01:57:14 +01:00
@SuppressWarnings("deprecation")
2014-11-05 14:42:08 +11:00
final Player player = Bukkit.getPlayer(name);
if (player != null) {
2014-12-13 22:59:43 +11:00
final UUID uuid = getUUID(player);
2014-11-10 21:54:36 +11:00
add(nameWrap, uuid);
2014-11-05 14:42:08 +11:00
return uuid;
}
UUID uuid;
if (online) {
if (Settings.CUSTOM_API) {
if ((uuid = getUuidOnlinePlayer(nameWrap)) != null) {
return uuid;
}
try {
return PlotMain.getUUIDSaver().mojangUUID(name);
2014-11-08 20:27:09 +01:00
} catch (final Exception e) {
try {
2014-11-05 14:42:08 +11:00
final UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
uuid = fetcher.call().get(name);
add(nameWrap, uuid);
2014-11-08 20:27:09 +01:00
} catch (final Exception ex) {
ex.printStackTrace();
}
}
2014-11-08 20:27:09 +01:00
} else {
try {
2014-11-05 14:42:08 +11:00
final UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
uuid = fetcher.call().get(name);
add(nameWrap, uuid);
2014-11-08 20:27:09 +01:00
} catch (final Exception ex) {
ex.printStackTrace();
}
}
2014-11-08 20:27:09 +01:00
} else {
2014-11-05 14:42:08 +11:00
return getUuidOfflineMode(nameWrap);
}
return null;
2014-11-05 14:42:08 +11:00
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param uuid to use as key
2014-11-05 14:42:08 +11:00
* @return name (cache)
*/
private static StringWrapper loopSearch(final UUID uuid) {
return uuidMap.inverse().get(uuid);
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param uuid to use as key
2014-11-05 14:42:08 +11:00
* @return Name
*/
public static String getName(final UUID uuid) {
if (uuidExists(uuid)) {
return loopSearch(uuid).value;
}
String name;
if ((name = getNameOnlinePlayer(uuid)) != null) {
return name;
}
if ((name = getNameOfflinePlayer(uuid)) != null) {
return name;
}
if (online && !Settings.OFFLINE_MODE) {
2014-11-05 14:42:08 +11:00
if (!Settings.CUSTOM_API) {
try {
2014-11-05 14:42:08 +11:00
final NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
name = fetcher.call().get(uuid);
add(new StringWrapper(name), uuid);
return name;
2014-11-08 20:27:09 +01:00
} catch (final Exception ex) {
ex.printStackTrace();
}
2014-11-08 20:27:09 +01:00
} else {
try {
return PlotMain.getUUIDSaver().mojangName(uuid);
2014-11-08 20:27:09 +01:00
} catch (final Exception e) {
try {
2014-11-05 14:42:08 +11:00
final NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
name = fetcher.call().get(uuid);
add(new StringWrapper(name), uuid);
return name;
2014-11-08 20:27:09 +01:00
} catch (final Exception ex) {
e.printStackTrace();
}
}
}
try {
return PlotMain.getUUIDSaver().mojangName(uuid);
2014-11-08 20:27:09 +01:00
} catch (final Exception e) {
try {
2014-11-05 14:42:08 +11:00
final NameFetcher fetcher = new NameFetcher(Arrays.asList(uuid));
name = fetcher.call().get(uuid);
add(new StringWrapper(name), uuid);
return name;
2014-11-08 20:27:09 +01:00
} catch (final Exception ex) {
ex.printStackTrace();
}
}
2014-11-08 20:27:09 +01:00
} else {
2014-11-05 14:42:08 +11:00
return "unknown";
}
return "";
2014-11-05 14:42:08 +11:00
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param name to use as key
2014-11-05 14:42:08 +11:00
* @return UUID (name hash)
*/
private static UUID getUuidOfflineMode(final StringWrapper name) {
final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
add(name, uuid);
return uuid;
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param uuid to use as key
2014-11-05 14:42:08 +11:00
* @return String - name
*/
private static String getNameOnlinePlayer(final UUID uuid) {
final Player player = uuidWrapper.getPlayer(uuid);
2014-11-05 14:42:08 +11:00
if ((player == null) || !player.isOnline()) {
return null;
}
final String name = player.getName();
add(new StringWrapper(name), uuid);
return name;
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param uuid to use as key
2014-11-05 14:42:08 +11:00
* @return String - name
*/
private static String getNameOfflinePlayer(final UUID uuid) {
final OfflinePlayer player = uuidWrapper.getOfflinePlayer(uuid);
2014-11-05 14:42:08 +11:00
if ((player == null) || !player.hasPlayedBefore()) {
return null;
}
final String name = player.getName();
add(new StringWrapper(name), uuid);
return name;
}
2014-11-05 14:42:08 +11:00
/**
2014-11-08 20:27:09 +01:00
* @param name to use as key
2014-11-05 14:42:08 +11:00
* @return UUID
*/
private static UUID getUuidOnlinePlayer(final StringWrapper name) {
2014-10-28 01:57:14 +01:00
@SuppressWarnings("deprecation")
2014-11-05 14:42:08 +11:00
final Player player = Bukkit.getPlayer(name.value);
if (player == null) {
return null;
}
2014-12-13 22:59:43 +11:00
final UUID uuid = getUUID(player);
2014-11-05 14:42:08 +11:00
add(name, uuid);
return uuid;
}
2014-10-28 01:57:14 +01:00
/**
* Handle saving of uuids
2014-11-05 14:42:08 +11:00
*
2014-10-28 01:57:14 +01:00
* @see com.intellectualcrafters.plot.uuid.UUIDSaver#globalSave(com.google.common.collect.BiMap)
*/
@SuppressWarnings("unused")
public static void handleSaving() {
2014-11-05 14:42:08 +11:00
final UUIDSaver saver = PlotMain.getUUIDSaver();
saver.globalSave(getUuidMap());
}
2014-12-13 22:59:43 +11:00
public static UUID getUUID(Player player) {
if (uuidWrapper == null) {
try {
getUUID(player);
uuidWrapper = new DefaultUUIDWrapper();
}
catch (Throwable e) {
uuidWrapper = new OfflineUUIDWrapper();
}
}
return uuidWrapper.getUUID(player);
}
public static UUID getUUID(OfflinePlayer player) {
if (uuidWrapper == null) {
try {
getUUID(player);
uuidWrapper = new DefaultUUIDWrapper();
}
catch (Throwable e) {
uuidWrapper = new OfflineUUIDWrapper();
}
}
return uuidWrapper.getUUID(player);
}
2014-10-06 14:51:28 +02:00
}