2024-10-23 23:07:37 -04:00
import lang from './mc_lang.js'
2024-10-22 23:11:33 -04:00
const consoleColors = {
dark _red : '§4' ,
red : '§c' ,
dark _green : '§2' ,
green : '§a' ,
gold : '§6' ,
yellow : '§e' ,
dark _blue : '§1' ,
blue : '§9' ,
dark _purple : '§5' ,
light _purple : '§d' ,
dark _aqua : '§3' ,
aqua : '§b' ,
black : '§0' ,
gray : '§7' ,
dark _gray : '§8' ,
white : '§f' ,
reset : '§r§f'
}
const processColor = ( col , rcol ) => {
let out
if ( col === 'reset' ) {
out = rcol
} else if ( col . startsWith ( '#' ) ) {
out = ` § ${ col } `
} else {
out = consoleColors [ col ]
}
return out
}
const parse = function ( _data , l = 0 , resetColor = consoleColors . reset ) {
if ( l >= 4 ) {
return ''
}
let data
if ( typeof _data === 'string' ) {
data = { text : _data , color : 'reset' }
} else if ( typeof _data === 'number' ) {
data = { text : _data + '' , color : 'reset' }
} else if ( _data . constructor === Array ) {
data = { extra : _data , color : 'reset' }
} else {
data = _data
}
if ( data [ '' ] ) {
data . text = data [ '' ]
if ( ! data . color ) data . color = 'reset'
}
let out = ''
if ( data . color ) {
out += processColor ( data . color , resetColor )
} else {
out += resetColor
}
if ( data . text ) {
let _text = data . text
if ( typeof _text === 'number' ) {
_text = _text . toString ( )
}
out += _text . replaceAll ( '\x1b' , '' ) . replaceAll ( '\x0e' , '' )
}
if ( data . translate ) {
let trans = data . translate . replaceAll ( '%%' , '\ud900\ud801' ) . replaceAll ( '\x1b' , '' ) . replaceAll ( '\x0e' , '' )
if ( lang [ trans ] !== undefined ) {
trans = lang [ trans ] . replace ( /%%/g , '\ue123' )
}
if ( data . with ) {
data . with . forEach ( ( item , i ) => {
const j2 = parse ( item , l + 1 , data . color ? processColor ( data . color , resetColor ) : resetColor )
trans = trans . replace ( /%s/ , j2 . replaceAll ( '%s' , '\ud900\ud804' ) . replaceAll ( '$s' , '\ud900\ud805' ) )
trans = trans . replaceAll ( ` % ${ + i + 1 } $ s ` , j2 . replaceAll ( '%s' , '\ud900\ud804' ) . replaceAll ( '$s' , '\ud900\ud805' ) )
} )
}
out += trans . replaceAll ( '\ud900\ud801' , '%' ) . replaceAll ( '\ud900\ud804' , '%s' ) . replaceAll ( '\ud900\ud805' , '$s' )
}
if ( data . extra ) {
for ( const item of data . extra ) {
const parsed = parse ( item , l , data . color ? processColor ( data . color , resetColor ) : resetColor )
out += parsed
}
}
out += resetColor
return out
}
2024-10-23 22:59:54 -04:00
export default function parse2 ( _data , l , resetColor ) {
2024-10-22 23:11:33 -04:00
try {
return parse ( _data )
} catch ( e ) {
console . error ( e )
return ` \x 1B[0m \x 1B[38;2;255;85;85mAn error occured while parsing a message. See console for more information. \n JSON that caused the error: ${ JSON . stringify ( _data ) } `
}
}