From 9e89ca5754cfa6c9e818b98b4136a21c983155ed Mon Sep 17 00:00:00 2001 From: Stanislav Panchenko Date: Tue, 26 Nov 2024 14:06:17 +0500 Subject: [PATCH] issue-1 fix GameProfile Skulls Signed-off-by: Stanislav Panchenko --- README.md | 8 +- build.gradle | 2 +- .../services/ProfileStorage.java | 53 +++++-------- .../ru/abstractmenus/util/bukkit/Skulls.java | 79 ++++++++++--------- 4 files changed, 69 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 3896f01..2498c5d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # AbstractMenus license -license +license AbstractMenus is a GUI plugin for SpigotMC servers that allows server owners and developers to create custom GUIs with ease. The plugin is designed to be user-friendly and easy to use, with a wide range of features and options to customize the GUIs to your liking. The plugin is also highly customizable, allowing you to create GUIs that fit your server's theme and style. @@ -10,6 +10,7 @@ This allows us to keep the code clean without using a bunch of wrappers for back ## 🔭Table of contents - [Links](#links) +- [Supported versions](#supported-versions) - [Installation and build](#installation-and-build) - [Contributions and feedback](#contributions-and-feedback) - [Licence](#licence) @@ -20,6 +21,11 @@ This allows us to keep the code clean without using a bunch of wrappers for back - [Discord](https://discord.gg/kt4P9Cgw) - [Documentation](https://abstractmenus.github.io/docs/index.html) +## 💊Supported versions +- ❓1.21.3 +- ✅1.21.1 +- ❌<1.21.x + ## Installation and build Requirements: diff --git a/build.gradle b/build.gradle index 7527bad..d3c0da6 100644 --- a/build.gradle +++ b/build.gradle @@ -40,7 +40,7 @@ java { group 'ru.abstractmenus' -version '1.17.4-beta' +version '1.17.5-beta' repositories { mavenLocal() diff --git a/src/main/java/ru/abstractmenus/services/ProfileStorage.java b/src/main/java/ru/abstractmenus/services/ProfileStorage.java index d483123..29a8e91 100644 --- a/src/main/java/ru/abstractmenus/services/ProfileStorage.java +++ b/src/main/java/ru/abstractmenus/services/ProfileStorage.java @@ -1,19 +1,16 @@ package ru.abstractmenus.services; -import com.mojang.authlib.GameProfile; -import com.mojang.authlib.properties.Property; +import com.destroystokyo.paper.profile.PlayerProfile; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -import ru.abstractmenus.api.Logger; -import ru.abstractmenus.util.bukkit.MojangApi; -import ru.abstractmenus.util.bukkit.BukkitTasks; +import org.bukkit.profile.PlayerTextures; import ru.abstractmenus.util.StringUtil; +import ru.abstractmenus.util.bukkit.BukkitTasks; -import java.lang.reflect.Method; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -22,9 +19,9 @@ public final class ProfileStorage implements Listener { private static ProfileStorage instance; - public static final GameProfile DEF_PROFILE = new GameProfile(UUID.randomUUID(), StringUtil.generateRandom(16)); + public static final PlayerProfile DEF_PROFILE = Bukkit.createProfile(UUID.randomUUID(), StringUtil.generateRandom(16)); - private final Map profiles = new HashMap<>(); + private final Map profiles = new HashMap<>(); public ProfileStorage() { instance = this; @@ -36,11 +33,11 @@ public ProfileStorage() { * @param playerName Player name * @return Found texture or null */ - public GameProfile getProfile(String playerName) { + public PlayerProfile getProfile(String playerName) { return profiles.get(playerName.toLowerCase()); } - public void add(String playerName, GameProfile profile) { + public void add(String playerName, PlayerProfile profile) { profiles.put(playerName.toLowerCase(), profile); } @@ -55,31 +52,23 @@ public static ProfileStorage instance() { @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent event) { BukkitTasks.runTaskAsync(() -> { - GameProfile profile = fetchProfile(event.getPlayer()); - - add(event.getPlayer().getName(), profile); - - if (profile != null) { - Collection texture = profile.getProperties().get("textures"); + Player player = event.getPlayer(); + PlayerProfile profile = player.getPlayerProfile(); - if (texture == null || texture.isEmpty()) - profile = MojangApi.loadProfileWithSkin(event.getPlayer().getName()); + try { + if (!profile.isComplete()) { + profile.complete(true); + } - if (profile != null) { - add(event.getPlayer().getName(), profile); + PlayerTextures textures = profile.getTextures(); + if (textures.getSkin() == null) { + add(player.getName(), DEF_PROFILE); + } else { + add(player.getName(), profile); } + } catch (Exception e) { + add(player.getName(), DEF_PROFILE); } }); } - - private GameProfile fetchProfile(Player player) { - try { - Method method = player.getClass().getMethod("getProfile"); - return (GameProfile) method.invoke(player); - } catch (Throwable t) { - Logger.warning("Cannot fetch game profile: " + t.getMessage()); - return null; - } - } - } diff --git a/src/main/java/ru/abstractmenus/util/bukkit/Skulls.java b/src/main/java/ru/abstractmenus/util/bukkit/Skulls.java index 1cad30c..3d5d826 100644 --- a/src/main/java/ru/abstractmenus/util/bukkit/Skulls.java +++ b/src/main/java/ru/abstractmenus/util/bukkit/Skulls.java @@ -1,14 +1,16 @@ package ru.abstractmenus.util.bukkit; -import com.mojang.authlib.GameProfile; +import com.destroystokyo.paper.profile.PlayerProfile; +import org.bukkit.Bukkit; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; -import ru.abstractmenus.api.Logger; +import org.bukkit.profile.PlayerTextures; import ru.abstractmenus.services.ProfileStorage; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.UUID; public final class Skulls { @@ -16,51 +18,55 @@ 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; + if (url == null || url.isEmpty()) { + return head; + } SkullMeta headMeta = (SkullMeta) head.getItemMeta(); + if (headMeta == null) { + return null; + } - if (headMeta == null) return null; - - Field profileField; + PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID()); 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(); - } + PlayerTextures textures = profile.getTextures(); + textures.setSkin(new URI(url).toURL()); + profile.setTextures(textures); + + headMeta.setPlayerProfile(profile); + head.setItemMeta(headMeta); + } catch (MalformedURLException | URISyntaxException e) { + throw new RuntimeException(String.format("Bad URL [%s] for texture [%s]", url, profile.getTextures()), e); } + return head; + } + + public static ItemStack getCustomSkull(PlayerProfile profile) { + ItemStack head = createSkullItem(); + if (profile == null) return head; + + SkullMeta headMeta = (SkullMeta) head.getItemMeta(); + if (headMeta == null) return null; + + headMeta.setPlayerProfile(profile); head.setItemMeta(headMeta); return head; } public static ItemStack getPlayerSkull(String playerName) { - GameProfile profile = ProfileStorage.instance().getProfile(playerName); + PlayerProfile 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 = Bukkit.createProfile(null, playerName); + try { + profile.complete(true); + } catch (Exception e) { profile = ProfileStorage.DEF_PROFILE; - + } ProfileStorage.instance().add(playerName, profile); } @@ -68,11 +74,6 @@ public static ItemStack getPlayerSkull(String playerName) { } public static ItemStack createSkullItem() { - try { - return new ItemStack(ItemUtil.getHeadMaterial(), 1, (short) 3); - } catch (Throwable t) { - return new ItemStack(ItemUtil.getHeadMaterial()); - } + return new ItemStack(ItemUtil.getHeadMaterial()); } - }