Allow fetching player head using player name
This commit is contained in:
parent
5b241b871d
commit
b64250404a
26
src/util.ts
26
src/util.ts
@ -17,8 +17,8 @@
|
|||||||
import {DynmapPlayer} from "@/dynmap";
|
import {DynmapPlayer} from "@/dynmap";
|
||||||
import {useStore} from "@/store";
|
import {useStore} from "@/store";
|
||||||
|
|
||||||
const headCache = new Map<DynmapPlayer, HTMLImageElement>(),
|
const headCache = new Map<string, HTMLImageElement>(),
|
||||||
headUnresolvedCache = new Map<DynmapPlayer, Promise<HTMLImageElement>>();
|
headUnresolvedCache = new Map<string, Promise<HTMLImageElement>>();
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getMinecraftTime(serverTime: number) {
|
getMinecraftTime(serverTime: number) {
|
||||||
@ -38,33 +38,35 @@ export default {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
getMinecraftHead(player: DynmapPlayer, size: string): Promise<HTMLImageElement> {
|
getMinecraftHead(player: DynmapPlayer | string, size: string): Promise<HTMLImageElement> {
|
||||||
if(headCache.has(player)) {
|
const account = typeof player === 'string' ? player : player.account;
|
||||||
return Promise.resolve(headCache.get(player) as HTMLImageElement);
|
|
||||||
|
if(headCache.has(account)) {
|
||||||
|
return Promise.resolve(headCache.get(account) as HTMLImageElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(headUnresolvedCache.has(player)) {
|
if(headUnresolvedCache.has(account)) {
|
||||||
return headUnresolvedCache.get(player) as Promise<HTMLImageElement>;
|
return headUnresolvedCache.get(account) as Promise<HTMLImageElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const promise = new Promise((resolve, reject) => {
|
const promise = new Promise((resolve, reject) => {
|
||||||
const faceImage = new Image();
|
const faceImage = new Image();
|
||||||
|
|
||||||
faceImage.onload = function() {
|
faceImage.onload = function() {
|
||||||
headCache.set(player, faceImage);
|
headCache.set(account, faceImage);
|
||||||
resolve(faceImage);
|
resolve(faceImage);
|
||||||
};
|
};
|
||||||
|
|
||||||
faceImage.onerror = function(e) {
|
faceImage.onerror = function(e) {
|
||||||
console.warn(`Failed to retrieve face of ${player.account} with size ${size}!`);
|
console.warn(`Failed to retrieve face of ${account} with size ${size}!`);
|
||||||
reject(e);
|
reject(e);
|
||||||
};
|
};
|
||||||
|
|
||||||
const src = (size === 'body') ? `faces/body/${player.account}.png` :`faces/${size}x${size}/${player.account}.png`;
|
const src = (size === 'body') ? `faces/body/${account}.png` :`faces/${size}x${size}/${account}.png`;
|
||||||
faceImage.src = this.concatURL(window.config.url.markers, src);
|
faceImage.src = this.concatURL(window.config.url.markers, src);
|
||||||
}).finally(() => headUnresolvedCache.delete(player)) as Promise<HTMLImageElement>;
|
}).finally(() => headUnresolvedCache.delete(account)) as Promise<HTMLImageElement>;
|
||||||
|
|
||||||
headUnresolvedCache.set(player, promise);
|
headUnresolvedCache.set(account, promise);
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user