Huge UUID Update.

This commit is contained in:
Sauilitired 2015-07-24 16:06:58 +02:00
parent 57c849cbaa
commit 2a52ad4702
23 changed files with 1013 additions and 241 deletions

View File

@ -122,7 +122,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
} }
List<World> worlds = Bukkit.getWorlds(); List<World> worlds = Bukkit.getWorlds();
if (worlds.size() > 0) { if (worlds.size() > 0) {
UUIDHandler.cacheAll(worlds.get(0).getName()); UUIDHandler.startCaching();
for (World world : worlds) { for (World world : worlds) {
try { try {
SetGenCB.setGenerator(world); SetGenCB.setGenerator(world);
@ -531,19 +531,19 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
final boolean checkVersion = checkVersion(1, 7, 6); final boolean checkVersion = checkVersion(1, 7, 6);
if (Settings.OFFLINE_MODE) { if (Settings.OFFLINE_MODE) {
if (Settings.UUID_LOWERCASE) { if (Settings.UUID_LOWERCASE) {
UUIDHandler.uuidWrapper = new LowerOfflineUUIDWrapper(); UUIDHandler.setUUIDWrapper(new LowerOfflineUUIDWrapper());
} else { } else {
UUIDHandler.uuidWrapper = new OfflineUUIDWrapper(); UUIDHandler.setUUIDWrapper(new OfflineUUIDWrapper());
} }
Settings.OFFLINE_MODE = true; Settings.OFFLINE_MODE = true;
} else if (checkVersion) { } else if (checkVersion) {
UUIDHandler.uuidWrapper = new DefaultUUIDWrapper(); UUIDHandler.setUUIDWrapper(new DefaultUUIDWrapper());
Settings.OFFLINE_MODE = false; Settings.OFFLINE_MODE = false;
} else { } else {
if (Settings.UUID_LOWERCASE) { if (Settings.UUID_LOWERCASE) {
UUIDHandler.uuidWrapper = new LowerOfflineUUIDWrapper(); UUIDHandler.setUUIDWrapper(new LowerOfflineUUIDWrapper());
} else { } else {
UUIDHandler.uuidWrapper = new OfflineUUIDWrapper(); UUIDHandler.setUUIDWrapper(new OfflineUUIDWrapper());
} }
Settings.OFFLINE_MODE = true; Settings.OFFLINE_MODE = true;
} }
@ -553,9 +553,9 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
FlagManager.removeFlag(FlagManager.getFlag("titles")); FlagManager.removeFlag(FlagManager.getFlag("titles"));
} else { } else {
AbstractTitle.TITLE_CLASS = new DefaultTitle(); AbstractTitle.TITLE_CLASS = new DefaultTitle();
if (UUIDHandler.uuidWrapper instanceof DefaultUUIDWrapper) { if (UUIDHandler.getUUIDWrapper() instanceof DefaultUUIDWrapper) {
Settings.TWIN_MODE_UUID = true; Settings.TWIN_MODE_UUID = true;
} else if (UUIDHandler.uuidWrapper instanceof OfflineUUIDWrapper && !Bukkit.getOnlineMode()) { } else if (UUIDHandler.getUUIDWrapper() instanceof OfflineUUIDWrapper && !Bukkit.getOnlineMode()) {
Settings.TWIN_MODE_UUID = true; Settings.TWIN_MODE_UUID = true;
} }
} }
@ -564,7 +564,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
} else { } else {
log(C.PREFIX.s() + " &6PlotSquared is using online UUIDs"); log(C.PREFIX.s() + " &6PlotSquared is using online UUIDs");
} }
return UUIDHandler.uuidWrapper; return UUIDHandler.getUUIDWrapper();
} }
@Override @Override

View File

