From 9c976b381b69e23c74f820782ff65174f51e0e88 Mon Sep 17 00:00:00 2001 From: JeromSar Date: Wed, 27 Aug 2014 11:40:25 +0200 Subject: [PATCH] Added TFM_UuidManager to cache UUIDs. Resolves #255 All methods related to UUIDs in TFM_Util have been moved to TFM_UuidManager --- buildnumber.properties | 4 +- .../Commands/Command_gadmin.java | 5 +- .../Commands/Command_glist.java | 11 +- .../Commands/Command_gtfo.java | 3 +- .../Commands/Command_tban.java | 3 +- .../Commands/Command_tempban.java | 3 +- .../Commands/Command_uuid.java | 17 +- .../Listener/TFM_PlayerListener.java | 2 +- .../TotalFreedomMod/TFM_AdminList.java | 16 +- .../TotalFreedomMod/TFM_BanManager.java | 2 +- .../TotalFreedomMod/TFM_FrontDoor.java | 2 +- .../TotalFreedomMod/TFM_PlayerData.java | 2 +- .../TotalFreedomMod/TFM_PlayerList.java | 6 +- .../TotalFreedomMod/TFM_ServerInterface.java | 2 +- .../TotalFreedomMod/TFM_Util.java | 75 +-------- .../TotalFreedomMod/TFM_UuidManager.java | 158 ++++++++++++++++++ .../TotalFreedomMod/TotalFreedomMod.java | 1 + 17 files changed, 212 insertions(+), 100 deletions(-) create mode 100644 src/me/StevenLawson/TotalFreedomMod/TFM_UuidManager.java diff --git a/buildnumber.properties b/buildnumber.properties index 71f709d..52495b8 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Wed Aug 27 10:56:44 CEST 2014 -build.number=943 +#Wed Aug 27 11:36:20 CEST 2014 +build.number=946 diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gadmin.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gadmin.java index c2822c7..83718aa 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gadmin.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gadmin.java @@ -5,6 +5,7 @@ import java.util.Iterator; import me.StevenLawson.TotalFreedomMod.TFM_BanManager; import me.StevenLawson.TotalFreedomMod.TFM_PlayerData; import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TFM_UuidManager; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -76,7 +77,7 @@ public class Command_gadmin extends TFM_Command while (it.hasNext()) { final Player player = it.next(); - final String hash = TFM_Util.getUniqueId(player).toString().substring(0, 4); + final String hash = TFM_UuidManager.getUniqueId(player).toString().substring(0, 4); sender.sendMessage(ChatColor.GRAY + String.format("[ %s ] : [ %s ] - %s", player.getName(), ChatColor.stripColor(player.getDisplayName()), @@ -94,7 +95,7 @@ public class Command_gadmin extends TFM_Command while (it.hasNext() && target == null) { final Player player = it.next(); - final String hash = TFM_Util.getUniqueId(player).toString().substring(0, 4); + final String hash = TFM_UuidManager.getUniqueId(player).toString().substring(0, 4); if (hash.equalsIgnoreCase(args[1])) { diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_glist.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_glist.java index 239227f..f6c2729 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_glist.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_glist.java @@ -8,6 +8,7 @@ import me.StevenLawson.TotalFreedomMod.TFM_BanManager; import me.StevenLawson.TotalFreedomMod.TFM_Player; import me.StevenLawson.TotalFreedomMod.TFM_PlayerList; import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TFM_UuidManager; import net.minecraft.util.org.apache.commons.lang3.StringUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -54,7 +55,7 @@ public class Command_glist extends TFM_Command if (player == null) { - final TFM_Player entry = TFM_PlayerList.getEntry(TFM_Util.getUniqueId(args[1])); + final TFM_Player entry = TFM_PlayerList.getEntry(TFM_UuidManager.getUniqueId(args[1])); if (entry == null) { @@ -68,7 +69,7 @@ public class Command_glist extends TFM_Command else { username = player.getName(); - final TFM_Player entry = TFM_PlayerList.getEntry(TFM_Util.getUniqueId(player)); + final TFM_Player entry = TFM_PlayerList.getEntry(TFM_UuidManager.getUniqueId(player)); ips.addAll(entry.getIps()); } @@ -80,12 +81,12 @@ public class Command_glist extends TFM_Command final Player target = getPlayer(username, true); if (target != null) { - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(target), target.getName())); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(target), target.getName())); target.kickPlayer("You have been banned by " + sender.getName() + "\n If you think you have been banned wrongly, appeal here: http://www.totalfreedom.boards.net"); } else { - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(username), username)); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(username), username)); } for (String ip : ips) @@ -97,7 +98,7 @@ public class Command_glist extends TFM_Command else if (mode.equalsIgnoreCase("unban")) { TFM_Util.adminAction(sender.getName(), "Unbanning " + username + " and IPs: " + StringUtils.join(ips, ", "), true); - TFM_BanManager.unbanUuid(TFM_Util.getUniqueId(username)); + TFM_BanManager.unbanUuid(TFM_UuidManager.getUniqueId(username)); for (String ip : ips) { diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gtfo.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gtfo.java index df0559d..1b363b7 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gtfo.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_gtfo.java @@ -5,6 +5,7 @@ import me.StevenLawson.TotalFreedomMod.TFM_Ban; import me.StevenLawson.TotalFreedomMod.TFM_BanManager; import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager; import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TFM_UuidManager; import net.minecraft.util.org.apache.commons.lang3.ArrayUtils; import net.minecraft.util.org.apache.commons.lang3.StringUtils; import org.bukkit.ChatColor; @@ -94,7 +95,7 @@ public class Command_gtfo extends TFM_Command TFM_BanManager.addIpBan(new TFM_Ban(ip, player.getName(), sender.getName(), null, reason)); // ban username: - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), sender.getName(), null, reason)); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), sender.getName(), null, reason)); // kick Player: player.kickPlayer(ChatColor.RED + "GTFO" + (reason != null ? ("\nReason: " + ChatColor.YELLOW + reason) : "")); diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tban.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tban.java index 4d8ef6b..907a79a 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tban.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tban.java @@ -3,6 +3,7 @@ package me.StevenLawson.TotalFreedomMod.Commands; import me.StevenLawson.TotalFreedomMod.TFM_Ban; import me.StevenLawson.TotalFreedomMod.TFM_BanManager; import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TFM_UuidManager; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.command.Command; @@ -42,7 +43,7 @@ public class Command_tban extends TFM_Command TFM_Util.adminAction(sender.getName(), "Tempbanning: " + player.getName() + " for 5 minutes.", true); TFM_BanManager.addUuidBan( - new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), sender.getName(), TFM_Util.parseDateOffset("5m"), ChatColor.RED + "You have been temporarily banned for 5 minutes.")); + new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), sender.getName(), TFM_Util.parseDateOffset("5m"), ChatColor.RED + "You have been temporarily banned for 5 minutes.")); player.kickPlayer(ChatColor.RED + "You have been temporarily banned for five minutes. Please read totalfreedom.me for more info."); diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java index d6ce336..6e02cc3 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java @@ -5,6 +5,7 @@ import java.util.Date; import me.StevenLawson.TotalFreedomMod.TFM_Ban; import me.StevenLawson.TotalFreedomMod.TFM_BanManager; import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TFM_UuidManager; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import net.minecraft.util.org.apache.commons.lang3.ArrayUtils; import net.minecraft.util.org.apache.commons.lang3.StringUtils; @@ -69,7 +70,7 @@ public class Command_tempban extends TFM_Command TFM_Util.adminAction(sender.getName(), message.toString(), true); TFM_BanManager.addIpBan(new TFM_Ban(TFM_Util.getIp(player), player.getName(), sender.getName(), expires, reason)); - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), sender.getName(), expires, reason)); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), sender.getName(), expires, reason)); player.kickPlayer(sender.getName() + " - " + message.toString()); diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_uuid.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_uuid.java index 34528c0..6a39a26 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_uuid.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_uuid.java @@ -9,23 +9,36 @@ import me.StevenLawson.TotalFreedomMod.TFM_Admin; import me.StevenLawson.TotalFreedomMod.TFM_AdminList; import me.StevenLawson.TotalFreedomMod.TFM_Player; import me.StevenLawson.TotalFreedomMod.TFM_PlayerList; +import me.StevenLawson.TotalFreedomMod.TFM_UuidManager; import me.StevenLawson.TotalFreedomMod.TFM_UuidResolver; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @CommandPermissions(level = AdminLevel.SENIOR, source = SourceType.ONLY_CONSOLE) -@CommandParameters(description = "Provides uuid tools", usage = "/ recalculate ") +@CommandParameters(description = "Provides uuid tools", usage = "/ >") public class Command_uuid extends TFM_Command { @Override public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (args.length < 2) + if (args.length == 0 || args.length > 2) { return false; } + if (args.length == 1) + { + + if (!"purge".equals(args[0])) + { + return false; + } + + playerMsg("Purged " + TFM_UuidManager.purge() + " cached UUIDs."); + return true; + } + if ("recalculate".equals(args[0])) { diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index 4d3e8ea..a19e702 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -799,7 +799,7 @@ public class TFM_PlayerListener implements Listener TFM_BanManager.unbanIp(TFM_Util.getFuzzyIp(storedIp)); } - TFM_BanManager.unbanUuid(TFM_Util.getUniqueId(player)); + TFM_BanManager.unbanUuid(TFM_UuidManager.getUniqueId(player)); player.setOp(true); diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java b/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java index 162d95c..3fc0abf 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java @@ -173,7 +173,7 @@ public class TFM_AdminList for (String uuidString : section.getKeys(false)) { - if (!TFM_Util.isUniqueId(uuidString)) + if (!TFM_UuidManager.isUniqueId(uuidString)) { TFM_Log.warning("Invalid Unique ID: " + uuidString + " in superadmin.yml, ignoring"); continue; @@ -245,7 +245,7 @@ public class TFM_AdminList for (String admin : config.getConfigurationSection("superadmins").getKeys(false)) { - final UUID uuid = TFM_Util.getUniqueId(admin); + final UUID uuid = TFM_UuidManager.getUniqueId(admin); if (uuid == null) { @@ -327,7 +327,7 @@ public class TFM_AdminList public static TFM_Admin getEntry(Player player) { - return getEntry(TFM_Util.getUniqueId(player)); + return getEntry(TFM_UuidManager.getUniqueId(player)); } public static TFM_Admin getEntry(UUID uuid) @@ -433,7 +433,7 @@ public class TFM_AdminList final Player player = (Player) sender; - if (Bukkit.getOnlineMode() && superUUIDs.contains(TFM_Util.getUniqueId(player))) + if (Bukkit.getOnlineMode() && superUUIDs.contains(TFM_UuidManager.getUniqueId(player))) { return true; } @@ -488,7 +488,7 @@ public class TFM_AdminList return false; } - return entry.getUniqueId().equals(TFM_Util.getUniqueId(player)); + return entry.getUniqueId().equals(TFM_UuidManager.getUniqueId(player)); } @Deprecated @@ -544,7 +544,7 @@ public class TFM_AdminList public static boolean isAdminImpostor(Player player) { - if (superUUIDs.contains(TFM_Util.getUniqueId(player))) + if (superUUIDs.contains(TFM_UuidManager.getUniqueId(player))) { return !isSuperAdmin(player); } @@ -554,7 +554,7 @@ public class TFM_AdminList public static void addSuperadmin(OfflinePlayer player) { - final UUID uuid = TFM_Util.getUniqueId(player); + final UUID uuid = TFM_UuidManager.getUniqueId(player); final String ip = TFM_Util.getIp(player); if (adminList.containsKey(uuid)) @@ -597,7 +597,7 @@ public class TFM_AdminList public static void removeSuperadmin(OfflinePlayer player) { - final UUID uuid = TFM_Util.getUniqueId(player); + final UUID uuid = TFM_UuidManager.getUniqueId(player); if (!adminList.containsKey(uuid)) { diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_BanManager.java b/src/me/StevenLawson/TotalFreedomMod/TFM_BanManager.java index 0d9c5a0..34ed65c 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_BanManager.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_BanManager.java @@ -207,7 +207,7 @@ public class TFM_BanManager public static void addUuidBan(Player player) { - addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(player), player.getName())); + addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName())); } public static void addUuidBan(TFM_Ban ban) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java b/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java index 9a36805..1251ec4 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java @@ -210,7 +210,7 @@ public class TFM_FrontDoor } TFM_BanManager.addUuidBan( - new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), "FrontDoor", null, ChatColor.RED + "WOOPS\n-Frontdoor")); + new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), "FrontDoor", null, ChatColor.RED + "WOOPS\n-Frontdoor")); break; } diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java b/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java index a4f44b1..50100ea 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java @@ -60,7 +60,7 @@ public class TFM_PlayerData private TFM_PlayerData(Player player) { this.player = player; - this.uuid = TFM_Util.getUniqueId(player.getName()); + this.uuid = TFM_UuidManager.getUniqueId(player.getName()); this.ip = player.getAddress().getAddress().getHostAddress(); } diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerList.java b/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerList.java index 80146a0..1b2a964 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerList.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerList.java @@ -79,7 +79,7 @@ public class TFM_PlayerList public static TFM_Player getEntry(Player player) { - final UUID uuid = TFM_Util.getUniqueId(player); + final UUID uuid = TFM_UuidManager.getUniqueId(player); TFM_Player entry = getEntry(uuid); if (entry != null) @@ -103,7 +103,7 @@ public class TFM_PlayerList public static void removeEntry(Player player) { - final UUID uuid = TFM_Util.getUniqueId(player); + final UUID uuid = TFM_UuidManager.getUniqueId(player); if (!PLAYER_LIST.containsKey(uuid)) { @@ -117,7 +117,7 @@ public class TFM_PlayerList public static boolean existsEntry(Player player) { - return existsEntry(TFM_Util.getUniqueId(player)); + return existsEntry(TFM_UuidManager.getUniqueId(player)); } public static boolean existsEntry(UUID uuid) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_ServerInterface.java b/src/me/StevenLawson/TotalFreedomMod/TFM_ServerInterface.java index 26a322c..10b019a 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_ServerInterface.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_ServerInterface.java @@ -66,7 +66,7 @@ public class TFM_ServerInterface final Server server = TotalFreedomMod.server; final Player player = event.getPlayer(); final String username = player.getName(); - final UUID uuid = TFM_Util.getUniqueId(username); + final UUID uuid = TFM_UuidManager.getUniqueId(username); final String ip = event.getAddress().getHostAddress().trim(); // Perform username checks diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java index 8facbf2..cae0a2a 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java @@ -117,71 +117,6 @@ public class TFM_Util throw new AssertionError(); } - public static boolean isUniqueId(String uuid) - { - try - { - UUID.fromString(uuid); - } - catch (IllegalArgumentException ex) - { - return false; - } - - return true; - } - - public static UUID getUniqueId(OfflinePlayer offlinePlayer) - { - if (offlinePlayer instanceof Player) - { - return TFM_PlayerData.getPlayerData((Player) offlinePlayer).getUniqueId(); - } - - return getUniqueId(offlinePlayer.getName()); - } - - public static UUID getUniqueId(String offlineplayer) - { - final UUID uuid = TFM_UuidResolver.getUUIDOf(offlineplayer); - - if (uuid == null) - { - return generateUuidForName(offlineplayer); - } - - return uuid; - } - - public static UUID generateUuidForName(String name) - { - TFM_Log.info("Generating spoof UUID for " + name); - name = name.toLowerCase(); - try - { - final MessageDigest mDigest = MessageDigest.getInstance("SHA1"); - byte[] result = mDigest.digest(name.getBytes()); - final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < result.length; i++) - { - sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1)); - } - - return UUID.fromString( - sb.substring(0, 8) - + "-" + sb.substring(8, 12) - + "-" + sb.substring(12, 16) - + "-" + sb.substring(16, 20) - + "-" + sb.substring(20, 32)); - } - catch (NoSuchAlgorithmException ex) - { - TFM_Log.severe(ex); - } - - return UUID.randomUUID(); - } - public static void bcastMsg(String message, ChatColor color) { TFM_Log.info(message, true); @@ -221,7 +156,7 @@ public class TFM_Util return player.getPlayer().getAddress().getAddress().getHostAddress().trim(); } - final UUID uuid = getUniqueId(player); + final UUID uuid = TFM_UuidManager.getUniqueId(player); final TFM_Player entry = TFM_PlayerList.getEntry(uuid); @@ -244,7 +179,7 @@ public class TFM_Util public static String formatPlayer(OfflinePlayer player) { - return player.getName() + " (" + TFM_Util.getUniqueId(player) + ")"; + return player.getName() + " (" + TFM_UuidManager.getUniqueId(player) + ")"; } /** @@ -546,7 +481,7 @@ public class TFM_Util TFM_Util.bcastMsg(ChatColor.RED + player.getName() + " has been banned for 1 minute."); TFM_BanManager.addIpBan(new TFM_Ban(ip, player.getName(), "AutoEject", expires, kickMessage)); - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), "AutoEject", expires, kickMessage)); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), "AutoEject", expires, kickMessage)); player.kickPlayer(kickMessage); break; @@ -560,7 +495,7 @@ public class TFM_Util TFM_Util.bcastMsg(ChatColor.RED + player.getName() + " has been banned for 3 minutes."); TFM_BanManager.addIpBan(new TFM_Ban(ip, player.getName(), "AutoEject", expires, kickMessage)); - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), "AutoEject", expires, kickMessage)); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), "AutoEject", expires, kickMessage)); player.kickPlayer(kickMessage); break; } @@ -570,7 +505,7 @@ public class TFM_Util TFM_BanManager.addIpBan(new TFM_Ban(ip, player.getName(), "AutoEject", null, kickMessage)); TFM_BanManager.addIpBan(new TFM_Ban(ipAddressParts[0] + "." + ipAddressParts[1] + ".*.*", player.getName(), "AutoEject", null, kickMessage)); - TFM_BanManager.addUuidBan(new TFM_Ban(TFM_Util.getUniqueId(player), player.getName(), "AutoEject", null, kickMessage)); + TFM_BanManager.addUuidBan(new TFM_Ban(TFM_UuidManager.getUniqueId(player), player.getName(), "AutoEject", null, kickMessage)); TFM_Util.bcastMsg(ChatColor.RED + player.getName() + " has been banned."); diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_UuidManager.java b/src/me/StevenLawson/TotalFreedomMod/TFM_UuidManager.java new file mode 100644 index 0000000..5f1435c --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_UuidManager.java @@ -0,0 +1,158 @@ +package me.StevenLawson.TotalFreedomMod; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import me.StevenLawson.TotalFreedomMod.Config.TFM_Config; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +public class TFM_UuidManager +{ + private static final Map UUID_CACHE = new HashMap(); + + private TFM_UuidManager() + { + throw new AssertionError(); + } + + public static void load() + { + UUID_CACHE.clear(); + + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, "uuid.yml", false); + config.load(); + + if (!config.isList("cache")) + { + config.set("cache", new ArrayList()); + config.save(); + return; + } + + for (String cache : config.getStringList("cache")) + { + final String[] parts = cache.split(":"); + if (parts.length != 2) + { + TFM_Log.warning("Invalid cached UUID: " + cache); + continue; + } + + final String playerName = parts[0]; + final String uuidString = parts[1]; + + if (!isUniqueId(uuidString)) + { + TFM_Log.warning("Invalid cached UUID: " + cache); + continue; + } + + UUID_CACHE.put(playerName.toLowerCase(), UUID.fromString(uuidString)); + } + + TFM_Log.info("Cached " + UUID_CACHE.size() + " UUIDs"); + } + + public static void save() + { + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, "uuid.yml", false); + config.load(); + + final List uuids = new ArrayList(); + + for (String playerName : UUID_CACHE.keySet()) + { + uuids.add(playerName + ":" + UUID_CACHE.get(playerName)); + } + + config.set("cache", uuids); + config.save(); + } + + public static int purge() + { + final int size = UUID_CACHE.size(); + UUID_CACHE.clear(); + save(); + return size; + } + + public static boolean isUniqueId(String uuid) + { + try + { + UUID.fromString(uuid); + } + catch (IllegalArgumentException ex) + { + return false; + } + + return true; + } + + public static UUID getUniqueId(OfflinePlayer offlinePlayer) + { + if (offlinePlayer.isOnline()) + { + return TFM_PlayerData.getPlayerData(offlinePlayer.getPlayer()).getUniqueId(); + } + + return getUniqueId(offlinePlayer.getName()); + } + + public static UUID getUniqueId(String playerName) + { + if (UUID_CACHE.containsKey(playerName.toLowerCase())) + { + return UUID_CACHE.get(playerName.toLowerCase()); + } + + UUID uuid = TFM_UuidResolver.getUUIDOf(playerName); + + if (uuid == null) + { + uuid = generateSpoofUuid(playerName); + } + + UUID_CACHE.put(playerName, uuid); + + save(); + + return uuid; + } + + private static UUID generateSpoofUuid(String name) + { + TFM_Log.info("Generating spoof UUID for " + name); + name = name.toLowerCase(); + try + { + final MessageDigest digest = MessageDigest.getInstance("SHA1"); + final byte[] result = digest.digest(name.getBytes()); + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < result.length; i++) + { + builder.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1)); + } + + return UUID.fromString( + "deadbeef" + + "-" + builder.substring(8, 12) + + "-" + builder.substring(12, 16) + + "-" + builder.substring(16, 20) + + "-" + builder.substring(20, 32)); + } + catch (NoSuchAlgorithmException ex) + { + TFM_Log.severe(ex); + } + + return UUID.randomUUID(); + } +} diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 5b2b6ca..c57d85a 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -90,6 +90,7 @@ public class TotalFreedomMod extends JavaPlugin TFM_Util.createBackups(PERMBAN_FILENAME); // Load services + TFM_UuidManager.load(); TFM_AdminList.load(); TFM_PermbanList.load(); TFM_PlayerList.load();