1
1
const mojangson = require ( 'mojangson' )
2
2
const vsprintf = require ( './format' )
3
+ const debug = require ( 'debug' ) ( 'minecraft-protocol' )
3
4
4
5
module . exports = loader
6
+ const getValueSafely = ( obj , key , def ) => Object . hasOwn ( obj , key ) ? obj [ key ] : def
5
7
6
8
function loader ( registryOrVersion ) {
7
9
const registry = typeof registryOrVersion === 'string' ? require ( 'prismarine-registry' ) ( registryOrVersion ) : registryOrVersion
@@ -76,6 +78,7 @@ function loader (registryOrVersion) {
76
78
} else {
77
79
throw new Error ( 'Expected String or Object for Message argument' )
78
80
}
81
+ this . warn = displayWarning ? console . warn : debug
79
82
this . parse ( displayWarning )
80
83
}
81
84
@@ -84,7 +87,7 @@ function loader (registryOrVersion) {
84
87
* Called by the Constructor
85
88
* @return {void }
86
89
*/
87
- parse ( displayWarning = false ) {
90
+ parse ( ) {
88
91
const json = this . json
89
92
// Message scope for callback functions
90
93
// There is EITHER, a text property or a translate property
@@ -183,19 +186,18 @@ function loader (registryOrVersion) {
183
186
break
184
187
}
185
188
// Make sure color is valid
186
- if ( Array . prototype . indexOf && this . color &&
187
- supportedColors . indexOf ( this . color ) === - 1 &&
188
- ! this . color . match ( / # [ a - f A - F \d ] { 6 } / ) && displayWarning ) {
189
- console . warn ( 'ChatMessage parsed with unsupported color' , this . color )
189
+ if ( this . color && ! supportedColors . includes ( this . color ) && ! this . color . match ( / # [ a - f A - F \d ] { 6 } / ) ) {
190
+ this . warn ( 'ChatMessage parsed with unsupported color' , this . color )
191
+ this . color = null
190
192
}
191
193
192
194
// Parse click event
193
195
if ( typeof json . clickEvent === 'object' ) {
194
196
this . clickEvent = json . clickEvent
195
197
if ( typeof this . clickEvent . action !== 'string' ) {
196
198
throw new Error ( 'ClickEvent action missing in ChatMessage' )
197
- } else if ( Array . prototype . indexOf && supportedClick . indexOf ( this . clickEvent . action ) === - 1 && displayWarning ) {
198
- console . warn ( 'ChatMessage parsed with unsupported clickEvent' , this . clickEvent . action )
199
+ } else if ( ! supportedClick . includes ( this . clickEvent . action ) ) {
200
+ this . warn ( 'ChatMessage parsed with unsupported clickEvent' , this . clickEvent . action )
199
201
}
200
202
}
201
203
@@ -204,8 +206,8 @@ function loader (registryOrVersion) {
204
206
this . hoverEvent = json . hoverEvent
205
207
if ( typeof this . hoverEvent . action !== 'string' ) {
206
208
throw new Error ( 'HoverEvent action missing in ChatMessage' )
207
- } else if ( Array . prototype . indexOf && supportedHover . indexOf ( this . hoverEvent . action ) === - 1 && displayWarning ) {
208
- console . warn ( 'ChatMessage parsed with unsupported hoverEvent' , this . hoverEvent . action )
209
+ } else if ( ! supportedHover . includes ( this . hoverEvent . action ) ) {
210
+ this . warn ( 'ChatMessage parsed with unsupported hoverEvent' , this . hoverEvent . action )
209
211
}
210
212
// Special case
211
213
if ( this . hoverEvent . action === 'show_item' ) {
@@ -226,7 +228,7 @@ function loader (registryOrVersion) {
226
228
try {
227
229
this . hoverEvent . value = mojangson . parse ( content )
228
230
} catch ( err ) {
229
-
231
+ debug ( err )
230
232
}
231
233
}
232
234
}
@@ -302,8 +304,7 @@ function loader (registryOrVersion) {
302
304
const _with = this . with ?? [ ]
303
305
304
306
const args = _with . map ( entry => entry . toString ( lang ) )
305
- const format = lang [ this . translate ] ?? this . translate
306
-
307
+ const format = getValueSafely ( lang , this . translate , this . translate )
307
308
message += vsprintf ( format , args )
308
309
}
309
310
if ( this . extra ) {
@@ -355,16 +356,15 @@ function loader (registryOrVersion) {
355
356
return codes [ code ]
356
357
} ) . join ( '' )
357
358
358
- if ( ( typeof this . text === 'string' || typeof this . text === 'number' ) /* && this.text !== '' */ ) message += this . text
359
+ if ( ( typeof this . text === 'string' ) || ( typeof this . text === 'number' ) ) message += this . text
359
360
else if ( this . translate !== undefined ) {
360
361
const _with = this . with ?? [ ]
361
362
362
363
const args = _with . map ( entry => {
363
364
const entryAsMotd = entry . toMotd ( lang , this )
364
365
return entryAsMotd + ( entryAsMotd . includes ( '§' ) ? '§r' + message : '' )
365
366
} )
366
- const format = lang [ this . translate ] ?? this . translate
367
-
367
+ const format = getValueSafely ( lang , this . translate , this . translate )
368
368
message += vsprintf ( format , args )
369
369
}
370
370
if ( this . extra ) {
@@ -416,7 +416,7 @@ function loader (registryOrVersion) {
416
416
params . push ( param . toHTML ( lang , styles , allowedFormats ) )
417
417
}
418
418
}
419
- const format = lang [ this . translate ] ?? this . translate
419
+ const format = getValueSafely ( lang , this . translate , this . translate )
420
420
str += vsprintf ( escapeHtml ( format ) , params )
421
421
}
422
422
@@ -442,8 +442,9 @@ function loader (registryOrVersion) {
442
442
// For example,
443
443
// printf("<%s> %s" /* fmt string */, [sender], [content])
444
444
static fromNetwork ( type , params ) {
445
- const format = registry . chatFormattingById [ type ]
446
- return new ChatMessage ( { translate : format . formatString , with : format . parameters . map ( p => params [ p ] ) } )
445
+ const format = getValueSafely ( registry . chatFormattingById , type )
446
+ if ( format == null ) throw new Error ( 'unknown chat format code: ' + type ) // The server may be attempting to send a chat message before sending a login codec, which is not allowed
447
+ return new ChatMessage ( { translate : format . formatString , with : format . parameters . map ( p => getValueSafely ( params , p , '' ) ) } )
447
448
}
448
449
}
449
450
0 commit comments