parent
315f12b2c1
commit
616f7e52a0
|
@ -3,24 +3,24 @@ import axios from "axios";
|
||||||
import TwoWayMap from "../utils/two-way-map.js";
|
import TwoWayMap from "../utils/two-way-map.js";
|
||||||
|
|
||||||
class MojangAPI {
|
class MojangAPI {
|
||||||
private cached: TwoWayMap<string, string> = new TwoWayMap<string, string>(); // uuid, id
|
private cache: TwoWayMap<string, string> = new TwoWayMap<string, string>(); // uuid, id
|
||||||
|
|
||||||
async getIdFromUUID(minecraft_uuid: string): Promise<string> {
|
async getIdFromUUID(minecraft_uuid: string): Promise<string> {
|
||||||
let id = this.cached.get_by_first(minecraft_uuid);
|
let id = this.cache.get_by_first(minecraft_uuid);
|
||||||
if (id) return id;
|
if (id) return id;
|
||||||
|
|
||||||
const request = await axios.get(
|
const request = await axios.get(
|
||||||
`https://api.mojang.com/user/profile/${minecraft_uuid}`
|
`https://api.mojang.com/user/profile/${minecraft_uuid}`
|
||||||
);
|
);
|
||||||
id = request.data.name;
|
id = request.data.name;
|
||||||
|
|
||||||
if (!id) throw new Error("Failed to get minecraft id from api");
|
if (!id) throw new Error("Failed to get minecraft id from api");
|
||||||
|
|
||||||
this.cached.set_by_first(minecraft_uuid, id);
|
this.cache.remove_by_second(id);
|
||||||
|
this.cache.set_by_first(minecraft_uuid, id);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
async getUUIDFromId(minecraft_id): Promise<string> {
|
async getUUIDFromId(minecraft_id): Promise<string> {
|
||||||
let uuid = this.cached.get_by_second(minecraft_id);
|
let uuid = this.cache.get_by_second(minecraft_id);
|
||||||
if (uuid) return uuid;
|
if (uuid) return uuid;
|
||||||
|
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
|
@ -29,8 +29,8 @@ class MojangAPI {
|
||||||
uuid = response.data.id;
|
uuid = response.data.id;
|
||||||
if (!uuid) throw new Error("Failed to get minecraft id from api");
|
if (!uuid) throw new Error("Failed to get minecraft id from api");
|
||||||
|
|
||||||
this.cached.remove_by_first(uuid);
|
this.cache.remove_by_first(uuid);
|
||||||
this.cached.set_by_second(uuid, minecraft_id);
|
this.cache.set_by_second(uuid, minecraft_id);
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue