2015-02-20 07:28:21 +01:00
|
|
|
package com.intellectualcrafters.plot.util.bukkit;
|
2014-11-05 04:42:08 +01:00
|
|
|
|
2015-07-05 17:44:10 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
2014-10-11 10:05:50 +02:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2015-06-24 16:32:27 +02:00
|
|
|
import com.google.common.io.Files;
|
|
|
|
import com.google.common.io.InputSupplier;
|
2015-07-03 14:15:20 +02:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
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;
|
2015-01-29 07:02:13 +01:00
|
|
|
import com.intellectualcrafters.plot.database.DBFunc;
|
2015-07-05 17:44:10 +02:00
|
|
|
import com.intellectualcrafters.plot.object.BukkitOfflinePlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.StringWrapper;
|
2015-05-29 10:41:33 +02:00
|
|
|
import com.intellectualcrafters.plot.util.ExpireManager;
|
2015-06-24 16:32:27 +02:00
|
|
|
import com.intellectualcrafters.plot.util.NbtFactory;
|
|
|
|
import com.intellectualcrafters.plot.util.NbtFactory.NbtCompound;
|
|
|
|
import com.intellectualcrafters.plot.util.NbtFactory.StreamOptions;
|
2015-07-03 11:30:26 +02:00
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
2015-01-20 06:36:29 +01:00
|
|
|
import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper;
|
2014-12-30 07:50:55 +01:00
|
|
|
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-07-03 11:30:26 +02:00
|
|
|
public class UUIDHandler {
|
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>());
|
2015-07-03 11:30:26 +02:00
|
|
|
public static boolean CACHED = false;
|
|
|
|
public static UUIDWrapper uuidWrapper = null;
|
|
|
|
public static HashMap<String, PlotPlayer> players = new HashMap<>();
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public static void add(final StringWrapper name, final UUID uuid) {
|
2015-02-20 07:34:19 +01:00
|
|
|
if ((uuid == null) || (name == null)) {
|
2014-12-30 07:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-26 12:13:17 +01:00
|
|
|
BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
|
|
|
|
if (inverse.containsKey(uuid)) {
|
|
|
|
if (uuidMap.containsKey(name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inverse.remove(uuid);
|
|
|
|
}
|
2015-02-25 08:22:42 +01:00
|
|
|
uuidMap.put(name, uuid);
|
2014-12-30 07:50:55 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
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;
|
|
|
|
}
|
2015-02-23 02:32:27 +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);
|
|
|
|
}
|
2015-02-23 02:32:27 +01: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);
|
|
|
|
}
|
2015-05-02 12:08:30 +02:00
|
|
|
|
|
|
|
public static HashSet<UUID> getAllUUIDS() {
|
|
|
|
HashSet<UUID> uuids = new HashSet<UUID>();
|
2015-07-03 14:15:20 +02:00
|
|
|
for (Plot plot : PS.get().getPlotsRaw()) {
|
2015-05-14 14:55:57 +02:00
|
|
|
for (UUID uuid : plot.trusted) {
|
2015-05-02 12:08:30 +02:00
|
|
|
uuids.add(uuid);
|
|
|
|
}
|
2015-05-14 14:55:57 +02:00
|
|
|
for (UUID uuid : plot.members) {
|
2015-05-02 12:08:30 +02:00
|
|
|
uuids.add(uuid);
|
|
|
|
}
|
|
|
|
for (UUID uuid : plot.denied) {
|
|
|
|
uuids.add(uuid);
|
|
|
|
}
|
|
|
|
if (plot.owner != null) {
|
|
|
|
uuids.add(plot.owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uuids;
|
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
|
2015-02-23 02:32:27 +01:00
|
|
|
public static void cacheAll(final String world) {
|
2015-02-19 09:51:10 +01:00
|
|
|
if (CACHED) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
final File container = Bukkit.getWorldContainer();
|
2014-12-30 07:50:55 +01:00
|
|
|
UUIDHandler.CACHED = true;
|
2015-06-24 16:32:27 +02:00
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-07-03 14:15:20 +02:00
|
|
|
PS.log(C.PREFIX.s() + "&6Starting player data caching for: " + world);
|
2015-06-24 16:32:27 +02:00
|
|
|
final HashMap<StringWrapper, UUID> toAdd = new HashMap<>();
|
|
|
|
toAdd.put(new StringWrapper("*"), DBFunc.everyone);
|
|
|
|
if (Settings.TWIN_MODE_UUID) {
|
|
|
|
HashSet<UUID> all = getAllUUIDS();
|
2015-07-06 11:38:37 +02:00
|
|
|
PS.log("&aFast mode UUID caching enabled!");
|
2015-06-24 16:32:27 +02:00
|
|
|
final File playerdataFolder = new File(container, world + File.separator + "playerdata");
|
|
|
|
String[] dat = playerdataFolder.list(new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(final File f, final String s) {
|
|
|
|
return s.endsWith(".dat");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
boolean check = all.size() == 0;
|
|
|
|
if (dat != null) {
|
|
|
|
for (final String current : dat) {
|
|
|
|
final String s = current.replaceAll(".dat$", "");
|
|
|
|
try {
|
|
|
|
UUID uuid = UUID.fromString(s);
|
|
|
|
if (check || all.contains(uuid)) {
|
|
|
|
File file = new File(playerdataFolder + File.separator + current);
|
|
|
|
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
|
|
|
|
NbtCompound compound = NbtFactory.fromStream(is, StreamOptions.GZIP_COMPRESSION);
|
|
|
|
NbtCompound bukkit = (NbtCompound) compound.get("bukkit");
|
|
|
|
String name = (String) bukkit.get("lastKnownName");
|
|
|
|
long last = (long) bukkit.get("lastPlayed");
|
|
|
|
ExpireManager.dates.put(uuid, last);
|
|
|
|
toAdd.put(new StringWrapper(name), uuid);
|
|
|
|
}
|
|
|
|
} catch (final Exception e) {
|
|
|
|
e.printStackTrace();
|
2015-07-03 14:15:20 +02:00
|
|
|
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
|
2015-06-24 16:32:27 +02:00
|
|
|
}
|
2015-05-02 12:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
cache(toAdd);
|
|
|
|
return;
|
2015-05-02 12:08:30 +02:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
final HashSet<String> worlds = new HashSet<>();
|
|
|
|
worlds.add(world);
|
|
|
|
worlds.add("world");
|
|
|
|
final HashSet<UUID> uuids = new HashSet<>();
|
|
|
|
final HashSet<String> names = new HashSet<>();
|
|
|
|
File playerdataFolder = null;
|
|
|
|
for (final String worldname : worlds) {
|
|
|
|
// Getting UUIDs
|
|
|
|
playerdataFolder = new File(container, worldname + File.separator + "playerdata");
|
|
|
|
String[] dat = playerdataFolder.list(new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(final File f, final String s) {
|
|
|
|
return s.endsWith(".dat");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (dat != null && dat.length != 0) {
|
|
|
|
for (final String current : dat) {
|
|
|
|
final String s = current.replaceAll(".dat$", "");
|
|
|
|
try {
|
|
|
|
final UUID uuid = UUID.fromString(s);
|
|
|
|
uuids.add(uuid);
|
|
|
|
} catch (final Exception e) {
|
2015-07-03 14:15:20 +02:00
|
|
|
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
|
2015-06-24 16:32:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Getting names
|
|
|
|
final File playersFolder = new File(worldname + File.separator + "players");
|
|
|
|
dat = playersFolder.list(new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(final File f, final String s) {
|
|
|
|
return s.endsWith(".dat");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (dat != null && dat.length != 0) {
|
|
|
|
for (final String current : dat) {
|
|
|
|
names.add(current.replaceAll(".dat$", ""));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-10-27 22:00:24 +01:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
for (UUID uuid : uuids) {
|
2015-01-01 14:22:59 +01:00
|
|
|
try {
|
2015-06-24 16:32:27 +02:00
|
|
|
File file = new File(playerdataFolder + File.separator + uuid.toString() + ".dat");
|
|
|
|
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
|
|
|
|
NbtCompound compound = NbtFactory.fromStream(is, StreamOptions.GZIP_COMPRESSION);
|
|
|
|
NbtCompound bukkit = (NbtCompound) compound.get("bukkit");
|
|
|
|
String name = (String) bukkit.get("lastKnownName");
|
|
|
|
long last = (long) bukkit.get("lastPlayed");
|
|
|
|
if (Settings.OFFLINE_MODE) {
|
|
|
|
if (!Settings.UUID_LOWERCASE || !name.toLowerCase().equals(name)) {
|
|
|
|
long most = (long) compound.get("UUIDMost");
|
|
|
|
long least = (long) compound.get("UUIDLeast");
|
|
|
|
uuid = new UUID(most, least);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExpireManager.dates.put(uuid, last);
|
|
|
|
toAdd.put(new StringWrapper(name), uuid);
|
|
|
|
} catch (final Throwable e) {
|
2015-07-03 14:15:20 +02:00
|
|
|
PS.log(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
|
2015-01-01 14:22:59 +01:00
|
|
|
}
|
2014-10-27 20:05:01 +01:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
for (final String name : names) {
|
|
|
|
final UUID uuid = uuidWrapper.getUUID(name);
|
|
|
|
final StringWrapper nameWrap = new StringWrapper(name);
|
|
|
|
toAdd.put(nameWrap, uuid);
|
2014-10-27 22:00:24 +01:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
|
|
|
|
if (uuidMap.size() == 0) {
|
|
|
|
for (OfflinePlotPlayer op : uuidWrapper.getOfflinePlayers()) {
|
|
|
|
if (op.getLastPlayed() != 0) {
|
|
|
|
String name = op.getName();
|
|
|
|
StringWrapper wrap = new StringWrapper(name);
|
|
|
|
UUID uuid = uuidWrapper.getUUID(op);
|
|
|
|
toAdd.put(wrap, uuid);
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 14:22:59 +01:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
cache(toAdd);
|
2014-10-27 22:00:24 +01:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void cache(final HashMap<StringWrapper, UUID> toAdd) {
|
|
|
|
TaskManager.runTask(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
|
|
|
|
add(entry.getKey(), entry.getValue());
|
2015-03-11 09:47:22 +01:00
|
|
|
}
|
2015-07-03 14:15:20 +02:00
|
|
|
PS.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs");
|
2015-03-11 09:47:22 +01:00
|
|
|
}
|
2015-06-24 16:32:27 +02:00
|
|
|
});
|
2014-12-22 16:12:40 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-21 13:52:50 +01:00
|
|
|
public static UUID getUUID(final PlotPlayer player) {
|
2014-12-30 07:50:55 +01:00
|
|
|
return UUIDHandler.uuidWrapper.getUUID(player);
|
2014-12-22 16:12:40 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-21 13:52:50 +01:00
|
|
|
public static UUID getUUID(final BukkitOfflinePlayer player) {
|
2014-12-30 07:50:55 +01:00
|
|
|
return UUIDHandler.uuidWrapper.getUUID(player);
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 07:34:19 +01:00
|
|
|
public static String getName(final 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
|
2015-02-21 13:52:50 +01:00
|
|
|
final PlotPlayer player = UUIDHandler.getPlayer(uuid);
|
2015-02-10 06:29:23 +01:00
|
|
|
if (player != null) {
|
|
|
|
return player.getName();
|
2014-12-13 12:59:43 +01:00
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
// check cache
|
2015-02-20 07:34:19 +01:00
|
|
|
final StringWrapper name = UUIDHandler.uuidMap.inverse().get(uuid);
|
2014-12-30 07:50:55 +01:00
|
|
|
if (name != null) {
|
|
|
|
return name.value;
|
|
|
|
}
|
|
|
|
return null;
|
2014-12-13 12:59:43 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
|
|
|
public static PlotPlayer getPlayer(final UUID uuid) {
|
|
|
|
for (final PlotPlayer player : players.values()) {
|
2015-02-21 13:52:50 +01:00
|
|
|
if (player.getUUID().equals(uuid)) {
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
|
|
|
public static PlotPlayer getPlayer(final String name) {
|
2015-02-21 13:52:50 +01:00
|
|
|
return players.get(name);
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-12-30 07:50:55 +01:00
|
|
|
public static UUID getUUID(final String name) {
|
2015-02-20 07:34:19 +01:00
|
|
|
if ((name == null) || (name.length() == 0)) {
|
2015-01-01 14:22:59 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-12-30 07:50:55 +01:00
|
|
|
// check online
|
2015-02-21 13:52:50 +01:00
|
|
|
final PlotPlayer player = getPlayer(name);
|
2014-12-30 07:50:55 +01:00
|
|
|
if (player != null) {
|
2015-02-21 13:52:50 +01:00
|
|
|
return player.getUUID();
|
2014-12-30 07:50:55 +01:00
|
|
|
}
|
|
|
|
// check cache
|
2015-02-20 07:34:19 +01:00
|
|
|
final StringWrapper wrap = new StringWrapper(name);
|
2015-01-09 16:02:02 +01:00
|
|
|
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
|
|
|
}
|
2015-01-20 06:36:29 +01:00
|
|
|
// Read from disk OR convert directly to offline UUID
|
2015-02-20 07:34:19 +01:00
|
|
|
if (Settings.UUID_FROM_DISK || (uuidWrapper instanceof OfflineUUIDWrapper)) {
|
2015-01-29 23:16:31 +01:00
|
|
|
uuid = UUIDHandler.uuidWrapper.getUUID(name);
|
2015-01-09 16:02:02 +01:00
|
|
|
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
|
|
|
}
|