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

View file

@ -2,20 +2,16 @@ package pw.kaboom.extras.modules.player;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent; import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import pw.kaboom.extras.Main; 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 @EventHandler
void onPlayerJoin(PlayerJoinEvent event) { void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
@ -80,12 +67,12 @@ public class PlayerConnection implements Listener {
if (!JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("enableJoinRestrictions")) { if (!JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("enableJoinRestrictions")) {
event.allow(); event.allow();
} }
if (event.getResult() == Result.KICK_FULL && if (event.getResult() == Result.KICK_FULL &&
JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("allowJoinOnFullServer")) { JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("allowJoinOnFullServer")) {
event.allow(); event.allow();
} }
if (JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("opOnJoin")) { if (JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("opOnJoin")) {
player.setOp(true); player.setOp(true);
} }