2014-11-16 10:48:18 +01:00
|
|
|
package com.intellectualcrafters.plot.util;
|
2014-11-05 04:42:08 +01:00
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2014-10-11 10:05:50 +02: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;
|
2014-12-30 07:50:55 +01:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
|
|
|
import com.intellectualcrafters.plot.object.StringWrapper;
|
2014-12-30 07:50:55 +01:00
|
|
|
import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper;
|
|
|
|
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
|
2014-12-16 06:03:20 +01:00
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public class UUIDHandler {
|
|
|
|
|
|
|
|
public static boolean CACHED = false;
|
2014-12-18 03:15:11 +01:00
|
|
|
public static UUIDWrapper uuidWrapper = null;
|
2014-12-30 07:50:55 +01:00
|
|
|
|
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-12-18 03:15:11 +01:00
|
|
|
private final static BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
|
2014-10-11 09:33:10 +02:00
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public static void add(final StringWrapper name, final UUID uuid) {
|
|
|
|
if (uuid == null || name == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!uuidMap.containsKey(name) && !uuidMap.inverse().containsKey(uuid)) {
|
|
|
|
uuidMap.put(name, uuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 01:57:14 +01:00
|
|
|
/**
|
|
|
|
* Get the map containing all names/uuids
|
2014-11-05 04:42:08 +01:00
|
|
|
*
|
2014-10-28 01:57:14 +01:00
|
|
|
* @return map with names + uuids
|
2014-12-18 03:15:11 +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-10-21 11:58:52 +02:00
|
|
|
public static BiMap<StringWrapper, UUID> getUuidMap() {
|
2014-10-12 01:18:47 +02:00
|
|
|
return uuidMap;
|
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
2014-10-28 01:57:14 +01:00
|
|
|
/**
|
|
|
|
* Check if a uuid is cached
|
2014-11-05 04:42:08 +01:00
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param uuid to check
|
|
|
|
*
|
2014-10-28 01:57:14 +01:00
|
|
|
* @return true of the uuid is cached
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
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 04:42:08 +01:00
|
|
|
public static boolean uuidExists(final UUID uuid) {
|
|
|
|
return uuidMap.containsValue(uuid);
|
|
|
|
}
|
2014-10-11 09:33:10 +02:00
|
|
|
|
2014-10-28 01:57:14 +01:00
|
|
|
/**
|
|
|
|
* Check if a name is cached
|
2014-11-05 04:42:08 +01:00
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param name to check
|
|
|
|
*
|
2014-10-28 01:57:14 +01:00
|
|
|
* @return true of the name is cached
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
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 04:42:08 +01:00
|
|
|
public static boolean nameExists(final StringWrapper name) {
|
|
|
|
return uuidMap.containsKey(name);
|
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
|
|
|
public static void cacheAll() {
|
|
|
|
UUIDHandler.CACHED = true;
|
|
|
|
HashSet<String> worlds = new HashSet<>();
|
|
|
|
worlds.add(Bukkit.getWorlds().get(0).getName());
|
|
|
|
worlds.add("world");
|
|
|
|
|
|
|
|
HashSet<UUID> uuids = new HashSet<>();
|
|
|
|
HashSet<String> names = new HashSet<>();
|
|
|
|
|
|
|
|
for (String worldname : worlds) {
|
|
|
|
// Getting UUIDs
|
|
|
|
File playerdataFolder = new File(worldname + File.separator + "playerdata");
|
|
|
|
String[] dat = playerdataFolder.list(new FilenameFilter() {
|
|
|
|
public boolean accept(File f, String s) {
|
|
|
|
return s.endsWith(".dat");
|
2014-10-27 22:00:24 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
});
|
2015-01-01 14:22:59 +01:00
|
|
|
if (dat != null) {
|
|
|
|
for (String current : dat) {
|
|
|
|
String s = current.replaceAll(".dat$", "");
|
|
|
|
try {
|
|
|
|
UUID uuid = UUID.fromString(s);
|
|
|
|
uuids.add(uuid);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
PlotMain.sendConsoleSenderMessage(C.PREFIX.s() + "Invalid playerdata: "+current);
|
|
|
|
}
|
2014-10-27 20:05:01 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
|
|
|
// Getting names
|
|
|
|
File playersFolder = new File(worldname + File.separator + "players");
|
|
|
|
dat = playersFolder.list(new FilenameFilter() {
|
|
|
|
public boolean accept(File f, String s) {
|
|
|
|
return s.endsWith(".dat");
|
2014-10-27 22:00:24 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
});
|
2015-01-01 14:22:59 +01:00
|
|
|
if (dat != null) {
|
|
|
|
for (String current : dat) {
|
|
|
|
names.add(current.replaceAll(".dat$", ""));
|
|
|
|
}
|
2014-10-27 22:00:24 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-01 14:22:59 +01:00
|
|
|
UUIDWrapper wrapper = new DefaultUUIDWrapper();
|
2014-12-30 07:50:55 +01:00
|
|
|
for (UUID uuid : uuids) {
|
2014-10-27 20:05:01 +01:00
|
|
|
try {
|
2014-12-30 07:50:55 +01:00
|
|
|
OfflinePlayer player = wrapper.getOfflinePlayer(uuid);
|
|
|
|
uuid = UUIDHandler.uuidWrapper.getUUID(player);
|
|
|
|
StringWrapper name = new StringWrapper(player.getName());
|
|
|
|
add(name, uuid);
|
|
|
|
}
|
|
|
|
catch (Throwable e) {
|
|
|
|
PlotMain.sendConsoleSenderMessage(C.PREFIX.s() + "&6Invalid playerdata: "+uuid.toString() + ".dat");
|
2014-10-27 20:05:01 +01:00
|
|
|
}
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
for (String name : names) {
|
|
|
|
OfflinePlayer player = Bukkit.getOfflinePlayer(name);
|
|
|
|
UUID uuid = UUIDHandler.uuidWrapper.getUUID(player);
|
|
|
|
StringWrapper nameWrap = new StringWrapper(name);
|
|
|
|
add(nameWrap, uuid);
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
2014-12-30 07:52:34 +01:00
|
|
|
PlotMain.sendConsoleSenderMessage(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs");
|
2014-12-22 16:12:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public static UUID getUUID(Player player) {
|
|
|
|
return UUIDHandler.uuidWrapper.getUUID(player);
|
2014-12-22 16:12:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public static UUID getUUID(OfflinePlayer player) {
|
|
|
|
return UUIDHandler.uuidWrapper.getUUID(player);
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
|
|
|
public static String getName(UUID uuid) {
|
2015-01-01 14:22:59 +01:00
|
|
|
if (uuid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
// check online
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
UUID u2 = UUIDHandler.uuidWrapper.getUUID(player);
|
|
|
|
if (uuid.equals(u2)) {
|
|
|
|
return player.getName();
|
2014-12-13 12:59:43 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
|
|
|
// check cache
|
|
|
|
StringWrapper name = UUIDHandler.uuidMap.inverse().get(uuid);
|
|
|
|
if (name != null) {
|
|
|
|
return name.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check drive
|
|
|
|
if (Settings.UUID_FROM_DISK) {
|
|
|
|
OfflinePlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid);
|
|
|
|
String string = op.getName();
|
|
|
|
StringWrapper sw = new StringWrapper(string);
|
|
|
|
add(sw, uuid);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2014-12-13 12:59:43 +01:00
|
|
|
}
|
2014-12-16 06:03:20 +01:00
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public static UUID getUUID(final String name) {
|
2015-01-01 14:22:59 +01:00
|
|
|
if (name == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
// check online
|
|
|
|
Player player = Bukkit.getPlayer(name);
|
|
|
|
if (player != null) {
|
|
|
|
UUID uuid = UUIDHandler.uuidWrapper.getUUID(player);
|
|
|
|
add(new StringWrapper(name), uuid);
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check cache
|
2015-01-09 16:02:02 +01:00
|
|
|
StringWrapper wrap = new StringWrapper(name);
|
|
|
|
UUID uuid = UUIDHandler.uuidMap.get(wrap);
|
2014-12-30 07:50:55 +01:00
|
|
|
if (uuid != null) {
|
|
|
|
return uuid;
|
2014-12-13 12:59:43 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
|
|
|
// Read from disk
|
|
|
|
if (Settings.UUID_FROM_DISK) {
|
|
|
|
OfflinePlayer op = Bukkit.getOfflinePlayer(name);
|
2015-01-09 16:02:02 +01:00
|
|
|
uuid = UUIDHandler.uuidWrapper.getUUID(op);
|
|
|
|
add(new StringWrapper(name), uuid);
|
|
|
|
return uuid;
|
2014-12-15 02:04:13 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
|
|
|
|
return null;
|
2014-12-13 12:59:43 +01:00
|
|
|
}
|
2014-10-06 14:51:28 +02:00
|
|
|
}
|