diff --git a/src/main/java/com/intellectualcrafters/plot/PS.java b/src/main/java/com/intellectualcrafters/plot/PS.java index 13b0812da..9c19c43f0 100644 --- a/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/src/main/java/com/intellectualcrafters/plot/PS.java @@ -193,7 +193,18 @@ public class PS { @Override public void run() { PS.debug("Starting UUID caching"); - UUIDHandler.startCaching(null); + UUIDHandler.startCaching(new Runnable() { + @Override + public void run() { + for (Plot plot : getPlots()) { + if (plot.owner != null && plot.temp != -1) { + if (UUIDHandler.getName(plot.owner) == null) { + UUIDHandler.implementation.unknown.add(plot.owner); + } + } + } + } + }); } }, 20); // create event util class diff --git a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index 88520a849..ea48bc90e 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -330,6 +330,15 @@ public interface AbstractDB { void resizeCluster(PlotCluster current, PlotClusterId resize); void movePlot(Plot originalPlot, Plot newPlot); + + /** + * Replace a old uuid with a new one in the database
+ * - Useful for replacing a few uuids (not the entire database)
+ * - For entire conversion, the uuidconvert command scales better + * @param old + * @param now + */ + void replaceUUID(UUID old, UUID now); /** * Don't fuck with this one, unless you enjoy it rough diff --git a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index fb0605f7e..1768a026e 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -420,6 +420,15 @@ public class DBFunc { public static void setPosition(final PlotCluster cluster, final String position) { dbManager.setPosition(cluster, position); } + + /** + * Replace all occurances of a uuid in the database with another one + * @param old + * @param now + */ + public static void replaceUUID(UUID old, UUID now) { + dbManager.replaceUUID(old, now); + } public static void close() { dbManager.close(); diff --git a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index 2305b9076..c4bf9192a 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -2572,6 +2572,28 @@ public class SQLManager implements AbstractDB { } commit(); } + + @Override + public void replaceUUID(final UUID old, final UUID now) { + addGlobalTask(new Runnable() { + @Override + public void run() { + try (Statement stmt = connection.createStatement()) { + stmt.executeUpdate("UPDATE `" + prefix + "cluster` SET `owner` = '" + now.toString() + "' WHERE `owner` = '" + old.toString() + "'"); + stmt.executeUpdate("UPDATE `" + prefix + "cluster_helpers` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'"); + stmt.executeUpdate("UPDATE `" + prefix + "cluster_invited` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'"); + stmt.executeUpdate("UPDATE `" + prefix + "plot` SET `owner` = '" + now.toString() + "' WHERE `owner` = '" + old.toString() + "'"); + stmt.executeUpdate("UPDATE `" + prefix + "plot_denied` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'"); + stmt.executeUpdate("UPDATE `" + prefix + "plot_helpers` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'"); + stmt.executeUpdate("UPDATE `" + prefix + "plot_trusted` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'"); + stmt.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + }); + } @Override public void close() { diff --git a/src/main/java/com/intellectualcrafters/plot/util/UUIDHandlerImplementation.java b/src/main/java/com/intellectualcrafters/plot/util/UUIDHandlerImplementation.java index 70986f50c..3deb4f649 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/UUIDHandlerImplementation.java +++ b/src/main/java/com/intellectualcrafters/plot/util/UUIDHandlerImplementation.java @@ -1,15 +1,20 @@ package com.intellectualcrafters.plot.util; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.UUID; +import com.google.common.base.Charsets; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; 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.OfflinePlotPlayer; +import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.object.StringWrapper; @@ -62,36 +67,82 @@ public abstract class UUIDHandlerImplementation { if (uuidMap.size() == 0) { uuidMap = toAdd; } - TaskManager.runTask(new Runnable() { - @Override - public void run() { - for (Map.Entry entry : toAdd.entrySet()) { - UUID uuid = entry.getValue(); - StringWrapper name = entry.getKey(); - if ((uuid == null) || (name == null)) { - continue; - } - BiMap inverse = uuidMap.inverse(); - if (inverse.containsKey(uuid)) { - if (uuidMap.containsKey(name)) { - continue; - } - rename(uuid, name); - continue; - } - uuidMap.put(name, uuid); - } - PS.debug(C.PREFIX.s() + "&6Cached a total of: " + uuidMap.size() + " UUIDs"); + for (Map.Entry entry : toAdd.entrySet()) { + UUID uuid = entry.getValue(); + StringWrapper name = entry.getKey(); + if ((uuid == null) || (name == null)) { + continue; } - }); + BiMap inverse = uuidMap.inverse(); + if (inverse.containsKey(uuid)) { + if (uuidMap.containsKey(name)) { + continue; + } + rename(uuid, name); + continue; + } + uuidMap.put(name, uuid); + } + PS.debug(C.PREFIX.s() + "&6Cached a total of: " + uuidMap.size() + " UUIDs"); } + public HashSet unknown = new HashSet<>(); + public boolean add(final StringWrapper name, final UUID uuid) { - if ((uuid == null) || (name == null)) { + if ((uuid == null)) { return false; } + if (name == null) { + try { + unknown.add(uuid); + } + catch (Exception e) { + PS.log("&c(minor) Invalid UUID mapping: " + uuid); + e.printStackTrace(); + } + return false; + } + + /* + * lazy UUID conversion: + * - Useful if the person misconfigured the database, or settings before PlotMe conversion + */ + if (!Settings.OFFLINE_MODE) { + UUID offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8)); + if (!unknown.contains(offline) && !name.value.equals(name.value.toLowerCase())){ + offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8)); + if (!unknown.contains(offline)) { + offline = null; + } + } + if (offline != null) { + unknown.remove(offline); + Set plots = PS.get().getPlots(offline); + if (plots.size() > 0) { + for (Plot plot : PS.get().getPlots(offline)) { + plot.owner = uuid; + } + DBFunc.replaceUUID(offline, uuid); + PS.debug("&cDetected invalid UUID stored for: " + name.value); + PS.debug("&7 - Did you recently switch to online-mode storage without running `uuidconvert`?"); + PS.debug("&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database."); + } + } + } try { - uuidMap.put(name, uuid); + UUID offline = uuidMap.put(name, uuid); + if (offline != null && !offline.equals(uuid)) { + Set plots = PS.get().getPlots(offline); + if (plots.size() > 0) { + for (Plot plot : PS.get().getPlots(offline)) { + plot.owner = uuid; + } + DBFunc.replaceUUID(offline, uuid); + PS.debug("&cDetected invalid UUID stored for (1): " + name.value); + PS.debug("&7 - Did you recently switch to online-mode storage without running `uuidconvert`?"); + PS.debug("&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database."); + } + } } catch (Exception e) { BiMap inverse = uuidMap.inverse(); diff --git a/target/PlotSquared-Bukkit.jar b/target/PlotSquared-Bukkit.jar index d9a41481e..b0e1b072c 100644 Binary files a/target/PlotSquared-Bukkit.jar and b/target/PlotSquared-Bukkit.jar differ diff --git a/target/PlotSquared-Sponge.jar b/target/PlotSquared-Sponge.jar index 2ba5f269f..6356c96ff 100644 Binary files a/target/PlotSquared-Sponge.jar and b/target/PlotSquared-Sponge.jar differ