Skip to content

Commit bf03660

Browse files
committed
fix inspectkit lag for offline players by replacing getOfflinePlayers() with Mojang API lookup
1 parent d7c9c5f commit bf03660

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

src/main/java/dev/noah/perplayerkit/commands/InspectCommandUtil.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package dev.noah.perplayerkit.commands;
2020

21+
import com.squareup.okhttp.OkHttpClient;
22+
import com.squareup.okhttp.Request;
23+
import com.squareup.okhttp.Response;
2124
import dev.noah.perplayerkit.util.BroadcastManager;
2225
import net.kyori.adventure.text.Component;
2326
import net.kyori.adventure.text.minimessage.MiniMessage;
@@ -27,6 +30,8 @@
2730
import org.jetbrains.annotations.NotNull;
2831
import org.jetbrains.annotations.Nullable;
2932

33+
import java.io.IOException;
34+
import java.nio.charset.StandardCharsets;
3035
import java.util.UUID;
3136
import java.util.concurrent.CompletableFuture;
3237

@@ -63,14 +68,36 @@ public static CompletableFuture<UUID> resolvePlayerIdentifierAsync(String identi
6368
return CompletableFuture.completedFuture(onlinePlayer.getUniqueId());
6469
}
6570

66-
// Search offline players asynchronously (this can be slow)
71+
// Look up UUID via Mojang API (avoids the very slow Bukkit.getOfflinePlayers() scan)
6772
return CompletableFuture.supplyAsync(() -> {
68-
for (OfflinePlayer offlinePlayer : Bukkit.getOfflinePlayers()) {
69-
if (identifier.equalsIgnoreCase(offlinePlayer.getName())) {
70-
return offlinePlayer.getUniqueId();
73+
try {
74+
OkHttpClient client = new OkHttpClient();
75+
Request request = new Request.Builder()
76+
.url("https://api.mojang.com/users/profiles/minecraft/" + identifier)
77+
.build();
78+
try (Response response = client.newCall(request).execute()) {
79+
if (response.isSuccessful() && response.body() != null) {
80+
String body = response.body().string();
81+
// Parse the "id" field: {"id":"<uuid-no-dashes>","name":"<name>"}
82+
int idStart = body.indexOf("\"id\":\"") + 6;
83+
int idEnd = body.indexOf("\"", idStart);
84+
if (idStart > 5 && idEnd > idStart) {
85+
String raw = body.substring(idStart, idEnd);
86+
// Insert dashes into the 32-char UUID string
87+
String formatted = raw.substring(0, 8) + "-"
88+
+ raw.substring(8, 12) + "-"
89+
+ raw.substring(12, 16) + "-"
90+
+ raw.substring(16, 20) + "-"
91+
+ raw.substring(20);
92+
return UUID.fromString(formatted);
93+
}
94+
}
7195
}
96+
} catch (IOException ignored) {
97+
// Fall through to offline UUID computation
7298
}
73-
return null;
99+
// Fallback for offline/cracked-mode servers: compute deterministic offline UUID
100+
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + identifier).getBytes(StandardCharsets.UTF_8));
74101
});
75102
}
76103

0 commit comments

Comments
 (0)