Change A Players Skin

Discussion in 'Plugin Development' started by DennisUnplugged, Jan 12, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    DennisUnplugged

    I'm trying to use the Mojang API to create a command that would change your skin to another player's skin. I'm using the command /skin <PlayerName> and then I'm turning the name into a UUID with the Mojang API. Finally I'm replacing the texture of the sender using his game profile. I've got no errors at all but the only feedback I get in game it's an actual repeat of the command (So basically the command usage that I specified in the plugin.yml)

    That's my code

    Code:
    public class SkinChanger extends JavaPlugin implements CommandExecutor {
    
        private UUID targetId;
        private String textureID;
        private String Signature;
    
        @Override
        public void onEnable() {
    
            System.out.println(ChatColor.GREEN + "Skin changer enabled!");
        }
    
        public UUID fromTrimmed(String trimmedUUID) throws IllegalArgumentException{
            if(trimmedUUID == null) throw new IllegalArgumentException();
            StringBuilder builder = new StringBuilder(trimmedUUID.trim());
            /* Backwards adding to avoid index adjustments */
            try {
                builder.insert(20, "-");
                builder.insert(16, "-");
                builder.insert(12, "-");
                builder.insert(8, "-");
            } catch (StringIndexOutOfBoundsException e){
                throw new IllegalArgumentException();
            }
    
            return UUID.fromString(builder.toString());
        }
    
    
        public void ChangeSkin(GameProfile profile) {
    
            String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + targetId.toString();
    
            try {
                URL site = new URL(url);
    
                HttpURLConnection connection = (HttpURLConnection) site.openConnection();
    
                connection.setRequestMethod("GET");
    
                connection.connect();
    
                InputStream input = connection.getInputStream();
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
    
                StringBuffer buffer = new StringBuffer();
    
                String line;
    
                while ((line = reader.readLine()) != null) {
    
                    buffer.append(line.trim());
    
                }
    
                reader.close();
                connection.disconnect();
    
                String result = buffer.toString();
    
                JsonObject object = (JsonObject)(new Gson()).fromJson(result, JsonObject.class);
    
                JsonArray array = object.getAsJsonArray("properties");
    
                textureID = new String(array.get(0).getAsJsonObject().get("value").getAsString());
    
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            profile.getProperties().put("textures", new Property("textures", textureID));
    
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("skin")) {
    
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Do you really think that the console can change it's skin?");
    
                    return true;
                }
    
                Player p = (Player) sender;
    
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify the name of the player whose skin you'd like to put on!");
    
                    return true;
                }
    
    
    
                try {
    
                    URL site = new URL("https://api.mojang.com/users/profiles/minecraft/" + args[0]);
    
                    HttpURLConnection connection = (HttpURLConnection) site.openConnection();
    
                    connection.setRequestMethod("GET");
    
                    connection.connect();
    
                    InputStream input = connection.getInputStream();
    
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
    
                    StringBuffer buffer = new StringBuffer();
    
                    String line;
    
                    while ((line = reader.readLine()) != null) {
    
                        buffer.append(line.trim());
    
                    }
    
                    reader.close();
                    connection.disconnect();
    
                    String result = buffer.toString();
    
                    JsonObject object = (JsonObject)(new Gson()).fromJson(result, JsonObject.class);
    
                    targetId = fromTrimmed(object.get("id").getAsString());
                } catch (IOException ex) {
                    sender.sendMessage(ChatColor.RED + "An internal error occurred and we couldn't change your skin!");
    
                    return true;
                }
    
    
                try {
                    ChangeSkin(new GameProfile(p.getUniqueId(), p.getName()));
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return true;
                }
            } else {
                return true;
            }
    
            return false;
        }
    }
    
    Any help???
     
Thread Status:
Not open for further replies.

Share This Page