|
| 1 | +package io.github.projectunified.craftitem.spigot.skull; |
| 2 | + |
| 3 | +import com.destroystokyo.paper.profile.PlayerProfile; |
| 4 | +import com.destroystokyo.paper.profile.ProfileProperty; |
| 5 | +import org.bukkit.Bukkit; |
| 6 | +import org.bukkit.OfflinePlayer; |
| 7 | +import org.bukkit.inventory.meta.SkullMeta; |
| 8 | + |
| 9 | +import java.net.URL; |
| 10 | +import java.util.Base64; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.UUID; |
| 13 | +import java.util.concurrent.ConcurrentHashMap; |
| 14 | + |
| 15 | +/** |
| 16 | + * Skull handler for Paper servers using the Paper-specific PlayerProfile API. |
| 17 | + * |
| 18 | + * <p>Uses Paper's faster PlayerProfile implementation compared to Bukkit's standard API. |
| 19 | + * Caches PlayerProfile instances to avoid recreation for repeated textures. |
| 20 | + * |
| 21 | + * <p><strong>Implementation Details:</strong> |
| 22 | + * <ul> |
| 23 | + * <li>Uses Bukkit.createProfile() to create profiles (Paper-optimized)</li> |
| 24 | + * <li>Uses ProfileProperty for direct property assignment</li> |
| 25 | + * <li>Caches profiles for performance optimization</li> |
| 26 | + * </ul> |
| 27 | + */ |
| 28 | +public class PaperSkullHandler implements SkullHandler { |
| 29 | + private final Map<String, PlayerProfile> profileMap = new ConcurrentHashMap<>(); |
| 30 | + |
| 31 | + /** |
| 32 | + * Sets a PlayerProfile on the SkullMeta. |
| 33 | + * |
| 34 | + * @param meta the SkullMeta to modify |
| 35 | + * @param profile the PlayerProfile with texture data |
| 36 | + */ |
| 37 | + private void setSkull(SkullMeta meta, PlayerProfile profile) { |
| 38 | + meta.setPlayerProfile(profile); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Sets skull texture using an OfflinePlayer. |
| 43 | + * Results are cached to avoid recreating profiles for the same player. |
| 44 | + * |
| 45 | + * @param meta the SkullMeta to modify |
| 46 | + * @param player the OfflinePlayer |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public void setSkullByPlayer(SkullMeta meta, OfflinePlayer player) { |
| 50 | + PlayerProfile profile = profileMap.computeIfAbsent(player.getUniqueId().toString(), s -> Bukkit.createProfile(player.getUniqueId())); |
| 51 | + setSkull(meta, profile); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets skull texture from a URL using the PlayerProfile API. |
| 56 | + * Results are cached to avoid recreating profiles for the same URL. |
| 57 | + * |
| 58 | + * @param meta the SkullMeta to modify |
| 59 | + * @param url the texture URL |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public void setSkullByURL(SkullMeta meta, URL url) { |
| 63 | + PlayerProfile profile = profileMap.computeIfAbsent(url.toString(), url1 -> { |
| 64 | + PlayerProfile playerProfile = Bukkit.createProfile(UUID.randomUUID()); |
| 65 | + playerProfile.setProperty(new ProfileProperty("textures", |
| 66 | + Base64.getEncoder().encodeToString( |
| 67 | + String.format("{\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}", url1).getBytes() |
| 68 | + ) |
| 69 | + )); |
| 70 | + return playerProfile; |
| 71 | + }); |
| 72 | + setSkull(meta, profile); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets skull texture from Base64-encoded texture data. |
| 77 | + * Results are cached to avoid recreating profiles for the same Base64 string. |
| 78 | + * |
| 79 | + * @param meta the SkullMeta to modify |
| 80 | + * @param base64 the Base64-encoded texture data |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public void setSkullByBase64(SkullMeta meta, String base64) { |
| 84 | + PlayerProfile profile = profileMap.computeIfAbsent(base64, b -> { |
| 85 | + PlayerProfile playerProfile = Bukkit.createProfile(UUID.randomUUID()); |
| 86 | + playerProfile.setProperty(new ProfileProperty("textures", base64)); |
| 87 | + return playerProfile; |
| 88 | + }); |
| 89 | + setSkull(meta, profile); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Extracts the skull texture value (Base64 data) from SkullMeta. |
| 94 | + * Searches for the textures property in the PlayerProfile. |
| 95 | + * |
| 96 | + * @param meta the SkullMeta to query |
| 97 | + * @return the Base64 texture value, or empty string if not found |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public String getSkullValue(SkullMeta meta) { |
| 101 | + PlayerProfile profile = meta.getPlayerProfile(); |
| 102 | + if (profile == null) { |
| 103 | + return ""; |
| 104 | + } |
| 105 | + |
| 106 | + ProfileProperty texturesProperty = null; |
| 107 | + for (ProfileProperty property : profile.getProperties()) { |
| 108 | + if (property.getName().equalsIgnoreCase("textures")) { |
| 109 | + texturesProperty = property; |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + if (texturesProperty == null) { |
| 114 | + return ""; |
| 115 | + } |
| 116 | + |
| 117 | + return texturesProperty.getValue(); |
| 118 | + } |
| 119 | +} |
0 commit comments