patch some message not show

This commit is contained in:
Yaode_owo 2024-06-14 08:01:48 -04:00
parent ad1542b21b
commit 3912f76ff9

View file

@ -1,10 +1,8 @@
const mojangson = require('mojangson')
const vsprintf = require('./format.js')
const vsprintf = require('./format')
const debug = require('debug')('minecraft-protocol')
const nbt = require('prismarine-nbt')
const getValueSafely = (obj, key, def) => Object.hasOwn(obj, key) ? obj[key] : def
const MAX_CHAT_DEPTH = 8
const MAX_CHAT_LENGTH = 4096
function loader (registryOrVersion) {
const registry = typeof registryOrVersion === 'string' ? require('prismarine-registry')(registryOrVersion) : registryOrVersion
@ -298,29 +296,27 @@ function loader (registryOrVersion) {
* Flattens the message in to plain-text
* @return {String}
*/
toString (lang = defaultLang, _depth = 0) {
if (_depth > MAX_CHAT_DEPTH) return ''
toString (lang = defaultLang) {
let message = ''
if (typeof this.text === 'string' || typeof this.text === 'number') message += this.text
else if (this.translate !== undefined) {
const _with = this.with ?? []
const args = _with.map(entry => entry.toString(lang, _depth + 1))
const args = _with.map(entry => entry.toString(lang))
const format = getValueSafely(lang, this.translate, this.translate)
message += vsprintf(format, args)
}
if (this.extra) {
message += this.extra.map((entry) => entry.toString(lang, _depth + 1)).join('')
message += this.extra.map((entry) => entry.toString(lang)).join('')
}
return message.replace(/§[0-9a-flnmokr]/g, '').slice(0, MAX_CHAT_LENGTH)
return message.replace(/§[0-9a-flnmokr]/g, '')
}
valueOf () {
return this.toString()
}
toMotd (lang = defaultLang, parent = {}, _depth = 0) {
if (_depth > MAX_CHAT_DEPTH) return ''
toMotd (lang = defaultLang, parent = {}) {
const codes = {
color: {
black: '§0',
@ -364,16 +360,16 @@ function loader (registryOrVersion) {
const _with = this.with ?? []
const args = _with.map(entry => {
const entryAsMotd = entry.toMotd(lang, this, _depth + 1)
const entryAsMotd = entry.toMotd(lang, this)
return entryAsMotd + (entryAsMotd.includes('§') ? '§r' + message : '')
})
const format = getValueSafely(lang, this.translate, this.translate)
message += vsprintf(format, args)
}
if (this.extra) {
message += this.extra.map(entry => entry.toMotd(lang, this, _depth + 1)).join('§r')
message += this.extra.map(entry => entry.toMotd(lang, this)).join('§r')
}
return message.slice(0, MAX_CHAT_LENGTH)
return message
}
toAnsi (lang = defaultLang, codes = defaultAnsiCodes) {
@ -392,12 +388,11 @@ function loader (registryOrVersion) {
// ANSI from https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#rgb-colors
message = message.replace(hexRegex, `\u001b[38;2;${red};${green};${blue}m`)
}
return codes['§r'] + message.slice(0, MAX_CHAT_LENGTH) + codes['§r']
return codes['§r'] + message + codes['§r']
}
// NOTE : Have to be be mindful here as bad HTML gen may lead to arbitrary code execution from server
toHTML (lang = registry.language, styles = cssDefaultStyles, allowedFormats = formatMembers, _depth = 0) {
if (_depth > MAX_CHAT_DEPTH) return ''
toHTML (lang = registry.language, styles = cssDefaultStyles, allowedFormats = formatMembers) {
let str = ''
if (allowedFormats.some(member => this[member])) {
const cssProps = allowedFormats.reduce((acc, cur) => this[cur]
@ -417,7 +412,7 @@ function loader (registryOrVersion) {
const params = []
if (this.with) {
for (const param of this.with) {
params.push(param.toHTML(lang, styles, allowedFormats, _depth + 1))
params.push(param.toHTML(lang, styles, allowedFormats))
}
}
const format = getValueSafely(lang, this.translate, this.translate)
@ -425,13 +420,10 @@ function loader (registryOrVersion) {
}
if (this.extra) {
str += this.extra.map(entry => entry.toHTML(lang, styles, allowedFormats, _depth + 1)).join('')
str += this.extra.map(entry => entry.toHTML(lang, styles, allowedFormats)).join('')
}
str += '</span>'
// It's not safe to truncate HTML so just return unformatted text
return str > MAX_CHAT_LENGTH
? escapeHtml(this.toString())
: str
return str
}
static fromNotch (msg) {