Ensure UUID is never null for non-existing players

Currently, the player's name is SHA-1'ed and the first 32 hexadecimal
characters are formed into a 'spoofed UUID'. I think this is the best solution
This commit is contained in:
unknown 2014-06-29 16:39:25 +02:00
parent 8b4b2f97e0
commit aa062a9d11
4 changed files with 47 additions and 4 deletions

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Sun Jun 29 12:03:50 CEST 2014
build.number=904
#Sun Jun 29 16:33:24 CEST 2014
build.number=909

View File

@ -45,6 +45,11 @@ public class TFM_Player
protected TFM_Player(UUID uuid)
{
if (uuid == null)
{
throw new IllegalArgumentException("UUID can not be null!");
}
this.uuid = uuid;
this.ips = new ArrayList<String>();
}

View File

@ -59,7 +59,7 @@ public class TFM_PlayerData
private TFM_PlayerData(Player player)
{
this.player = player;
this.uuid = TFM_UuidResolver.getUUIDOf(player.getName());
this.uuid = TFM_Util.getUuid(player.getName());
this.ip = player.getAddress().getAddress().getHostAddress();
}

View File

@ -6,6 +6,8 @@ import java.lang.reflect.Field;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@ -121,7 +123,43 @@ public class TFM_Util
public static UUID getUuid(String offlineplayer)
{
return TFM_UuidResolver.getUUIDOf(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)