Solved How do you get the GameProfile of a Player Head?

Discussion in 'Plugin Development' started by RedKaneChironic, Nov 21, 2020 at 8:34 PM.

  1. Offline

    RedKaneChironic

    So I have this code that sets the player head to a specific texture using Mojang's GameProfile.
    Code:
    public ItemStack getSkull(String username, UUID uuid) {
            ItemStack skull = new ItemStack(Material.PLAYER_HEAD);
            String url = getSkinUrl(username);
            if (url == null || url.isEmpty())
                return skull;
            SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
            GameProfile profile = new GameProfile(uuid, null);
            byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", url).getBytes());
            profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
            Field profileField = null;
            try {
                profileField = skullMeta.getClass().getDeclaredField("profile");
            } catch (NoSuchFieldException | SecurityException e) {
                e.printStackTrace();
            }
            profileField.setAccessible(true);
            try {
                profileField.set(skullMeta, profile);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
            skull.setItemMeta(skullMeta);
            return skull;
        }
        static private JsonParser parser = new JsonParser();
        static private String API_PROFILE_LINK = "https://sessionserver.mojang.com/session/minecraft/profile/";
        public static String getSkinUrl(String uuid){
                String json = getContent(API_PROFILE_LINK + uuid);
                JsonObject o = parser.parse(json).getAsJsonObject();
                String jsonBase64 = o.get("properties").getAsJsonArray().get(0).getAsJsonObject().get("value").getAsString();
       
                o = parser.parse(new String(Base64.decodeBase64(jsonBase64))).getAsJsonObject();
                String skinUrl = o.get("textures").getAsJsonObject().get("SKIN").getAsJsonObject().get("url").getAsString();
                return skinUrl;
        }
        public static String getContent(String link){
                try {
                    URL url = new URL(link);
                    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String outputLine = "";
               
                    String inputLine;
                    while ((inputLine = br.readLine()) != null) {
                        outputLine += inputLine;
                    }
                    br.close();
                    return outputLine;
           
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
        }
    
    I would like to get the GameProfile so I can test if a block broken is a specific head.
    How would I get the GameProfile of a head this way?
     
  2. Offline

    Kars

    Get username/UUID from the head and get it that way?
     
  3. Offline

    RedKaneChironic

  4. Offline

    Kars

    @RedKaneChironic judging by your code this line might do it...
    PHP:
    GameProfile profile = new GameProfile(uuidnull);
     
  5. Offline

    RedKaneChironic

    My code to get heads looks like this
    PHP:
    @EventHandler
        
    public void onBlockBreak(BlockBreakEvent e) {
            
    Block block e.getBlock();
            
    Location loc block.getLocation();
            if (
    block.getType() == Material.PLAYER_HEAD|| block.getType() == Material.PLAYER_WALL_HEAD){
                
    loc.getBlock().setType(Material.WATER);
            }
        }
    How would I get a comparison method using what you made?
     
  6. Offline

    Kars

    @RedKaneChironic extract the UUID from the head, should be in the meta or somewhere.
     
    RedKaneChironic likes this.
  7. Offline

    KarimAKL

    RedKaneChironic likes this.
  8. Offline

    RedKaneChironic

    PHP:
    Skull skull = (Skullblock;
    //^ This line is causing a ClassCastException: class org.bukkit.craftbukkit.v1_16_R2.block.CraftBlock cannot be cast to class org.bukkit.block.Skull
                
    if (skull.getOwningPlayer() == Bukkit.getOfflinePlayer(UUID.fromString("c7c92b81-3987-4220-9069-207790a780d7"))) {
                    
    loc.getBlock().setType(Material.WATER);
                }
    How would I fix that?
    EDIT: Changed to block.getState() as Skull is a captured State. The code now works!
     
    Last edited: Nov 23, 2020 at 1:29 PM

Share This Page