-
Notifications
You must be signed in to change notification settings - Fork 17
/
UUIDRegistry.java
180 lines (150 loc) · 5.12 KB
/
UUIDRegistry.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.griefcraft.util;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class UUIDRegistry {
/**
* Temporal caches
*/
private static final Map<String, PlayerInfo> nameToUUIDCache = new HashMap<String, PlayerInfo>();
private static final Map<UUID, PlayerInfo> UUIDToNameCache = new HashMap<UUID, PlayerInfo>();
static class PlayerInfo {
private UUID uuid;
private String name;
public PlayerInfo(UUID uuid, String name) {
this.uuid = uuid;
this.name = name;
}
public UUID getUUID() {
return uuid;
}
public String getName() {
return name;
}
}
/**
* Update the cache with a new UUID/name pair
*
* @param uuid
* @param name
*/
public static void updateCache(UUID uuid, String name) {
PlayerInfo playerInfo = new PlayerInfo(uuid, name);
nameToUUIDCache.put(name.toLowerCase(), playerInfo);
UUIDToNameCache.put(uuid, playerInfo);
}
/**
* Check if a uuid in string form is a valid Java UUID
*
* @param uuid
* @return true if the string is a valid UUID
*/
public static boolean isValidUUID(String uuid) {
return uuid.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
}
/**
* Get the name for the given UUID. If it is not already known, it will be retrieved from the account servers.
*
* @param uuid
* @return
*/
public static String getName(UUID uuid) {
if (uuid == null) {
return null;
}
if (UUIDToNameCache.containsKey(uuid)) {
return UUIDToNameCache.get(uuid).getName();
}
// First way: if they're on the server already
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
updateCache(uuid, player.getName());
return player.getName();
}
// Second way: if they have been on the server before
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
if (offlinePlayer != null && offlinePlayer.getName() != null) {
updateCache(uuid, offlinePlayer.getName());
return offlinePlayer.getName();
}
// Third way: use the web API
try {
Map<UUID, String> results = new NameFetcher(Arrays.asList(uuid)).call();
if (results.containsKey(uuid)) {
return results.get(uuid);
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
/**
* Get the UUID for the given name. If it is not already known, it will be retrieved from the account servers.
*
* @param name
* @return
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static UUID getUUID(String name) {
String nameLower = name.toLowerCase();
try {
if (nameToUUIDCache.containsKey(nameLower)) {
return nameToUUIDCache.get(nameLower).getUUID();
}
if (isValidUUID(name)) {
return UUID.fromString(name);
}
if (Bukkit.getOnlineMode()) {
Map<String, UUID> results = new UUIDFetcher(Arrays.asList(nameLower)).call();
// The returned name is the exact casing; so we need to look for it
// in the case-insensitive version
for (String key : results.keySet()) {
if (key.equalsIgnoreCase(name)) {
UUID uuid = results.get(key);
updateCache(uuid, key);
return uuid;
}
}
} else {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(name);
if (offlinePlayer != null && offlinePlayer.getUniqueId() != null) {
if (offlinePlayer.getName() != null) {
name = offlinePlayer.getName();
}
updateCache(offlinePlayer.getUniqueId(), name);
return offlinePlayer.getUniqueId();
}
}
nameToUUIDCache.put(nameLower, null);
return null;
} catch (Exception e) {
nameToUUIDCache.put(nameLower, null);
return null;
}
}
/**
* Attempts to format a player's name, which can be a name or a UUID. If the owner is a UUID and then
* UUID is unknown, then "Unknown (uuid)" will be returned.
*
* @param name
* @return
*/
public static String formatPlayerName(String name) {
if (isValidUUID(name)) {
String formattedName = getName(UUID.fromString(name));
if (formattedName == null) {
return "Unknown (" + name + ")";
} else {
return formattedName + " (" + name + ")";
}
} else {
return name;
}
}
}