@@ -11,6 +11,9 @@ import {defaultSkin, delay, download, mergeStats, writeJSON} from './helper'
11
11
import * as logger from './logger'
12
12
13
13
const config = loadConfig ( )
14
+ const outputDir = config . resolve ( config . render . output )
15
+ const playersPath = path . join ( outputDir , 'players.json' )
16
+ const oldPlayers : NSPlayerInfoData [ ] | null = fs . existsSync ( playersPath ) ? fs . readJsonSync ( playersPath ) : null
14
17
15
18
export default class Utils {
16
19
apiLimited : boolean
@@ -111,11 +114,10 @@ export default class Utils {
111
114
}
112
115
logger . PlayerData . info ( 'READ' , datafile )
113
116
const uuidShort = uuid . replace ( / - / g, '' )
114
- let history
115
- try {
116
- history = await this . getNameHistory ( uuidShort )
117
- } catch ( error ) {
118
- return reject ( )
117
+ let history = oldPlayers ?. find ( p => p . uuid === uuidShort ) ?. names ?? null
118
+ if ( ! history ) {
119
+ const name = await this . getCurrentName ( uuid )
120
+ history = name ? [ { name, detectedAt : Date . now ( ) } ] : null
119
121
}
120
122
if ( history && history [ 0 ] ) {
121
123
let lived : number | undefined
@@ -169,19 +171,15 @@ export default class Utils {
169
171
}
170
172
}
171
173
172
- async getNameHistory ( uuid : LongUuid ) : Promise < McNameHistory | null > {
173
- const apiNameHistory = `https://api .mojang.com/user/profiles/ ${ uuid } /names `
174
- let history
174
+ async getCurrentName ( uuid : LongUuid ) : Promise < string | null > {
175
+ const apiProfile = `https://sessionserver .mojang.com/session/minecraft/profile/ ${ uuid } `
176
+ let profile
175
177
try {
176
- history = await this . getMojangAPI < McNameHistory > ( apiNameHistory )
178
+ profile = await this . getMojangAPI < McPlayerProfile | '' > ( apiProfile )
179
+ return profile === '' ? null : profile . name
177
180
} catch ( err ) {
178
181
return null
179
182
}
180
- if ( ! history ) return null
181
- // The order of the response data from Mojang API is uncertain,
182
- // so manually sort it (to descending order) for making sure.
183
- history . sort ( ( a , b ) => ( b . changedToAt || 0 ) - ( a . changedToAt || 0 ) )
184
- return history
185
183
}
186
184
187
185
async getMojangAPI < T > ( apiPath : string ) : Promise < T > {
@@ -239,11 +237,17 @@ export default class Utils {
239
237
}
240
238
241
239
async createPlayerData ( uuid : LongUuid , banned = false ) : Promise < NSPlayerStatsJson > {
242
- const playerpath = path . join ( config . get < string > ( 'render.output' ) , uuid . replace ( / - / g, '' ) )
240
+ const uuidShort = uuid . replace ( / - / g, '' )
241
+ const playerpath = path . join ( config . get < string > ( 'render.output' ) , uuidShort )
243
242
let data
244
243
try {
245
244
if ( fs . existsSync ( path . join ( playerpath , 'stats.json' ) ) ) {
246
245
data = JSON . parse ( fs . readFileSync ( path . join ( playerpath , 'stats.json' ) , 'utf-8' ) )
246
+ // Name data is currently updated only in players.json
247
+ // so we need to duplicate it into stats.json
248
+ const playerInfo = oldPlayers ! . find ( p => p . uuid === uuidShort ) !
249
+ data . data . playername = playerInfo . playername
250
+ data . data . names = playerInfo . names
247
251
} else {
248
252
data = await this . getPlayerTotalData ( uuid )
249
253
}
0 commit comments