Optimize player join

This commit is contained in:
mathias 2018-12-20 17:14:41 +02:00
parent 3f6bed0783
commit 159bd3c28d
3 changed files with 79 additions and 30 deletions

View file

@ -2,7 +2,7 @@ package pw.kaboom.extras;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
@ -189,47 +189,56 @@ class CommandSkin implements CommandExecutor {
public void run() {
try {
final String name = args[0];
URL nameurl = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
HttpURLConnection nameconnection = (HttpURLConnection) nameurl.openConnection();
URL nameUrl = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
HttpsURLConnection nameConnection = (HttpsURLConnection) nameUrl.openConnection();
if (nameconnection.getResponseCode() == 200) {
InputStreamReader namestream = new InputStreamReader(nameconnection.getInputStream());
String uuid = new JsonParser().parse(namestream).getAsJsonObject().get("id").getAsString();
URL uuidurl = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
HttpURLConnection uuidconnection = (HttpURLConnection) uuidurl.openConnection();
if (nameConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
InputStreamReader nameStream = new InputStreamReader(nameConnection.getInputStream());
String uuid = new JsonParser().parse(nameStream).getAsJsonObject().get("id").getAsString();
nameStream.close();
nameConnection.disconnect();
if (uuidconnection.getResponseCode() == 200) {
InputStreamReader uuidstream = new InputStreamReader(uuidconnection.getInputStream());
JsonObject response = new JsonParser().parse(uuidstream).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
URL uuidUrl = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
HttpsURLConnection uuidConnection = (HttpsURLConnection) uuidUrl.openConnection();
if (uuidConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
InputStreamReader uuidStream = new InputStreamReader(uuidConnection.getInputStream());
JsonObject response = new JsonParser().parse(uuidStream).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
final String texture = response.get("value").getAsString();
final String signature = response.get("signature").getAsString();
uuidStream.close();
uuidConnection.disconnect();
Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
public void run() {
PlayerProfile textureprofile = player.getPlayerProfile();
textureprofile.setProperty(new ProfileProperty("textures", texture, signature));
player.setPlayerProfile(textureprofile);
final PlayerProfile textureProfile = player.getPlayerProfile();
textureProfile.clearProperties();
textureProfile.setProperty(new ProfileProperty("textures", texture, signature));
Bukkit.getScheduler().runTask(main, new Runnable() {
@Override
public void run() {
player.setPlayerProfile(textureProfile);
player.sendMessage("Successfully set your skin to " + name + "'s");
}
});
} else {
Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
public void run() {
uuidConnection.disconnect();
Bukkit.getScheduler().runTask(main, new Runnable() {
@Override
public void run() {
player.sendMessage("Failed to change skin. Try again later");
}
});
}
uuidconnection.disconnect();
} else {
Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
public void run() {
nameConnection.disconnect();
Bukkit.getScheduler().runTask(main, new Runnable() {
@Override
public void run() {
player.sendMessage("A player with that username doesn't exist");
}
});
}
nameconnection.disconnect();
} catch (Exception exception) {
exception.printStackTrace();
}
}
});

View file

@ -168,9 +168,48 @@ class Events implements Listener {
}
@EventHandler
void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) {
main.commandMillisList.put(event.getUniqueId(), System.currentTimeMillis());
main.interactMillisList.put(event.getUniqueId(), System.currentTimeMillis());
/*try {
URL nameUrl = new URL("https://api.mojang.com/users/profiles/minecraft/" + event.getName());
HttpsURLConnection nameConnection = (HttpsURLConnection) nameUrl.openConnection();
if (nameConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
System.out.println("ok");
InputStreamReader nameStream = new InputStreamReader(nameConnection.getInputStream());
String uuid = new JsonParser().parse(nameStream).getAsJsonObject().get("id").getAsString();
nameStream.close();
nameConnection.disconnect();
URL uuidUrl = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
HttpsURLConnection uuidConnection = (HttpsURLConnection) uuidUrl.openConnection();
if (uuidConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
InputStreamReader uuidStream = new InputStreamReader(uuidConnection.getInputStream());
JsonObject response = new JsonParser().parse(uuidStream).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
final String texture = response.get("value").getAsString();
final String signature = response.get("signature").getAsString();
uuidStream.close();
uuidConnection.disconnect();
Bukkit.getScheduler().runTask(main, new Runnable() {
@Override
public void run() {
PlayerProfile textureprofile = event.getPlayerProfile();
textureprofile.setProperty(new ProfileProperty("textures", texture, signature));
event.setPlayerProfile(textureprofile);
}
});
} else {
uuidConnection.disconnect();
}
} else {
nameConnection.disconnect();
}
} catch (Exception exception) {
}*/
}
@EventHandler
@ -537,9 +576,9 @@ class Events implements Listener {
event.disallow(Result.KICK_OTHER, "The server is throttled due to bot attacks. Please try logging in again.");
main.onlineCount++;
}
/*} else if (!(event.getHostname().startsWith("play.kaboom.pw") &&
} else if (!(event.getHostname().startsWith("play.kaboom.pw") &&
event.getHostname().endsWith(":64518"))) {
event.disallow(Result.KICK_OTHER, "You connected to the server using an outdated server address/IP.\nPlease use the following address/IP:\n\nkaboom.pw");*/
event.disallow(Result.KICK_OTHER, "You connected to the server using an outdated server address/IP.\nPlease use the following address/IP:\n\nkaboom.pw");
} else {
event.allow();
}

View file

@ -457,7 +457,6 @@ public class Main extends JavaPlugin {
HttpsURLConnection nameConnection = (HttpsURLConnection) nameUrl.openConnection();
if (nameConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
System.out.println("ok");
InputStreamReader nameStream = new InputStreamReader(nameConnection.getInputStream());
String uuid = new JsonParser().parse(nameStream).getAsJsonObject().get("id").getAsString();
nameStream.close();
@ -474,12 +473,14 @@ public class Main extends JavaPlugin {
uuidStream.close();
uuidConnection.disconnect();
final PlayerProfile textureProfile = player.getPlayerProfile();
textureProfile.clearProperties();
textureProfile.setProperty(new ProfileProperty("textures", texture, signature));
Bukkit.getScheduler().runTask(this, new Runnable() {
@Override
public void run() {
PlayerProfile textureprofile = player.getPlayerProfile();
textureprofile.setProperty(new ProfileProperty("textures", texture, signature));
player.setPlayerProfile(textureprofile);
player.setPlayerProfile(textureProfile);
}
});
} else {