forked from kaboomserver/extras
Move skin downloader to separate class
This commit is contained in:
parent
4fe8d0ee8d
commit
551f9f4807
5 changed files with 109 additions and 125 deletions
4
pom.xml
4
pom.xml
|
@ -4,8 +4,8 @@
|
|||
<artifactId>Extras</artifactId>
|
||||
<version>master</version>
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
</properties>
|
||||
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
@ -38,25 +28,14 @@ class CommandSkin implements CommandExecutor {
|
|||
} else if (!Main.skinInProgress.contains(player.getUniqueId())) {
|
||||
Main.skinInProgress.add(player.getUniqueId());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||
|
||||
player.sendMessage("Successfully set your skin to " + args[0] + "'s");
|
||||
|
@ -67,11 +46,9 @@ class CommandSkin implements CommandExecutor {
|
|||
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());
|
||||
}
|
||||
});
|
||||
}
|
||||
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
|
||||
} else {
|
||||
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -41,33 +34,17 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
final PlayerProfile profile = player.getPlayerProfile();
|
||||
profile.setName(nameShort);
|
||||
|
||||
if (!("".equals(texture)) &&
|
||||
!("".equals(signature))) {
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -79,7 +56,8 @@ class CommandUsername implements CommandExecutor {
|
|||
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.");
|
||||
}
|
||||
|
|
47
src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java
Normal file
47
src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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,25 +119,13 @@ 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);
|
||||
}
|
||||
|
||||
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() {
|
||||
|
@ -155,7 +136,8 @@ class PlayerConnection implements Listener {
|
|||
}
|
||||
}.runTask(JavaPlugin.getPlugin(Main.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
Loading…
Reference in a new issue