From 035d894e88376a170d883d7c5e6339284e69164e Mon Sep 17 00:00:00 2001
From: EpicKnarvik97
Date: Thu, 19 Jan 2023 15:54:30 +0100
Subject: [PATCH] Adds methods for getting head skins from id
---
.../item/PlayerHeadGUIItemFactory.java | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java b/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java
index 7611151..116f23f 100644
--- a/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java
+++ b/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java
@@ -7,7 +7,12 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.profile.PlayerProfile;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Base64;
import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* An item factory for generating player head-based icons
@@ -15,6 +20,9 @@ import java.util.UUID;
@SuppressWarnings("unused")
public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactory {
+ private static final String textureBaseURL = "https://textures.minecraft.net/texture/";
+ private static final Pattern texturePattern = Pattern.compile("/texture/([0-9a-z]+)");
+
/**
* Instantiates a player head gui item factory
*/
@@ -53,4 +61,43 @@ public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactoryThis methods takes the id after "
+ * https://textures.minecraft.net/texture/" as input. A base64 JSON value can also be used, though it's not
+ * recommended.
+ *
+ * @param textureId The id of the texture to use
+ * @return The factory. Used for chaining commands
+ */
+ public PlayerHeadGUIItemFactory useSkin(String textureId) {
+ //Get the texture id from a Base64 encoded JSON string
+ if (!textureId.isBlank() && textureId.length() % 4 == 0) {
+ String decoded = new String(Base64.getDecoder().decode(textureId));
+ Matcher matcher = texturePattern.matcher(decoded);
+ if (matcher.find()) {
+ textureId = matcher.group(1);
+ }
+ }
+
+ try {
+ return useSkin(new URL(textureBaseURL + textureId));
+ } catch (MalformedURLException exception) {
+ throw new IllegalArgumentException("Texture id resulted in invalid URL");
+ }
+ }
+
+ /**
+ * Uses the head skin from the given URL
+ *
+ * @param url The URL to use
+ * @return The factory. Used for chaining commands
+ */
+ public PlayerHeadGUIItemFactory useSkin(URL url) {
+ PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID());
+ profile.getTextures().setSkin(url);
+ return changeItemMeta(SkullMeta.class, (meta) -> meta.setOwnerProfile(profile));
+ }
+
}