Improve SkinDownloader performance

This commit is contained in:
mathiascode 2019-12-20 21:05:02 +02:00
parent 26b10af495
commit b1c132a461
2 changed files with 34 additions and 43 deletions

View file

@ -1,5 +1,6 @@
package pw.kaboom.extras.helpers;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
@ -17,15 +18,18 @@ import com.google.gson.JsonParser;
import pw.kaboom.extras.Main;
public class SkinDownloader {
private HttpsURLConnection skinConnection;
private InputStreamReader skinStream;
private String texture;
private String signature;
public void applySkin(Player player, String name, boolean shouldChangeName, boolean shouldSendMessage) {
Main.skinInProgress.add(player.getUniqueId());
new BukkitRunnable() {
@Override
public void run() {
Main.skinInProgress.add(player.getUniqueId());
final PlayerProfile profile = player.getPlayerProfile();
if (shouldChangeName && shouldSendMessage) {
@ -33,13 +37,24 @@ public class SkinDownloader {
player.sendMessage("Changing your username. Please wait...");
}
if (fetchSkinData(name)) {
try {
fetchSkinData(name);
profile.setProperty(new ProfileProperty("textures", texture, signature));
if (!shouldChangeName && shouldSendMessage) {
player.sendMessage("Successfully set your skin to " + name + "'s");
}
} else if (!shouldChangeName && shouldSendMessage) {
} catch (Exception exception) {
try {
skinStream.close();
skinConnection.disconnect();
} catch (Exception e) {
}
if (!shouldChangeName && shouldSendMessage) {
player.sendMessage("A player with that username doesn't exist");
}
Main.skinInProgress.remove(player.getUniqueId());
return;
}
@ -65,29 +80,18 @@ public class SkinDownloader {
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
}
private boolean fetchSkinData(String playerName) {
try {
private void fetchSkinData(String playerName) throws IOException {
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName);
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
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());
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;
skinConnection.disconnect();
}
}

View file

@ -2,20 +2,16 @@ package pw.kaboom.extras.modules.player;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import pw.kaboom.extras.Main;
@ -30,15 +26,6 @@ public class PlayerConnection implements Listener {
}
}
/*@EventHandler
void onInventoryClose(InventoryCloseEvent event) {
for (ItemStack item : event.getInventory().getContents()) {
if (EntitySpawn.isIllegalItem(item)) {
event.getInventory().clear();
}
}
}*/
@EventHandler
void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();