@ -79,6 +79,8 @@ import com.intellectualcrafters.plot.util.PlayerManager;
import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.SetupUtils;
import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.FileUUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.bukkit.WorldEditPlugin;
/** /**
@ -173,7 +175,7 @@ public class PS {
IMP.registerChunkProcessor(); IMP.registerChunkProcessor();
} }
// create UUIDWrapper // create UUIDWrapper
UUIDHandler.uuidWrapper = IMP.initUUIDHandler(); UUIDHandler.setUUIDWrapper(IMP.initUUIDHandler());
// create event util class // create event util class
EventUtil.manager = IMP.initEventUtil(); EventUtil.manager = IMP.initEventUtil();
// create Hybrid utility class // create Hybrid utility class
@ -1113,6 +1115,7 @@ public class PS {
public void disable() { public void disable() {
try { try {
database.closeConnection(); database.closeConnection();
UUIDHandler.handleShutdown();
} catch (NullPointerException | SQLException e) { } catch (NullPointerException | SQLException e) {
log("&cCould not close database connection!"); log("&cCould not close database connection!");
} }
@ -1310,6 +1313,7 @@ public class PS {
options.put("plotme-convert.cache-uuids", Settings.CACHE_PLOTME); options.put("plotme-convert.cache-uuids", Settings.CACHE_PLOTME);
// UUID // UUID
options.put("uuid.use_sqluuidhandler", Settings.USE_SQLUUIDHANDLER);
options.put("UUID.offline", Settings.OFFLINE_MODE); options.put("UUID.offline", Settings.OFFLINE_MODE);
options.put("UUID.force-lowercase", Settings.UUID_LOWERCASE); options.put("UUID.force-lowercase", Settings.UUID_LOWERCASE);
options.put("uuid.read-from-disk", Settings.UUID_FROM_DISK); options.put("uuid.read-from-disk", Settings.UUID_FROM_DISK);
@ -1426,6 +1430,7 @@ public class PS {
Settings.CACHE_PLOTME = config.getBoolean("plotme-convert.cache-uuids"); Settings.CACHE_PLOTME = config.getBoolean("plotme-convert.cache-uuids");
// UUID // UUID
Settings.USE_SQLUUIDHANDLER = config.getBoolean("uuid.use_sqluuidhandler");
Settings.OFFLINE_MODE = config.getBoolean("UUID.offline"); Settings.OFFLINE_MODE = config.getBoolean("UUID.offline");
Settings.UUID_LOWERCASE = Settings.OFFLINE_MODE && config.getBoolean("UUID.force-lowercase"); Settings.UUID_LOWERCASE = Settings.OFFLINE_MODE && config.getBoolean("UUID.force-lowercase");
Settings.UUID_FROM_DISK = config.getBoolean("uuid.read-from-disk"); Settings.UUID_FROM_DISK = config.getBoolean("uuid.read-from-disk");
@ -1511,6 +1516,8 @@ public class PS {
Settings.FANCY_CHAT = false; Settings.FANCY_CHAT = false;
} }
Settings.METRICS = config.getBoolean("metrics"); Settings.METRICS = config.getBoolean("metrics");
UUIDHandler.implementation = Settings.USE_SQLUUIDHANDLER ? new SQLUUIDHandler() : new FileUUIDHandler();
} }

View File

@ -199,7 +199,7 @@ import com.intellectualcrafters.plot.uuid.UUIDWrapper;
* @see com.intellectualcrafters.plot.uuid.UUIDWrapper * @see com.intellectualcrafters.plot.uuid.UUIDWrapper
*/ */
public UUIDWrapper getUUIDWrapper() { public UUIDWrapper getUUIDWrapper() {
return UUIDHandler.uuidWrapper; return UUIDHandler.getUUIDWrapper();
} }
/** /**

View File

@ -32,6 +32,7 @@ import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
public class Add extends SubCommand { public class Add extends SubCommand {
public Add() { public Add() {
@ -64,7 +65,11 @@ public class Add extends SubCommand {
uuid = UUIDHandler.getUUID(args[0]); uuid = UUIDHandler.getUUID(args[0]);
} }
if (uuid == null) { if (uuid == null) {
if (UUIDHandler.implementation instanceof SQLUUIDHandler) {
MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]);
} else {
MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]);
}
return false; return false;
} }
if (plot.isOwner(uuid)) { if (plot.isOwner(uuid)) {

View File

@ -99,7 +99,7 @@ public class Buy extends SubCommand {
} }
EconHandler.manager.withdrawMoney(plr, price); EconHandler.manager.withdrawMoney(plr, price);
sendMessage(plr, C.REMOVED_BALANCE, price + ""); sendMessage(plr, C.REMOVED_BALANCE, price + "");
EconHandler.manager.depositMoney(UUIDHandler.uuidWrapper.getOfflinePlayer(plot.owner), initPrice); EconHandler.manager.depositMoney(UUIDHandler.getUUIDWrapper().getOfflinePlayer(plot.owner), initPrice);
final PlotPlayer owner = UUIDHandler.getPlayer(plot.owner); final PlotPlayer owner = UUIDHandler.getPlayer(plot.owner);
if (owner != null) { if (owner != null) {
sendMessage(plr, C.PLOT_SOLD, plot.id + "", plr.getName(), initPrice + ""); sendMessage(plr, C.PLOT_SOLD, plot.id + "", plr.getName(), initPrice + "");

View File

@ -216,7 +216,7 @@ public class DebugExec extends SubCommand {
if (uuid == null) { if (uuid == null) {
return MainUtil.sendMessage(player, "player not found: " + args[1]); return MainUtil.sendMessage(player, "player not found: " + args[1]);
} }
final OfflinePlotPlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid); final OfflinePlotPlayer op = UUIDHandler.getUUIDWrapper().getOfflinePlayer(uuid);
if ((op == null) || (op.getLastPlayed() == 0)) { if ((op == null) || (op.getLastPlayed() == 0)) {
return MainUtil.sendMessage(player, "player hasn't connected before: " + args[1]); return MainUtil.sendMessage(player, "player hasn't connected before: " + args[1]);
} }

View File

@ -64,7 +64,7 @@ public class DebugUUID extends SubCommand {
return false; return false;
} }
UUIDWrapper currentUUIDWrapper = UUIDHandler.uuidWrapper; UUIDWrapper currentUUIDWrapper = UUIDHandler.getUUIDWrapper();
UUIDWrapper newWrapper = null; UUIDWrapper newWrapper = null;
switch (args[0].toLowerCase()) { switch (args[0].toLowerCase()) {
@ -109,7 +109,7 @@ public class DebugUUID extends SubCommand {
} }
MainUtil.sendConsoleMessage("&6Beginning UUID mode conversion"); MainUtil.sendConsoleMessage("&6Beginning UUID mode conversion");
MainUtil.sendConsoleMessage("&7 - Disconnecting players"); MainUtil.sendConsoleMessage("&7 - Disconnecting players");
for (PlotPlayer user : UUIDHandler.players.values()) { for (PlotPlayer user : UUIDHandler.getPlayers().values()) {
PlayerManager.manager.kickPlayer(user, "PlotSquared UUID conversion has been initiated. You may reconnect when finished."); PlayerManager.manager.kickPlayer(user, "PlotSquared UUID conversion has been initiated. You may reconnect when finished.");
} }
@ -212,7 +212,7 @@ public class DebugUUID extends SubCommand {
} }
MainUtil.sendConsoleMessage("&7 - Replacing wrapper"); MainUtil.sendConsoleMessage("&7 - Replacing wrapper");
UUIDHandler.uuidWrapper = newWrapper; UUIDHandler.setUUIDWrapper(newWrapper);
MainUtil.sendConsoleMessage("&7 - Updating plot objects"); MainUtil.sendConsoleMessage("&7 - Updating plot objects");

View File

@ -50,7 +50,7 @@ public class Delete extends SubCommand {
if (!MainUtil.getTopPlot(plot).equals(MainUtil.getBottomPlot(plot))) { if (!MainUtil.getTopPlot(plot).equals(MainUtil.getBottomPlot(plot))) {
return !sendMessage(plr, C.UNLINK_REQUIRED); return !sendMessage(plr, C.UNLINK_REQUIRED);
} }
if ((((plot == null) || !plot.hasOwner() || !plot.isOwner(UUIDHandler.uuidWrapper.getUUID(plr)))) && !Permissions.hasPermission(plr, "plots.admin.command.delete")) { if ((((plot == null) || !plot.hasOwner() || !plot.isOwner(UUIDHandler.getUUIDWrapper().getUUID(plr)))) && !Permissions.hasPermission(plr, "plots.admin.command.delete")) {
return !sendMessage(plr, C.NO_PLOT_PERMS); return !sendMessage(plr, C.NO_PLOT_PERMS);
} }
assert plot != null; assert plot != null;

View File

@ -35,6 +35,7 @@ import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -69,7 +70,11 @@ public class Deny extends SubCommand {
uuid = UUIDHandler.getUUID(args[0]); uuid = UUIDHandler.getUUID(args[0]);
} }
if (uuid == null) { if (uuid == null) {
if (UUIDHandler.implementation instanceof SQLUUIDHandler) {
MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]);
} else {
MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]);
}
return false; return false;
} }
if (plot.isOwner(uuid)) { if (plot.isOwner(uuid)) {

View File

@ -31,6 +31,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
public class Remove extends SubCommand { public class Remove extends SubCommand {
public Remove() { public Remove() {
@ -112,7 +113,11 @@ public class Remove extends SubCommand {
} }
} }
if (count == 0) { if (count == 0) {
if (UUIDHandler.implementation instanceof SQLUUIDHandler) {
MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]);
} else {
MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]);
}
return false; return false;
} }
else { else {

View File

@ -32,6 +32,7 @@ import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
public class Trust extends SubCommand { public class Trust extends SubCommand {
public Trust() { public Trust() {
@ -64,7 +65,11 @@ public class Trust extends SubCommand {
uuid = UUIDHandler.getUUID(args[0]); uuid = UUIDHandler.getUUID(args[0]);
} }
if (uuid == null) { if (uuid == null) {
if (UUIDHandler.implementation instanceof SQLUUIDHandler) {
MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]);
} else {
MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]);
}
return false; return false;
} }
if (plot.isOwner(uuid)) { if (plot.isOwner(uuid)) {

View File

@ -30,6 +30,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
public class Undeny extends SubCommand { public class Undeny extends SubCommand {
public Undeny() { public Undeny() {
@ -83,7 +84,11 @@ public class Undeny extends SubCommand {
} }
} }
if (count == 0) { if (count == 0) {
if (UUIDHandler.implementation instanceof SQLUUIDHandler) {
MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]);
} else {
MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]);
}
return false; return false;
} }
else { else {

View File

@ -282,6 +282,7 @@ public enum C {
/* /*
* Player not found * Player not found
*/ */
INVALID_PLAYER_WAIT("$2Player not found: $1%s$2, fetching it. Try again soon.", "Errors"),
INVALID_PLAYER("$2Player not found: $1%s.", "Errors"), INVALID_PLAYER("$2Player not found: $1%s.", "Errors"),
// SETTINGS_PASTE_UPLOADED("$2settings.yml was uploaded to: $1%url%", "Paste"), // SETTINGS_PASTE_UPLOADED("$2settings.yml was uploaded to: $1%url%", "Paste"),
// LATEST_LOG_UPLOADED("$2latest.log was uploaded to: $1%url%", "Paste"), // LATEST_LOG_UPLOADED("$2latest.log was uploaded to: $1%url%", "Paste"),

