mirror of
https://github.com/kaboomserver/extras.git
synced 2025-07-24 14:33:47 +00:00
Move skin downloader to separate class
This commit is contained in:
parent
4fe8d0ee8d
commit
551f9f4807
5 changed files with 109 additions and 125 deletions
|
@ -1,12 +1,5 @@
|
|||
package pw.kaboom.extras;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodyHandlers;
|
||||
import java.net.URI;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
|
@ -40,46 +33,31 @@ class CommandUsername implements CommandExecutor {
|
|||
|
||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
||||
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://api.ashcon.app/mojang/v2/user/" + nameShort.replace(" ", "%20")))
|
||||
.build();
|
||||
client.sendAsync(request, BodyHandlers.ofInputStream())
|
||||
.thenAccept(response -> {
|
||||
String texture = "";
|
||||
String signature = "";
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
final InputStreamReader skinStream = new InputStreamReader(response.body());
|
||||
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();
|
||||
try {
|
||||
skinStream.close();
|
||||
} catch (Exception exception) {
|
||||
System.out.println(exception);
|
||||
}
|
||||
}
|
||||
|
||||
final PlayerProfile profile = player.getPlayerProfile();
|
||||
profile.setName(nameShort);
|
||||
|
||||
if (!("".equals(texture)) &&
|
||||
!("".equals(signature))) {
|
||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||
}
|
||||
|
||||
player.sendMessage("Successfully set your username to \"" + nameShort + "\"");
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
final PlayerProfile profile = player.getPlayerProfile();
|
||||
profile.setName(nameShort);
|
||||
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
player.setPlayerProfile(profile);
|
||||
Main.usernameInProgress.remove(player.getUniqueId());
|
||||
SkinDownloader skinDownloader = new SkinDownloader();
|
||||
|
||||
if (skinDownloader.fetchSkinData(args[0])) {
|
||||
final String texture = skinDownloader.getTexture();
|
||||
final String signature = skinDownloader.getSignature();
|
||||
|
||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||
}
|
||||
}.runTask(JavaPlugin.getPlugin(Main.class));
|
||||
});
|
||||
|
||||
player.sendMessage("Successfully set your username to \"" + nameShort + "\"");
|
||||
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
player.setPlayerProfile(profile);
|
||||
Main.usernameInProgress.remove(player.getUniqueId());
|
||||
}
|
||||
}.runTask(JavaPlugin.getPlugin(Main.class));
|
||||
}
|
||||
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
|
||||
} else {
|
||||
player.sendMessage("Your username is already being changed. Please wait a few seconds.");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue