|
1 | 1 | package ru.abstractmenus.util.bukkit; |
2 | 2 |
|
3 | | -import com.mojang.authlib.GameProfile; |
| 3 | +import com.destroystokyo.paper.profile.PlayerProfile; |
| 4 | +import org.bukkit.Bukkit; |
4 | 5 | import org.bukkit.inventory.ItemStack; |
5 | 6 | import org.bukkit.inventory.meta.SkullMeta; |
6 | | -import ru.abstractmenus.api.Logger; |
| 7 | +import org.bukkit.profile.PlayerTextures; |
7 | 8 | import ru.abstractmenus.services.ProfileStorage; |
8 | 9 |
|
9 | | -import java.lang.reflect.Field; |
10 | | -import java.lang.reflect.InvocationTargetException; |
11 | | -import java.lang.reflect.Method; |
| 10 | +import java.net.MalformedURLException; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.util.UUID; |
12 | 14 |
|
13 | 15 | public final class Skulls { |
14 | 16 |
|
15 | 17 | private Skulls() { |
16 | 18 | } |
17 | 19 |
|
18 | 20 | public static ItemStack getCustomSkull(String url) { |
19 | | - GameProfile profile = MojangApi.createProfile(url); |
20 | | - return getCustomSkull(profile); |
21 | | - } |
22 | | - |
23 | | - public static ItemStack getCustomSkull(GameProfile profile) { |
24 | 21 | ItemStack head = createSkullItem(); |
25 | | - |
26 | | - if (profile == null) return head; |
| 22 | + if (url == null || url.isEmpty()) { |
| 23 | + return head; |
| 24 | + } |
27 | 25 |
|
28 | 26 | SkullMeta headMeta = (SkullMeta) head.getItemMeta(); |
| 27 | + if (headMeta == null) { |
| 28 | + return null; |
| 29 | + } |
29 | 30 |
|
30 | | - if (headMeta == null) return null; |
31 | | - |
32 | | - Field profileField; |
| 31 | + PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID()); |
33 | 32 |
|
34 | 33 | try { |
35 | | - Method method = headMeta.getClass().getDeclaredMethod("setProfile", GameProfile.class); |
36 | | - method.setAccessible(true); |
37 | | - method.invoke(headMeta, profile); |
38 | | - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { |
39 | | - try { |
40 | | - profileField = headMeta.getClass().getDeclaredField("profile"); |
41 | | - profileField.setAccessible(true); |
42 | | - profileField.set(headMeta, profile); |
43 | | - } catch (NoSuchFieldException | IllegalAccessException ex2) { |
44 | | - ex2.printStackTrace(); |
45 | | - } |
| 34 | + PlayerTextures textures = profile.getTextures(); |
| 35 | + textures.setSkin(new URI(url).toURL()); |
| 36 | + profile.setTextures(textures); |
| 37 | + |
| 38 | + headMeta.setPlayerProfile(profile); |
| 39 | + head.setItemMeta(headMeta); |
| 40 | + } catch (MalformedURLException | URISyntaxException e) { |
| 41 | + throw new RuntimeException(String.format("Bad URL [%s] for texture [%s]", url, profile.getTextures()), e); |
46 | 42 | } |
47 | 43 |
|
| 44 | + return head; |
| 45 | + } |
| 46 | + |
| 47 | + public static ItemStack getCustomSkull(PlayerProfile profile) { |
| 48 | + ItemStack head = createSkullItem(); |
| 49 | + if (profile == null) return head; |
| 50 | + |
| 51 | + SkullMeta headMeta = (SkullMeta) head.getItemMeta(); |
| 52 | + if (headMeta == null) return null; |
| 53 | + |
| 54 | + headMeta.setPlayerProfile(profile); |
48 | 55 | head.setItemMeta(headMeta); |
49 | 56 |
|
50 | 57 | return head; |
51 | 58 | } |
52 | 59 |
|
53 | 60 | public static ItemStack getPlayerSkull(String playerName) { |
54 | | - GameProfile profile = ProfileStorage.instance().getProfile(playerName); |
| 61 | + PlayerProfile profile = ProfileStorage.instance().getProfile(playerName); |
55 | 62 |
|
56 | 63 | if (profile == null) { |
57 | | - Logger.info("Profile '" + playerName + "' not found. Trying to load ..."); |
58 | | - |
59 | | - profile = MojangApi.loadProfileWithSkin(playerName); |
60 | | - |
61 | | - if (profile == null) |
| 64 | + profile = Bukkit.createProfile(null, playerName); |
| 65 | + try { |
| 66 | + profile.complete(true); |
| 67 | + } catch (Exception e) { |
62 | 68 | profile = ProfileStorage.DEF_PROFILE; |
63 | | - |
| 69 | + } |
64 | 70 | ProfileStorage.instance().add(playerName, profile); |
65 | 71 | } |
66 | 72 |
|
67 | 73 | return getCustomSkull(profile); |
68 | 74 | } |
69 | 75 |
|
70 | 76 | public static ItemStack createSkullItem() { |
71 | | - try { |
72 | | - return new ItemStack(ItemUtil.getHeadMaterial(), 1, (short) 3); |
73 | | - } catch (Throwable t) { |
74 | | - return new ItemStack(ItemUtil.getHeadMaterial()); |
75 | | - } |
| 77 | + return new ItemStack(ItemUtil.getHeadMaterial()); |
76 | 78 | } |
77 | | - |
78 | 79 | } |
0 commit comments