2022-12-31 03:23:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-present RedisBungee contributors
|
|
|
|
*
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
*
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*/
|
|
|
|
|
2022-07-16 12:50:09 +00:00
|
|
|
package com.imaginarycode.minecraft.redisbungee.api.util.uuid;
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2022-04-13 13:14:08 +00:00
|
|
|
import com.google.gson.Gson;
|
2023-07-18 00:48:20 +00:00
|
|
|
import com.google.gson.JsonObject;
|
2015-02-04 15:02:30 +00:00
|
|
|
import com.squareup.okhttp.OkHttpClient;
|
2015-01-25 05:17:52 +00:00
|
|
|
import com.squareup.okhttp.Request;
|
2016-03-08 01:56:58 +00:00
|
|
|
import com.squareup.okhttp.ResponseBody;
|
2023-07-18 00:48:20 +00:00
|
|
|
|
2015-01-09 14:44:03 +00:00
|
|
|
import java.io.IOException;
|
2023-07-18 00:48:20 +00:00
|
|
|
import java.util.Collections;
|
2015-01-09 14:45:45 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2015-01-09 14:44:03 +00:00
|
|
|
public class NameFetcher {
|
2024-04-12 18:37:02 +00:00
|
|
|
private static OkHttpClient httpClient;
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
|
|
|
|
public static void setHttpClient(OkHttpClient httpClient) {
|
|
|
|
NameFetcher.httpClient = httpClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
|
|
|
|
String name = getName(uuid);
|
|
|
|
if (name == null) return Collections.emptyList();
|
|
|
|
return Collections.singletonList(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getName(UUID uuid) throws IOException {
|
|
|
|
String url = "https://playerdb.co/api/player/minecraft/" + uuid.toString();
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
.addHeader("User-Agent", "RedisBungee-ProxioDev")
|
|
|
|
.url(url)
|
|
|
|
.get()
|
|
|
|
.build();
|
|
|
|
ResponseBody body = httpClient.newCall(request).execute().body();
|
|
|
|
String response = body.string();
|
|
|
|
body.close();
|
|
|
|
|
|
|
|
JsonObject json = gson.fromJson(response, JsonObject.class);
|
|
|
|
if (!json.has("success") || !json.get("success").getAsBoolean()) return null;
|
|
|
|
if (!json.has("data")) return null;
|
|
|
|
JsonObject data = json.getAsJsonObject("data");
|
|
|
|
if (!data.has("player")) return null;
|
|
|
|
JsonObject player = data.getAsJsonObject("player");
|
|
|
|
if (!player.has("username")) return null;
|
|
|
|
|
|
|
|
return player.get("username").getAsString();
|
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|