import { Player } from "./internal/Player" ; import type { Intent } from "matrix-bot-sdk" ; import { LogService } from "matrix-bot-sdk" ; type MarcoProfile = { skin : string ; } ; /**
 * The PlayerManager class handles interaction with the Mojang API to get
 * all the player details of a certain player on Minecraft. It even goes to
 * the extent of cropping the head out of a player's skin to keep their
 * Matrix puppet in sync
 */ export class PlayerManager { private readonly players : Map < string , Player > ; constructor ( ) { this . players = new Map ( ) ; } example param { ? string } name param { ? string } uuid returns { Promise < void > } throws public async getPlayer ( name ? , uuid ? ) { let player = this . players . get ( uuid || '' ) ; if ( player ) return player ; else if ( ! name && ! uuid ) throw new Error ( 'Both parameters can not be undefined' ) ; else { player = new Player ( name , uuid ) ; uuid = await player . getUUID ( ) ; this . players . set ( uuid , player ) ; return player ; } } param { Intent } intent param { Player } player returns { Promise < void > } public async sync ( intent , player ) { const mxProfile = await intent . underlyingClient . getUserProfile ( intent . userId ) ; const playerName = await player . getName ( ) ; const mxName = mxProfile [ 'displayname' ] ; let storedSkin ; try { let marcoProfile = await intent . underlyingClient . getAccountData ( "dev.dhdf.marco" ) ; storedSkin = marcoProfile . skin || '' ; } catch ( err ) { storedSkin = '' ; } if ( mxName != playerName ) { await intent . underlyingClient . setDisplayName ( playerName ) ; } const currentSkin = await player . getSkinURL ( ) ; LogService . debug ( 'marco-PlayerManager' , `Player's current skin:\n${ currentSkin }\n` + `Player's stored skin:\n${ storedSkin }\n` + `Difference? ${ storedSkin == currentSkin ? 'no' : 'yes' }` ) ; if ( storedSkin != currentSkin ) { const head = await player . getHead ( ) ; const marcoProfile = { skin : currentSkin } ; const mxUrl = await intent . underlyingClient . uploadContent ( head , 'image/png' , playerName + '-head.png' ) ; await intent . underlyingClient . setAvatarUrl ( mxUrl ) ; await intent . underlyingClient . setAccountData ( "dev.dhdf.marco" , marcoProfile ) ; } } }