Skip to content

Commit

Permalink
Use other api for getting skins (to get around mojang's ratelimits)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Nov 21, 2023
1 parent 80f6e0a commit 16d13ec
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ dependencies {
implementation("us.ajg0702.commands.platforms.bukkit:bukkit:1.0.0")
implementation("us.ajg0702.commands.api:api:1.0.0")

implementation("com.squareup.okhttp3:okhttp:4.11.0")

compileOnly("net.luckperms:api:5.4")

implementation("io.papermc:paperlib:1.0.7")
Expand Down Expand Up @@ -87,6 +89,11 @@ tasks.shadowJar {
relocate("org.yaml", "us.ajg0702.leaderboards.libs")
relocate("io.leangen", "us.ajg0702.leaderboards.libs")
relocate("io.papermc.lib", "us.ajg0702.leaderboards.libs.paperlib")
relocate("com.squareup", "us.ajg0702.leaderboards.libs")
relocate("okhttp3", "us.ajg0702.leaderboards.libs.okhttp3")
relocate("okio", "us.ajg0702.leaderboards.libs.okio")
relocate("org", "us.ajg0702.leaderboards.libs")
relocate("kotlin", "us.ajg0702.leaderboards.kotlin")

archiveBaseName.set("ajLeaderboards")
archiveClassifier.set("")
Expand Down
1 change: 1 addition & 0 deletions nms/nms-legacy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
compileOnly("us.ajg0702:ajUtils:1.1.36")
compileOnly("net.skinsrestorer:skinsrestorer-api:14.1.10")
compileOnly("com.squareup.okhttp3:okhttp:4.11.0")
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
//import net.skinsrestorer.api.SkinsRestorerAPI;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.bukkit.*;
import org.bukkit.inventory.ItemStack;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
Expand Down Expand Up @@ -76,7 +80,11 @@ public ItemStack getHeadItem(UUID uuid, String name) {
}
String value;
//if(skinsRestorerAPI == null) {
value = getHeadValue(Bukkit.getOfflinePlayer(uuid).getName());
if(uuid != null && Bukkit.getOnlineMode()) {
value = getHeadValue(uuid);
} else {
value = getHeadValue(name);
}
/*} else {
IProperty profile = skinsRestorerAPI.getProfile(uuid.toString());
if(profile == null) {
Expand All @@ -98,34 +106,35 @@ public ItemStack getHeadItem(UUID uuid, String name) {
Map<String, CachedData<String>> skinCache = new HashMap<>();
final long lastClear = System.currentTimeMillis();

public String getHeadValue(String name) {
if(System.currentTimeMillis() - lastClear > 5400e3) { // completly wipe the cache every hour and a half
public String getHeadValue(String nameOrUUID) {

if(System.currentTimeMillis() - lastClear > 5400e3) { // completely wipe the cache every hour and a half
skinCache = new HashMap<>();
}

if(skinCache.containsKey(name) && skinCache.get(name).getTimeSince() < 300e3) {
return skinCache.get(name).getData();
if(skinCache.containsKey(nameOrUUID) && skinCache.get(nameOrUUID).getTimeSince() < 300e3) {
return skinCache.get(nameOrUUID).getData();
}

String result = getURLContent("https://api.mojang.com/users/profiles/minecraft/" + name);

String profile = getURLContent("https://api.ashcon.app/mojang/v2/user/" + nameOrUUID);
if(profile.isEmpty()) return "";
Gson g = new Gson();
JsonObject jObj = g.fromJson(result, JsonObject.class);
if(jObj == null || jObj.get("id") == null) return "";
String uuid = jObj.get("id").toString().replace("\"","");
String signature = getURLContent("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
jObj = g.fromJson(signature, JsonObject.class);
if(jObj == null || jObj.get("id") == null) return "";
String value = jObj.getAsJsonArray("properties").get(0).getAsJsonObject().get("value").getAsString();
String decoded = new String(Base64.getDecoder().decode(value));
jObj = g.fromJson(decoded, JsonObject.class);
String skin = jObj.getAsJsonObject("textures").getAsJsonObject("SKIN").get("url").getAsString();
byte[] skinByte = ("{\"textures\":{\"SKIN\":{\"url\":\"" + skin + "\"}}}").getBytes();
JsonObject jObj = g.fromJson(profile, JsonObject.class);
if(jObj == null || jObj.get("textures") == null) return "";
String url = jObj.getAsJsonObject("textures").getAsJsonObject("skin").get("url").getAsString();
// String decoded = new String(Base64.getDecoder().decode(value));
// jObj = g.fromJson(decoded, JsonObject.class);
// String skin = jObj.getAsJsonObject("textures").getAsJsonObject("SKIN").get("url").getAsString();
byte[] skinByte = ("{\"textures\":{\"SKIN\":{\"url\":\"" + url + "\"}}}").getBytes();
String finalSkin = new String(Base64.getEncoder().encode(skinByte));
skinCache.put(name, new CachedData<>(finalSkin));
skinCache.put(nameOrUUID, new CachedData<>(finalSkin));
return finalSkin;
}

public String getHeadValue(UUID uuid) {
return getHeadValue(uuid.toString());
}


public static GameProfile getNonPlayerProfile(String skinURL) {
GameProfile newSkinProfile = new GameProfile(UUID.randomUUID(), null);
Expand All @@ -140,6 +149,8 @@ public static GameProfile getNonPlayerProfile(String skinURL) {
}


private final OkHttpClient httpClient = new OkHttpClient();

final HashMap<String, String> urlCache = new HashMap<>();
final HashMap<String, Long> urlLastget = new HashMap<>();
private String getURLContent(String urlStr) {
Expand All @@ -149,28 +160,28 @@ private String getURLContent(String urlStr) {
) {
return urlCache.get(urlStr);
}
URL url;
BufferedReader in = null;
StringBuilder sb = new StringBuilder();
try{
url = new URL(urlStr);
in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8) );
String str;
while((str = in.readLine()) != null) {
sb.append( str );
Request request = new Request.Builder()
.url(urlStr)
.build();



try(Response response = httpClient.newCall(request).execute()) {
ResponseBody body = response.body();
if(!response.isSuccessful() || body == null) {
/*if(body != null) {
logger.info("Unsuccessful (" + response.code() + ") with: " + body.string());
} else {
logger.info("Null body with " + response.code());
}*/
return urlCache.getOrDefault(urlStr, "");
}
} catch (Exception ignored) { }
finally{
try{
if(in!=null) {
in.close();
}
}catch(IOException ignored) { }
String r = body.string();
urlCache.put(urlStr, r);
urlLastget.put(urlStr, System.currentTimeMillis());
return r;
} catch (IOException e) {
throw new RuntimeException(e);
}

String r = sb.toString();
urlCache.put(urlStr, r);
urlLastget.put(urlStr, System.currentTimeMillis());
return r;
}
}

0 comments on commit 16d13ec

Please sign in to comment.