Skip to content

Commit

Permalink
fix slimefun#4097 (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
J3fftw1 authored Jan 16, 2024
1 parent a2364de commit fcdbd45
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
*/
}

0 comments on commit fcdbd45

Please sign in to comment.