Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.destroystokyo.paper.profile.PlayerProfile;
- import com.destroystokyo.paper.profile.ProfileProperty;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import lombok.NonNull;
- import org.bukkit.Bukkit;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.Skull;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.SkullMeta;
- import javax.annotation.Nullable;
- import java.util.Base64;
- import java.util.UUID;
- public class Heads {
- public static void cloneProfile(@NonNull ItemStack from, @NonNull ItemStack to) {
- ItemMeta fromMeta = from.getItemMeta();
- if (!(fromMeta instanceof SkullMeta)) throw new IllegalArgumentException();
- if (to.getType() != Material.PLAYER_HEAD) to.setType(Material.PLAYER_HEAD);
- SkullMeta toMeta = (SkullMeta) to.getItemMeta();
- toMeta.setPlayerProfile(((SkullMeta) fromMeta).getPlayerProfile());
- to.setItemMeta(toMeta);
- }
- @NonNull
- public static ItemStack getHeadByRawData(@NonNull String data) {
- data = replaceFirst(data, "http://");
- data = replaceFirst(data, "https://");
- data = replaceFirst(data, "textures.minecraft.net/texture/");
- if (data.length() <= 16) {
- return getHeadByLicenseName(data);
- } else if (data.length() <= 64) {
- return getHeadByHash(data);
- } else {
- return getHeadByTextureData(data, true);
- }
- }
- @NonNull
- private static String replaceFirst(@NonNull String input, @NonNull String prefix) {
- if (!input.startsWith(prefix)) {
- return input;
- }
- return input.substring(prefix.length());
- }
- @NonNull
- public static ItemStack getHeadByLicenseName(@NonNull String name) {
- return createPlayerHeadWithProfile(Bukkit.createProfile(UUID.randomUUID(), name));
- }
- @NonNull
- public static ItemStack getHeadByGamer(@NonNull Player player) {
- return createPlayerHeadWithProfile(player.getPlayerProfile());
- }
- @NonNull
- public static ItemStack getHeadByHash(@NonNull String hash) {
- return getHeadByTextureData(
- "{\"textures\":{\"SKIN\":{\"url\":\"https://textures.minecraft.net/texture/" + hash + "\"}}}",
- false
- );
- }
- @NonNull
- public static ItemStack getHeadByTextureData(@NonNull String value, boolean base64) {
- PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID(), null);
- if (!base64) {
- value = new String(Base64.getEncoder().encode(value.getBytes()));
- }
- profile.setProperty(new ProfileProperty("textures", value));
- return createPlayerHeadWithProfile(profile);
- }
- @NonNull
- public static ItemStack getHeadWithoutSkin() {
- return createPlayerHeadWithProfile(null);
- }
- @NonNull
- private static ItemStack createPlayerHeadWithProfile(@Nullable PlayerProfile profile) {
- ItemStack head = new ItemStack(Material.PLAYER_HEAD);
- if (profile != null) {
- SkullMeta headMeta = (SkullMeta) head.getItemMeta();
- headMeta.setPlayerProfile(profile);
- head.setItemMeta(headMeta);
- }
- return head;
- }
- @Nullable
- public static String getBlockSkinHash(@NonNull Block block) {
- if (!(block.getState() instanceof Skull state)) return null;
- PlayerProfile oldProfile = state.getPlayerProfile();
- if (oldProfile == null) return null;
- String headHash = getProfileSkinHash(oldProfile);
- if (headHash == null)
- throw new IllegalArgumentException("Не удалось найти хеш скина блока головы на координатах "
- + block.getWorld().getName()
- + " " + block.getX()
- + " " + block.getY()
- + " " + block.getZ());
- return headHash;
- }
- @Nullable
- public static String getProfileSkinHash(@NonNull PlayerProfile profile) {
- String url = getProfileSkinUrl(profile);
- return url == null ? null : url.substring(url.lastIndexOf("/") + 1);
- }
- @Nullable
- public static String getProfileSkinUrl(@NonNull PlayerProfile profile) {
- for (ProfileProperty property : profile.getProperties()) {
- if (property.getName().equals("textures")) {
- String json = new String(Base64.getDecoder().decode(property.getValue()));
- JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
- jsonObject = jsonObject.getAsJsonObject("textures").getAsJsonObject("SKIN");
- return jsonObject.get("url").getAsString();
- }
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment