201 lines
9.2 KiB
Java
Raw Normal View History

2015-07-31 03:24:01 +10:00
package com.plotsquared.bukkit.uuid;
2015-07-24 16:06:58 +02:00
2015-07-31 00:25:16 +10:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
2015-07-24 16:06:58 +02:00
import com.google.common.collect.HashBiMap;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
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;
2015-07-31 00:25:16 +10:00
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.NbtFactory;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualcrafters.plot.util.UUIDHandlerImplementation;
2015-07-24 16:06:58 +02:00
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
2015-07-27 02:14:34 +10:00
2015-07-27 17:26:50 +10:00
public class FileUUIDHandler extends UUIDHandlerImplementation {
2015-07-24 16:06:58 +02:00
2015-07-27 17:26:50 +10:00
public FileUUIDHandler(UUIDWrapper wrapper) {
super(wrapper);
2015-07-24 16:06:58 +02:00
}
2015-07-27 17:26:50 +10:00
2015-07-24 16:06:58 +02:00
@Override
2015-07-27 23:10:14 +10:00
public boolean startCaching(Runnable whenDone) {
if (!super.startCaching(whenDone)) {
2015-07-27 17:26:50 +10:00
return false;
2015-07-24 16:06:58 +02:00
}
2015-07-27 23:10:14 +10:00
return cache(whenDone);
}
public boolean cache(final Runnable whenDone) {
2015-07-24 16:06:58 +02:00
final File container = Bukkit.getWorldContainer();
2015-07-27 17:26:50 +10:00
List<World> worlds = Bukkit.getWorlds();
final String world;
if (worlds.size() == 0) {
world = "world";
}
else {
world = worlds.get(0).getName();
}
2015-07-24 16:06:58 +02:00
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
2015-07-31 00:25:16 +10:00
PS.debug(C.PREFIX.s() + "&6Starting player data caching for: " + world);
2015-07-27 17:26:50 +10:00
final HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
2015-07-24 16:06:58 +02:00
toAdd.put(new StringWrapper("*"), DBFunc.everyone);
if (Settings.TWIN_MODE_UUID) {
HashSet<UUID> all = UUIDHandler.getAllUUIDS();
2015-07-31 00:25:16 +10:00
PS.debug("&aFast mode UUID caching enabled!");
2015-07-24 16:06:58 +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);
2015-07-31 19:46:07 +10:00
if (check || all.remove(uuid)) {
2015-07-24 16:06:58 +02:00
File file = new File(playerdataFolder + File.separator + current);
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
NbtFactory.NbtCompound compound = NbtFactory.fromStream(is, NbtFactory.StreamOptions.GZIP_COMPRESSION);
NbtFactory.NbtCompound bukkit = (NbtFactory.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-31 00:25:16 +10:00
PS.debug(C.PREFIX.s() + "Invalid playerdata: " + current);
2015-07-24 16:06:58 +02:00
}
}
}
2015-07-27 17:26:50 +10:00
add(toAdd);
2015-07-31 19:46:07 +10:00
if (all.size() == 0) {
if (whenDone != null) whenDone.run();
return;
}
else {
PS.debug("Failed to cache: " + all.size() + " uuids - slowly processing all files");
}
2015-07-24 16:06:58 +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-31 00:25:16 +10:00
PS.debug(C.PREFIX.s() + "Invalid playerdata: " + current);
2015-07-24 16:06:58 +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;
}
}
for (UUID uuid : uuids) {
try {
File file = new File(playerdataFolder + File.separator + uuid.toString() + ".dat");
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
NbtFactory.NbtCompound compound = NbtFactory.fromStream(is, NbtFactory.StreamOptions.GZIP_COMPRESSION);
NbtFactory.NbtCompound bukkit = (NbtFactory.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)) {
uuid = uuidWrapper.getUUID(name);
} else {
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-31 00:25:16 +10:00
PS.debug(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
2015-07-24 16:06:58 +02:00
}
}
for (final String name : names) {
final UUID uuid = uuidWrapper.getUUID(name);
final StringWrapper nameWrap = new StringWrapper(name);
toAdd.put(nameWrap, uuid);
}
2015-07-27 17:26:50 +10:00
if (getUUIDMap().size() == 0) {
2015-07-24 16:06:58 +02:00
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-07-27 17:26:50 +10:00
add(toAdd);
2015-07-27 23:10:14 +10:00
if (whenDone != null) 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
}
@Override
2015-07-27 17:26:50 +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
public void run() {
2015-07-27 17:26:50 +10:00
ifFetch.value = uuidWrapper.getUUID(name);
TaskManager.runTask(ifFetch);
2015-07-24 16:06:58 +02:00
}
});
}
}