extras/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java

116 lines
4 KiB
Java
Raw Normal View History

2019-12-17 12:37:59 +00:00
package pw.kaboom.extras.helpers;
2019-12-02 16:12:05 +00:00
2019-12-20 19:05:02 +00:00
import java.io.IOException;
2019-12-02 16:12:05 +00:00
import java.io.InputStreamReader;
import java.net.URL;
2020-01-22 14:33:19 +00:00
import java.util.HashMap;
2019-12-21 14:12:26 +00:00
import java.util.UUID;
2019-12-17 18:23:24 +00:00
2019-12-02 16:12:05 +00:00
import javax.net.ssl.HttpsURLConnection;
2019-12-02 17:07:05 +00:00
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
2022-05-20 21:52:15 +00:00
import net.kyori.adventure.text.Component;
2019-12-02 16:12:05 +00:00
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
2019-12-17 12:37:59 +00:00
import pw.kaboom.extras.Main;
2019-12-21 13:39:31 +00:00
public final class SkinDownloader {
2022-05-20 02:35:48 +00:00
private static HashMap<UUID, PlayerProfile> skinProfiles = new HashMap<UUID, PlayerProfile>();
private HttpsURLConnection skinConnection;
private InputStreamReader skinStream;
private String texture;
private String signature;
public void applySkin(final Player player, final String name, final boolean shouldSendMessage) {
new BukkitRunnable() {
@Override
public void run() {
final PlayerProfile profile = player.getPlayerProfile();
try {
fetchSkinData(name);
profile.setProperty(new ProfileProperty("textures", texture, signature));
if (shouldSendMessage) {
player.sendMessage(
Component.text("Successfully set your skin to ")
.append(Component.text(name))
.append(Component.text("'s"))
);
2022-05-20 02:35:48 +00:00
}
} catch (Exception exception) {
try {
skinStream.close();
skinConnection.disconnect();
} catch (Exception ignored) {
}
if (shouldSendMessage) {
player.sendMessage(Component
.text("A player with that username doesn't exist"));
2022-05-20 02:35:48 +00:00
}
return;
}
new BukkitRunnable() {
@Override
public void run() {
try {
player.setPlayerProfile(profile);
} catch (Exception ignored) {
}
}
}.runTask(JavaPlugin.getPlugin(Main.class));
}
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
}
public void fillJoinProfile(final PlayerProfile profile, final String name, final UUID uuid) {
try {
fetchSkinData(name);
profile.setProperty(new ProfileProperty("textures", texture, signature));
skinProfiles.put(uuid, profile);
} catch (Exception exception) {
try {
skinStream.close();
skinConnection.disconnect();
} catch (Exception ignored) {
}
}
}
private void fetchSkinData(final String playerName) throws IOException {
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName);
skinConnection = (HttpsURLConnection) skinUrl.openConnection();
skinConnection.setConnectTimeout(0);
skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject responseJson = new JsonParser().parse(skinStream).getAsJsonObject();
final JsonObject rawSkin = responseJson.getAsJsonObject("textures").getAsJsonObject("raw");
texture = rawSkin.get("value").getAsString();
signature = rawSkin.get("signature").getAsString();
skinStream.close();
skinConnection.disconnect();
}
public static PlayerProfile getProfile(final UUID uuid) {
return skinProfiles.get(uuid);
}
public static void removeProfile(final UUID uuid) {
skinProfiles.remove(uuid);
}
}