Added TFM_UuidManager to cache UUIDs. Resolves #255

All methods related to UUIDs in TFM_Util have been moved to TFM_UuidManager
This commit is contained in:
JeromSar 2014-08-27 11:40:25 +02:00
parent 0c99862793
commit 9c976b381b
17 changed files with 212 additions and 100 deletions

View file

@ -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

View file

@ -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]))
{

View file

@ -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)
{

View file

@ -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) : ""));

View file

@ -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.");

View file

@ -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());

View file

@ -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 = "/<command> recalculate <admin | player>")
@CommandParameters(description = "Provides uuid tools", usage = "/<command> <purge | recalculate <admin | player>>")
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]))
{

View file

@ -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);

View file

@ -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))
{

View file

@ -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)

View file

@ -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;
}

View file

@ -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();
}

View file

@ -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)

View file

@ -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

View file

@ -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.");

View file

@ -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<String, UUID> UUID_CACHE = new HashMap<String, UUID>();
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<String>());
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<String> uuids = new ArrayList<String>();
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();
}
}

View file

@ -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();