Guest User

Heads.java

a guest
Mar 14th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. import com.destroystokyo.paper.profile.PlayerProfile;
  2. import com.destroystokyo.paper.profile.ProfileProperty;
  3. import com.google.gson.JsonObject;
  4. import com.google.gson.JsonParser;
  5. import lombok.NonNull;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.block.Skull;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.inventory.meta.ItemMeta;
  13. import org.bukkit.inventory.meta.SkullMeta;
  14.  
  15. import javax.annotation.Nullable;
  16. import java.util.Base64;
  17. import java.util.UUID;
  18.  
  19. // Author: Dymeth.Ru
  20. public class Heads {
  21.     public static void cloneProfile(@NonNull ItemStack from, @NonNull ItemStack to) {
  22.         ItemMeta fromMeta = from.getItemMeta();
  23.         if (!(fromMeta instanceof SkullMeta)) throw new IllegalArgumentException();
  24.         if (to.getType() != Material.PLAYER_HEAD) to.setType(Material.PLAYER_HEAD);
  25.         SkullMeta toMeta = (SkullMeta) to.getItemMeta();
  26.         toMeta.setPlayerProfile(((SkullMeta) fromMeta).getPlayerProfile());
  27.         to.setItemMeta(toMeta);
  28.     }
  29.  
  30.     @NonNull
  31.     public static ItemStack getHeadByRawData(@NonNull String data) {
  32.         data = replaceFirst(data, "http://");
  33.         data = replaceFirst(data, "https://");
  34.         data = replaceFirst(data, "textures.minecraft.net/texture/");
  35.         if (data.length() <= 16) {
  36.             return getHeadByLicenseName(data);
  37.         } else if (data.length() <= 64) {
  38.             return getHeadByHash(data);
  39.         } else {
  40.             return getHeadByTextureData(data, !data.startsWith("{"));
  41.         }
  42.     }
  43.  
  44.     @NonNull
  45.     private static String replaceFirst(@NonNull String input, @NonNull String prefix) {
  46.         if (!input.startsWith(prefix)) {
  47.             return input;
  48.         }
  49.         return input.substring(prefix.length());
  50.     }
  51.  
  52.     @NonNull
  53.     public static ItemStack getHeadByLicenseName(@NonNull String name) {
  54.         return createPlayerHeadWithProfile(Bukkit.createProfile(UUID.randomUUID(), name));
  55.     }
  56.  
  57.     @NonNull
  58.     public static ItemStack getHeadByGamer(@NonNull Player player) {
  59.         return createPlayerHeadWithProfile(player.getPlayerProfile());
  60.     }
  61.  
  62.     @NonNull
  63.     public static ItemStack getHeadByHash(@NonNull String hash) {
  64.         return getHeadByTextureData(
  65.                 "{\"textures\":{\"SKIN\":{\"url\":\"https://textures.minecraft.net/texture/" + hash + "\"}}}",
  66.                 false
  67.         );
  68.     }
  69.  
  70.     @NonNull
  71.     public static ItemStack getHeadByTextureData(@NonNull String value, boolean base64) {
  72.         PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID(), null);
  73.         if (!base64) {
  74.             value = new String(Base64.getEncoder().encode(value.getBytes()));
  75.         }
  76.         profile.setProperty(new ProfileProperty("textures", value));
  77.         return createPlayerHeadWithProfile(profile);
  78.     }
  79.  
  80.     @NonNull
  81.     public static ItemStack getHeadWithoutSkin() {
  82.         return createPlayerHeadWithProfile(null);
  83.     }
  84.  
  85.     @NonNull
  86.     private static ItemStack createPlayerHeadWithProfile(@Nullable PlayerProfile profile) {
  87.         ItemStack head = new ItemStack(Material.PLAYER_HEAD);
  88.         if (profile != null) {
  89.             SkullMeta headMeta = (SkullMeta) head.getItemMeta();
  90.             headMeta.setPlayerProfile(profile);
  91.             head.setItemMeta(headMeta);
  92.         }
  93.         return head;
  94.     }
  95.  
  96.     @Nullable
  97.     public static String getBlockSkinHash(@NonNull Block block) {
  98.         if (!(block.getState() instanceof Skull state)) return null;
  99.         PlayerProfile oldProfile = state.getPlayerProfile();
  100.         if (oldProfile == null) return null;
  101.         String headHash = getProfileSkinHash(oldProfile);
  102.         if (headHash == null) {
  103.             throw new IllegalArgumentException("Не удалось найти хеш скина блока головы на координатах "
  104.                     + block.getWorld().getName()
  105.                     + " " + block.getX()
  106.                     + " " + block.getY()
  107.                     + " " + block.getZ());
  108.         }
  109.         return headHash;
  110.     }
  111.  
  112.     @Nullable
  113.     public static String getProfileSkinHash(@NonNull PlayerProfile profile) {
  114.         String url = getProfileSkinUrl(profile);
  115.         return url == null ? null : url.substring(url.lastIndexOf("/") + 1);
  116.     }
  117.  
  118.     @Nullable
  119.     public static String getProfileSkinUrl(@NonNull PlayerProfile profile) {
  120.         for (ProfileProperty property : profile.getProperties()) {
  121.             if (property.getName().equals("textures")) {
  122.                 String json = new String(Base64.getDecoder().decode(property.getValue()));
  123.                 JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
  124.                 jsonObject = jsonObject.getAsJsonObject("textures").getAsJsonObject("SKIN");
  125.                 return jsonObject.get("url").getAsString();
  126.             }
  127.         }
  128.         return null;
  129.     }
  130. }
  131.  
Add Comment
Please, Sign In to add comment