Skip to content

Commit 99941c7

Browse files
authored
Use PlayerDB.co for name lookups (#82)
I noticed that currently name lookups for UUIDs that are not cached, are not supported in RedisBungee due to Mojang removing name history. When looking at the usages, RedisBungee internally only uses the current name (So no need for full name history), so I moved name lookups to [PlayerDB](https://playerdb.co/), which does not have API rate limits unlike Mojang's API. closes #59
1 parent 7fb9c46 commit 99941c7

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/util/uuid/NameFetcher.java

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,49 @@
1111
package com.imaginarycode.minecraft.redisbungee.api.util.uuid;
1212

1313
import com.google.gson.Gson;
14-
import com.google.gson.reflect.TypeToken;
14+
import com.google.gson.JsonObject;
1515
import com.squareup.okhttp.OkHttpClient;
1616
import com.squareup.okhttp.Request;
1717
import com.squareup.okhttp.ResponseBody;
18+
1819
import java.io.IOException;
19-
import java.lang.reflect.Type;
20-
import java.util.ArrayList;
20+
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.UUID;
2323

24-
@Deprecated
2524
public class NameFetcher {
26-
private static OkHttpClient httpClient;
27-
private static final Gson gson = new Gson();
28-
29-
@Deprecated
30-
public static void setHttpClient(OkHttpClient httpClient) {
31-
throw new UnsupportedOperationException("Due mojang disabled the Names API NameFetcher no longer functions and has been disabled");
32-
// NameFetcher.httpClient = httpClient;
33-
}
34-
35-
@Deprecated
36-
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
37-
throw new UnsupportedOperationException("Due mojang disabled the Names API NameFetcher no longer functions and has been disabled");
38-
// String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names";
39-
// Request request = new Request.Builder().url(url).get().build();
40-
// ResponseBody body = httpClient.newCall(request).execute().body();
41-
// String response = body.string();
42-
// body.close();
43-
//
44-
// Type listType = new TypeToken<List<Name>>() {
45-
// }.getType();
46-
// List<Name> names = gson.fromJson(response, listType);
47-
//
48-
// List<String> humanNames = new ArrayList<>();
49-
// for (Name name : names) {
50-
// humanNames.add(name.name);
51-
// }
52-
// return humanNames;
53-
}
54-
55-
@Deprecated
56-
public static class Name {
57-
private String name;
58-
private long changedToAt;
59-
}
25+
private static OkHttpClient httpClient;
26+
private static final Gson gson = new Gson();
27+
28+
public static void setHttpClient(OkHttpClient httpClient) {
29+
NameFetcher.httpClient = httpClient;
30+
}
31+
32+
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
33+
String name = getName(uuid);
34+
if (name == null) return Collections.emptyList();
35+
return Collections.singletonList(name);
36+
}
37+
38+
public static String getName(UUID uuid) throws IOException {
39+
String url = "https://playerdb.co/api/player/minecraft/" + uuid.toString();
40+
Request request = new Request.Builder()
41+
.addHeader("User-Agent", "RedisBungee-ProxioDev")
42+
.url(url)
43+
.get()
44+
.build();
45+
ResponseBody body = httpClient.newCall(request).execute().body();
46+
String response = body.string();
47+
body.close();
48+
49+
JsonObject json = gson.fromJson(response, JsonObject.class);
50+
if (!json.has("success") || !json.get("success").getAsBoolean()) return null;
51+
if (!json.has("data")) return null;
52+
JsonObject data = json.getAsJsonObject("data");
53+
if (!data.has("player")) return null;
54+
JsonObject player = data.getAsJsonObject("player");
55+
if (!player.has("username")) return null;
56+
57+
return player.get("username").getAsString();
58+
}
6059
}

RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/util/uuid/UUIDTranslator.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import com.google.common.base.Charsets;
1414
import com.google.common.collect.ImmutableMap;
15-
import com.google.common.collect.Iterables;
1615
import com.google.gson.Gson;
1716
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
1817

@@ -169,17 +168,12 @@ public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
169168
if (!expensiveLookups || !plugin.isOnlineMode())
170169
return null;
171170

172-
// That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane.
173-
//
174-
// UPDATE: Mojang has removed the API somewhere in september/2022 due privacy issues
175-
// this is expected to fail now, so we will keep logging it until we figure out something or remove name fetching completely
176-
// Name fetching class was deprecated as result
171+
// That didn't work. Let's ask PlayerDB.
177172
String name;
178173
try {
179-
plugin.logFatal("Due Mojang removing the naming API, we were unable to fetch player names.");
180-
name = Iterables.getLast(NameFetcher.nameHistoryFromUuid(player));
174+
name = NameFetcher.getName(player);
181175
} catch (Exception e) {
182-
plugin.logFatal("Unable to fetch name from Mojang for " + player);
176+
plugin.logFatal("Unable to fetch name from PlayerDB for " + player);
183177
return null;
184178
}
185179

RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void initialize() {
223223
httpClient = new OkHttpClient();
224224
Dispatcher dispatcher = new Dispatcher(getExecutorService());
225225
httpClient.setDispatcher(dispatcher);
226-
//NameFetcher.setHttpClient(httpClient);
226+
NameFetcher.setHttpClient(httpClient);
227227
UUIDFetcher.setHttpClient(httpClient);
228228
InitialUtils.checkRedisVersion(this);
229229
// check if this proxy is recovering from a crash and start heart the beat.

RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public RedisBungeeVelocityPlugin(ProxyServer server, Logger logger, @DataDirecto
110110
this.httpClient = new OkHttpClient();
111111
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(6));
112112
this.httpClient.setDispatcher(dispatcher);
113-
//NameFetcher.setHttpClient(httpClient);
113+
NameFetcher.setHttpClient(httpClient);
114114
UUIDFetcher.setHttpClient(httpClient);
115115
}
116116

0 commit comments

Comments
 (0)