Skip to content

Commit

Permalink
fix: Bring back UsernameUtils to grab player usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Oct 17, 2024
1 parent 529ca72 commit 8c74e9d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,6 @@ public static String getNameFromUUID(UUID uuid) {
return info.getProfile().getName();
}
}
return "Unknown Player";
return UsernameUtils.INSTANCE.getName(uuid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Steam 'n' Rails
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.railwayteam.railways.compat.journeymap;

import com.google.gson.JsonParser;
import net.minecraft.client.Minecraft;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public enum UsernameUtils {
INSTANCE;

private final HashMap<UUID, String> uuidNameMap = new HashMap<>();
private final Set<UUID> tried = new HashSet<>();

private static final String url = "https://sessionserver.mojang.com/session/minecraft/profile/";

public String getName(UUID uuid) {
if (uuid == null) return "Unknown";
if (!uuidNameMap.containsKey(uuid)) {
if (Minecraft.getInstance().getUser().getUuid().equals(uuid.toString())) {
uuidNameMap.put(uuid, Minecraft.getInstance().getUser().getName());
return uuidNameMap.get(uuid);
}
if (!tried.contains(uuid)) {
CompletableFuture.runAsync(() -> {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url + uuid.toString().replace("-", "")))
.GET()
.build();
try {
String body = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
uuidNameMap.put(uuid, JsonParser.parseString(body).getAsJsonObject().get("name").getAsString());
} catch (Exception ignored) {}
});
tried.add(uuid);
}
return "Unknown Player";
}
return uuidNameMap.get(uuid);
}
}

0 comments on commit 8c74e9d

Please sign in to comment.