Guest User

Heads.java

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