Skip to content

Commit

Permalink
Add ItemUtils.skull
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Dec 23, 2021
1 parent 8ff3eec commit c7c6f26
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion res/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: RedLib
main: redempt.redlib.RedLib
version: 2021-12-23 05:52
version: 2021-12-23 22:17
author: Redempt
api-version: 1.13
load: STARTUP
32 changes: 31 additions & 1 deletion src/redempt/redlib/itemutils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
Expand All @@ -15,6 +16,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import redempt.redlib.RedLib;
import redempt.redlib.json.JSONList;
Expand All @@ -23,7 +25,12 @@
import redempt.redlib.nms.NMSHelper;

import java.lang.reflect.Method;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiPredicate;

/**
Expand All @@ -33,6 +40,29 @@
*/
public class ItemUtils {

/**
* Creates a player skull
* @param owner The owning player
* @return The skull for the player
*/
public static ItemStack skull(OfflinePlayer owner) {
ItemStack base = getBaseSkull();
SkullMeta meta = (SkullMeta) base.getItemMeta();
if (RedLib.MID_VERSION >= 13) {
meta.setOwningPlayer(owner);
} else {
meta.setOwner(owner.getName());
}
base.setItemMeta(meta);
return base;
}

private static Material skullType = RedLib.MID_VERSION >= 13 ? Material.valueOf("PLAYER_HEAD") : Material.valueOf("SKULL_ITEM");

private static ItemStack getBaseSkull() {
return RedLib.MID_VERSION >= 13 ? new ItemStack(skullType) : new ItemStack(skullType, 1, (short) 3);
}

/**
* Renames an ItemStack, functionally identical to {@link ItemUtils#setName(ItemStack, String)} but kept for legacy reasons
* @param item The ItemStack to be renamed
Expand Down

0 comments on commit c7c6f26

Please sign in to comment.