Skip to content

Commit

Permalink
improve SkullUtil
Browse files Browse the repository at this point in the history
- add SkullUtil#isPlayerSkull
- add SkullUtil#skull to create a new player skull
  • Loading branch information
Gabriel Dumitru authored and Matt committed Feb 23, 2022
1 parent 004c006 commit 0622ec5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static FireworkBuilder star(@NotNull final ItemStack itemStack) {
*/
@Deprecated
public ItemBuilder setSkullTexture(@NotNull final String texture) {
if (getItemStack().getType() != SkullUtil.SKULL) return this;
if (!SkullUtil.isPlayerSkull(getItemStack())) return this;

final SkullMeta skullMeta = (SkullMeta) getMeta();
final GameProfile profile = new GameProfile(UUID.randomUUID(), null);
Expand Down Expand Up @@ -256,7 +256,7 @@ public ItemBuilder setSkullTexture(@NotNull final String texture) {
*/
@Deprecated
public ItemBuilder setSkullOwner(@NotNull final OfflinePlayer player) {
if (getItemStack().getType() != SkullUtil.SKULL) return this;
if (!SkullUtil.isPlayerSkull(getItemStack())) return this;

final SkullMeta skullMeta = (SkullMeta) getMeta();
skullMeta.setOwningPlayer(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class SkullBuilder extends BaseItemBuilder<SkullBuilder> {
Field field;

try {
final SkullMeta skullMeta = (SkullMeta) new ItemStack(SkullUtil.SKULL).getItemMeta();
final SkullMeta skullMeta = (SkullMeta) SkullUtil.skull().getItemMeta();
field = skullMeta.getClass().getDeclaredField("profile");
field.setAccessible(true);
} catch (NoSuchFieldException e) {
Expand All @@ -61,12 +61,12 @@ public final class SkullBuilder extends BaseItemBuilder<SkullBuilder> {
}

SkullBuilder() {
super(new ItemStack(SkullUtil.SKULL));
super(SkullUtil.skull());
}

SkullBuilder(final @NotNull ItemStack itemStack) {
super(itemStack);
if (itemStack.getType() != SkullUtil.SKULL) {
if (!SkullUtil.isPlayerSkull(itemStack)) {
throw new GuiException("SkullBuilder requires the material to be a PLAYER_HEAD/SKULL_ITEM!");
}
}
Expand All @@ -81,7 +81,7 @@ public final class SkullBuilder extends BaseItemBuilder<SkullBuilder> {
@NotNull
@Contract("_, _ -> this")
public SkullBuilder texture(@NotNull final String texture, @NotNull final UUID profileId) {
if (getItemStack().getType() != SkullUtil.SKULL) return this;
if (!SkullUtil.isPlayerSkull(getItemStack())) return this;

if (PROFILE_FIELD == null) {
return this;
Expand Down Expand Up @@ -122,7 +122,7 @@ public SkullBuilder texture(@NotNull final String texture) {
@NotNull
@Contract("_ -> this")
public SkullBuilder owner(@NotNull final OfflinePlayer player) {
if (getItemStack().getType() != SkullUtil.SKULL) return this;
if (!SkullUtil.isPlayerSkull(getItemStack())) return this;

final SkullMeta skullMeta = (SkullMeta) getMeta();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* MIT License
*
* <p>
* Copyright (c) 2021 TriumphTeam
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -24,13 +24,15 @@
package dev.triumphteam.gui.components.util;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public final class SkullUtil {

/**
* The main SKULL material for the version
*/
public static final Material SKULL = getSkullMaterial();
private static final Material SKULL = getSkullMaterial();

/**
* Gets the correct {@link Material} for the version
Expand All @@ -42,7 +44,32 @@ private static Material getSkullMaterial() {
return Material.valueOf("SKULL_ITEM");
}

return Material.valueOf("PLAYER_HEAD");
return Material.PLAYER_HEAD;
}

/**
* Create a player skull
*
* @return player skull
*/
@SuppressWarnings("deprecation")
public static ItemStack skull() {
return VersionHelper.IS_ITEM_LEGACY ? new ItemStack(SKULL, 1, (short) 3) : new ItemStack(SKULL);
}

/**
* Checks if an {@link ItemStack} is a player skull
*
* @param item item to check
* @return whether the item is a player skull
*/
@SuppressWarnings("deprecation")
public static boolean isPlayerSkull(@NotNull final ItemStack item) {
if (VersionHelper.IS_ITEM_LEGACY) {
return item.getType() == SKULL && item.getDurability() == (short) 3;
}

return item.getType() == SKULL;
}

}

0 comments on commit 0622ec5

Please sign in to comment.