Resource UUID Util - Easy gets UUID of Offline Players

Discussion in 'Spigot Plugin Development' started by ClashiCrafter, Mar 29, 2020.

  1. This is a nice Util, when you wan't to get the UUID of an offline Player.

    This works with the official mojang player API


    Class:
    Code (Java):
    package de.trainmc.coinsystem.utils;

    import java.net.URL;
    import java.util.Date;
    import java.util.Scanner;
    import java.util.UUID;

    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.JSONValue;

    public class UUIDUtil {
       
        private UUIDUtil() {}

        private static UUID getUUIDTime(String name, long time) throws Exception {
            Scanner scanner;
            if(time == -1) {
                scanner = new Scanner(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream());
            } else {
                scanner = new Scanner(new URL("https://api.mojang.com/users/profiles/minecraft/" + name + "?at=" + String.valueOf(time)).openStream());
            }

            String input = scanner.nextLine();
            scanner.close();

            JSONObject UUIDObject = (JSONObject) JSONValue.parseWithException(input);
            String uuidString = UUIDObject.get("id").toString();
            String uuidSeperation = uuidString.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5");
            UUID uuid = UUID.fromString(uuidSeperation);
            return uuid;
        }

        public static UUID getUUID(String name) throws Exception {
            return getUUIDTime(name, -1);
        }
       
    }
    Now you can easy get the UUID of an Player: UUIDUtil.getUUID(NAME);

    Credits: Cuuky
     
  2. Can't this API only be accessed so many times per second? I feel like i saw that somewhere.
     
  3. Choco

    Moderator

    Is the UUID of an offline player not obtainable using Bukkit#getOfflinePlayer() and calling OfflinePlayer#getUniqueId()? I'd say the only difference between that and this resource is that this one is done asynchronously, but it's not ;P
     
    • Agree Agree x 1
  4. Seems useful, though I made some improvements and minimized the code.
    Code (Text):
    import java.io.IOException;
    import java.net.URL;
    import java.util.Scanner;
    import java.util.UUID;

    public class UUIDUtil {
        public UUID getUUID(String name) throws IOException {
            Scanner scanner = new Scanner(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream());
            String rawData = scanner.nextLine();
            scanner.close();
            return UUID.fromString(rawData.split("\"")[3].replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5"));
        }
    }
    As for,
    I believe that using `getOfflinePlayer` with a String parameter is deprecated in the latest versions of Spigot.
     
    #4 View, Mar 29, 2020
    Last edited: Mar 29, 2020
  5. GC.

    GC.

    Yes, 16 per minute.
     
    • Informative Informative x 1
  6. Choco

    Moderator

    try with resources, but it doesn't matter anyways because:
    the utility method really should be delegating the exception to the caller. There's no reason to hide it.

    It was deprecated to discourage developers from writing persistent data for players and assigning it to their names. This deprecation notice really should be removed at this point as it was done in 1.7.6.
     
    • Agree Agree x 3
    • Informative Informative x 1
  7. On second thought, you're right. I will update my minimized version in my post.
     
  8. Actually no. Bukkit::getOfflinePlayer return data (uuid) depends on what online-mode is setup (premium, non-premium).
    This Util make sense but written in wrong way (and code smells everywhere). Async part could be done in different
    abstraction level.
     
  9. Choco

    Moderator

    Your server should not be in offline mode anyways unless you're routing it through a BungeeCord proxy (or a fork / third party proxy, whatever). If you're writing your plugin with offline servers in mind, you're writing it wrong. Offline servers should not be a priority support, or even a point of support at all. Work with the API you're given. Especially because the server caches player UUIDs whereas this would query them every time it's called.

    As far the asynchronization, any IO call should be done asynchronously unless there's an explicit reason not to. If you really want to give developers that option, you should have an overloaded method that accepts a boolean argument to state whether or not it should be asynchronous where the default value is true.
     
    • Agree Agree x 1
    • Funny Funny x 1
  10. Everything depends on what you want to do and be ready for every corner cases. For example we want create auth plugin that keeps compatibility with premium and nonpremium servers (login based on uuid). (We should take care of this case because even spigot taking care of nonpremium online-mode).

    Its util method, its should be simple as possible. That method've ONLY ONE thing to do - reading property from website. As i said. Async should be done in other abstraction level.
     
    #10 Subtelny, Mar 30, 2020
    Last edited: Mar 30, 2020
  11. Choco

    Moderator

    And in doing so you're further encouraging people to pirate the game. Authentication plugins such as these shouldn't have to exist. If you're running an offline server, you should suffer the risks and consequences of doing so. Though we're getting offtopic. This thread is about a code snippet. So long as we agree on the asynchronization.