Resource UUID and Name Fetcher Class

Discussion in 'Spigot Plugin Development' started by Seby_Plays, Jun 17, 2021.

  1. Basically that what the title says.
    Usage:

    FETCH NAME:

    new PlayerFetcher(UUID.fromString("9e5cc160-a6c5-4682-8c7d-6aba48b967f0")).getName()

    FETCH UUID:


    new PlayerFetcher("Seby_Plays").getUUID()

    It will return the value

    Code (Java):
    package de.mcsilentde.sebyplays.silentproxy.utils.fetchers;

    import lombok.Getter;
    import lombok.SneakyThrows;
    import org.json.JSONObject;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.UUID;

    public class PlayerFetcher {

        @Getter private String name;
        @Getter private UUID uuid;
        @Getter private String uuidURL = "https://sessionserver.mojang.com/session/minecraft/profile/";
        @Getter private String nameURL = "https://api.mojang.com/users/profiles/minecraft/";

        public PlayerFetcher(String name) {
            this.name = name;
        }

        public PlayerFetcher(UUID uuid) {
            this.uuid = uuid;
        }

        @SneakyThrows
        public JSONObject getPlayerJson() {
            URL url;

            if (this.name != null) {
                url = new URL(nameURL + name);
            } else {
                url = new URL(uuidURL + uuid);
            }
            URLConnection urlConnection = url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(urlConnection.getInputStream()));
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            bufferedReader.close();
            if(!stringBuilder.toString().startsWith("{")){
                return null;
            }
            return new JSONObject(stringBuilder.toString());
        }

        public UUID getUUID(){
            String uuidTemp = (String) getPlayerJson().get("id");
            String uuid = "";
            for (int i = 0; i <= 31; i++) {
                uuid = uuid + uuidTemp.charAt(i);
                if (i == 7 || i == 11 || i == 15 || i == 19) {
                    uuid = uuid + "-";
                }
            }

            if(getPlayerJson() != null){
                return UUID.fromString(uuid);
            }
            return null;
        }

        public String getName(){
            if(getPlayerJson() != null){
                return (String) getPlayerJson().get("name");
            }
            return null;
        }

        @SneakyThrows
        public static void main(String[] args) {
            System.out.println(new PlayerFetcher(UUID.fromString("9e5cc160-a6c5-4682-8c7d-6aba48b967f0")).getName());
        }

    }
     
     
  2. SteelPhoenix

    Moderator

    You should close resources (e.g. with a try-with-resources block), and disconnect your HttpURLConnection
    Also, checking if a string starts with '{' is kinda weird.

    Also, for #getUUID() you don't use a StringBuilder

    Additionally, your #main(String[]) is not really a part of the resource.
     
    • Agree Agree x 1
  3. Whats wrong with just doing
    Bukkit.getServer().getOfflinePlayer(uuid).getName() ?
     
  4. If player never visited the server u cant do that
     
  5. This code might be a bit ugly, but....
    [​IMG]
     
  6. I tried using other player that never visited the server, it seems working.
     
  7. Unneccessary use of object instantiation
     
    • Like Like x 1
  8. Wouldn't this be better as a static utility class?

    Also If they aren't using lombok or something similar this wouldn't work afaik

    Same with SneakyThrows (I would still use try-catch tho even with lombok)