diff --git a/pom.xml b/pom.xml index ae36eab..989c440 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 2.4.3 + 2.4.4 jar HeadDB diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index 25e42a9..5212ed5 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -8,6 +8,7 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import tsp.headdb.api.Head; import tsp.headdb.util.Log; +import tsp.headdb.util.Utils; import java.io.BufferedReader; import java.io.IOException; @@ -166,8 +167,13 @@ public class HeadDatabase { JSONArray array = (JSONArray) parser.parse(response.toString()); for (Object o : array) { JSONObject obj = (JSONObject) o; - UUID uuid = UUID.fromString(obj.get("uuid").toString()); - if (uuid.toString().length() != 36) { + String rawUUID = obj.get("uuid").toString(); + + UUID uuid; + if (Utils.validateUniqueId(rawUUID)) { + uuid = UUID.fromString(rawUUID); + } else { + Log.debug("UUID " + rawUUID + " is invalid. Using random one for head id: " + id); uuid = UUID.randomUUID(); } diff --git a/src/main/java/tsp/headdb/util/Utils.java b/src/main/java/tsp/headdb/util/Utils.java index 2c38532..a107138 100644 --- a/src/main/java/tsp/headdb/util/Utils.java +++ b/src/main/java/tsp/headdb/util/Utils.java @@ -3,8 +3,23 @@ package tsp.headdb.util; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import java.util.regex.Pattern; + public class Utils { + + public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); + + /** + * Validate a UUID (version 4) + * + * @param uuid UUID to be validated + * @return Returns true if the string is a valid UUID + */ + public static boolean validateUniqueId(String uuid) { + return UUID_PATTERN.matcher(uuid).matches(); + } + public static void sendMessage(CommandSender sender, String message) { sender.sendMessage(colorize(message)); }