1.16.5 HttpsURLConnection error?

Discussion in 'Spigot Plugin Development' started by NuggetRP, Jul 7, 2021.

  1. Hi there, im trying out a basic code to try to change the skin of a player using this code:
    Code (Text):
    public static boolean setSkin(GameProfile profile, UUID uuid) {
            try {
                HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false", UUIDTypeAdapter.fromUUID(uuid))).openConnection();
                if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
                    String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
                    String skin = reply.split("\"value\":\"")[1].split("\"")[0];
                    String signature = reply.split("\"signature\":\"")[1].split("\"")[0];
                    profile.getProperties().put("textures", new Property("textures", skin, signature));
                    return true;
                } else {
                    System.out.println("Connection could not be opened (Response code " + connection.getResponseCode() + ", " + connection.getResponseMessage() + ")");
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
                }
            }
    But im getting an java.lang.ArrayIndexOutOfBoundsException: 1 on this line: String signature = reply.split("\"signature\":\"")[1].split("\"")[0];

    Anyone have a fix for this? x)
     
  2. Hi, I hope this will solve your problem
    Code (Java):
    public static Skin getSkinByName(@NotNull final String skinName, @NotNull final String playerName) {
            try {
                final URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + playerName);
                final InputStreamReader inputStreamReader = new InputStreamReader(url.openStream());
                final String uuid = new JsonParser().parse(inputStreamReader).getAsJsonObject().get("id").getAsString();

                final URL sessionURL = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
                final InputStreamReader reader = new InputStreamReader(sessionURL.openStream());
                final JsonObject textureProperty = new JsonParser().parse(reader).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
                String value = textureProperty.get("value").getAsString();
                String signature = textureProperty.get("signature").getAsString();
                return new Skin(skinName, value, signature);
            } catch (IOException e) {
                ExceptionHandler.handle(e);
            }
            return null;
        }