Question Change Skin with a command?

niqyu

New member
Dec 19, 2022
1
1
1
Hello. I have a question. How can I change a player's skin with a command, for example: /skin <name>? I have already tried the following code:
  1. PlayerProfile target = Bukkit.createProfile(args[0]);
  2. target.complete(true);
  3. PlayerProfile profile = player.getPlayerProfile();
  4. profile.setTextures(target.getTextures());
  5. player.setPlayerProfile(profile);
But it doesn't work. The player flickers briefly in-game and that's it. There is no error in the log or anything like that.
I'm running Paper Spigot 1.19.2
 
  • Like
Reactions: Zymo

Zymo

New member
Jan 16, 2023
4
1
0
1
Hello. I have a question. How can I change a player's skin with a command, for example: /skin <name>? I have already tried the following code:

But it doesn't work. The player flickers briefly in-game and that's it. There is no error in the log or anything like that.
I'm running Paper Spigot 1.19.2

I found a solution. Even though the author may have forgotten about this post, I'll show you the solution:

Java:
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
    if (!(sender instanceof Player))
    {
        return true;
    }
    if (!label.equalsIgnoreCase("skin"))
    {
        return true;
    }
    Player player = ((Player) sender);
    if (args.length != 1)
    {
        player.sendMessage("Incorrect arguments");
        return true;
    }
    PlayerProfile playerProfile = player.getPlayerProfile();
    String targetName = args[0];
    try {
        URL url_0 = new URL("https://api.mojang.com/users/profiles/minecraft/" + targetName);

        InputStreamReader reader_0 = new InputStreamReader(url_0.openStream());

        String uuid = JsonParser.parseReader(reader_0).getAsJsonObject().get("id").getAsString();

        URL url_1 = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");

        InputStreamReader reader_1 = new InputStreamReader(url_1.openStream());

        JsonObject properties = JsonParser.parseReader(reader_1).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();

        String value = properties.get("value").getAsString();
        String signature = properties.get("signature").getAsString();

        playerProfile.setProperty(new ProfileProperty("textures", value, signature));
        player.setPlayerProfile(playerProfile);

        player.sendMessage("Your skin has been changed");

    } catch (IllegalStateException | IOException | NullPointerException exception)
    {
        player.sendMessage("Failed to set skin");
    }
    return true;
}

Okay, here's the code, but what's going on here?
Well, first you need to get the player's uuid, because there is no information whether the server is offline or online. On offline servers, the uuid is generated from the nickname, so we need to check it on the Mojang database. Then we extract the uuid and go to the next Mojang page "sessionserver" from which we extract the signature and value, and then set it to property "textures" and finally set the player's profile. Some of the code is in try and catch brackets because, for example, the provided target player does not exist in the Mojang database. That's all.