From 2a52ad47029f836a5b2c8e8d11dd8696603ae4bb Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Fri, 24 Jul 2015 16:06:58 +0200 Subject: [PATCH] Huge UUID Update. --- .../intellectualcrafters/plot/BukkitMain.java | 18 +- .../com/intellectualcrafters/plot/PS.java | 9 +- .../plot/api/PlotAPI.java | 2 +- .../plot/commands/Add.java | 7 +- .../plot/commands/Buy.java | 2 +- .../plot/commands/DebugExec.java | 2 +- .../plot/commands/DebugUUID.java | 6 +- .../plot/commands/Delete.java | 2 +- .../plot/commands/Deny.java | 7 +- .../plot/commands/Remove.java | 7 +- .../plot/commands/Trust.java | 7 +- .../plot/commands/Undeny.java | 7 +- .../intellectualcrafters/plot/config/C.java | 1 + .../plot/config/Settings.java | 2 + .../plot/listeners/PlayerEvents.java | 2 +- .../plot/listeners/WorldEvents.java | 2 +- .../plot/util/MainUtil.java | 2 +- .../plot/util/bukkit/BukkitUtil.java | 6 +- .../plot/util/bukkit/UUIDHandler.java | 255 ++------ .../util/bukkit/uuid/FileUUIDHandler.java | 304 ++++++++++ .../plot/util/bukkit/uuid/SQLUUIDHandler.java | 570 ++++++++++++++++++ .../uuid/UUIDHandlerImplementation.java | 32 + .../plot/uuid/LowerOfflineUUIDWrapper.java | 2 +- 23 files changed, 1013 insertions(+), 241 deletions(-) create mode 100644 src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/FileUUIDHandler.java create mode 100644 src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/SQLUUIDHandler.java create mode 100644 src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/UUIDHandlerImplementation.java diff --git a/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index ce2c69f9f..9bba2387c 100644 --- a/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -122,7 +122,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } List worlds = Bukkit.getWorlds(); if (worlds.size() > 0) { - UUIDHandler.cacheAll(worlds.get(0).getName()); + UUIDHandler.startCaching(); for (World world : worlds) { try { SetGenCB.setGenerator(world); @@ -531,19 +531,19 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { final boolean checkVersion = checkVersion(1, 7, 6); if (Settings.OFFLINE_MODE) { if (Settings.UUID_LOWERCASE) { - UUIDHandler.uuidWrapper = new LowerOfflineUUIDWrapper(); + UUIDHandler.setUUIDWrapper(new LowerOfflineUUIDWrapper()); } else { - UUIDHandler.uuidWrapper = new OfflineUUIDWrapper(); + UUIDHandler.setUUIDWrapper(new OfflineUUIDWrapper()); } Settings.OFFLINE_MODE = true; } else if (checkVersion) { - UUIDHandler.uuidWrapper = new DefaultUUIDWrapper(); + UUIDHandler.setUUIDWrapper(new DefaultUUIDWrapper()); Settings.OFFLINE_MODE = false; } else { if (Settings.UUID_LOWERCASE) { - UUIDHandler.uuidWrapper = new LowerOfflineUUIDWrapper(); + UUIDHandler.setUUIDWrapper(new LowerOfflineUUIDWrapper()); } else { - UUIDHandler.uuidWrapper = new OfflineUUIDWrapper(); + UUIDHandler.setUUIDWrapper(new OfflineUUIDWrapper()); } Settings.OFFLINE_MODE = true; } @@ -553,9 +553,9 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { FlagManager.removeFlag(FlagManager.getFlag("titles")); } else { AbstractTitle.TITLE_CLASS = new DefaultTitle(); - if (UUIDHandler.uuidWrapper instanceof DefaultUUIDWrapper) { + if (UUIDHandler.getUUIDWrapper() instanceof DefaultUUIDWrapper) { 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; } } @@ -564,7 +564,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } else { log(C.PREFIX.s() + " &6PlotSquared is using online UUIDs"); } - return UUIDHandler.uuidWrapper; + return UUIDHandler.getUUIDWrapper(); } @Override diff --git a/src/main/java/com/intellectualcrafters/plot/PS.java b/src/main/java/com/intellectualcrafters/plot/PS.java index 3196e1b9b..d102e9ad4 100644 --- a/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/src/main/java/com/intellectualcrafters/plot/PS.java @@ -79,6 +79,8 @@ import com.intellectualcrafters.plot.util.PlayerManager; import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.TaskManager; 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; /** @@ -173,7 +175,7 @@ public class PS { IMP.registerChunkProcessor(); } // create UUIDWrapper - UUIDHandler.uuidWrapper = IMP.initUUIDHandler(); + UUIDHandler.setUUIDWrapper(IMP.initUUIDHandler()); // create event util class EventUtil.manager = IMP.initEventUtil(); // create Hybrid utility class @@ -1113,6 +1115,7 @@ public class PS { public void disable() { try { database.closeConnection(); + UUIDHandler.handleShutdown(); } catch (NullPointerException | SQLException e) { log("&cCould not close database connection!"); } @@ -1310,6 +1313,7 @@ public class PS { options.put("plotme-convert.cache-uuids", Settings.CACHE_PLOTME); // UUID + options.put("uuid.use_sqluuidhandler", Settings.USE_SQLUUIDHANDLER); options.put("UUID.offline", Settings.OFFLINE_MODE); options.put("UUID.force-lowercase", Settings.UUID_LOWERCASE); 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"); // UUID + Settings.USE_SQLUUIDHANDLER = config.getBoolean("uuid.use_sqluuidhandler"); Settings.OFFLINE_MODE = config.getBoolean("UUID.offline"); Settings.UUID_LOWERCASE = Settings.OFFLINE_MODE && config.getBoolean("UUID.force-lowercase"); Settings.UUID_FROM_DISK = config.getBoolean("uuid.read-from-disk"); @@ -1511,6 +1516,8 @@ public class PS { Settings.FANCY_CHAT = false; } Settings.METRICS = config.getBoolean("metrics"); + + UUIDHandler.implementation = Settings.USE_SQLUUIDHANDLER ? new SQLUUIDHandler() : new FileUUIDHandler(); } diff --git a/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java b/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java index 1fd47fd74..21a51e749 100644 --- a/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java +++ b/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java @@ -199,7 +199,7 @@ import com.intellectualcrafters.plot.uuid.UUIDWrapper; * @see com.intellectualcrafters.plot.uuid.UUIDWrapper */ public UUIDWrapper getUUIDWrapper() { - return UUIDHandler.uuidWrapper; + return UUIDHandler.getUUIDWrapper(); } /** diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Add.java b/src/main/java/com/intellectualcrafters/plot/commands/Add.java index 3cca1b9f2..f758fac86 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Add.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Add.java @@ -32,6 +32,7 @@ import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; +import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler; public class Add extends SubCommand { public Add() { @@ -64,7 +65,11 @@ public class Add extends SubCommand { uuid = UUIDHandler.getUUID(args[0]); } if (uuid == null) { - MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + if (UUIDHandler.implementation instanceof SQLUUIDHandler) { + MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]); + } else { + MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + } return false; } if (plot.isOwner(uuid)) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Buy.java b/src/main/java/com/intellectualcrafters/plot/commands/Buy.java index 35fffb747..caf66837f 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Buy.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Buy.java @@ -99,7 +99,7 @@ public class Buy extends SubCommand { } EconHandler.manager.withdrawMoney(plr, 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); if (owner != null) { sendMessage(plr, C.PLOT_SOLD, plot.id + "", plr.getName(), initPrice + ""); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index 84d852af5..7984de22f 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -216,7 +216,7 @@ public class DebugExec extends SubCommand { if (uuid == null) { 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)) { return MainUtil.sendMessage(player, "player hasn't connected before: " + args[1]); } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java index be7faa4a1..ee4e59d63 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java @@ -64,7 +64,7 @@ public class DebugUUID extends SubCommand { return false; } - UUIDWrapper currentUUIDWrapper = UUIDHandler.uuidWrapper; + UUIDWrapper currentUUIDWrapper = UUIDHandler.getUUIDWrapper(); UUIDWrapper newWrapper = null; switch (args[0].toLowerCase()) { @@ -109,7 +109,7 @@ public class DebugUUID extends SubCommand { } MainUtil.sendConsoleMessage("&6Beginning UUID mode conversion"); 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."); } @@ -212,7 +212,7 @@ public class DebugUUID extends SubCommand { } MainUtil.sendConsoleMessage("&7 - Replacing wrapper"); - UUIDHandler.uuidWrapper = newWrapper; + UUIDHandler.setUUIDWrapper(newWrapper); MainUtil.sendConsoleMessage("&7 - Updating plot objects"); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Delete.java b/src/main/java/com/intellectualcrafters/plot/commands/Delete.java index 1d731a78c..90f3db07b 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Delete.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Delete.java @@ -50,7 +50,7 @@ public class Delete extends SubCommand { if (!MainUtil.getTopPlot(plot).equals(MainUtil.getBottomPlot(plot))) { 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); } assert plot != null; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Deny.java b/src/main/java/com/intellectualcrafters/plot/commands/Deny.java index d8230bbbf..78e3e789f 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Deny.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Deny.java @@ -35,6 +35,7 @@ import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; +import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -69,7 +70,11 @@ public class Deny extends SubCommand { uuid = UUIDHandler.getUUID(args[0]); } if (uuid == null) { - MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + if (UUIDHandler.implementation instanceof SQLUUIDHandler) { + MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]); + } else { + MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + } return false; } if (plot.isOwner(uuid)) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Remove.java b/src/main/java/com/intellectualcrafters/plot/commands/Remove.java index 6b04b23b6..ce014296e 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Remove.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Remove.java @@ -31,6 +31,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; +import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler; public class Remove extends SubCommand { public Remove() { @@ -112,7 +113,11 @@ public class Remove extends SubCommand { } } if (count == 0) { - MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + if (UUIDHandler.implementation instanceof SQLUUIDHandler) { + MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]); + } else { + MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + } return false; } else { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Trust.java b/src/main/java/com/intellectualcrafters/plot/commands/Trust.java index 725e804bf..885d04ffa 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Trust.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Trust.java @@ -32,6 +32,7 @@ import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; +import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler; public class Trust extends SubCommand { public Trust() { @@ -64,7 +65,11 @@ public class Trust extends SubCommand { uuid = UUIDHandler.getUUID(args[0]); } if (uuid == null) { - MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + if (UUIDHandler.implementation instanceof SQLUUIDHandler) { + MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]); + } else { + MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + } return false; } if (plot.isOwner(uuid)) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java b/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java index bfa1080c2..fee72687b 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Undeny.java @@ -30,6 +30,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; +import com.intellectualcrafters.plot.util.bukkit.uuid.SQLUUIDHandler; public class Undeny extends SubCommand { public Undeny() { @@ -83,7 +84,11 @@ public class Undeny extends SubCommand { } } if (count == 0) { - MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + if (UUIDHandler.implementation instanceof SQLUUIDHandler) { + MainUtil.sendMessage(plr, C.INVALID_PLAYER_WAIT, args[0]); + } else { + MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + } return false; } else { diff --git a/src/main/java/com/intellectualcrafters/plot/config/C.java b/src/main/java/com/intellectualcrafters/plot/config/C.java index af71c0d51..92bd133c7 100644 --- a/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -282,6 +282,7 @@ public enum C { /* * 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"), // SETTINGS_PASTE_UPLOADED("$2settings.yml was uploaded to: $1%url%", "Paste"), // LATEST_LOG_UPLOADED("$2latest.log was uploaded to: $1%url%", "Paste"), diff --git a/src/main/java/com/intellectualcrafters/plot/config/Settings.java b/src/main/java/com/intellectualcrafters/plot/config/Settings.java index 75e973845..394d9f22d 100644 --- a/src/main/java/com/intellectualcrafters/plot/config/Settings.java +++ b/src/main/java/com/intellectualcrafters/plot/config/Settings.java @@ -30,6 +30,8 @@ import java.util.List; * @author Empire92 */ public class Settings { + public static boolean USE_SQLUUIDHANDLER = false; + public static boolean ENABLE_CLUSTERS = false; public static boolean FAST_CLEAR = false; /** diff --git a/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 1b1e5f77e..2657bb5df 100644 --- a/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -136,7 +136,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi int x = bloc.getBlockX(); int z = bloc.getBlockZ(); int distance = Bukkit.getViewDistance() * 16; - for (PlotPlayer player : UUIDHandler.players.values()) { + for (PlotPlayer player : UUIDHandler.getPlayers().values()) { Location loc = player.getLocation(); if (loc.getWorld().equals(world)) { if (16 * (Math.abs(loc.getX() - x) / 16) > distance) { diff --git a/src/main/java/com/intellectualcrafters/plot/listeners/WorldEvents.java b/src/main/java/com/intellectualcrafters/plot/listeners/WorldEvents.java index 43c9c694d..42a30994d 100644 --- a/src/main/java/com/intellectualcrafters/plot/listeners/WorldEvents.java +++ b/src/main/java/com/intellectualcrafters/plot/listeners/WorldEvents.java @@ -43,6 +43,6 @@ public class WorldEvents implements Listener { @EventHandler public void worldLoad(final WorldLoadEvent event) { - UUIDHandler.cacheAll(event.getWorld().getName()); + UUIDHandler.cacheWorld(event.getWorld().getName()); } } diff --git a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index f758ae334..618283948 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -72,7 +72,7 @@ public class MainUtil { public static List getPlayersInPlot(Plot plot) { ArrayList players = new ArrayList<>(); - for (PlotPlayer pp : UUIDHandler.players.values()) { + for (PlotPlayer pp : UUIDHandler.getPlayers().values()) { if (plot.equals(MainUtil.getPlot(pp.getLocation()))) { players.add(pp); } diff --git a/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java b/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java index b6d5ca987..d71515d15 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java @@ -47,7 +47,7 @@ public class BukkitUtil extends BlockManager { public static void removePlayer(final String plr) { lastPlayer = null; lastPlotPlayer = null; - UUIDHandler.players.remove(plr); + UUIDHandler.getPlayers().remove(plr); } @Override @@ -69,12 +69,12 @@ public class BukkitUtil extends BlockManager { return lastPlotPlayer; } String name = player.getName(); - PlotPlayer pp = UUIDHandler.players.get(name); + PlotPlayer pp = UUIDHandler.getPlayers().get(name); if (pp != null) { return pp; } lastPlotPlayer = new BukkitPlayer(player); - UUIDHandler.players.put(name, lastPlotPlayer); + UUIDHandler.getPlayers().put(name, lastPlotPlayer); lastPlayer = player; return lastPlotPlayer; } diff --git a/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java b/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java index 62ded816f..bb2dd5e8a 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java +++ b/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java @@ -5,9 +5,13 @@ import java.io.FileInputStream; import java.io.FilenameFilter; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Map.Entry; 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 com.google.common.collect.BiMap; @@ -32,28 +36,11 @@ import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper; import com.intellectualcrafters.plot.uuid.UUIDWrapper; public class UUIDHandler { - /** - * Map containing names and UUIDs - * - * @see com.google.common.collect.BiMap - */ - private final static BiMap uuidMap = HashBiMap.create(new HashMap()); - public static boolean CACHED = false; - public static UUIDWrapper uuidWrapper = null; - public static HashMap players = new HashMap<>(); + + public static UUIDHandlerImplementation implementation; public static void add(final StringWrapper name, final UUID uuid) { - if ((uuid == null) || (name == null)) { - return; - } - BiMap inverse = uuidMap.inverse(); - if (inverse.containsKey(uuid)) { - if (uuidMap.containsKey(name)) { - return; - } - inverse.remove(uuid); - } - uuidMap.put(name, uuid); + implementation.add(name, uuid); } /** @@ -64,7 +51,7 @@ public class UUIDHandler { * @see com.google.common.collect.BiMap */ public static BiMap getUuidMap() { - return uuidMap; + return implementation.getUUIDMap(); } /** @@ -77,7 +64,7 @@ public class UUIDHandler { * @see com.google.common.collect.BiMap#containsValue(Object) */ 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) */ public static boolean nameExists(final StringWrapper name) { - return uuidMap.containsKey(name); + return implementation.nameExists(name); } public static HashSet getAllUUIDS() { - HashSet uuids = new HashSet(); + HashSet uuids = new HashSet<>(); for (Plot plot : PS.get().getPlotsRaw()) { if (plot.owner != null) { uuids.add(plot.owner); @@ -105,212 +92,56 @@ public class UUIDHandler { } return uuids; } - - public static void cacheAll(final String world) { - if (CACHED) { - 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 toAdd = new HashMap<>(); - toAdd.put(new StringWrapper("*"), DBFunc.everyone); - if (Settings.TWIN_MODE_UUID) { - HashSet 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 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 worlds = new HashSet<>(); - worlds.add(world); - worlds.add("world"); - final HashSet uuids = new HashSet<>(); - final HashSet 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 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); - } - }); + + public static UUIDWrapper getUUIDWrapper() { + return implementation.getUUIDWrapper(); } - - public static void cache(final HashMap toAdd) { - TaskManager.runTask(new Runnable() { - @Override - public void run() { - for (Entry entry : toAdd.entrySet()) { - add(entry.getKey(), entry.getValue()); - } - PS.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs"); - } - }); + + public static void setUUIDWrapper(final UUIDWrapper wrapper) { + implementation.setUUIDWrapper(wrapper); + } + + public static void startCaching() { + implementation.startCaching(); + } + + public static void cache(final Map toAdd) { + implementation.cache(toAdd); } public static UUID getUUID(final PlotPlayer player) { - return UUIDHandler.uuidWrapper.getUUID(player); + return implementation.getUUID(player); } public static UUID getUUID(final BukkitOfflinePlayer player) { - return UUIDHandler.uuidWrapper.getUUID(player); + return implementation.getUUID(player); } public static String getName(final UUID uuid) { - if (uuid == null) { - 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; + return implementation.getName(uuid); } public static PlotPlayer getPlayer(final UUID uuid) { - for (final PlotPlayer player : players.values()) { - if (player.getUUID().equals(uuid)) { - return player; - } - } - return null; + return implementation.getPlayer(uuid); } public static PlotPlayer getPlayer(final String name) { - return players.get(name); + return implementation.getPlayer(name); } public static 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 = UUIDHandler.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 = UUIDHandler.uuidWrapper.getUUID(name); - add(new StringWrapper(name), uuid); - return uuid; - } - return null; + return implementation.getUUID(name); + } + + public static Map getPlayers() { + return implementation.getPlayers(); + } + + public static void cacheWorld(String world) { + implementation.cacheWorld(world); + } + + public static void handleShutdown() { + implementation.handleShutdown(); } } diff --git a/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/FileUUIDHandler.java b/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/FileUUIDHandler.java new file mode 100644 index 000000000..3284cb19f --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/FileUUIDHandler.java @@ -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 uuidMap = HashBiMap.create(new HashMap()); + public boolean CACHED = false; + public UUIDWrapper uuidWrapper = null; + public HashMap players = new HashMap<>(); + + @Override + public void add(final StringWrapper name, final UUID uuid) { + if ((uuid == null) || (name == null)) { + return; + } + BiMap 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 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 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 toAdd = new HashMap<>(); + toAdd.put(new StringWrapper("*"), DBFunc.everyone); + if (Settings.TWIN_MODE_UUID) { + HashSet 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 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 worlds = new HashSet<>(); + worlds.add(world); + worlds.add("world"); + final HashSet uuids = new HashSet<>(); + final HashSet 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 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 toAdd) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + for (Map.Entry 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; + } +} diff --git a/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/SQLUUIDHandler.java b/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/SQLUUIDHandler.java new file mode 100644 index 000000000..45f99ab99 --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/SQLUUIDHandler.java @@ -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 _uuidMap; + private UUIDWrapper _uuidWrapper; + private final Map _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())); + + _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 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 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 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 iterator = toFetch.iterator(); + while(iterator.hasNext()) { + StringBuilder url = new StringBuilder("http://api.intellectualsites.com/uuid/?user="); + List 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 getUUIDMap() { + BiMap map = HashBiMap.create(); + for (Map.Entry 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 toAdd = new HashMap<>(); + if (Settings.TWIN_MODE_UUID) { + Set 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 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 worlds = new HashSet<>(); + worlds.add(world); + worlds.add("world"); + final HashSet uuids = new HashSet<>(); + final HashSet 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 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 toAdd) { + for (Map.Entry 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; + } +} diff --git a/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/UUIDHandlerImplementation.java b/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/UUIDHandlerImplementation.java new file mode 100644 index 000000000..529060fd5 --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/bukkit/uuid/UUIDHandlerImplementation.java @@ -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 getUUIDMap(); + boolean nameExists(final StringWrapper wrapper); + void handleShutdown(); + void cacheWorld(String world); + void cache(final Map 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 getPlayers(); + +} diff --git a/src/main/java/com/intellectualcrafters/plot/uuid/LowerOfflineUUIDWrapper.java b/src/main/java/com/intellectualcrafters/plot/uuid/LowerOfflineUUIDWrapper.java index 1b1e212e2..5101f517a 100644 --- a/src/main/java/com/intellectualcrafters/plot/uuid/LowerOfflineUUIDWrapper.java +++ b/src/main/java/com/intellectualcrafters/plot/uuid/LowerOfflineUUIDWrapper.java @@ -18,7 +18,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.StringWrapper; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; -public class LowerOfflineUUIDWrapper extends UUIDWrapper { +public class LowerOfflineUUIDWrapper extends OfflineUUIDWrapper { private Method getOnline = null; private final Object[] arg = new Object[0];