View File

@ -30,6 +30,8 @@ import java.util.List;
* @author Empire92 * @author Empire92
*/ */
public class Settings { public class Settings {
public static boolean USE_SQLUUIDHANDLER = false;
public static boolean ENABLE_CLUSTERS = false; public static boolean ENABLE_CLUSTERS = false;
public static boolean FAST_CLEAR = false; public static boolean FAST_CLEAR = false;
/** /**

View File

@ -136,7 +136,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
int x = bloc.getBlockX(); int x = bloc.getBlockX();
int z = bloc.getBlockZ(); int z = bloc.getBlockZ();
int distance = Bukkit.getViewDistance() * 16; int distance = Bukkit.getViewDistance() * 16;
for (PlotPlayer player : UUIDHandler.players.values()) { for (PlotPlayer player : UUIDHandler.getPlayers().values()) {
Location loc = player.getLocation(); Location loc = player.getLocation();
if (loc.getWorld().equals(world)) { if (loc.getWorld().equals(world)) {
if (16 * (Math.abs(loc.getX() - x) / 16) > distance) { if (16 * (Math.abs(loc.getX() - x) / 16) > distance) {

View File

@ -43,6 +43,6 @@ public class WorldEvents implements Listener {
@EventHandler @EventHandler
public void worldLoad(final WorldLoadEvent event) { public void worldLoad(final WorldLoadEvent event) {
UUIDHandler.cacheAll(event.getWorld().getName()); UUIDHandler.cacheWorld(event.getWorld().getName());
} }
} }

View File

@ -72,7 +72,7 @@ public class MainUtil {
public static List<PlotPlayer> getPlayersInPlot(Plot plot) { public static List<PlotPlayer> getPlayersInPlot(Plot plot) {
ArrayList<PlotPlayer> players = new ArrayList<>(); ArrayList<PlotPlayer> players = new ArrayList<>();
for (PlotPlayer pp : UUIDHandler.players.values()) { for (PlotPlayer pp : UUIDHandler.getPlayers().values()) {
if (plot.equals(MainUtil.getPlot(pp.getLocation()))) { if (plot.equals(MainUtil.getPlot(pp.getLocation()))) {
players.add(pp); players.add(pp);
} }

View File

@ -47,7 +47,7 @@ public class BukkitUtil extends BlockManager {
public static void removePlayer(final String plr) { public static void removePlayer(final String plr) {
lastPlayer = null; lastPlayer = null;
lastPlotPlayer = null; lastPlotPlayer = null;
UUIDHandler.players.remove(plr); UUIDHandler.getPlayers().remove(plr);
} }
@Override @Override
@ -69,12 +69,12 @@ public class BukkitUtil extends BlockManager {
return lastPlotPlayer; return lastPlotPlayer;
} }
String name = player.getName(); String name = player.getName();
PlotPlayer pp = UUIDHandler.players.get(name); PlotPlayer pp = UUIDHandler.getPlayers().get(name);
if (pp != null) { if (pp != null) {
return pp; return pp;
} }
lastPlotPlayer = new BukkitPlayer(player); lastPlotPlayer = new BukkitPlayer(player);
UUIDHandler.players.put(name, lastPlotPlayer); UUIDHandler.getPlayers().put(name, lastPlotPlayer);
lastPlayer = player; lastPlayer = player;
return lastPlotPlayer; return lastPlotPlayer;
} }

View File

@ -5,9 +5,13 @@ import java.io.FileInputStream;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import com.intellectualcrafters.plot.util.bukkit.uuid.FileUUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler;
import com.intellectualcrafters.plot.util.bukkit.uuid.UUIDHandlerImplementation;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
@ -32,28 +36,11 @@ import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper;
import com.intellectualcrafters.plot.uuid.UUIDWrapper; import com.intellectualcrafters.plot.uuid.UUIDWrapper;
public class UUIDHandler { public class UUIDHandler {
/**
* Map containing names and UUIDs public static UUIDHandlerImplementation implementation;
*
* @see com.google.common.collect.BiMap
*/
private final static BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
public static boolean CACHED = false;
public static UUIDWrapper uuidWrapper = null;
public static HashMap<String, PlotPlayer> players = new HashMap<>();
public static void add(final StringWrapper name, final UUID uuid) { public static void add(final StringWrapper name, final UUID uuid) {
if ((uuid == null) || (name == null)) { implementation.add(name, uuid);
return;
}
BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
if (inverse.containsKey(uuid)) {
if (uuidMap.containsKey(name)) {
return;
}
inverse.remove(uuid);
}
uuidMap.put(name, uuid);
} }
/** /**
@ -64,7 +51,7 @@ public class UUIDHandler {
* @see com.google.common.collect.BiMap * @see com.google.common.collect.BiMap
*/ */
public static BiMap<StringWrapper, UUID> getUuidMap() { public static BiMap<StringWrapper, UUID> getUuidMap() {
return uuidMap; return implementation.getUUIDMap();
} }
/** /**
@ -77,7 +64,7 @@ public class UUIDHandler {
* @see com.google.common.collect.BiMap#containsValue(Object) * @see com.google.common.collect.BiMap#containsValue(Object)
*/ */
public static boolean uuidExists(final UUID uuid) { public static boolean uuidExists(final UUID uuid) {
return uuidMap.containsValue(uuid); return implementation.uuidExists(uuid);
} }
/** /**
@ -90,11 +77,11 @@ public class UUIDHandler {
* @see com.google.common.collect.BiMap#containsKey(Object) * @see com.google.common.collect.BiMap#containsKey(Object)
*/ */
public static boolean nameExists(final StringWrapper name) { public static boolean nameExists(final StringWrapper name) {
return uuidMap.containsKey(name); return implementation.nameExists(name);
} }
public static HashSet<UUID> getAllUUIDS() { public static HashSet<UUID> getAllUUIDS() {
HashSet<UUID> uuids = new HashSet<UUID>(); HashSet<UUID> uuids = new HashSet<>();
for (Plot plot : PS.get().getPlotsRaw()) { for (Plot plot : PS.get().getPlotsRaw()) {
if (plot.owner != null) { if (plot.owner != null) {
uuids.add(plot.owner); uuids.add(plot.owner);
@ -106,211 +93,55 @@ public class UUIDHandler {
return uuids; return uuids;
} }
public static void cacheAll(final String world) { public static UUIDWrapper getUUIDWrapper() {
if (CACHED) { return implementation.getUUIDWrapper();
return;
}
final File container = Bukkit.getWorldContainer();
UUIDHandler.CACHED = true;
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
PS.log(C.PREFIX.s() + "&6Starting player data caching for: " + world);
final HashMap<StringWrapper, UUID> toAdd = new HashMap<>();
toAdd.put(new StringWrapper("*"), DBFunc.everyone);
if (Settings.TWIN_MODE_UUID) {
HashSet<UUID> all = getAllUUIDS();
PS.log("&aFast mode UUID caching enabled!");
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();
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
}
cache(toAdd);
return;
}
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) {
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
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);
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)) {
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) {
PS.log(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
}
}
for (final String name : names) {
final UUID uuid = uuidWrapper.getUUID(name);
final StringWrapper nameWrap = new StringWrapper(name);
toAdd.put(nameWrap, uuid);
} }
if (uuidMap.size() == 0) { public static void setUUIDWrapper(final UUIDWrapper wrapper) {
for (OfflinePlotPlayer op : uuidWrapper.getOfflinePlayers()) { implementation.setUUIDWrapper(wrapper);
if (op.getLastPlayed() != 0) {
String name = op.getName();
StringWrapper wrap = new StringWrapper(name);
UUID uuid = uuidWrapper.getUUID(op);
toAdd.put(wrap, uuid);
}
}
}
cache(toAdd);
}
});
} }
public static void cache(final HashMap<StringWrapper, UUID> toAdd) { public static void startCaching() {
TaskManager.runTask(new Runnable() { implementation.startCaching();
@Override
public void run() {
for (Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
add(entry.getKey(), entry.getValue());
} }
PS.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs");
} public static void cache(final Map<StringWrapper, UUID> toAdd) {
}); implementation.cache(toAdd);
} }
public static UUID getUUID(final PlotPlayer player) { public static UUID getUUID(final PlotPlayer player) {
return UUIDHandler.uuidWrapper.getUUID(player); return implementation.getUUID(player);
} }
public static UUID getUUID(final BukkitOfflinePlayer player) { public static UUID getUUID(final BukkitOfflinePlayer player) {
return UUIDHandler.uuidWrapper.getUUID(player); return implementation.getUUID(player);
} }
public static String getName(final UUID uuid) { public static String getName(final UUID uuid) {
if (uuid == null) { return implementation.getName(uuid);
return null;
}
// check online
final PlotPlayer player = UUIDHandler.getPlayer(uuid);
if (player != null) {
return player.getName();
}
// check cache
final StringWrapper name = UUIDHandler.uuidMap.inverse().get(uuid);
if (name != null) {
return name.value;
}
return null;
} }
public static PlotPlayer getPlayer(final UUID uuid) { public static PlotPlayer getPlayer(final UUID uuid) {
for (final PlotPlayer player : players.values()) { return implementation.getPlayer(uuid);
if (player.getUUID().equals(uuid)) {
return player;
}
}
return null;
} }
public static PlotPlayer getPlayer(final String name) { public static PlotPlayer getPlayer(final String name) {
return players.get(name); return implementation.getPlayer(name);
} }
public static UUID getUUID(final String name) { public static UUID getUUID(final String name) {
if ((name == null) || (name.length() == 0)) { return implementation.getUUID(name);
return null;
} }
// check online
final PlotPlayer player = getPlayer(name); public static Map<String, PlotPlayer> getPlayers() {
if (player != null) { return implementation.getPlayers();
return player.getUUID();
} }
// check cache
final StringWrapper wrap = new StringWrapper(name); public static void cacheWorld(String world) {
UUID uuid = UUIDHandler.uuidMap.get(wrap); implementation.cacheWorld(world);
if (uuid != null) {
return uuid;
} }
// Read from disk OR convert directly to offline UUID
if (Settings.UUID_FROM_DISK || (uuidWrapper instanceof OfflineUUIDWrapper)) { public static void handleShutdown() {
uuid = UUIDHandler.uuidWrapper.getUUID(name); implementation.handleShutdown();
add(new StringWrapper(name), uuid);
return uuid;
}
return null;
} }
} }

