From fcdbd45aa0b7da3962c6de272fd17fdd1ad736d6 Mon Sep 17 00:00:00 2001 From: J3fftw <44972470+J3fftw1@users.noreply.github.com> Date: Tue, 16 Jan 2024 21:31:17 +0100 Subject: [PATCH] fix slimefun#4097 (#246) --- .../dough/skins/CustomGameProfile.java | 12 +++--- .../dough/skins/TestCustomGameProfile.java | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 dough-skins/src/test/java/io/github/bakedlibs/dough/skins/TestCustomGameProfile.java diff --git a/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/CustomGameProfile.java b/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/CustomGameProfile.java index 8dd84703..7976a7bb 100644 --- a/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/CustomGameProfile.java +++ b/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/CustomGameProfile.java @@ -6,7 +6,6 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import com.mojang.authlib.properties.PropertyMap; import org.bukkit.Bukkit; import org.bukkit.inventory.meta.SkullMeta; @@ -25,12 +24,12 @@ public final class CustomGameProfile extends GameProfile { * The player name for this profile. * "CS-CoreLib" for historical reasons and backwards compatibility. */ - static final String PLAYER_NAME = "CS-CoreLib"; + private static final String PLAYER_NAME = "CS-CoreLib"; /** * The skin's property key. */ - static final String PROPERTY_KEY = "textures"; + private static final String PROPERTY_KEY = "textures"; private final URL skinUrl; private final String texture; @@ -46,16 +45,15 @@ public final class CustomGameProfile extends GameProfile { } void apply(@Nonnull SkullMeta meta) throws NoSuchFieldException, IllegalAccessException, UnknownServerVersionException { - - // Forces SkullMeta to properly deserialize and serialize the profile - // setOwnerProfile was added in 1.18, but setOwningPlayer throws a NullPointerException since 1.20.2 + // setOwnerProfile was added in 1.18, but getOwningPlayer throws a NullPointerException since 1.20.2 if (MinecraftVersion.get().isAtLeast(MinecraftVersion.parse("1.20"))) { - PlayerProfile playerProfile = Bukkit.createPlayerProfile(UUID.randomUUID(), PLAYER_NAME); + PlayerProfile playerProfile = Bukkit.createPlayerProfile(this.getId(), PLAYER_NAME); PlayerTextures playerTextures = playerProfile.getTextures(); playerTextures.setSkin(this.skinUrl); playerProfile.setTextures(playerTextures); meta.setOwnerProfile(playerProfile); } else { + // Forces SkullMeta to properly deserialize and serialize the profile ReflectionUtils.setFieldValue(meta, "profile", this); meta.setOwningPlayer(meta.getOwningPlayer()); diff --git a/dough-skins/src/test/java/io/github/bakedlibs/dough/skins/TestCustomGameProfile.java b/dough-skins/src/test/java/io/github/bakedlibs/dough/skins/TestCustomGameProfile.java new file mode 100644 index 00000000..906dc676 --- /dev/null +++ b/dough-skins/src/test/java/io/github/bakedlibs/dough/skins/TestCustomGameProfile.java @@ -0,0 +1,38 @@ +package io.github.bakedlibs.dough.skins; + +import org.junit.jupiter.api.BeforeAll; + +import be.seeseemelk.mockbukkit.MockBukkit; + +class TestCustomGameProfile { + + @BeforeAll + static void setup() { + MockBukkit.mock(); + } + + // Test for issue Slimefun#4097 + // TODO: Enable - needs MockBukkit updated to 1.20 + /* + @Test + void testUuidIsSameAsProfile() throws NoSuchFieldException, IllegalAccessException, UnknownServerVersionException { + UUID expectedUUID = UUID.randomUUID(); + + CustomGameProfile gameProfile = new CustomGameProfile( + expectedUUID, + "test", + null + ); + + ItemStack itemStack = new ItemStack(Material.PLAYER_HEAD); + ItemMeta meta = itemStack.getItemMeta(); + SkullMeta skullMeta = (SkullMeta) meta; + + gameProfile.apply(skullMeta); + itemStack.setItemMeta(skullMeta); + + // Assert the applied skin has the same UUID + Assertions.assertEquals(expectedUUID, skullMeta.getOwningPlayer().getUniqueId()); + } + */ +}