2015-07-31 03:24:01 +10:00
|
|
|
package com.plotsquared.bukkit.uuid;
|
2015-07-24 16:06:58 +02:00
|
|
|
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2016-06-02 13:42:32 -04:00
|
|
|
import com.google.common.collect.Sets;
|
2016-02-13 20:01:18 -05:00
|
|
|
import com.google.common.io.ByteSource;
|
2015-07-24 16:06:58 +02:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
|
|
|
import com.intellectualcrafters.plot.config.C;
|
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
2015-07-27 17:26:50 +10:00
|
|
|
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.RunnableVal;
|
|
|
|
import com.intellectualcrafters.plot.object.StringWrapper;
|
2016-03-20 23:19:37 +01:00
|
|
|
import com.intellectualcrafters.plot.util.StringMan;
|
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandler;
|
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandlerImplementation;
|
2016-06-05 20:11:02 +10:00
|
|
|
import com.intellectualcrafters.plot.util.expiry.ExpireManager;
|
2015-07-24 16:06:58 +02:00
|
|
|
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
|
2016-02-11 05:59:51 +11:00
|
|
|
import com.plotsquared.bukkit.util.NbtFactory;
|
2016-02-13 20:01:18 -05:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2016-06-05 20:11:02 +10:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.World;
|
2015-07-27 02:14:34 +10:00
|
|
|
|
2015-09-13 14:04:31 +10:00
|
|
|
public class FileUUIDHandler extends UUIDHandlerImplementation {
|
2016-03-20 23:19:37 +01:00
|
|
|
|
2016-03-22 21:41:37 -04:00
|
|
|
public FileUUIDHandler(UUIDWrapper wrapper) {
|
2015-07-27 17:26:50 +10:00
|
|
|
super(wrapper);
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
2016-03-20 23:19:37 +01:00
|
|
|
|
2015-07-24 16:06:58 +02:00
|
|
|
@Override
|
2016-03-22 21:41:37 -04:00
|
|
|
public boolean startCaching(Runnable whenDone) {
|
2016-02-13 20:01:18 -05:00
|
|
|
return super.startCaching(whenDone) && cache(whenDone);
|
2015-07-27 23:10:14 +10:00
|
|
|
}
|
2016-03-20 23:19:37 +01:00
|
|
|
|
2015-09-13 14:04:31 +10:00
|
|
|
public boolean cache(final Runnable whenDone) {
|
2015-07-24 16:06:58 +02:00
|
|
|
final File container = Bukkit.getWorldContainer();
|
2016-03-22 21:41:37 -04:00
|
|
|
List<World> worlds = Bukkit.getWorlds();
|
2015-07-27 17:26:50 +10:00
|
|
|
final String world;
|
2016-02-13 20:01:18 -05:00
|
|
|
if (worlds.isEmpty()) {
|
2015-07-27 17:26:50 +10:00
|
|
|
world = "world";
|
2015-09-13 14:04:31 +10:00
|
|
|
} else {
|
2015-07-27 17:26:50 +10:00
|
|
|
world = worlds.get(0).getName();
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
2015-07-24 16:06:58 +02:00
|
|
|
@Override
|
2015-09-13 14:04:31 +10:00
|
|
|
public void run() {
|
2016-03-20 23:19:37 +01:00
|
|
|
PS.debug(C.PREFIX + "&6Starting player data caching for: " + world);
|
2016-03-29 15:47:59 -04:00
|
|
|
File uuidFile = new File(PS.get().IMP.getDirectory(), "uuids.txt");
|
|
|
|
if (uuidFile.exists()) {
|
2015-09-13 14:04:31 +10:00
|
|
|
try {
|
2016-03-29 15:47:59 -04:00
|
|
|
List<String> lines = Files.readAllLines(uuidFile.toPath(), StandardCharsets.UTF_8);
|
2015-09-13 14:04:31 +10:00
|
|
|
for (String line : lines) {
|
|
|
|
try {
|
2015-08-26 14:21:48 +10:00
|
|
|
line = line.trim();
|
2016-02-13 20:01:18 -05:00
|
|
|
if (line.isEmpty()) {
|
2015-08-26 14:21:48 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
|
2016-03-22 21:41:37 -04:00
|
|
|
String[] split = line.split("\\|");
|
|
|
|
String name = split[0];
|
|
|
|
if (name.isEmpty() || (name.length() > 16) || !StringMan.isAlphanumericUnd(name)) {
|
2015-08-26 14:21:48 +10:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-22 21:41:37 -04:00
|
|
|
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
|
2015-09-13 14:04:31 +10:00
|
|
|
if (uuid == null) {
|
2015-08-26 14:21:48 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
UUIDHandler.add(new StringWrapper(name), uuid);
|
2016-03-22 21:41:37 -04:00
|
|
|
} catch (Exception e2) {
|
2015-08-26 14:21:48 +10:00
|
|
|
e2.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2016-03-22 21:41:37 -04:00
|
|
|
} catch (IOException e) {
|
2015-08-26 14:21:48 +10:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 17:28:34 +10:00
|
|
|
if (Settings.UUID.NATIVE_UUID_PROVIDER) {
|
2016-03-22 21:41:37 -04:00
|
|
|
HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
|
|
|
|
HashSet<UUID> all = UUIDHandler.getAllUUIDS();
|
2015-07-31 00:25:16 +10:00
|
|
|
PS.debug("&aFast mode UUID caching enabled!");
|
2016-03-29 15:47:59 -04:00
|
|
|
File playerDataFolder = new File(container, world + File.separator + "playerdata");
|
2016-06-02 13:42:32 -04:00
|
|
|
String[] dat = playerDataFolder.list(new DatFileFilter());
|
2016-03-22 21:41:37 -04:00
|
|
|
boolean check = all.isEmpty();
|
2015-09-13 14:04:31 +10:00
|
|
|
if (dat != null) {
|
2016-03-22 21:41:37 -04:00
|
|
|
for (String current : dat) {
|
|
|
|
String s = current.replaceAll(".dat$", "");
|
2015-09-13 14:04:31 +10:00
|
|
|
try {
|
2016-03-22 21:41:37 -04:00
|
|
|
UUID uuid = UUID.fromString(s);
|
2015-09-13 14:04:31 +10:00
|
|
|
if (check || all.remove(uuid)) {
|
2016-06-02 13:42:32 -04:00
|
|
|
File file = new File(playerDataFolder, current);
|
|
|
|
ByteSource is = com.google.common.io.Files.asByteSource(file);
|
2016-03-22 21:41:37 -04:00
|
|
|
NbtFactory.NbtCompound compound = NbtFactory.fromStream(is, NbtFactory.StreamOptions.GZIP_COMPRESSION);
|
2016-06-02 13:42:32 -04:00
|
|
|
if (!compound.containsKey("bukkit")) {
|
|
|
|
PS.debug("ERROR: Player data does not contain the the key \"bukkit\"");
|
|
|
|
} else {
|
|
|
|
NbtFactory.NbtCompound bukkit = (NbtFactory.NbtCompound) compound.get("bukkit");
|
|
|
|
String name = (String) bukkit.get("lastKnownName");
|
|
|
|
long last = (long) bukkit.get("lastPlayed");
|
|
|
|
if (ExpireManager.IMP != null) {
|
|
|
|
ExpireManager.IMP.storeDate(uuid, last);
|
|
|
|
}
|
|
|
|
toAdd.put(new StringWrapper(name), uuid);
|
2016-03-02 06:56:50 +11:00
|
|
|
}
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
2016-04-28 16:38:51 -04:00
|
|
|
} catch (IOException e) {
|
2015-07-24 16:06:58 +02:00
|
|
|
e.printStackTrace();
|
2016-03-20 23:19:37 +01:00
|
|
|
PS.debug(C.PREFIX + "Invalid playerdata: " + current);
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 17:26:50 +10:00
|
|
|
add(toAdd);
|
2016-02-13 20:01:18 -05:00
|
|
|
if (all.isEmpty()) {
|
2015-09-13 14:04:31 +10:00
|
|
|
if (whenDone != null) {
|
2015-09-11 20:09:22 +10:00
|
|
|
whenDone.run();
|
|
|
|
}
|
2015-07-31 19:46:07 +10:00
|
|
|
return;
|
2015-09-13 14:04:31 +10:00
|
|
|
} else {
|
2015-07-31 19:46:07 +10:00
|
|
|
PS.debug("Failed to cache: " + all.size() + " uuids - slowly processing all files");
|
|
|
|
}
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
2016-03-22 21:41:37 -04:00
|
|
|
HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
|
2016-06-02 13:42:32 -04:00
|
|
|
HashSet<String> worlds = Sets.newHashSet(world, "world");
|
2016-03-22 21:41:37 -04:00
|
|
|
HashSet<UUID> uuids = new HashSet<>();
|
|
|
|
HashSet<String> names = new HashSet<>();
|
2016-03-29 15:47:59 -04:00
|
|
|
File playerDataFolder = null;
|
2016-03-22 21:41:37 -04:00
|
|
|
for (String worldName : worlds) {
|
2015-07-24 16:06:58 +02:00
|
|
|
// Getting UUIDs
|
2016-03-29 15:47:59 -04:00
|
|
|
playerDataFolder = new File(container, worldName + File.separator + "playerdata");
|
2016-06-02 13:42:32 -04:00
|
|
|
String[] dat = playerDataFolder.list(new DatFileFilter());
|
2015-09-13 14:04:31 +10:00
|
|
|
if ((dat != null) && (dat.length != 0)) {
|
2016-03-22 21:41:37 -04:00
|
|
|
for (String current : dat) {
|
|
|
|
String s = current.replaceAll(".dat$", "");
|
2015-09-13 14:04:31 +10:00
|
|
|
try {
|
2016-03-22 21:41:37 -04:00
|
|
|
UUID uuid = UUID.fromString(s);
|
2015-07-24 16:06:58 +02:00
|
|
|
uuids.add(uuid);
|
2016-04-28 16:38:51 -04:00
|
|
|
} catch (Exception ignored) {
|
2016-03-29 15:47:59 -04:00
|
|
|
PS.debug(C.PREFIX + "Invalid PlayerData: " + current);
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Getting names
|
2016-03-22 21:41:37 -04:00
|
|
|
File playersFolder = new File(worldName + File.separator + "players");
|
2016-06-02 13:42:32 -04:00
|
|
|
dat = playersFolder.list(new DatFileFilter());
|
2015-09-13 14:04:31 +10:00
|
|
|
if ((dat != null) && (dat.length != 0)) {
|
2016-03-22 21:41:37 -04:00
|
|
|
for (String current : dat) {
|
2015-07-24 16:06:58 +02:00
|
|
|
names.add(current.replaceAll(".dat$", ""));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
for (UUID uuid : uuids) {
|
|
|
|
try {
|
2016-03-29 15:47:59 -04:00
|
|
|
File file = new File(playerDataFolder + File.separator + uuid.toString() + ".dat");
|
2016-02-28 07:04:57 +11:00
|
|
|
if (!file.exists()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-22 21:41:37 -04:00
|
|
|
ByteSource is = com.google.common.io.Files.asByteSource(file);
|
|
|
|
NbtFactory.NbtCompound compound = NbtFactory.fromStream(is, NbtFactory.StreamOptions.GZIP_COMPRESSION);
|
2016-06-02 13:42:32 -04:00
|
|
|
if (!compound.containsKey("bukkit")) {
|
|
|
|
PS.debug("ERROR: Player data does not contain the the key \"bukkit\"");
|
|
|
|
} else {
|
|
|
|
NbtFactory.NbtCompound bukkit = (NbtFactory.NbtCompound) compound.get("bukkit");
|
|
|
|
String name = (String) bukkit.get("lastKnownName");
|
|
|
|
long last = (long) bukkit.get("lastPlayed");
|
2016-06-05 17:28:34 +10:00
|
|
|
if (Settings.UUID.OFFLINE) {
|
|
|
|
if (Settings.UUID.FORCE_LOWERCASE && !name.toLowerCase().equals(name)) {
|
2016-06-02 13:42:32 -04:00
|
|
|
uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
|
|
|
|
} else {
|
|
|
|
long most = (long) compound.get("UUIDMost");
|
|
|
|
long least = (long) compound.get("UUIDLeast");
|
|
|
|
uuid = new UUID(most, least);
|
|
|
|
}
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
2016-06-02 13:42:32 -04:00
|
|
|
if (ExpireManager.IMP != null) {
|
|
|
|
ExpireManager.IMP.storeDate(uuid, last);
|
|
|
|
}
|
|
|
|
toAdd.put(new StringWrapper(name), uuid);
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
2016-05-22 01:16:04 +10:00
|
|
|
} catch (Exception ignored) {
|
2016-03-29 15:47:59 -04:00
|
|
|
PS.debug(C.PREFIX + "&6Invalid PlayerData: " + uuid.toString() + ".dat");
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-22 21:41:37 -04:00
|
|
|
for (String name : names) {
|
|
|
|
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
|
|
|
|
StringWrapper nameWrap = new StringWrapper(name);
|
2015-07-24 16:06:58 +02:00
|
|
|
toAdd.put(nameWrap, uuid);
|
|
|
|
}
|
2016-02-13 20:01:18 -05:00
|
|
|
|
|
|
|
if (getUUIDMap().isEmpty()) {
|
2016-03-22 21:41:37 -04:00
|
|
|
for (OfflinePlotPlayer op : FileUUIDHandler.this.uuidWrapper.getOfflinePlayers()) {
|
|
|
|
long last = op.getLastPlayed();
|
2015-09-13 14:04:31 +10:00
|
|
|
if (last != 0) {
|
2016-03-22 21:41:37 -04:00
|
|
|
String name = op.getName();
|
|
|
|
StringWrapper wrap = new StringWrapper(name);
|
|
|
|
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(op);
|
2015-07-24 16:06:58 +02:00
|
|
|
toAdd.put(wrap, uuid);
|
2016-03-02 06:56:50 +11:00
|
|
|
if (ExpireManager.IMP != null) {
|
|
|
|
ExpireManager.IMP.storeDate(uuid, last);
|
|
|
|
}
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 17:26:50 +10:00
|
|
|
add(toAdd);
|
2015-09-13 14:04:31 +10:00
|
|
|
if (whenDone != null) {
|
2015-09-11 20:09:22 +10:00
|
|
|
whenDone.run();
|
|
|
|
}
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
|
|
|
});
|
2015-07-27 17:26:50 +10:00
|
|
|
return true;
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
2016-03-20 23:19:37 +01:00
|
|
|
|
2015-07-24 16:06:58 +02:00
|
|
|
@Override
|
2015-09-13 14:04:31 +10:00
|
|
|
public void fetchUUID(final String name, final RunnableVal<UUID> ifFetch) {
|
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
2015-07-24 16:06:58 +02:00
|
|
|
@Override
|
2015-09-13 14:04:31 +10:00
|
|
|
public void run() {
|
2016-03-22 21:41:37 -04:00
|
|
|
ifFetch.value = FileUUIDHandler.this.uuidWrapper.getUUID(name);
|
2015-07-27 17:26:50 +10:00
|
|
|
TaskManager.runTask(ifFetch);
|
2015-07-24 16:06:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|