通过MinecraftID来获取UUID
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import org.json.JSONObject;
public class GetUUID {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String uuid = input.next();
String[] uuids = uuid.split("#");
for (String s : uuids) {
System.out.println("{\n \"name\":\"" + s + "\",\n \"uuid\":\"" + getFullUUID(getUUID(s)) + "\"\n},");
}
}
public static String getUUID(String username) {
try {
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
JSONObject json = new JSONObject(response.toString());
return json.getString("id");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getFullUUID(String trimmedUUID) {
return trimmedUUID.substring(0, 8) + "-" +
trimmedUUID.substring(8, 12) + "-" +
trimmedUUID.substring(12, 16) + "-" +
trimmedUUID.substring(16, 20) + "-" +
trimmedUUID.substring(20);
}
}