View File

@ -0,0 +1,304 @@
package com.intellectualcrafters.plot.util.bukkit.uuid;
import com.google.common.collect.BiMap;
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;
import com.intellectualcrafters.plot.object.*;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.NbtFactory;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import org.bukkit.Bukkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;
public class FileUUIDHandler implements UUIDHandlerImplementation {
private final BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
public boolean CACHED = false;
public UUIDWrapper uuidWrapper = null;
public HashMap<String, PlotPlayer> players = new HashMap<>();
@Override
public void add(final StringWrapper name, final UUID uuid) {
if ((uuid == null) || (name == null)) {
return;
}
BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
if (inverse.containsKey(uuid)) {
if (uuidMap.containsKey(name)) {
return;
}
inverse.remove(uuid);
}
uuidMap.put(name, uuid);
}
@Override
public void handleShutdown() {}
@Override
public BiMap<StringWrapper, UUID> getUUIDMap() {
return uuidMap;
}
@Override
public boolean uuidExists(final UUID uuid) {
return uuidMap.containsValue(uuid);
}
@Override
public boolean nameExists(final StringWrapper name) {
return uuidMap.containsKey(name);
}
@Override
public void startCaching() {
if (Bukkit.getWorlds().size() > 1) {
cacheWorld(Bukkit.getWorlds().get(0).getName());
}
}
@Override
public void setUUIDWrapper(UUIDWrapper wrapper) {
this.uuidWrapper = wrapper;
}
@Override
public UUIDWrapper getUUIDWrapper() {
return uuidWrapper;
}
@Override
public Map<String, PlotPlayer> getPlayers() {
return players;
}
@Override
public void cacheWorld(final String world) {
if (CACHED) {
return;
}
final File container = Bukkit.getWorldContainer();
CACHED = true;
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
PS.log(C.PREFIX.s() + "&6Starting player data caching for: " + world);
final HashMap<StringWrapper, UUID> toAdd = new HashMap<>();
toAdd.put(new StringWrapper("*"), DBFunc.everyone);
if (Settings.TWIN_MODE_UUID) {
HashSet<UUID> all = UUIDHandler.getAllUUIDS();
PS.log("&aFast mode UUID caching enabled!");
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);
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();
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
}
cache(toAdd);
return;
}
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) {
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
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) {
PS.log(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
}
}
for (final String name : names) {
final UUID uuid = uuidWrapper.getUUID(name);
final StringWrapper nameWrap = new StringWrapper(name);
toAdd.put(nameWrap, uuid);
}
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);
}
}
}
cache(toAdd);
}
});
}
@Override
public void cache(final Map<StringWrapper, UUID> toAdd) {
TaskManager.runTask(new Runnable() {
@Override
public void run() {
for (Map.Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
add(entry.getKey(), entry.getValue());
}
PS.log(C.PREFIX.s() + "&6Cached a total of: " + uuidMap.size() + " UUIDs");
}
});
}
@Override
public UUID getUUID(final PlotPlayer player) {
return uuidWrapper.getUUID(player);
}
@Override
public UUID getUUID(final BukkitOfflinePlayer player) {
return uuidWrapper.getUUID(player);
}
@Override
public String getName(final UUID uuid) {
if (uuid == null) {
return null;
}
// check online
final PlotPlayer player = getPlayer(uuid);
if (player != null) {
return player.getName();
}
// check cache
final StringWrapper name = uuidMap.inverse().get(uuid);
if (name != null) {
return name.value;
}
return null;
}
@Override
public PlotPlayer getPlayer(final UUID uuid) {
for (final PlotPlayer player : players.values()) {
if (player.getUUID().equals(uuid)) {
return player;
}
}
return null;
}
@Override
public PlotPlayer getPlayer(final String name) {
return players.get(name);
}
@Override
public UUID getUUID(final String name) {
if ((name == null) || (name.length() == 0)) {
return null;
}
// check online
final PlotPlayer player = getPlayer(name);
if (player != null) {
return player.getUUID();
}
// check cache
final StringWrapper wrap = new StringWrapper(name);
UUID uuid = uuidMap.get(wrap);
if (uuid != null) {
return uuid;
}
// Read from disk OR convert directly to offline UUID
if (Settings.UUID_FROM_DISK || (uuidWrapper instanceof OfflineUUIDWrapper)) {
uuid = uuidWrapper.getUUID(name);
add(new StringWrapper(name), uuid);
return uuid;
}
return null;
}
}

