Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/main/java/ru/abstractmenus/util/bukkit/Skulls.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;
Expand All @@ -9,10 +11,12 @@
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() { }
private Skulls() {}

public static ItemStack getCustomSkull(String url) {
GameProfile profile = MojangApi.createProfile(url);
Expand All @@ -21,32 +25,51 @@ public static ItemStack getCustomSkull(String url) {

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;
}

if (headMeta == null) return null;
try {
Field profileField = headMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(headMeta, profile);
} catch (NoSuchFieldException | IllegalAccessException ignored) {}

Field profileField;
head.setItemMeta(headMeta);
return head;
}

private static boolean applyModernProfile(SkullMeta headMeta, GameProfile profile) {
try {
Method method = headMeta.getClass().getDeclaredMethod("setProfile", GameProfile.class);
method.setAccessible(true);
method.invoke(headMeta, profile);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
try {
profileField = headMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(headMeta, profile);
} catch (NoSuchFieldException | IllegalAccessException ex2) {
ex2.printStackTrace();
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());
}
}

head.setItemMeta(headMeta);
Method setPlayerProfile = headMeta.getClass().getMethod("setPlayerProfile", playerProfileClass);
setPlayerProfile.invoke(headMeta, playerProfile);
return true;
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}

return head;
return false;
}

public static ItemStack getPlayerSkull(String playerName) {
Expand All @@ -73,5 +96,4 @@ public static ItemStack createSkullItem() {
return new ItemStack(ItemUtil.getHeadMaterial());
}
}

}