mirror of
https://github.com/kaboomserver/extras.git
synced 2024-12-02 12:16:59 -05: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
4
pom.xml
4
pom.xml
|
@ -4,8 +4,8 @@
|
||||||
<artifactId>Extras</artifactId>
|
<artifactId>Extras</artifactId>
|
||||||
<version>master</version>
|
<version>master</version>
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
package pw.kaboom.extras;
|
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.ChatColor;
|
||||||
|
|
||||||
import org.bukkit.command.Command;
|
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.PlayerProfile;
|
||||||
import com.destroystokyo.paper.profile.ProfileProperty;
|
import com.destroystokyo.paper.profile.ProfileProperty;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
|
|
||||||
class CommandSkin implements CommandExecutor {
|
class CommandSkin implements CommandExecutor {
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
||||||
if (sender instanceof ConsoleCommandSender) {
|
if (sender instanceof ConsoleCommandSender) {
|
||||||
|
@ -38,40 +28,27 @@ class CommandSkin implements CommandExecutor {
|
||||||
} else if (!Main.skinInProgress.contains(player.getUniqueId())) {
|
} else if (!Main.skinInProgress.contains(player.getUniqueId())) {
|
||||||
Main.skinInProgress.add(player.getUniqueId());
|
Main.skinInProgress.add(player.getUniqueId());
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
new BukkitRunnable() {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
public void run() {
|
||||||
.uri(URI.create("https://api.ashcon.app/mojang/v2/user/" + args[0].replace(" ", "%20")))
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
.build();
|
if (skinDownloader.fetchSkinData(args[0])) {
|
||||||
client.sendAsync(request, BodyHandlers.ofInputStream())
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
.thenAccept(response -> {
|
final String texture = skinDownloader.getTexture();
|
||||||
if (response.statusCode() == 200) {
|
final String signature = skinDownloader.getSignature();
|
||||||
final InputStreamReader skinStream = new InputStreamReader(response.body());
|
|
||||||
final JsonObject responseJson = new JsonParser().parse(skinStream).getAsJsonObject();
|
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||||
final JsonObject rawSkin = responseJson.getAsJsonObject("textures").getAsJsonObject("raw");
|
|
||||||
final String texture = rawSkin.get("value").getAsString();
|
player.sendMessage("Successfully set your skin to " + args[0] + "'s");
|
||||||
final String signature = rawSkin.get("signature").getAsString();
|
|
||||||
try {
|
new BukkitRunnable() {
|
||||||
skinStream.close();
|
public void run() {
|
||||||
} catch (Exception exception) {
|
player.setPlayerProfile(profile);
|
||||||
System.out.println(exception);
|
Main.skinInProgress.remove(player.getUniqueId());
|
||||||
|
}
|
||||||
|
}.runTask(JavaPlugin.getPlugin(Main.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
final PlayerProfile profile = player.getPlayerProfile();
|
|
||||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
});
|
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
package pw.kaboom.extras;
|
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.ChatColor;
|
||||||
|
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
@ -41,45 +34,30 @@ class CommandUsername implements CommandExecutor {
|
||||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
||||||
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
|
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
new BukkitRunnable() {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
public void run() {
|
||||||
.uri(URI.create("https://api.ashcon.app/mojang/v2/user/" + nameShort.replace(" ", "%20")))
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
.build();
|
profile.setName(nameShort);
|
||||||
client.sendAsync(request, BodyHandlers.ofInputStream())
|
|
||||||
.thenAccept(response -> {
|
|
||||||
String texture = "";
|
|
||||||
String signature = "";
|
|
||||||
|
|
||||||
if (response.statusCode() == 200) {
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
final InputStreamReader skinStream = new InputStreamReader(response.body());
|
|
||||||
final JsonObject responseJson = new JsonParser().parse(skinStream).getAsJsonObject();
|
if (skinDownloader.fetchSkinData(args[0])) {
|
||||||
final JsonObject rawSkin = responseJson.getAsJsonObject("textures").getAsJsonObject("raw");
|
final String texture = skinDownloader.getTexture();
|
||||||
texture = rawSkin.get("value").getAsString();
|
final String signature = skinDownloader.getSignature();
|
||||||
signature = rawSkin.get("signature").getAsString();
|
|
||||||
try {
|
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||||
skinStream.close();
|
|
||||||
} catch (Exception exception) {
|
|
||||||
System.out.println(exception);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
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() {
|
|
||||||
player.setPlayerProfile(profile);
|
|
||||||
Main.usernameInProgress.remove(player.getUniqueId());
|
|
||||||
}
|
|
||||||
}.runTask(JavaPlugin.getPlugin(Main.class));
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("Your username is already being changed. Please wait a few seconds.");
|
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;
|
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 java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -35,14 +29,13 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import pw.kaboom.extras.SkinDownloader;
|
||||||
|
|
||||||
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
||||||
|
|
||||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||||
import com.destroystokyo.paper.profile.ProfileProperty;
|
import com.destroystokyo.paper.profile.ProfileProperty;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
|
|
||||||
class PlayerConnection implements Listener {
|
class PlayerConnection implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
|
void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
|
||||||
|
@ -126,36 +119,25 @@ class PlayerConnection implements Listener {
|
||||||
player.setOp(true);
|
player.setOp(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
new BukkitRunnable() {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
public void run() {
|
||||||
.uri(URI.create("https://api.ashcon.app/mojang/v2/user/" + player.getName().replace(" ", "%20")))
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
.build();
|
if (skinDownloader.fetchSkinData(player.getName())) {
|
||||||
client.sendAsync(request, BodyHandlers.ofInputStream())
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
.thenAccept(response -> {
|
final String texture = skinDownloader.getTexture();
|
||||||
if (response.statusCode() == 200) {
|
final String signature = skinDownloader.getSignature();
|
||||||
final InputStreamReader skinStream = new InputStreamReader(response.body());
|
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||||
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();
|
new BukkitRunnable() {
|
||||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
public void run() {
|
||||||
|
if (player.isOnline()) {
|
||||||
new BukkitRunnable() {
|
player.setPlayerProfile(profile);
|
||||||
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
|
@EventHandler
|
||||||
|
|
Loading…
Reference in a new issue