mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
Added PlayerSkin#fromUuid and PlayerSkin#fromUsername
This commit is contained in:
parent
f8758d2933
commit
00143078e3
@ -1,5 +1,14 @@
|
||||
package net.minestom.server.entity;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.minestom.server.utils.url.URLUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Contains all the data required to store a skin
|
||||
*/
|
||||
@ -30,4 +39,56 @@ public class PlayerSkin {
|
||||
public String getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a skin from a Mojang UUID
|
||||
*
|
||||
* @param uuid Mojang UUID
|
||||
* @return a player skin based on the UUID
|
||||
*/
|
||||
public static PlayerSkin fromUuid(String uuid) {
|
||||
final String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false";
|
||||
|
||||
try {
|
||||
final String response = URLUtils.getText(url);
|
||||
JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject();
|
||||
JsonArray propertiesArray = jsonObject.get("properties").getAsJsonArray();
|
||||
|
||||
Iterator<JsonElement> iterator = propertiesArray.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
JsonObject propertyObject = iterator.next().getAsJsonObject();
|
||||
final String name = propertyObject.get("name").getAsString();
|
||||
if (!name.equals("textures"))
|
||||
continue;
|
||||
final String textureValue = propertyObject.get("value").getAsString();
|
||||
final String signatureValue = propertyObject.get("signature").getAsString();
|
||||
return new PlayerSkin(textureValue, signatureValue);
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a skin from a Minecraft username
|
||||
*
|
||||
* @param username the Minecraft username
|
||||
* @return a skin based on a Minecraft username
|
||||
*/
|
||||
public static PlayerSkin fromUsername(String username) {
|
||||
final String url = "https://api.mojang.com/users/profiles/minecraft/" + username;
|
||||
|
||||
try {
|
||||
final String response = URLUtils.getText(url);
|
||||
JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject();
|
||||
final String uuid = jsonObject.get("id").getAsString();
|
||||
return fromUuid(uuid);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
40
src/main/java/net/minestom/server/utils/url/URLUtils.java
Normal file
40
src/main/java/net/minestom/server/utils/url/URLUtils.java
Normal file
@ -0,0 +1,40 @@
|
||||
package net.minestom.server.utils.url;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class URLUtils {
|
||||
|
||||
public static String getText(String url) throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
//add headers to the connection, or check the status if desired..
|
||||
|
||||
// handle error response code it occurs
|
||||
int responseCode = connection.getResponseCode();
|
||||
InputStream inputStream;
|
||||
if (200 <= responseCode && responseCode <= 299) {
|
||||
inputStream = connection.getInputStream();
|
||||
} else {
|
||||
inputStream = connection.getErrorStream();
|
||||
}
|
||||
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
inputStream));
|
||||
|
||||
StringBuilder response = new StringBuilder();
|
||||
String currentLine;
|
||||
|
||||
while ((currentLine = in.readLine()) != null)
|
||||
response.append(currentLine);
|
||||
|
||||
in.close();
|
||||
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user