-
Notifications
You must be signed in to change notification settings - Fork 4
/
NameFetcher.java
89 lines (80 loc) · 2.51 KB
/
NameFetcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//package your.package.here;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Helper-class for getting names of players.
*/
public final class NameFetcher {
private static final String NAME_URL = "https://sessionserver.mojang.com"
+ "/session/minecraft/profile/";
private static final Pattern NAME_PATTERN = Pattern.compile(",\\s*\"name\"\\s*:\\s*\"(.*?)\"");
private NameFetcher() {
throw new UnsupportedOperationException();
}
/**
* Returns the name of the searched player.
*
* @param uuid The UUID of a player.
* @return The name of the given player.
*/
public static String getName(UUID uuid) {
return getName(uuid.toString());
}
/**
* Returns the name of the searched player.
*
* @param uuid The UUID of a player (can be trimmed or the normal version).
* @return The name of the given player.
*/
public static String getName(String uuid) {
uuid = uuid.replace("-", "");
String output = callURL(NAME_URL + uuid);
Matcher m = NAME_PATTERN.matcher(output);
if (m.find()) {
return m.group(1);
}
return null;
}
private static String callURL(String urlStr) {
StringBuilder sb = new StringBuilder();
URLConnection conn;
BufferedReader br = null;
InputStreamReader in = null;
try {
conn = new URL(urlStr).openConnection();
if (conn != null) {
conn.setReadTimeout(60 * 1000);
}
if (conn != null && conn.getInputStream() != null) {
in = new InputStreamReader(conn.getInputStream(), "UTF-8");
br = new BufferedReader(in);
String line = br.readLine();
while (line != null) {
sb.append(line).append("\n");
line = br.readLine();
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Throwable ignored) {
}
}
if (in != null) {
try {
in.close();
} catch (Throwable ignored) {
}
}
}
return sb.toString();
}
}