Skip to content

issue-1 fix GameProfile Skulls #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AbstractMenus

<a href="https://github.com/AbstractMenus/plugin/blob/master/LICENSE"><img src="https://img.shields.io/badge/License-MIT-red.svg" alt="license"/></a>
<a href="https://github.com/AbstractMenus/plugin/blob/master/LICENSE"><img src="https://img.shields.io/badge/version-1.17.4--beta-blue" alt="license"/></a>
<a href="https://github.com/AbstractMenus/plugin/blob/master/LICENSE"><img src="https://img.shields.io/badge/version-1.17.5--beta-blue" alt="license"/></a>

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.

Expand All @@ -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)
Expand All @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ java {


group 'ru.abstractmenus'
version '1.17.4-beta'
version '1.17.5-beta'

repositories {
mavenLocal()
Expand Down
53 changes: 21 additions & 32 deletions src/main/java/ru/abstractmenus/services/ProfileStorage.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, GameProfile> profiles = new HashMap<>();
private final Map<String, PlayerProfile> profiles = new HashMap<>();

public ProfileStorage() {
instance = this;
Expand All @@ -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);
}

Expand All @@ -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<Property> 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;
}
}

}
79 changes: 40 additions & 39 deletions src/main/java/ru/abstractmenus/util/bukkit/Skulls.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,79 @@
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 {

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

return getCustomSkull(profile);
}

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

}