CachedPlayerHeads/src/main/java/org/warp/cachedplayerheads/MojangAPI.java
Andrea Cavalli 9d1f489996 First commit
2021-04-19 20:53:45 +02:00

210 lines
7.2 KiB
Java

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.warp.cachedplayerheads;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import net.skinsrestorer.shared.exception.SkinRequestException;
import net.skinsrestorer.shared.storage.Locale;
import net.skinsrestorer.shared.storage.SkinStorage;
import net.skinsrestorer.shared.utils.MetricsCounter;
import net.skinsrestorer.shared.utils.Property;
public class MojangAPI {
private static final String UUID_URL = "https://api.minetools.eu/uuid/%name%";
private static final String UUID_URL_MOJANG = "https://api.mojang.com/users/profiles/minecraft/%name%";
private static final String UUID_URL_BACKUP = "https://api.ashcon.app/mojang/v2/user/%name%";
private static final String SKIN_URL = "https://api.minetools.eu/profile/%uuid%";
private static final String SKIN_URL_MOJANG = "https://sessionserver.mojang.com/session/minecraft/profile/%uuid%?unsigned=false";
private static final String SKIN_URL_BACKUP = "https://api.ashcon.app/mojang/v2/user/%uuid%";
private SkinStorage skinStorage;
public MojangAPI() {
}
public Object getSkinProperty(String uuid) {
return this.getSkinProperty(uuid, true);
}
public Object getSkinProperty(String uuid, boolean tryNext) {
try {
String output = this.readURL("https://api.minetools.eu/profile/%uuid%".replace("%uuid%", uuid));
JsonObject obj = (JsonObject)(new Gson()).fromJson(output, JsonObject.class);
Property property = new Property();
if (obj.has("raw")) {
JsonObject raw = obj.getAsJsonObject("raw");
if (raw.has("status") && raw.get("status").getAsString().equalsIgnoreCase("ERR")) {
return this.getSkinPropertyMojang(uuid);
}
if (property.valuesFromJson(raw)) {
return this.getSkinStorage().createProperty("textures", property.getValue(), property.getSignature());
}
}
} catch (Exception var7) {
if (tryNext) {
return this.getSkinPropertyMojang(uuid);
}
}
return null;
}
public Object getSkinPropertyMojang(String uuid) {
return this.getSkinPropertyMojang(uuid, true);
}
public Object getSkinPropertyMojang(String uuid, boolean tryNext) {
if (tryNext) {
System.out.println("Trying Mojang API to get skin property for " + uuid + ".");
}
try {
String output = this.readURL("https://sessionserver.mojang.com/session/minecraft/profile/%uuid%?unsigned=false".replace("%uuid%", uuid));
JsonObject obj = (JsonObject)(new Gson()).fromJson(output, JsonObject.class);
Property property = new Property();
if (obj.has("properties") && property.valuesFromJson(obj)) {
return this.getSkinStorage().createProperty("textures", property.getValue(), property.getSignature());
}
} catch (Exception var6) {
if (tryNext) {
return this.getSkinPropertyBackup(uuid);
}
}
return null;
}
public Object getSkinPropertyBackup(String uuid) {
return this.getSkinPropertyBackup(uuid, true);
}
public Object getSkinPropertyBackup(String uuid, boolean tryNext) {
if (tryNext) {
System.out.println("Trying backup API to get skin property for " + uuid + ".");
}
try {
String output = this.readURL("https://api.ashcon.app/mojang/v2/user/%uuid%".replace("%uuid%", uuid), 10000);
JsonObject obj = (JsonObject)(new Gson()).fromJson(output, JsonObject.class);
JsonObject textures = obj.get("textures").getAsJsonObject();
JsonObject rawTextures = textures.get("raw").getAsJsonObject();
Property property = new Property();
property.setValue(rawTextures.get("value").getAsString());
property.setSignature(rawTextures.get("signature").getAsString());
return this.getSkinStorage().createProperty("textures", property.getValue(), property.getSignature());
} catch (Exception var8) {
System.err.println("Failed to get skin property from backup API. (" + uuid + ")");
return null;
}
}
public String getUUID(String name, boolean tryNext) throws SkinRequestException {
try {
String output = this.readURL("https://api.minetools.eu/uuid/%name%".replace("%name%", name));
JsonObject obj = (JsonObject)(new Gson()).fromJson(output, JsonObject.class);
if (obj.has("status") && obj.get("status").getAsString().equalsIgnoreCase("ERR")) {
return this.getUUIDMojang(name);
} else if (obj.get("id") == null) {
throw new SkinRequestException(Locale.NOT_PREMIUM);
} else {
return obj.get("id").getAsString();
}
} catch (IOException var5) {
return tryNext ? this.getUUIDMojang(name) : null;
}
}
public String getUUIDMojang(String name) throws SkinRequestException {
return this.getUUIDMojang(name, true);
}
public String getUUIDMojang(String name, boolean tryNext) throws SkinRequestException {
if (tryNext) {
System.out.println("Trying Mojang API to get UUID for player " + name + ".");
}
try {
String output = this.readURL("https://api.mojang.com/users/profiles/minecraft/%name%".replace("%name%", name));
if (output.isEmpty()) {
throw new SkinRequestException(Locale.NOT_PREMIUM);
} else {
JsonObject obj = (JsonObject)(new Gson()).fromJson(output, JsonObject.class);
if (obj.has("error")) {
return tryNext ? this.getUUIDBackup(name) : null;
} else {
return obj.get("id").getAsString();
}
}
} catch (IOException var5) {
return tryNext ? this.getUUIDBackup(name) : null;
}
}
public String getUUIDBackup(String name) throws SkinRequestException {
return this.getUUIDBackup(name, true);
}
public String getUUIDBackup(String name, boolean tryNext) throws SkinRequestException {
if (tryNext) {
System.out.println("Trying backup API to get UUID for player " + name + ".");
}
try {
String output = this.readURL("https://api.ashcon.app/mojang/v2/user/%name%".replace("%name%", name), 10000);
JsonObject obj = (JsonObject)(new Gson()).fromJson(output, JsonObject.class);
if (obj.has("code")) {
if (obj.get("error").getAsString().equalsIgnoreCase("Not Found")) {
throw new SkinRequestException(Locale.NOT_PREMIUM);
} else {
throw new SkinRequestException(Locale.ALT_API_FAILED);
}
} else {
return obj.get("uuid").getAsString().replace("-", "");
}
} catch (IOException var5) {
throw new SkinRequestException(Locale.NOT_PREMIUM);
}
}
private String readURL(String url) throws IOException {
return this.readURL(url, 5000);
}
private String readURL(String url, int timeout) throws IOException {
HttpURLConnection con = (HttpURLConnection)(new URL(url)).openConnection();
MetricsCounter.incrAPI(url);
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "SkinsRestorer");
con.setConnectTimeout(timeout);
con.setReadTimeout(timeout);
con.setDoOutput(true);
StringBuilder output = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line = in.readLine()) != null) {
output.append(line);
}
in.close();
return output.toString();
}
public SkinStorage getSkinStorage() {
return this.skinStorage;
}
public void setSkinStorage(final SkinStorage skinStorage) {
this.skinStorage = skinStorage;
}
}