Skip to content

Commit d1debb7

Browse files
committed
Fix fonts throwing exception
Fixes #1494
1 parent 69d394c commit d1debb7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

IF/src/main/java/com/github/stefvanschie/inventoryframework/util/SkullUtil.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.github.stefvanschie.inventoryframework.util;
22

3+
import com.github.stefvanschie.inventoryframework.util.version.Version;
34
import com.mojang.authlib.GameProfile;
45
import com.mojang.authlib.properties.Property;
6+
import org.bukkit.Bukkit;
57
import org.bukkit.Material;
68
import org.bukkit.inventory.ItemStack;
79
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;
813
import org.jetbrains.annotations.NotNull;
914

1015
import java.lang.reflect.Field;
1116
import java.lang.reflect.InvocationTargetException;
1217
import java.lang.reflect.Method;
18+
import java.net.MalformedURLException;
19+
import java.net.URL;
1320
import java.util.Base64;
1421
import java.util.Objects;
1522
import java.util.UUID;
@@ -53,9 +60,50 @@ public static ItemStack getSkull(@NotNull String id) {
5360
* @param id the skull id
5461
*/
5562
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) {
56104
GameProfile profile = new GameProfile(UUID.randomUUID(), "");
57105
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());
59107
profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
60108
String itemDisplayName = meta.getDisplayName();
61109

IF/src/main/java/com/github/stefvanschie/inventoryframework/util/version/Version.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ public boolean isInventoryViewInterface() {
237237
return INTERFACE_INVENTORY_VIEW.contains(this);
238238
}
239239

240+
/**
241+
* Checks if this version is older than the provided version.
242+
*
243+
* @param version the version to check if it is newer
244+
* @return true if this version is older, false otherwise
245+
* @since 0.11.6
246+
*/
247+
public boolean isOlderThan(@NotNull Version version) {
248+
return ordinal() < version.ordinal();
249+
}
250+
240251
/**
241252
* Checks whether modern smithing tables exist on this version. Returns true if they do, otherwise false.
242253
*

0 commit comments

Comments
 (0)