Optimize skin URL connections

This commit is contained in:
mathias 2019-08-07 01:38:58 +03:00
parent 8ac5e83044
commit 6835013bef
3 changed files with 37 additions and 9 deletions

View file

@ -38,9 +38,17 @@ class CommandSkin implements CommandExecutor {
public void run() { public void run() {
try { try {
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + name); final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + name);
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection(); final HttpsURLConnection premiumCheck = (HttpsURLConnection) skinUrl.openConnection();
premiumCheck.setConnectTimeout(0);
premiumCheck.setRequestMethod("HEAD");
premiumCheck.setDefaultUseCaches(false);
premiumCheck.setUseCaches(false);
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { if (premiumCheck.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
skinConnection.setConnectTimeout(0);
skinConnection.setDefaultUseCaches(false);
skinConnection.setUseCaches(false);
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream()); final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject(); final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
final String uuid = response.get("uuid").getAsString(); final String uuid = response.get("uuid").getAsString();
@ -48,6 +56,7 @@ class CommandSkin implements CommandExecutor {
final String texture = rawSkin.get("value").getAsString(); final String texture = rawSkin.get("value").getAsString();
final String signature = rawSkin.get("signature").getAsString(); final String signature = rawSkin.get("signature").getAsString();
skinStream.close(); skinStream.close();
skinConnection.disconnect();
final PlayerProfile textureProfile = player.getPlayerProfile(); final PlayerProfile textureProfile = player.getPlayerProfile();
textureProfile.clearProperties(); textureProfile.clearProperties();
@ -63,7 +72,7 @@ class CommandSkin implements CommandExecutor {
player.sendMessage("A player with that username doesn't exist"); player.sendMessage("A player with that username doesn't exist");
} }
skinConnection.disconnect(); premiumCheck.disconnect();
} catch (Exception exception) { } catch (Exception exception) {
} }
} }

View file

@ -45,9 +45,17 @@ class CommandUsername implements CommandExecutor {
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length())); final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + nameShort); final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + nameShort);
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection(); final HttpsURLConnection premiumCheck = (HttpsURLConnection) skinUrl.openConnection();
premiumCheck.setConnectTimeout(0);
premiumCheck.setRequestMethod("HEAD");
premiumCheck.setDefaultUseCaches(false);
premiumCheck.setUseCaches(false);
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { if (premiumCheck.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
skinConnection.setConnectTimeout(0);
skinConnection.setDefaultUseCaches(false);
skinConnection.setUseCaches(false);
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream()); final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject(); final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
final String uuid = response.get("uuid").getAsString(); final String uuid = response.get("uuid").getAsString();
@ -55,9 +63,10 @@ class CommandUsername implements CommandExecutor {
texture = rawSkin.get("value").getAsString(); texture = rawSkin.get("value").getAsString();
signature = rawSkin.get("signature").getAsString(); signature = rawSkin.get("signature").getAsString();
skinStream.close(); skinStream.close();
skinConnection.disconnect();
} }
skinConnection.disconnect(); premiumCheck.disconnect();
final PlayerProfile profile = player.getPlayerProfile(); final PlayerProfile profile = player.getPlayerProfile();
profile.setName(nameShort); profile.setName(nameShort);

View file

@ -42,9 +42,18 @@ class PlayerConnection implements Listener {
void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) { void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
try { try {
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + event.getName()); final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + event.getName());
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection(); final HttpsURLConnection premiumCheck = (HttpsURLConnection) skinUrl.openConnection();
premiumCheck.setConnectTimeout(0);
premiumCheck.setRequestMethod("HEAD");
premiumCheck.setDefaultUseCaches(false);
premiumCheck.setUseCaches(false);
System.out.println(premiumCheck.getResponseCode());
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { if (premiumCheck.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
skinConnection.setConnectTimeout(0);
skinConnection.setDefaultUseCaches(false);
skinConnection.setUseCaches(false);
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream()); final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject(); final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
final String uuid = response.get("uuid").getAsString(); final String uuid = response.get("uuid").getAsString();
@ -52,6 +61,7 @@ class PlayerConnection implements Listener {
final String texture = rawSkin.get("value").getAsString(); final String texture = rawSkin.get("value").getAsString();
final String signature = rawSkin.get("signature").getAsString(); final String signature = rawSkin.get("signature").getAsString();
skinStream.close(); skinStream.close();
skinConnection.disconnect();
final PlayerProfile textureProfile = event.getPlayerProfile(); final PlayerProfile textureProfile = event.getPlayerProfile();
textureProfile.clearProperties(); textureProfile.clearProperties();
@ -60,7 +70,7 @@ class PlayerConnection implements Listener {
main.playerProfile.put(event.getName(), textureProfile); main.playerProfile.put(event.getName(), textureProfile);
} }
skinConnection.disconnect(); premiumCheck.disconnect();
} catch (Exception exception) { } catch (Exception exception) {
} }