Start chat parser rewrite

This commit is contained in:
7cc5c4f330d47060 2024-08-01 20:11:59 -04:00
parent eb89f9e657
commit f85a34481b
5 changed files with 40 additions and 110 deletions

View file

@ -1,5 +1,3 @@
const parse = require('../util/chatparse.js')
const parse1204 = require('../util/chatparse_1204.js')
module.exports = { module.exports = {
load: (b) => { load: (b) => {
b.players = {} b.players = {}

View file

@ -1,14 +1,14 @@
// HOW TO WRITE CLASS JS // HOW TO WRITE CLASS JS
const index = require('../index.js') const index = require('../index.js')
const { cmds } = require('../plugins/command.js') const { cmds } = require('../plugins/command.js')
const parse = require('../util/chatparse.js') const parse = require('../util/chatparse_console.js')
const settings = require('../settings.json') const settings = require('../settings.json')
const {getMessage} = require('../util/lang.js') const {getMessage} = require('../util/lang.js')
const lang = settings.defaultLang const lang = settings.defaultLang
class ConsoleCommand { class ConsoleCommand {
constructor (cmd, index2) { constructor (cmd, index2) {
this.send = () => {} // not needed for console this.send = () => {} // not needed for console
this.reply = text => process.stdout.write(parse(text).console + '\n') this.reply = text => process.stdout.write(parse(text) + '\n')
this.uuid = 'dde5a2a6-ebdd-4bbb-8eac-f75b10c10446_console' // hard-coded because uuid does not exist at console this.uuid = 'dde5a2a6-ebdd-4bbb-8eac-f75b10c10446_console' // hard-coded because uuid does not exist at console
this.username = 'Owner' this.username = 'Owner'
this.nickname = 'Console' this.nickname = 'Console'

View file

@ -1,9 +1,6 @@
const _lang = require('minecraft-data')('1.20.2').language;
const settings = require('../settings.json'); const settings = require('../settings.json');
const lang = Object.create(null) // Without constructor function const lang = require('./mc_lang.js');
for (const i in _lang) {
lang[i] = _lang[i]
}
const _consoleColors = require("./consolecolors.json"); const _consoleColors = require("./consolecolors.json");
let consoleColors; let consoleColors;
let consoleColors24; let consoleColors24;
@ -14,6 +11,7 @@ if(_consoleColors[settings.terminalMode]){
consoleColors=_consoleColors.none.fourBit consoleColors=_consoleColors.none.fourBit
consoleColors24=_consoleColors.none.twentyFourBit consoleColors24=_consoleColors.none.twentyFourBit
} }
const hexColorParser = (color) => { const hexColorParser = (color) => {
if(!consoleColors24.enabled || consoleColors24.bit!==24){ //Non-24bit hex color parsing is not implemented yet if(!consoleColors24.enabled || consoleColors24.bit!==24){ //Non-24bit hex color parsing is not implemented yet
return ""; return "";
@ -29,6 +27,7 @@ const hexColorParser = (color) => {
} }
return out + `38;2;${redChannel};${greenChannel};${blueChannel}m` return out + `38;2;${redChannel};${greenChannel};${blueChannel}m`
} }
const processColor = (col, rcol) => { const processColor = (col, rcol) => {
let out; let out;
if (col === 'reset') { if (col === 'reset') {
@ -40,6 +39,7 @@ const processColor = (col, rcol) => {
} }
return out return out
} }
const parse = function (_data, l = 0, resetColor = consoleColors.reset) { const parse = function (_data, l = 0, resetColor = consoleColors.reset) {
if (l >= 12) { if (l >= 12) {
return ['', '', ''] return ['', '', '']
@ -55,18 +55,15 @@ const parse = function (_data, l = 0, resetColor = consoleColors.reset) {
data = _data data = _data
} }
let nkt = false let nkt = false
const out = { let out = ""
console: "", // Console formatting using ANSI escape codes for colors
plain: "" // Plain formatting with no colors
}
if (data['']) { if (data['']) {
data.text = data[''] data.text = data['']
nkt = true nkt = true
} }
if (data.color) { if (data.color) {
out.console += processColor(data.color, resetColor) out += processColor(data.color, resetColor)
} else { } else {
out.console += resetColor out += resetColor
} }
if (data.text) { if (data.text) {
let _text = data.text let _text = data.text
@ -74,36 +71,29 @@ const parse = function (_data, l = 0, resetColor = consoleColors.reset) {
_text = _text.toString() _text = _text.toString()
} }
if (nkt) { if (nkt) {
out.console += resetColor out += resetColor
} }
out.console += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format out += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format
out.plain += _text
} }
if (data.translate) { if (data.translate) {
let trans = data.translate.replace(/%%/g, '\ue123').replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes from console format let trans = data.translate.replace(/%%/g, '\ue123').replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes from console format
let trans2 = data.translate.replace(/%%/g, '\ue123')
if (lang[trans] !== undefined) { if (lang[trans] !== undefined) {
trans = lang[trans].replace(/%%/g, '\ue123') trans = lang[trans].replace(/%%/g, '\ue123')
trans2 = lang[trans2].replace(/%%/g, '\ue123')
} }
for (const i in data.with) { for (const i in data.with) {
const j2 = parse(data.with[i], l + 1, data.color ? processColor(data.color, resetColor) : resetColor) const j2 = parse(data.with[i], l + 1, data.color ? processColor(data.color, resetColor) : resetColor)
trans = trans.replace(/%s/, j2.console.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) trans = trans.replace(/%s/, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%s/, j2.plain.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) trans = trans.replaceAll(`%${+i + 1}$s`, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans = trans.replaceAll(`%${+i + 1}$s`, j2.console.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replaceAll(`%${+i + 1}$s`, j2.plain.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
} }
out.console += trans.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s') out += trans.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
out.plain += trans2.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
} }
if (data.extra) { if (data.extra) {
for (const i in data.extra) { for (const i in data.extra) {
const parsed = parse(data.extra[i], l, data.color ? processColor(data.color, resetColor) : resetColor) const parsed = parse(data.extra[i], l, data.color ? processColor(data.color, resetColor) : resetColor)
out.console += parsed.console out += parsed
out.plain += parsed.plain
} }
} }
out.console += resetColor out += resetColor
return out return out
} }
const parse2 = function (_data, l, resetColor) { const parse2 = function (_data, l, resetColor) {
@ -111,10 +101,7 @@ const parse2 = function (_data, l, resetColor) {
return parse(_data) return parse(_data)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
return { return '\x1B[0m\x1B[38;2;255;85;85mAn error occured while parsing a message. See console for more information.\nJSON that caused the error: ' + JSON.stringify(_data)
console: '\x1B[0m\x1B[38;2;255;85;85mAn error occured while parsing a message. See console for more information.\nJSON that caused the error: ' + JSON.stringify(_data),
plain: 'An error occured while parsing a message. See console for more information. JSON that caused the error: ' + JSON.stringify(_data)
}
} }
} }
module.exports = parse2 module.exports = parse2

View file

@ -1,120 +1,59 @@
const _lang = require('minecraft-data')('1.20.2').language; const lang = require('./mc_lang.js');
const settings = require('../settings.json');
const lang = Object.create(null) // Without constructor function const parse = function (_data, l = 0) {
for (const i in _lang) {
lang[i] = _lang[i]
}
const _consoleColors = require("./consolecolors.json");
let consoleColors;
let consoleColors24;
if(_consoleColors[settings.terminalMode]){
consoleColors=_consoleColors[settings.terminalMode].fourBit
consoleColors24=_consoleColors[settings.terminalMode].twentyFourBit
} else {
consoleColors=_consoleColors.none.fourBit
consoleColors24=_consoleColors.none.twentyFourBit
}
const hexColorParser = (color) => {
if(!consoleColors24.enabled || consoleColors24.bit!==24){ //Non-24bit hex color parsing is not implemented yet
return "";
}
let out = '\x1B[0;'
const redChannel = Number('0x' + color.slice(1, 3))
const greenChannel = Number('0x' + color.slice(3, 5))
const blueChannel = Number('0x' + color.slice(5, 7))
if (!consoleColors24.lightMode && redChannel < 64 && greenChannel < 64 && blueChannel < 64) {
out += '48;2;220;220;220;'
} else if (consoleColors24.lightMode && ((redChannel > 192 && greenChannel > 192 && blueChannel > 192) || greenChannel > 160)) {
out += '48;2;0;0;0;'
}
return out + `38;2;${redChannel};${greenChannel};${blueChannel}m`
}
const processColor = (col, rcol) => {
let out;
if (col === 'reset') {
out = rcol
} else if (col.startsWith('#')) {
out = hexColorParser(col)
} else {
out = consoleColors[col]
}
return out
}
const parse = function (_data, l = 0, resetColor = consoleColors.reset) {
if (l >= 12) { if (l >= 12) {
return ['', '', ''] return ['', '', '']
} }
let data let data
if (typeof _data === 'string') { if (typeof _data === 'string') {
data = { text: _data, color: 'reset' } data = { text: _data }
} else if (typeof _data === 'number') { } else if (typeof _data === 'number') {
data = { text: _data + '', color: 'reset' } data = { text: _data + '' }
} else if (_data.constructor === Array) { } else if (_data.constructor === Array) {
data = { extra: _data, color: 'reset' } data = { extra: _data }
} else { } else {
data = _data data = _data
} }
let nkt = false let nkt = false
const out = { const out = ""
console: "", // Console formatting using ANSI escape codes for colors
plain: "" // Plain formatting with no colors
}
if (data['']) { if (data['']) {
data.text = data[''] data.text = data['']
nkt = true nkt = true
} }
if (data.color) {
out.console += processColor(data.color, resetColor)
} else {
out.console += resetColor
}
if (data.text) { if (data.text) {
let _text = data.text let _text = data.text
if (typeof _text === 'number') { if (typeof _text === 'number') {
_text = _text.toString() _text = _text.toString()
} }
if (nkt) { out += _text
out.console += resetColor
}
out.console += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format
out.plain += _text
} }
if (data.translate) { if (data.translate) {
let trans = data.translate.replace(/%%/g, '\ue123').replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes from console format
let trans2 = data.translate.replace(/%%/g, '\ue123') let trans2 = data.translate.replace(/%%/g, '\ue123')
if (lang[trans] !== undefined) { if (lang[trans] !== undefined) {
trans = lang[trans].replace(/%%/g, '\ue123') trans = lang[trans].replace(/%%/g, '\ue123')
trans2 = lang[trans2].replace(/%%/g, '\ue123') trans2 = lang[trans2].replace(/%%/g, '\ue123')
} }
for (const i in data.with) { for (const i in data.with) {
const j2 = parse(data.with[i], l + 1, data.color ? processColor(data.color, resetColor) : resetColor) const j2 = parse(data.with[i], l + 1)
trans = trans.replace(/%s/, j2.console.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) trans2 = trans2.replace(/%s/, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%s/, j2.plain.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) trans2 = trans2.replaceAll(`%${+i + 1}$s`, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans = trans.replaceAll(`%${+i + 1}$s`, j2.console.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replaceAll(`%${+i + 1}$s`, j2.plain.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
} }
out.console += trans.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s') out += trans2.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
out.plain += trans2.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
} }
if (data.extra) { if (data.extra) {
for (const i in data.extra) { for (const i in data.extra) {
const parsed = parse(data.extra[i], l, data.color ? processColor(data.color, resetColor) : resetColor) const parsed = parse(data.extra[i], l)
out.console += parsed.console out += parsed
out.plain += parsed.plain
} }
} }
out.console += resetColor
return out return out
} }
const parse2 = function (_data, l, resetColor) { const parse2 = function (_data, l) {
try { try {
return parse(_data) return parse(_data)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
return { return 'An error occured while parsing a message. See console for more information. JSON that caused the error: ' + JSON.stringify(_data)
console: '\x1B[0m\x1B[38;2;255;85;85mAn error occured while parsing a message. See console for more information.\nJSON that caused the error: ' + JSON.stringify(_data),
plain: 'An error occured while parsing a message. See console for more information. JSON that caused the error: ' + JSON.stringify(_data)
}
} }
} }
module.exports = parse2 module.exports = parse2

6
util/mc_lang.js Normal file
View file

@ -0,0 +1,6 @@
const _lang = require('minecraft-data')('1.20.2').language;
const lang = Object.create(null) // Without constructor function
for (const i in _lang) {
lang[i] = _lang[i]
}
module.exports=lang