From 551f9f480743067bf087533b61ed34cce6b8e2d4 Mon Sep 17 00:00:00 2001 From: mathiascode Date: Mon, 2 Dec 2019 18:12:05 +0200 Subject: [PATCH] Move skin downloader to separate class --- pom.xml | 4 +- .../kaboom/extras/commands/CommandSkin.java | 61 ++++++----------- .../extras/commands/CommandUsername.java | 66 +++++++------------ .../kaboom/extras/helpers/SkinDownloader.java | 47 +++++++++++++ .../modules/player/PlayerConnection.java | 56 ++++++---------- 5 files changed, 109 insertions(+), 125 deletions(-) create mode 100644 src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java diff --git a/pom.xml b/pom.xml index c2c6ca8..dcd9861 100644 --- a/pom.xml +++ b/pom.xml @@ -4,8 +4,8 @@ Extras master - 11 - 11 + 1.8 + 1.8 diff --git a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java index de25d18..b612882 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java @@ -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; @@ -23,9 +16,6 @@ import org.bukkit.scheduler.BukkitRunnable; import com.destroystokyo.paper.profile.PlayerProfile; import com.destroystokyo.paper.profile.ProfileProperty; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - class CommandSkin implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { @@ -37,41 +27,28 @@ class CommandSkin implements CommandExecutor { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else if (!Main.skinInProgress.contains(player.getUniqueId())) { Main.skinInProgress.add(player.getUniqueId()); + + new BukkitRunnable() { + public void run() { + SkinDownloader skinDownloader = new SkinDownloader(); + if (skinDownloader.fetchSkinData(args[0])) { + final PlayerProfile profile = player.getPlayerProfile(); + final String texture = skinDownloader.getTexture(); + final String signature = skinDownloader.getSignature(); - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://api.ashcon.app/mojang/v2/user/" + args[0].replace(" ", "%20"))) - .build(); - client.sendAsync(request, BodyHandlers.ofInputStream()) - .thenAccept(response -> { - 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"); - final String texture = rawSkin.get("value").getAsString(); - final String signature = rawSkin.get("signature").getAsString(); - try { - skinStream.close(); - } catch (Exception exception) { - System.out.println(exception); - } - - final PlayerProfile profile = player.getPlayerProfile(); - profile.setProperty(new ProfileProperty("textures", texture, signature)); + profile.setProperty(new ProfileProperty("textures", texture, signature)); + + player.sendMessage("Successfully set your skin to " + args[0] + "'s"); - player.sendMessage("Successfully set your skin to " + args[0] + "'s"); - - new BukkitRunnable() { - public void run() { - player.setPlayerProfile(profile); - Main.skinInProgress.remove(player.getUniqueId()); - } - }.runTask(JavaPlugin.getPlugin(Main.class)); - } else { - player.sendMessage("A player with that username doesn't exist"); - Main.skinInProgress.remove(player.getUniqueId()); + new BukkitRunnable() { + public void run() { + player.setPlayerProfile(profile); + Main.skinInProgress.remove(player.getUniqueId()); + } + }.runTask(JavaPlugin.getPlugin(Main.class)); + } } - }); + }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); } else { player.sendMessage("You are already applying a skin. Please wait a few seconds."); } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java index eb6ac75..82b55d4 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java @@ -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."); } diff --git a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java new file mode 100644 index 0000000..61c8412 --- /dev/null +++ b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java @@ -0,0 +1,47 @@ +package pw.kaboom.extras; + +import java.io.InputStreamReader; +import java.net.URL; +import javax.net.ssl.HttpsURLConnection; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +class SkinDownloader { + private String texture; + private String signature; + + public boolean fetchSkinData(String playerName) { + try { + final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName); + final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection(); + skinConnection.setConnectTimeout(0); + skinConnection.setDefaultUseCaches(false); + skinConnection.setUseCaches(false); + + if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { + final InputStreamReader 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(); + try { + skinStream.close(); + } catch (Exception exception) { + System.out.println(exception); + } + return true; + } + } catch (Exception exception) { + } + return false; + } + + public String getSignature() { + return signature; + } + + public String getTexture() { + return texture; + } +} \ No newline at end of file diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index b0bba6b..77c5149 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -1,11 +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 java.util.UUID; import org.bukkit.Bukkit; @@ -35,14 +29,13 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; +import pw.kaboom.extras.SkinDownloader; + import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent; import com.destroystokyo.paper.profile.PlayerProfile; import com.destroystokyo.paper.profile.ProfileProperty; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - class PlayerConnection implements Listener { @EventHandler void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) { @@ -126,36 +119,25 @@ class PlayerConnection implements Listener { player.setOp(true); } - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://api.ashcon.app/mojang/v2/user/" + player.getName().replace(" ", "%20"))) - .build(); - client.sendAsync(request, BodyHandlers.ofInputStream()) - .thenAccept(response -> { - 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"); - final String texture = rawSkin.get("value").getAsString(); - final String signature = rawSkin.get("signature").getAsString(); - try { - skinStream.close(); - } catch (Exception exception) { - System.out.println(exception); - } - - final PlayerProfile profile = player.getPlayerProfile(); - profile.setProperty(new ProfileProperty("textures", texture, signature)); - - new BukkitRunnable() { - public void run() { - if (player.isOnline()) { - player.setPlayerProfile(profile); + new BukkitRunnable() { + public void run() { + SkinDownloader skinDownloader = new SkinDownloader(); + if (skinDownloader.fetchSkinData(player.getName())) { + final PlayerProfile profile = player.getPlayerProfile(); + final String texture = skinDownloader.getTexture(); + final String signature = skinDownloader.getSignature(); + profile.setProperty(new ProfileProperty("textures", texture, signature)); + + new BukkitRunnable() { + public void run() { + if (player.isOnline()) { + player.setPlayerProfile(profile); + } } - } - }.runTask(JavaPlugin.getPlugin(Main.class)); + }.runTask(JavaPlugin.getPlugin(Main.class)); + } } - }); + }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); } @EventHandler