View File

@ -0,0 +1,570 @@
package com.intellectualcrafters.plot.util.bukkit.uuid;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.collect.Maps;
import com.intellectualcrafters.json.JSONObject;
import com.intellectualcrafters.plot.BukkitMain;
import com.intellectualcrafters.plot.database.SQLite;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.uuid.LowerOfflineUUIDWrapper;
import org.bukkit.Bukkit;
import com.google.common.collect.BiMap;
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;
import com.intellectualcrafters.plot.object.BukkitOfflinePlayer;
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.NbtFactory;
import com.intellectualcrafters.plot.util.NbtFactory.NbtCompound;
import com.intellectualcrafters.plot.util.NbtFactory.StreamOptions;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class SQLUUIDHandler implements Listener, UUIDHandlerImplementation {
private class SQLUUIDHandlerException extends RuntimeException {
SQLUUIDHandlerException(String s, Throwable c) {
super("SQLUUIDHandler caused an exception: " + s, c);
}
@SuppressWarnings("unused")
SQLUUIDHandlerException(String s) {
super("SQLUUIDHandler caused an exception: " + s);
}
}
private final SQLite _sqLite;
private final BiMap<String, UUID> _uuidMap;
private UUIDWrapper _uuidWrapper;
private final Map<String, PlotPlayer> _players = new ConcurrentHashMap<>();
private Connection getConnection() {
synchronized (_sqLite) {
return _sqLite.getConnection();
}
}
public SQLUUIDHandler() {
Bukkit.getPluginManager().registerEvents(this, JavaPlugin.getPlugin(BukkitMain.class));
_sqLite = new SQLite("./plugins/PlotSquared/usercache.db");
try {
_sqLite.openConnection();
} catch(final Exception e) {
e.printStackTrace();
}
try {
PreparedStatement stmt = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS `usercache` (cache_key INTEGER PRIMARY KEY, uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL)");
stmt.execute();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
_uuidMap = Maps.synchronizedBiMap(HashBiMap.create(new HashMap<String, UUID>()));
_startCaching();
}
@Override
public void handleShutdown() {
try {
getConnection().close();
} catch (SQLException e) {
throw new SQLUUIDHandlerException("Couldn't close database connection", e);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@SuppressWarnings("unused")
public void onPlayerJoin(final PlayerLoginEvent event) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
if (!nameExists(new StringWrapper(event.getPlayer().getName()))) {
add(new StringWrapper(event.getPlayer().getName()), event.getPlayer().getUniqueId());
}
}
});
}
@Override
public void startCaching() {
// startCaching();
}
@Override
public void setUUIDWrapper(UUIDWrapper wrapper) {
this._uuidWrapper = wrapper;
}
@Override
public UUIDWrapper getUUIDWrapper() {
return _uuidWrapper;
}
@Override
public Map<String, PlotPlayer> getPlayers() {
return _players;
}
public void _startCaching() {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
try {
PreparedStatement statement = getConnection().prepareStatement("SELECT `uuid`, `username` FROM `usercache`");
ResultSet resultSet = statement.executeQuery();
StringWrapper username;
UUID uuid;
boolean found = false;
while (resultSet.next()) {
found = true;
username = new StringWrapper(resultSet.getString("username"));
uuid = UUID.fromString(resultSet.getString("uuid"));
_uuidMap.put(username.value, uuid);
}
_uuidMap.put("*", DBFunc.everyone);
statement.close();
if (!found) {
PS.log(C.PREFIX.s() + "&cUsing player data files, couldn't find any cached UUIDs");
for (World world : Bukkit.getWorlds()) {
_cacheAll(world.getName());
}
PS.log(C.PREFIX.s() + "&cWill fetch the uuids for all plots!");
List<UUID> toFetch = new ArrayList<>();
for (UUID u : UUIDHandler.getAllUUIDS()) {
if (!uuidExists(u)) {
toFetch.add(u);
}
}
PS.log(C.PREFIX.s() + "&cFetching &6" + toFetch.size() + "&c uuids!");
if (Settings.OFFLINE_MODE) {
if (!(_uuidWrapper instanceof OfflineUUIDWrapper)) {
PS.log(C.PREFIX.s() + "Offline mode is on, but the uuid wrapper isn't set for offline mode. Activating appropriate uuid wrapper");
if (Settings.UUID_LOWERCASE) {
_uuidWrapper = new LowerOfflineUUIDWrapper();
} else {
_uuidWrapper = new OfflineUUIDWrapper();
}
}
}
List<UUID> fetched = new ArrayList<>();
for (UUID u : toFetch) {
OfflinePlayer plr = Bukkit.getOfflinePlayer(u);
if (plr != null) {
if (plr.getName() != null) {
add(new StringWrapper(plr.getName()), u);
fetched.add(u);
}
}
}
PS.log(C.PREFIX.s() + "&cFetched &6" + fetched.size() + "&c from player files!");
toFetch.removeAll(fetched);
if (!Settings.OFFLINE_MODE) {
if (toFetch.isEmpty()) {
return;
}
PS.log(C.PREFIX.s() + "&cWill fetch &6" + toFetch.size() + "&c from mojang!");
int i = 0;
Iterator<UUID> iterator = toFetch.iterator();
while(iterator.hasNext()) {
StringBuilder url = new StringBuilder("http://api.intellectualsites.com/uuid/?user=");
List<UUID> currentIteration = new ArrayList<>();
while (i++ <= 15 && iterator.hasNext()) {
UUID _uuid = iterator.next();
url.append(_uuid.toString());
if (iterator.hasNext()) {
url.append(",");
}
currentIteration.add(_uuid);
}
PS.log(C.PREFIX.s() + "&cWill attempt to fetch &6" + currentIteration.size() + "&c uuids from: &6" + url.toString());
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null) {
rawJSON.append(line);
}
reader.close();
JSONObject object = new JSONObject(rawJSON.toString());
for (UUID _u : currentIteration) {
Object o = object.getJSONObject(_u.toString().replace("-", "")).get("username");
if (o == null || !(o instanceof String)) {
continue;
}
add(new StringWrapper(o.toString()), _u);
}
} catch(final Exception e) {
e.printStackTrace();
}
i = 0;
}
}
}
} catch (SQLException e) {
throw new SQLUUIDHandlerException("Couldn't select :s", e);
}
}
});
}
@Override
public void add(final StringWrapper name, final UUID uuid) {
if ((uuid == null) || (name == null)) {
PS.log(C.PREFIX.s() + "&cSQL Caching Failed: name/uuid was null??");
return;
}
if (name.value == null) {
PS.log(C.PREFIX.s() + "&cname.value == null for: " + uuid);
return;
}
if (_uuidMap.containsKey(name.value)) {
_uuidMap.remove(name.value);
}
if (_uuidMap.containsValue(uuid)) {
_uuidMap.inverse().remove(uuid);
}
_uuidMap.put(name.value, uuid);
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
try {
PreparedStatement statement = getConnection().prepareStatement("INSERT INTO usercache (`uuid`, `username`) VALUES(?, ?)");
statement.setString(1, uuid.toString());
statement.setString(2, name.toString());
statement.execute();
PS.log(C.PREFIX.s() + "&cAdded '&6" + uuid + "&c' - '&6" + name + "&c'");
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean uuidExists(final UUID uuid) {
return _uuidMap.containsValue(uuid);
}
@Override
public BiMap<StringWrapper, UUID> getUUIDMap() {
BiMap<StringWrapper, UUID> map = HashBiMap.create();
for (Map.Entry<String, UUID> entry : _uuidMap.entrySet()) {
map.put(new StringWrapper(entry.getKey()), entry.getValue());
}
return map;
}
@Override
public boolean nameExists(final StringWrapper name) {
return _uuidMap.containsKey(name.value);
}
@Override
public void cacheWorld(String world) {}
@SuppressWarnings("deprecation")
private void _cacheAll(final String world) {
final File container = Bukkit.getWorldContainer();
PS.log(C.PREFIX.s() + "&6Starting player data caching for: " + world);
final HashMap<StringWrapper, UUID> toAdd = new HashMap<>();
if (Settings.TWIN_MODE_UUID) {
Set<UUID> all = getUUIDMap().values();
PS.log("&aFast mode UUID caching enabled!");
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();
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
}
cache(toAdd);
return;
}
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) {
PS.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
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);
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)) {
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) {
PS.log(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
}
}
for (final String name : names) {
final UUID uuid = _uuidWrapper.getUUID(name);
final StringWrapper nameWrap = new StringWrapper(name);
toAdd.put(nameWrap, uuid);
}
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);
}
}
}
cache(toAdd);
}
@Override
public void cache(final Map<StringWrapper, UUID> toAdd) {
for (Map.Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
add(entry.getKey(), entry.getValue());
}
}
@Override
public String getName(final UUID uuid) {
if (uuid == null) {
return null;
}
final PlotPlayer player = getPlayer(uuid);
if (player != null) {
return player.getName();
}
if (_uuidMap.containsValue(uuid)) {
return _uuidMap.inverse().get(uuid);
}
if (Settings.OFFLINE_MODE) {
if (!(_uuidWrapper instanceof OfflineUUIDWrapper)) {
PS.log(C.PREFIX.s() + "Offline mode is on, but the uuid wrapper isn't set for offline mode. Activating appropriate uuid wrapper");
if (Settings.UUID_LOWERCASE) {
_uuidWrapper = new LowerOfflineUUIDWrapper();
} else {
_uuidWrapper = new OfflineUUIDWrapper();
}
}
}
OfflinePlayer plr = Bukkit.getOfflinePlayer(uuid);
if (plr != null) {
add(new StringWrapper(plr.getName()), plr.getUniqueId());
return plr.getName();
}
if (!Settings.OFFLINE_MODE) {
PS.log(C.PREFIX.s() + "Name for '" + uuid + "' was null. We'll cache this from the mojang servers!");
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
String url = "http://api.intellectualsites.com/uuid/?user=" + uuid;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null) {
rawJSON.append(line);
}
reader.close();
JSONObject object = new JSONObject(rawJSON.toString());
String username = object.getJSONObject(uuid.toString().replace("-", "")).getString("username");
add(new StringWrapper(username), uuid);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
return null;
}
@Override
public UUID getUUID(final PlotPlayer player) {
return _uuidWrapper.getUUID(player);
}
@Override
public UUID getUUID(final BukkitOfflinePlayer player) {
return _uuidWrapper.getUUID(player);
}
@Override
public PlotPlayer getPlayer(final UUID uuid) {
for (final PlotPlayer player : _players.values()) {
if (player.getUUID().equals(uuid)) {
return player;
}
}
return null;
}
@Override
public PlotPlayer getPlayer(final String name) {
return _players.get(name);
}
@Override
public UUID getUUID(final String name) {
if ((name == null) || (name.length() == 0)) {
return null;
}
// check online
final PlotPlayer player = getPlayer(name);
if (player != null) {
return player.getUUID();
}
// check cache
UUID uuid = _uuidMap.get(name);
if (uuid != null) {
return uuid;
}
if (Settings.OFFLINE_MODE) {
if (!(_uuidWrapper instanceof OfflineUUIDWrapper)) {
PS.log(C.PREFIX.s() + "Offline mode is on, but the uuid wrapper isn't set for offline mode. Activating appropriate uuid wrapper");
if (Settings.UUID_LOWERCASE) {
_uuidWrapper = new LowerOfflineUUIDWrapper();
} else {
_uuidWrapper = new OfflineUUIDWrapper();
}
}
}
// Read from disk OR convert directly to offline UUID
if (Settings.UUID_FROM_DISK || (_uuidWrapper instanceof OfflineUUIDWrapper)) {
uuid = _uuidWrapper.getUUID(name);
add(new StringWrapper(name), uuid);
return uuid;
}
PS.log(C.PREFIX.s() + "UUID for '" + name + "' was null. We'll cache this from the mojang servers!");
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
String url = "http://api.intellectualsites.com/uuid/?user=" + name;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null) {
rawJSON.append(line);
}
reader.close();
JSONObject object = new JSONObject(rawJSON.toString());
UUID uuid = UUID.fromString(object.getJSONObject(name).getString("dashed"));
add(new StringWrapper(name), uuid);
} catch (IOException e) {
e.printStackTrace();
}
}
});
return null;
}
}

