Mojang API - Getting players name from UUID

Discussion in 'BungeeCord Plugin Development' started by Fractiion, Aug 17, 2018.

  1. Hello, im developing a BungeeCord plugin where i need to use the Mojang API to get the players name from their UUID, however i dont know how to do it. I have som codes that is to get the players UUID from their name and name from their UUID but that is for spigot and does not work in BungeeCord. I also do have a code for getting a players UUID from player name that works in bungeecord. Could anyone help me making a for getting the players name from UUID in BungeeCord?

    BungeeCord PlayerName --> UUID
    Code (Java):
    public static UUID getUUID(String playername) {
            String output = callURL("https://api.mojang.com/users/profiles/minecraft/" + playername);

            StringBuilder result = new StringBuilder();

            readData(output, result);

            String u = result.toString();

            String uuid = "";

            for(int i = 0; i <= 31; i++) {
                uuid = uuid + u.charAt(i);
                if(i == 7 || i == 11 || i == 15 || i == 19) {
                    uuid = uuid + "-";
                }
            }

            return UUID.fromString(uuid);
        }

        private static void readData(String toRead, StringBuilder result) {
            int i = 7;

            while(i < 200) {
                if(!String.valueOf(toRead.charAt(i)).equalsIgnoreCase("\"")) {

                    result.append(String.valueOf(toRead.charAt(i)));

                } else {
                    break;
                }

                i++;
            }
        }

        private static String callURL(String URL) {
            StringBuilder sb = new StringBuilder();
            URLConnection urlConn = null;
            InputStreamReader in = null;
            try {
                java.net.URL url = new URL(URL);
                urlConn = url.openConnection();

                if (urlConn != null) urlConn.setReadTimeout(60 * 1000);

                if (urlConn != null && urlConn.getInputStream() != null) {
                    in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
                    BufferedReader bufferedReader = new BufferedReader(in);

                    if (bufferedReader != null) {
                        int cp;

                        while ((cp = bufferedReader.read()) != -1) {
                            sb.append((char) cp);
                        }

                        bufferedReader.close();
                    }
                }

                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return sb.toString();
        }
    Spigot PlayerName --> UUID
    Code (Java):
    public static String getUUID(String playerName) {
            try {
                String url = "https://api.mojang.com/users/profiles/minecraft/" + playerName;

                String UUIDJson = IOUtils.toString(new URL(url));

                JSONObject UUIDObject = (JSONObject) JSONValue.parseWithException(UUIDJson);

                String uuid = UUIDObject.get("id").toString();
                return uuid;
            } catch (Exception e) {
                return "";
            }
        }
    Spigot UUID --> PlayerName
    Code (Java):
    public static String getName(String uuid) {
            String url = "https://api.mojang.com/user/profiles/"+uuid.replace("-", "")+"/names";
            try {
                @SuppressWarnings("deprecation")
                String nameJson = IOUtils.toString(new URL(url));
                JSONArray nameValue = (JSONArray) JSONValue.parseWithException(nameJson);
                String playerSlot = nameValue.get(nameValue.size()-1).toString();
                JSONObject nameObject = (JSONObject) JSONValue.parseWithException(playerSlot);
                return nameObject.get("name").toString();
            } catch (IOException | ParseException e) {
                return "error";
            }
        }
     
  2. If I understood correctly what you mean:
    Bungeecord servers are using your offline-uuid because they're running in offline mode, so there's no authentification with mojang.

    This may help you in a way.
     
  3. No, i need to get the player name of a offline player. All i have stored is their uuid
     
  4. Well, you have to store the UUID of every player ever joined, along with their name, as BungeeCord does not do it.
    You can't get the name mapped to an offline UUID from the mojang api (why do you think that would work?? You think they happily provide services for people pirating their game??) or from anywhere, as offline player uuids are computed like this: md5("OfflinePlayer:"+playername), and one does not simply reverse md5.
     
    • Creative Creative x 1
  5. I have stored their online UUID not their offline, and it works in Spigot so why should it not work in BungeeCord. The only thing i need help with is the code.
     
  6. If you're able to get the username from an online-uuid on a spigot-server and not on bungeecord, there's another problem rather than bungeecord.
     
  7. Why are you mixing online and offline uuids? On properly configured servers you only ever have either one.
     
  8. I do only use Online UUIDS, the server is in online mode. Where do you guys get the offline UUID's from? My goal is to get a player that is not on the servers name from his uuid wich is stored in a mysql table.
     
  9. if your server is in online mode, you can use the Mojang API. It's not hard, really.
    However, if you are only storing UUIDs of players that had once joined, just save which name maps to which id, it's much faster compared to downloading a website.
     
  10. https://wiki.vg/Mojang_API - There's everything you need.

    Fetching an username from it's (online) uuid is the same on spigot as on a bungeecord server. If it's not working on bungeecord for some reason, you did something wrong. The mojang request is just raw java and has nothing to do with minecraft.
     
  11. DiscoverSquishy

    Benefactor

    @Fractiion this may or may not help you in a way.

    A few months ago with my server a strategy was to cache skins needed or that were ordered/commanded/summoned.
    Then using them globally from the cache so it wouldn’t have to wait for the Mojang 60 second wait time with each skin.
    We used Redis then switched to Mongo but since Redis is meant for caching I’d personally use Redis/Jedis for that instead of MongoDB for anything.

    Best of luck.