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