From 0243f423f69d00280510c6d44eaea6fac5a880b2 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sat, 2 May 2015 20:08:30 +1000 Subject: [PATCH] Optimized UUID caching --- .../intellectualcrafters/plot/BukkitMain.java | 21 ++++--- .../plot/config/Settings.java | 1 + .../plot/util/bukkit/UUIDHandler.java | 56 ++++++++++++++++++- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index a76bd8301..779511ed9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -445,13 +445,6 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { @Override public UUIDWrapper initUUIDHandler() { final boolean checkVersion = checkVersion(1, 7, 6); - if (!checkVersion) { - log(C.PREFIX.s() + " &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature."); - Settings.TITLES = false; - FlagManager.removeFlag(FlagManager.getFlag("titles")); - } else { - AbstractTitle.TITLE_CLASS = new DefaultTitle(); - } if (Settings.OFFLINE_MODE) { if (Settings.UUID_LOWERCASE) { UUIDHandler.uuidWrapper = new LowerOfflineUUIDWrapper(); @@ -472,6 +465,20 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } Settings.OFFLINE_MODE = true; } + if (!checkVersion) { + log(C.PREFIX.s() + " &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature."); + Settings.TITLES = false; + FlagManager.removeFlag(FlagManager.getFlag("titles")); + } else { + AbstractTitle.TITLE_CLASS = new DefaultTitle(); + + if (UUIDHandler.uuidWrapper instanceof DefaultUUIDWrapper) { + Settings.TWIN_MODE_UUID = true; + } + else if (UUIDHandler.uuidWrapper instanceof OfflineUUIDWrapper && !Bukkit.getOnlineMode()) { + Settings.TWIN_MODE_UUID = true; + } + } if (Settings.OFFLINE_MODE) { log(C.PREFIX.s() + " &6PlotSquared is using Offline Mode UUIDs either because of user preference, or because you are using an old version of Bukkit"); } else { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java index e12d5a8e4..079ff51ea 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java @@ -149,6 +149,7 @@ public class Settings { /** * Use offline mode storage */ + public static boolean TWIN_MODE_UUID = false; public static boolean OFFLINE_MODE = false; public static boolean UUID_LOWERCASE = false; /** diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java index 3c8872251..47496872f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/UUIDHandler.java @@ -6,6 +6,9 @@ import java.util.HashMap; import java.util.HashSet; import java.util.UUID; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.intellectualcrafters.plot.PlotSquared; @@ -14,6 +17,7 @@ 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.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.StringWrapper; import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper; @@ -82,15 +86,63 @@ public class UUIDHandler { public static boolean nameExists(final StringWrapper name) { return uuidMap.containsKey(name); } + + public static HashSet getAllUUIDS() { + HashSet uuids = new HashSet(); + for (Plot plot : PlotSquared.getPlotsRaw()) { + for (UUID uuid : plot.helpers) { + uuids.add(uuid); + } + for (UUID uuid : plot.trusted) { + uuids.add(uuid); + } + for (UUID uuid : plot.denied) { + uuids.add(uuid); + } + if (plot.owner != null) { + uuids.add(plot.owner); + } + } + return uuids; + } public static void cacheAll(final String world) { if (CACHED) { return; } PlotSquared.log(C.PREFIX.s() + "&6Starting player data caching"); + long start = System.currentTimeMillis(); UUIDHandler.CACHED = true; - // OLD UUID CACHING SYSTEM + if (Settings.TWIN_MODE_UUID) { + HashSet all = getAllUUIDS(); + final File playerdataFolder = new File(world + 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) { + for (final String current : dat) { + final String s = current.replaceAll(".dat$", ""); + try { + final UUID uuid = UUID.fromString(s); + if (all.contains(uuid)) { + OfflinePlayer op = Bukkit.getOfflinePlayer(uuid); + add(new StringWrapper(op.getName()), uuid); + } + } catch (final Exception e) { + PlotSquared.log(C.PREFIX.s() + "Invalid playerdata: " + current); + } + } + } + PlotSquared.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs"); + System.out.print(System.currentTimeMillis() - start); + System.out.print(Settings.TWIN_MODE_UUID); + return; + } + final HashSet worlds = new HashSet<>(); worlds.add(world); worlds.add("world"); @@ -161,6 +213,8 @@ public class UUIDHandler { // add the Everyone '*' UUID add(new StringWrapper("*"), DBFunc.everyone); PlotSquared.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs"); + System.out.print(System.currentTimeMillis() - start); + System.out.print(Settings.TWIN_MODE_UUID); } public static UUID getUUID(final PlotPlayer player) {