-
Notifications
You must be signed in to change notification settings - Fork 4
/
UUIDFetcher.java
129 lines (116 loc) · 3.45 KB
/
UUIDFetcher.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//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;
/*
Uncomment this if you want the helper method for BungeeCord:
import net.md_5.bungee.api.connection.ProxiedPlayer;
*/
/*
Uncomment this if you want the helper method for Bukkit/Spigot:
import org.bukkit.entity.Player;
*/
/**
* Helper-class for getting UUIDs of players.
*/
public final class UUIDFetcher {
private static final String UUID_URL = "https://api.mojang.com/users"
+ "/profiles/minecraft/";
private static final Pattern UUID_PATTERN = Pattern.compile("\"id\"\\s*:\\s*\"(.*?)\"");
private UUIDFetcher() {
throw new UnsupportedOperationException();
}
/**
* Returns the UUID of the searched player.
*
* @param player The player.
* @return The UUID of the given player.
*/
//Uncomment this if you want the helper method for BungeeCord:
/*
public static UUID getUUID(ProxiedPlayer player) {
return getUUID(player.getName());
}
*/
/**
* Returns the UUID of the searched player.
*
* @param player The player.
* @return The UUID of the given player.
*/
//Uncomment this if you want the helper method for Bukkit/Spigot:
/*
public static UUID getUUID(Player player) {
return getUUID(player.getName());
}
*/
/**
* Returns the UUID of the searched player.
*
* @param name The name of the player.
* @return The UUID of the given player.
*/
public static UUID getUUID(String name) {
String output = callURL(UUID_URL + name);
Matcher m = UUID_PATTERN.matcher(output);
if (m.find()) {
return UUID.fromString(insertDashes(m.group(1)));
}
return null;
}
/**
* Helper method for inserting dashes into
* unformatted UUID.
*
* @return Formatted UUID with dashes.
*/
public static String insertDashes(String uuid) {
StringBuilder sb = new StringBuilder(uuid);
sb.insert(8, '-');
sb.insert(13, '-');
sb.insert(18, '-');
sb.insert(23, '-');
return sb.toString();
}
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();
}
}