mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
use URIs
This commit is contained in:
@@ -31,6 +31,8 @@ public final class SkinRestorer {
|
||||
private static SkinStorage skinStorage;
|
||||
private static Path configDir;
|
||||
|
||||
private SkinRestorer() {}
|
||||
|
||||
public static SkinStorage getSkinStorage() {
|
||||
return SkinRestorer.skinStorage;
|
||||
}
|
||||
|
||||
@@ -9,13 +9,20 @@ import net.lionarius.skinrestorer.util.PlayerUtils;
|
||||
import net.lionarius.skinrestorer.util.WebUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public final class MineskinSkinProvider implements SkinProvider {
|
||||
|
||||
private static final String API = "https://api.mineskin.org/generate/url";
|
||||
private static final String USER_AGENT = "SkinRestorer";
|
||||
private static final String TYPE = "application/json";
|
||||
private static final URI API_URL;
|
||||
|
||||
static {
|
||||
try {
|
||||
API_URL = new URI("https://api.mineskin.org/generate/url");
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getArgumentName() {
|
||||
@@ -30,10 +37,10 @@ public final class MineskinSkinProvider implements SkinProvider {
|
||||
@Override
|
||||
public SkinResult getSkin(String url, SkinVariant variant) {
|
||||
try {
|
||||
String input = ("{\"variant\":\"%s\",\"name\":\"%s\",\"visibility\":%d,\"url\":\"%s\"}")
|
||||
String body = ("{\"variant\":\"%s\",\"name\":\"%s\",\"visibility\":%d,\"url\":\"%s\"}")
|
||||
.formatted(variant.toString(), "none", 1, url);
|
||||
|
||||
JsonObject texture = JsonUtils.parseJson(WebUtils.postRequest(new URL(API), USER_AGENT, TYPE, TYPE, input))
|
||||
JsonObject texture = JsonUtils.parseJson(WebUtils.postRequest(API_URL.toURL(), "application/json", body))
|
||||
.getAsJsonObject("data").getAsJsonObject("texture");
|
||||
|
||||
return SkinResult.success(new Property(PlayerUtils.TEXTURES_KEY, texture.get("value").getAsString(), texture.get("signature").getAsString()));
|
||||
|
||||
@@ -9,13 +9,24 @@ import net.lionarius.skinrestorer.util.PlayerUtils;
|
||||
import net.lionarius.skinrestorer.util.WebUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class MojangSkinProvider implements SkinProvider {
|
||||
|
||||
private static final String API = "https://api.mojang.com/users/profiles/minecraft/";
|
||||
private static final String SESSION_SERVER = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
private static final URI API_URL;
|
||||
private static final URI SESSION_SERVER_URL;
|
||||
|
||||
static {
|
||||
try {
|
||||
API_URL = new URI("https://api.mojang.com/users/profiles/minecraft/");
|
||||
SESSION_SERVER_URL = new URI("https://sessionserver.mojang.com/session/minecraft/profile/");
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getArgumentName() {
|
||||
@@ -31,7 +42,7 @@ public final class MojangSkinProvider implements SkinProvider {
|
||||
public SkinResult getSkin(String username, SkinVariant variant) {
|
||||
try {
|
||||
UUID uuid = getUUID(username);
|
||||
JsonObject texture = JsonUtils.parseJson(WebUtils.getRequest(new URL(SESSION_SERVER + uuid + "?unsigned=false")))
|
||||
JsonObject texture = JsonUtils.parseJson(WebUtils.getRequest(SESSION_SERVER_URL.resolve(uuid + "?unsigned=false").toURL()))
|
||||
.getAsJsonArray("properties").get(0).getAsJsonObject();
|
||||
|
||||
return SkinResult.success(new Property(PlayerUtils.TEXTURES_KEY, texture.get("value").getAsString(), texture.get("signature").getAsString()));
|
||||
@@ -41,7 +52,7 @@ public final class MojangSkinProvider implements SkinProvider {
|
||||
}
|
||||
|
||||
private static UUID getUUID(String name) throws IOException {
|
||||
return UUID.fromString(JsonUtils.parseJson(WebUtils.getRequest(new URL(API + name))).get("id").getAsString()
|
||||
return UUID.fromString(JsonUtils.parseJson(WebUtils.getRequest(API_URL.resolve(name).toURL())).get("id").getAsString()
|
||||
.replaceFirst("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ import java.util.Base64;
|
||||
|
||||
public final class JsonUtils {
|
||||
|
||||
private JsonUtils() {}
|
||||
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
private JsonUtils() {}
|
||||
|
||||
public static <T> T fromJson(String json, Class<T> clazz) {
|
||||
return GSON.fromJson(json, clazz);
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ import java.util.List;
|
||||
|
||||
public final class PlayerUtils {
|
||||
|
||||
private PlayerUtils() {}
|
||||
|
||||
public static final String TEXTURES_KEY = "textures";
|
||||
|
||||
private PlayerUtils() {}
|
||||
|
||||
public static boolean isFakePlayer(ServerPlayerEntity player) {
|
||||
return player.getClass() != ServerPlayerEntity.class; // if the player isn't a server player entity, it must be someone's fake player
|
||||
}
|
||||
|
||||
@@ -12,19 +12,20 @@ public final class WebUtils {
|
||||
|
||||
private WebUtils() {}
|
||||
|
||||
public static String postRequest(URL url, String userAgent, String contentType, String responseType, String input)
|
||||
public static final String USER_AGENT = "SkinRestorer";
|
||||
|
||||
public static String postRequest(URL url, String contentType, String body)
|
||||
throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", contentType);
|
||||
connection.setRequestProperty("Accept", responseType);
|
||||
connection.setRequestProperty("User-Agent", userAgent);
|
||||
connection.setRequestProperty("User-Agent", WebUtils.USER_AGENT);
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
os.write(input.getBytes(StandardCharsets.UTF_8), 0, input.length());
|
||||
os.write(body.getBytes(StandardCharsets.UTF_8), 0, body.length());
|
||||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
|
||||
Reference in New Issue
Block a user