use library skull functions

This commit is contained in:
jascotty2 2019-09-09 10:25:44 -05:00
parent 7980340e17
commit 46bb5e346a
8 changed files with 22 additions and 91 deletions

View File

@ -1,6 +1,7 @@
package com.songoda.epicheads.commands;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.utils.Methods;
import org.bukkit.command.CommandSender;
@ -28,7 +29,7 @@ public class CommandBase64 extends AbstractCommand {
if (!item.hasItemMeta() || !(item.getItemMeta() instanceof SkullMeta)) return ReturnType.FAILURE;
String encodededStr = Methods.getEncodedTexture(item);
String encodededStr = ItemUtils.getSkullTexture(item);
if (encodededStr == null) return ReturnType.FAILURE;

View File

@ -1,6 +1,7 @@
package com.songoda.epicheads.commands;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.utils.Methods;
import org.bukkit.command.CommandSender;
@ -27,11 +28,11 @@ public class CommandUrl extends AbstractCommand {
if (!item.hasItemMeta() || !(item.getItemMeta() instanceof SkullMeta)) return ReturnType.FAILURE;
String encodededStr = Methods.getEncodedTexture(item);
String encodededStr = ItemUtils.getSkullTexture(item);
if (encodededStr == null) return ReturnType.FAILURE;
String url = Methods.getDecodedTexture(encodededStr);
String url = ItemUtils.getDecodedTexture(encodededStr);
instance.getLocale().newMessage("http://textures.minecraft.net/texture/" + url).sendPrefixedMessage(player);

View File

@ -3,6 +3,7 @@ package com.songoda.epicheads.gui;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.gui.Gui;
import com.songoda.core.gui.GuiUtils;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.head.Category;
import com.songoda.epicheads.head.Head;
@ -70,7 +71,7 @@ public class GUIOverview extends Gui {
if (!player.hasPermission("epicheads.category." + category.getName().replace(" ", "_"))) continue;
setButton(i + 10 + add, GuiUtils.createButtonItem(Methods.addTexture(CompatibleMaterial.PLAYER_HEAD.getItem(), firstHead.getURL()),
setButton(i + 10 + add, GuiUtils.createButtonItem(ItemUtils.getCustomHead(firstHead.getURL()),
plugin.getLocale().getMessage("gui.overview.headname")
.processPlaceholder("name", Color.getRandomColor() + category.getName())
.getMessage(),
@ -89,7 +90,7 @@ public class GUIOverview extends Gui {
(event) -> GUIHeads.doSearch(plugin, this, guiManager, event.player));
if (Settings.DISCORD.getBoolean()) {
setButton(41, GuiUtils.createButtonItem(Methods.addTexture(CompatibleMaterial.PLAYER_HEAD.getItem(),
setButton(41, GuiUtils.createButtonItem(ItemUtils.getCustomHead(
"a3b183b148b9b4e2b158334aff3b5bb6c2c2dbbc4d67f76a7be856687a2b623"),
plugin.getLocale().getMessage("gui.overview.discord").getMessage(),
plugin.getLocale().getMessage("gui.overview.discordlore").getMessage().split("\\|")),

View File

@ -1,6 +1,7 @@
package com.songoda.epicheads.head;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.settings.Settings;
import com.songoda.epicheads.utils.Methods;
@ -62,7 +63,7 @@ public class Head {
}
public ItemStack asItemStack(boolean favorite, boolean free) {
ItemStack item = Methods.addTexture(CompatibleMaterial.PLAYER_HEAD.getItem(), this.URL);
ItemStack item = ItemUtils.getCustomHead(this.URL);
ItemMeta meta = item.getItemMeta();
if(meta != null) {

View File

@ -1,6 +1,7 @@
package com.songoda.epicheads.listeners;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.head.Head;
import com.songoda.epicheads.settings.Settings;
@ -36,7 +37,7 @@ public class DeathListeners implements Listener {
if (event.getEntity() instanceof Player) {
if (!Settings.DROP_PLAYER_HEADS.getBoolean()) return;
String encodededStr = Methods.getEncodedTexture((Player) event.getEntity());
String encodededStr = ItemUtils.getSkullTexture((Player) event.getEntity());
if (encodededStr == null) {
itemNew = CompatibleMaterial.PLAYER_HEAD.getItem();
@ -46,7 +47,7 @@ public class DeathListeners implements Listener {
itemNew.setItemMeta(meta);
} else {
String url = Methods.getDecodedTexture(encodededStr);
String url = ItemUtils.getDecodedTexture(encodededStr);
Optional<Head> optional = plugin.getHeadManager().getHeads().stream()
.filter(head -> url.equals(head.getURL())).findFirst();

View File

@ -1,6 +1,7 @@
package com.songoda.epicheads.listeners;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.head.Head;
import com.songoda.epicheads.utils.Methods;
@ -28,11 +29,11 @@ public class ItemListeners implements Listener {
if (!CompatibleMaterial.PLAYER_HEAD.matches(item)) return;
String encodededStr = Methods.getEncodedTexture(item);
String encodededStr = ItemUtils.getSkullTexture(item);
if (encodededStr == null) return;
String url = Methods.getDecodedTexture(encodededStr);
String url = ItemUtils.getDecodedTexture(encodededStr);
if (url == null) return;
Optional<Head> optional = plugin.getHeadManager().getHeads().stream()

View File

@ -1,5 +1,6 @@
package com.songoda.epicheads.listeners;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.head.Head;
import com.songoda.epicheads.head.HeadManager;
@ -26,11 +27,11 @@ public class LoginListeners implements Listener {
Player player = event.getPlayer();
String encodededStr = Methods.getEncodedTexture(player);
String encodededStr = ItemUtils.getSkullTexture(player);
if (encodededStr == null) return;
String url = Methods.getDecodedTexture(encodededStr);
String url = ItemUtils.getDecodedTexture(encodededStr);
String tagStr = "Player Heads";

View File

@ -1,96 +1,20 @@
package com.songoda.epicheads.utils;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.settings.Settings;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
public class Methods {
private static Class<?> clazzCraftPlayer;
private static Method methodGetProfile;
public static ItemStack addTexture(ItemStack item, String headURL) {
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (headURL == null) return item;
GameProfile profile = new GameProfile(UUID.nameUUIDFromBytes(headURL.getBytes()), null);
byte[] encodedData = Base64.getEncoder().encode(String.format("{textures:{SKIN:{url:\"http://textures.minecraft.net/texture/%s\"}}}", new Object[]{headURL}).getBytes());
profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
Field profileField;
try {
profileField = meta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(meta, profile);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
item.setItemMeta(meta);
return item;
}
public static String getEncodedTexture(ItemStack item) {
try {
SkullMeta localSkullMeta = (SkullMeta) item.getItemMeta();
Field localField = localSkullMeta.getClass().getDeclaredField("profile");
localField.setAccessible(true);
GameProfile profile = (GameProfile) localField.get(localSkullMeta);
Iterator<Property> iterator = profile.getProperties().get("textures").iterator();
if (!iterator.hasNext()) return null;
Property property = iterator.next();
return property.getValue();
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static String getEncodedTexture(Player player) {
try {
if (clazzCraftPlayer == null) {
String ver = Bukkit.getServer().getClass().getPackage().getName().substring(23);
clazzCraftPlayer = Class.forName("org.bukkit.craftbukkit." + ver + ".entity.CraftPlayer");
methodGetProfile = clazzCraftPlayer.getMethod("getProfile");
}
Object craftPlayer = clazzCraftPlayer.cast(player);
Iterator<Property> iterator = ((GameProfile) methodGetProfile.invoke(craftPlayer)).getProperties().get("textures").iterator();
if (!iterator.hasNext()) return null;
Property property = iterator.next();
return property.getValue();
} catch (ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public static String getDecodedTexture(String encoded) {
return StringUtils.substringBetween(new String(Base64.getDecoder().decode(encoded)), "texture/", "\"");
}
public static ItemStack createToken(int amount) {
ItemStack itemStack = new ItemStack(Material.valueOf(Settings.ITEM_TOKEN_TYPE.getString()));