CachedPlayerHeads/src/main/java/org/warp/cachedplayerheads/ReflectionHandler.java
Andrea Cavalli 9d1f489996 First commit
2021-04-19 20:53:45 +02:00

108 lines
4.1 KiB
Java

package org.warp.cachedplayerheads;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.mojang.authlib.properties.Property;
import dev.dbassett.skullcreator.SkullCreator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import net.skinsrestorer.shared.exception.SkinRequestException;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
class ReflectionHandler {
private static final ExecutorService blockingTextures = Executors.newFixedThreadPool(4);
static CompletableFuture<ItemStack> getSkullItem(Player player) {
return ReflectionHandler
.getTexture(player)
.thenComposeAsync(texture -> getSkullItemFromTexture(player.getName(), texture), blockingTextures);
}
static CompletableFuture<ItemStack> getSkullItem(Player skinPlayer, Player namePlayer) {
return ReflectionHandler
.getTexture(skinPlayer)
.thenComposeAsync(texture -> getSkullItemFromTexture(namePlayer.getName(), texture), blockingTextures);
}
public static CompletableFuture<ItemStack> getSkullItem(String playerName) {
return ReflectionHandler
.getTexture(playerName)
.thenComposeAsync(texture -> getSkullItemFromTexture(playerName, texture), blockingTextures);
}
public static CompletableFuture<ItemStack> getSkullItem(String skinPlayerName, String namePlayerName) {
return ReflectionHandler
.getTexture(skinPlayerName)
.thenComposeAsync(texture -> getSkullItemFromTexture(namePlayerName, texture), blockingTextures);
}
public static CompletableFuture<ItemStack> getSkullItemFromTexture(String playerName, String texture) {
return CompletableFuture
.supplyAsync(() -> {
ItemStack item;
if (texture == null) {
// hardcoded steve skin
item = SkullCreator.itemFromBase64("eyJ0aW1lc3RhbXAiOjE1MDA4MzU1ODY5ODcsInByb2ZpbGVJZCI6ImMxZWQ5N2Q0ZDE2NzQyYzI5OGI1ODFiZmRiODhhMjFmIiwicHJvZmlsZU5hbWUiOiJ5b2xvX21hdGlzIiwic2lnbmF0dXJlUmVxdWlyZWQiOnRydWUsInRleHR1cmVzIjp7IlNLSU4iOnsidXJsIjoiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS9jYjNjY2NkOTUzNjVjNWQ4NTY0NGE1MzZlNjliNGJlNThlYmZiZjE2ZjQzY2Y1NjE3ODZiNzRkYTJiOGVlYiJ9fX0");
} else {
item = SkullCreator.itemFromBase64(texture);
}
SkullMeta skullMeta = (SkullMeta) item.getItemMeta();
PlayerProfile playerProfile = skullMeta.getPlayerProfile();
if (playerProfile != null) {
playerProfile.setName(playerName);
skullMeta.setPlayerProfile(playerProfile);
}
item.setItemMeta(skullMeta);
ItemMeta itemMeta = item.getItemMeta();
item.setItemMeta(itemMeta);
return item;
}, blockingTextures);
}
static CompletableFuture<String> getTexture(Player player) {
return getTexture(player.getName());
}
static CompletableFuture<String> getTexture(String playerName) {
try {
Function<String, CompletableFuture<String>> downloader = texture -> CompletableFuture
.supplyAsync(() -> {
if (texture != null) {
return texture;
}
// Try to download online skin as fallback
String uuid = null;
try {
uuid = Playerheads.mojangAPI.getUUID(playerName, true);
} catch (SkinRequestException e) {
return null;
}
Property prop = ((Property) Playerheads.mojangAPI.getSkinProperty(uuid));
if (prop != null) {
return prop.getValue();
}
return null;
}, blockingTextures);
if (Playerheads.skinsRestorerAPI != null) {
return CompletableFuture
.supplyAsync(() ->
SkinsRestorerAPIUtils.getSkinData(Playerheads.skinsRestorerAPI, playerName), blockingTextures)
.thenComposeAsync(downloader, blockingTextures);
} else if (Playerheads.skinsRestorer.isBungeeEnabled()) {
return Playerheads.bungeeAPI.getPlayerTexture(playerName)
.thenComposeAsync(downloader, blockingTextures);
} else {
return downloader.apply(null);
}
} catch (Exception ex) {
ex.printStackTrace();
return CompletableFuture.completedFuture(null);
}
}
}