|
1 | 1 | package com.github.stefvanschie.inventoryframework.util; |
2 | 2 |
|
| 3 | +import com.github.stefvanschie.inventoryframework.util.version.Version; |
3 | 4 | import com.mojang.authlib.GameProfile; |
4 | 5 | import com.mojang.authlib.properties.Property; |
| 6 | +import org.bukkit.Bukkit; |
5 | 7 | import org.bukkit.Material; |
6 | 8 | import org.bukkit.inventory.ItemStack; |
7 | 9 | import org.bukkit.inventory.meta.ItemMeta; |
| 10 | +import org.bukkit.inventory.meta.SkullMeta; |
| 11 | +import org.bukkit.profile.PlayerProfile; |
| 12 | +import org.bukkit.profile.PlayerTextures; |
8 | 13 | import org.jetbrains.annotations.NotNull; |
9 | 14 |
|
10 | 15 | import java.lang.reflect.Field; |
11 | 16 | import java.lang.reflect.InvocationTargetException; |
12 | 17 | import java.lang.reflect.Method; |
| 18 | +import java.net.MalformedURLException; |
| 19 | +import java.net.URL; |
13 | 20 | import java.util.Base64; |
14 | 21 | import java.util.Objects; |
15 | 22 | import java.util.UUID; |
@@ -53,9 +60,50 @@ public static ItemStack getSkull(@NotNull String id) { |
53 | 60 | * @param id the skull id |
54 | 61 | */ |
55 | 62 | public static void setSkull(@NotNull ItemMeta meta, @NotNull String id) { |
| 63 | + if (Version.getVersion().isOlderThan(Version.V1_18_2)) { |
| 64 | + setSkullReflection(meta, id); |
| 65 | + } else { |
| 66 | + setSkullProfile(meta, id); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Sets the skull's texture based on the player profile API introduced in 1.18.2. |
| 72 | + * |
| 73 | + * @param meta the {@link ItemMeta} of the skull to set |
| 74 | + * @param id the ID of the skin URL to apply |
| 75 | + * @since 0.11.6 |
| 76 | + */ |
| 77 | + private static void setSkullProfile(@NotNull ItemMeta meta, @NotNull String id) { |
| 78 | + if (!(meta instanceof SkullMeta)) { |
| 79 | + throw new IllegalArgumentException("Provided item meta is not of a skull"); |
| 80 | + } |
| 81 | + |
| 82 | + PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID()); |
| 83 | + PlayerTextures textures = profile.getTextures(); |
| 84 | + |
| 85 | + try { |
| 86 | + textures.setSkin(new URL("http://textures.minecraft.net/texture/" + id)); |
| 87 | + } catch (MalformedURLException exception) { |
| 88 | + throw new IllegalArgumentException("Provided ID is invalid", exception); |
| 89 | + } |
| 90 | + |
| 91 | + profile.setTextures(textures); |
| 92 | + |
| 93 | + ((SkullMeta) meta).setOwnerProfile(profile); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the skull's texture via reflection. |
| 98 | + * |
| 99 | + * @param meta the {@link ItemMeta} of the skull to set |
| 100 | + * @param id the ID of the skin URL to apply |
| 101 | + * @since 0.11.6 |
| 102 | + */ |
| 103 | + private static void setSkullReflection(@NotNull ItemMeta meta, @NotNull String id) { |
56 | 104 | GameProfile profile = new GameProfile(UUID.randomUUID(), ""); |
57 | 105 | byte[] encodedData = Base64.getEncoder().encode(String.format("{textures:{SKIN:{url:\"%s\"}}}", |
58 | | - "http://textures.minecraft.net/texture/" + id).getBytes()); |
| 106 | + "http://textures.minecraft.net/texture/" + id).getBytes()); |
59 | 107 | profile.getProperties().put("textures", new Property("textures", new String(encodedData))); |
60 | 108 | String itemDisplayName = meta.getDisplayName(); |
61 | 109 |
|
|
0 commit comments