View File

@ -0,0 +1,32 @@
package com.intellectualcrafters.plot.util.bukkit.uuid;
import com.google.common.collect.BiMap;
import com.intellectualcrafters.plot.object.BukkitOfflinePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import java.util.Map;
import java.util.UUID;
public interface UUIDHandlerImplementation {
void add(final StringWrapper name, final UUID uuid);
boolean uuidExists(final UUID uuid);
BiMap<StringWrapper, UUID> getUUIDMap();
boolean nameExists(final StringWrapper wrapper);
void handleShutdown();
void cacheWorld(String world);
void cache(final Map<StringWrapper, UUID> toAdd);
String getName(final UUID uuid);
UUID getUUID(final PlotPlayer player);
UUID getUUID(final BukkitOfflinePlayer player);
PlotPlayer getPlayer(final UUID uuid);
PlotPlayer getPlayer(String name);
UUID getUUID(String name);
void startCaching();
void setUUIDWrapper(UUIDWrapper wrapper);
UUIDWrapper getUUIDWrapper();
Map<String, PlotPlayer> getPlayers();
}

View File

@ -18,7 +18,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.StringWrapper; import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
public class LowerOfflineUUIDWrapper extends UUIDWrapper { public class LowerOfflineUUIDWrapper extends OfflineUUIDWrapper {
private Method getOnline = null; private Method getOnline = null;
private final Object[] arg = new Object[0]; private final Object[] arg = new Object[0];