Skip to content

Commit f6bd6c8

Browse files
committed
bring more of the old SkullModifier
1 parent 5c09930 commit f6bd6c8

File tree

1 file changed

+77
-34
lines changed

1 file changed

+77
-34
lines changed

src/main/java/me/hsgamer/bettergui/modifier/SkullModifier.java

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.mojang.authlib.GameProfile;
44
import com.mojang.authlib.properties.Property;
5-
import me.hsgamer.hscore.bukkit.item.modifier.ItemMetaComparator;
65
import me.hsgamer.hscore.bukkit.item.modifier.ItemMetaModifier;
76
import me.hsgamer.hscore.bukkit.utils.VersionUtils;
87
import me.hsgamer.hscore.common.StringReplacer;
@@ -13,6 +12,8 @@
1312
import org.bukkit.inventory.ItemStack;
1413
import org.bukkit.inventory.meta.ItemMeta;
1514
import org.bukkit.inventory.meta.SkullMeta;
15+
import org.bukkit.profile.PlayerProfile;
16+
import org.bukkit.profile.PlayerTextures;
1617
import org.jetbrains.annotations.NotNull;
1718
import org.jetbrains.annotations.Nullable;
1819

@@ -28,13 +29,13 @@
2829
* I decide to keep it here for compatibility.
2930
*/
3031
@SuppressWarnings("deprecation")
31-
public class SkullModifier implements ItemMetaModifier, ItemMetaComparator {
32+
public class SkullModifier implements ItemMetaModifier {
3233
/**
3334
* <a href="https://github.com/CryptoMorin/XSeries/blob/b633d00608435701f1045a566b98a81edd5f923c/src/main/java/com/cryptomorin/xseries/profiles/objects/ProfileInputType.java#L29C35-L29C50">...</a>
3435
*/
3536
private static final Pattern MOJANG_SHA256_APPROX = Pattern.compile("[0-9a-z]{55,70}");
3637
private static final SkullMeta delegateSkullMeta;
37-
private static final SkullHandler skullHandler = new SkullHandler();
38+
private static final SkullHandler skullHandler = getSkullHandler();
3839
private static final Map<String, GameProfile> cache = new ConcurrentHashMap<>();
3940

4041
static {
@@ -50,6 +51,15 @@ public class SkullModifier implements ItemMetaModifier, ItemMetaComparator {
5051

5152
private String skullString = "";
5253

54+
private static SkullHandler getSkullHandler() {
55+
try {
56+
Class.forName("org.bukkit.profile.PlayerProfile");
57+
return new NewSkullHandler();
58+
} catch (ClassNotFoundException e) {
59+
return new OldSkullHandler();
60+
}
61+
}
62+
5363
private static void setSkull(SkullMeta meta, String skull) {
5464
Optional<URL> url = Validate.getURL(skull);
5565
if (url.isPresent()) {
@@ -95,17 +105,6 @@ public boolean loadFromItemMeta(ItemMeta meta) {
95105
return false;
96106
}
97107

98-
@Override
99-
public boolean compare(@NotNull ItemMeta meta, @Nullable UUID uuid, @NotNull StringReplacer stringReplacer) {
100-
if (!(meta instanceof SkullMeta)) {
101-
return false;
102-
}
103-
return skullHandler.compareSkull(
104-
getSkullMeta(stringReplacer.replaceOrOriginal(skullString, uuid)),
105-
(SkullMeta) meta
106-
);
107-
}
108-
109108
@Override
110109
public Object toObject() {
111110
return skullString;
@@ -116,10 +115,35 @@ public void loadFromObject(Object object) {
116115
this.skullString = String.valueOf(object);
117116
}
118117

119-
private static class SkullHandler {
118+
private interface SkullHandler {
119+
@SuppressWarnings("deprecation")
120+
default void setSkullByName(SkullMeta meta, String name) {
121+
setSkullByPlayer(meta, Bukkit.getOfflinePlayer(name));
122+
}
123+
124+
default void setSkullByUUID(SkullMeta meta, UUID uuid) {
125+
setSkullByPlayer(meta, Bukkit.getOfflinePlayer(uuid));
126+
}
127+
128+
void setSkullByPlayer(SkullMeta meta, OfflinePlayer player);
129+
130+
void setSkullByURL(SkullMeta meta, URL url);
131+
132+
default void setSkullByURL(SkullMeta meta, String url) {
133+
try {
134+
setSkullByURL(meta, new URL(url));
135+
} catch (Exception e) {
136+
throw new RuntimeException(e);
137+
}
138+
}
139+
140+
String getSkullValue(SkullMeta meta);
141+
}
142+
143+
private static class OldSkullHandler implements SkullHandler {
120144
private final Method getProfileMethod;
121145

122-
private SkullHandler() {
146+
private OldSkullHandler() {
123147
Method method = null;
124148
try {
125149
//noinspection JavaReflectionMemberAccess
@@ -135,23 +159,8 @@ private SkullHandler() {
135159
getProfileMethod = method;
136160
}
137161

138-
public void setSkullByName(SkullMeta meta, String name) {
139-
setSkullByPlayer(meta, Bukkit.getOfflinePlayer(name));
140-
}
141-
142-
public void setSkullByUUID(SkullMeta meta, UUID uuid) {
143-
setSkullByPlayer(meta, Bukkit.getOfflinePlayer(uuid));
144-
}
145-
146-
public void setSkullByURL(SkullMeta meta, String url) {
147-
try {
148-
setSkullByURL(meta, new URL(url));
149-
} catch (Exception e) {
150-
throw new RuntimeException(e);
151-
}
152-
}
153-
154162
@SuppressWarnings("deprecation")
163+
@Override
155164
public void setSkullByPlayer(SkullMeta meta, OfflinePlayer player) {
156165
if (VersionUtils.isAtLeast(12)) {
157166
meta.setOwningPlayer(player);
@@ -160,6 +169,7 @@ public void setSkullByPlayer(SkullMeta meta, OfflinePlayer player) {
160169
}
161170
}
162171

172+
@Override
163173
public void setSkullByURL(SkullMeta meta, URL url) {
164174
GameProfile profile = cache.computeIfAbsent(url.toString(), s -> {
165175
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "");
@@ -182,6 +192,7 @@ public void setSkullByURL(SkullMeta meta, URL url) {
182192
}
183193
}
184194

195+
@Override
185196
public String getSkullValue(SkullMeta meta) {
186197
GameProfile profile;
187198
try {
@@ -211,9 +222,41 @@ public String getSkullValue(SkullMeta meta) {
211222
}
212223
return "";
213224
}
225+
}
226+
227+
private static class NewSkullHandler implements SkullHandler {
228+
private final Map<URL, PlayerProfile> profileMap = new HashMap<>();
229+
230+
@Override
231+
public void setSkullByPlayer(SkullMeta meta, OfflinePlayer player) {
232+
meta.setOwningPlayer(player);
233+
}
234+
235+
@Override
236+
public void setSkullByURL(SkullMeta meta, URL url) {
237+
PlayerProfile profile = profileMap.computeIfAbsent(url, url1 -> {
238+
PlayerProfile newProfile = Bukkit.createPlayerProfile(UUID.randomUUID(), "");
239+
PlayerTextures textures = newProfile.getTextures();
240+
textures.setSkin(url1);
241+
return newProfile;
242+
});
243+
meta.setOwnerProfile(profile);
244+
}
245+
246+
@Override
247+
public String getSkullValue(SkullMeta meta) {
248+
PlayerProfile profile = meta.getOwnerProfile();
249+
if (profile == null) {
250+
return "";
251+
}
252+
253+
PlayerTextures textures = profile.getTextures();
254+
URL url = textures.getSkin();
255+
if (url == null) {
256+
return "";
257+
}
214258

215-
public boolean compareSkull(SkullMeta meta1, SkullMeta meta2) {
216-
return getSkullValue(meta1).equals(getSkullValue(meta2));
259+
return url.toString();
217260
}
218261
}
219262
}

0 commit comments

Comments
 (0)