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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Version;
pub use uuid::{self, Uuid};
#[derive(Debug, Error)]
pub enum Error {
#[error("invalid uuid")]
InvalidUuid,
#[error("invalid username")]
InvalidUsername,
#[cfg(feature = "online")]
#[error("ureq transport error: {0}")]
Transport(ureq::Transport),
#[error("mojang api returned unexpected result")]
MojangAPIError,
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "Uuid")]
#[serde(into = "Uuid")]
pub struct OnlineUuid(Uuid);
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "Uuid")]
#[serde(into = "Uuid")]
pub struct OfflineUuid(Uuid);
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "Uuid")]
#[serde(into = "Uuid")]
pub enum PlayerUuid {
Online(OnlineUuid),
Offline(OfflineUuid),
}
#[cfg(feature = "online")]
#[derive(Deserialize)]
struct OnlineUuidResponse {
name: String,
id: PlayerUuid,
}
impl OnlineUuid {
#[cfg(feature = "online")]
pub fn get_username(&self) -> Result<String> {
let response = ureq::get(&format!(
"https://sessionserver.mojang.com/session/minecraft/profile/{}",
self.0
))
.call();
match response {
Ok(data) => {
let response: OnlineUuidResponse =
data.into_json().map_err(|_| Error::MojangAPIError)?;
Ok(response.name)
}
Err(ureq::Error::Status(_, _)) => Err(Error::InvalidUsername),
Err(ureq::Error::Transport(x)) => Err(Error::Transport(x)),
}
}
pub fn as_uuid(&self) -> &Uuid {
&self.0
}
pub fn as_bytes(&self) -> &[u8; 16] {
self.as_uuid().as_bytes()
}
}
impl OfflineUuid {
pub fn as_uuid(&self) -> &Uuid {
&self.0
}
pub fn as_bytes(&self) -> &[u8; 16] {
self.as_uuid().as_bytes()
}
}
impl PlayerUuid {
#[cfg(feature = "online")]
pub fn new_with_online_username(username: &str) -> Result<Self> {
let response = ureq::get(&format!(
"https://api.mojang.com/users/profiles/minecraft/{}",
username
))
.call();
match response {
Ok(data) => {
let response: OnlineUuidResponse =
data.into_json().map_err(|_| Error::MojangAPIError)?;
Ok(response.id)
}
Err(ureq::Error::Status(_, _)) => Err(Error::InvalidUsername),
Err(ureq::Error::Transport(x)) => Err(Error::Transport(x)),
}
}
#[cfg(feature = "offline")]
pub fn new_with_offline_username(username: &str) -> Self {
let mut hash = md5::compute(format!("OfflinePlayer:{}", username)).0;
hash[6] = hash[6] & 0x0f | 0x30; hash[8] = hash[8] & 0x3f | 0x80; let uuid = Uuid::from_bytes(hash);
Self::Offline(OfflineUuid(uuid))
}
pub fn new_with_uuid(uuid: Uuid) -> Result<Self> {
match uuid.get_version() {
Some(Version::Random) => Ok(Self::Online(OnlineUuid(uuid))),
Some(Version::Md5) => Ok(Self::Offline(OfflineUuid(uuid))),
_ => Err(Error::InvalidUuid),
}
}
pub fn as_uuid(&self) -> &Uuid {
match self {
Self::Online(uuid) => uuid.as_uuid(),
Self::Offline(uuid) => uuid.as_uuid(),
}
}
pub fn as_bytes(&self) -> &[u8; 16] {
self.as_uuid().as_bytes()
}
pub fn unwrap_offline(self) -> OfflineUuid {
match self {
Self::Online(_) => panic!("unwrap_offline called on an online uuid"),
Self::Offline(uuid) => uuid,
}
}
pub fn unwrap_online(self) -> OnlineUuid {
match self {
Self::Online(uuid) => uuid,
Self::Offline(_) => panic!("unwrap_online called on an offline uuid"),
}
}
pub fn offline(self) -> Option<OfflineUuid> {
match self {
Self::Online(_) => None,
Self::Offline(uuid) => Some(uuid),
}
}
pub fn online(self) -> Option<OnlineUuid> {
match self {
Self::Online(uuid) => Some(uuid),
Self::Offline(_) => None,
}
}
}
impl TryFrom<Uuid> for PlayerUuid {
type Error = Error;
fn try_from(value: Uuid) -> std::result::Result<Self, Self::Error> {
Self::new_with_uuid(value)
}
}
impl TryFrom<Uuid> for OnlineUuid {
type Error = Error;
fn try_from(value: Uuid) -> std::result::Result<Self, Self::Error> {
PlayerUuid::new_with_uuid(value)?
.online()
.ok_or(Error::InvalidUuid)
}
}
impl TryFrom<Uuid> for OfflineUuid {
type Error = Error;
fn try_from(value: Uuid) -> std::result::Result<Self, Self::Error> {
PlayerUuid::new_with_uuid(value)?
.offline()
.ok_or(Error::InvalidUuid)
}
}
impl From<PlayerUuid> for Uuid {
fn from(other: PlayerUuid) -> Self {
*other.as_uuid()
}
}
impl From<OnlineUuid> for Uuid {
fn from(other: OnlineUuid) -> Self {
*other.as_uuid()
}
}
impl From<OfflineUuid> for Uuid {
fn from(other: OfflineUuid) -> Self {
*other.as_uuid()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "offline")]
#[test]
fn offline_uuids() {
let values = vec![
("boolean_coercion", "db62bdfb-eddc-3acc-a14e-c703aba52549"),
("BooleanCoercion", "44d050b1-46c8-37a8-b511-7023ae304192"),
("bool", "e9fd750e-29c2-3d85-80c9-64618059d454"),
("BOOL", "c5d06acf-0ef6-3a68-bf0b-b57806bcbef5"),
("BoOl", "e38f2cf4-72d2-3a84-8278-fed6908d2746"),
("booleancoercion", "072a2e03-56ce-3960-9391-c56afe17e317"),
];
values
.into_iter()
.map(|(username, uuid)| {
(
*PlayerUuid::new_with_offline_username(username).as_uuid(),
Uuid::try_parse(uuid).unwrap(),
)
})
.for_each(|(uuid1, uuid2)| assert_eq!(uuid1, uuid2));
}
#[cfg(feature = "online")]
#[test]
fn online_uuids() {
let values = vec![
("Notch", "069a79f4-44e9-4726-a5be-fca90e38aaf5"),
("dinnerbone", "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"),
("Dinnerbone", "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"),
];
values
.into_iter()
.map(|(username, uuid)| {
(
*PlayerUuid::new_with_online_username(username)
.unwrap()
.as_uuid(),
Uuid::try_parse(uuid).unwrap(),
)
})
.for_each(|(uuid1, uuid2)| assert_eq!(uuid1, uuid2));
}
#[cfg(feature = "online")]
#[test]
fn online_uuids_to_names() {
let values = vec![
("Notch", "069a79f4-44e9-4726-a5be-fca90e38aaf5"),
("Dinnerbone", "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"),
];
values
.into_iter()
.map(|(username, uuid)| {
(
username,
PlayerUuid::new_with_uuid(Uuid::try_parse(uuid).unwrap())
.unwrap()
.unwrap_online()
.get_username()
.unwrap(),
)
})
.for_each(|(name1, name2)| assert_eq!(name1, name2));
}
}