-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSkulls.java
More file actions
99 lines (76 loc) · 3.42 KB
/
Copy pathSkulls.java
File metadata and controls
99 lines (76 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ru.abstractmenus.util.bukkit;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import ru.abstractmenus.api.Logger;
import ru.abstractmenus.services.ProfileStorage;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.UUID;
public final class Skulls {
private Skulls() {}
public static ItemStack getCustomSkull(String url) {
GameProfile profile = MojangApi.createProfile(url);
return getCustomSkull(profile);
}
public static ItemStack getCustomSkull(GameProfile profile) {
ItemStack head = createSkullItem();
if (profile == null) return head;
SkullMeta headMeta = (SkullMeta) head.getItemMeta();
if (headMeta == null) return head;
if (applyModernProfile(headMeta, profile)) {
head.setItemMeta(headMeta);
return head;
}
try {
Field profileField = headMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(headMeta, profile);
} catch (NoSuchFieldException | IllegalAccessException ignored) {}
head.setItemMeta(headMeta);
return head;
}
private static boolean applyModernProfile(SkullMeta headMeta, GameProfile profile) {
try {
Class<?> playerProfileClass = Class.forName("org.bukkit.profile.PlayerProfile");
Method createProfile = Bukkit.class.getMethod("createProfile", UUID.class, String.class);
Object playerProfile = createProfile.invoke(null, profile.getId(), profile.getName());
Method getProperties = playerProfileClass.getMethod("getProperties");
Object properties = getProperties.invoke(playerProfile);
Class<?> propertyMapClass = properties.getClass();
Method put = propertyMapClass.getMethod("put", Object.class, Object.class);
for (Map.Entry<String, Property> entry : profile.getProperties().entries()) {
put.invoke(properties, entry.getKey(), entry.getValue());
}
Method setPlayerProfile = headMeta.getClass().getMethod("setPlayerProfile", playerProfileClass);
setPlayerProfile.invoke(headMeta, playerProfile);
return true;
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
public static ItemStack getPlayerSkull(String playerName) {
GameProfile profile = ProfileStorage.instance().getProfile(playerName);
if (profile == null) {
Logger.info("Profile '" + playerName + "' not found. Trying to load ...");
profile = MojangApi.loadProfileWithSkin(playerName);
if (profile == null)
profile = ProfileStorage.DEF_PROFILE;
ProfileStorage.instance().add(playerName, profile);
}
return getCustomSkull(profile);
}
public static ItemStack createSkullItem() {
try {
return new ItemStack(ItemUtil.getHeadMaterial(), 1, (short) 3);
} catch (Throwable t) {
return new ItemStack(ItemUtil.getHeadMaterial());
}
}
}