diff --git a/plugins/player.js b/plugins/player.js index d1f27a8..ae130ec 100644 --- a/plugins/player.js +++ b/plugins/player.js @@ -1,5 +1,3 @@ -const parse = require('../util/chatparse.js') -const parse1204 = require('../util/chatparse_1204.js') module.exports = { load: (b) => { b.players = {} diff --git a/util/ConsoleCommand.js b/util/ConsoleCommand.js index 0091eb4..a7d3b28 100644 --- a/util/ConsoleCommand.js +++ b/util/ConsoleCommand.js @@ -1,14 +1,14 @@ // HOW TO WRITE CLASS JS const index = require('../index.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 {getMessage} = require('../util/lang.js') const lang = settings.defaultLang class ConsoleCommand { constructor (cmd, index2) { 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.username = 'Owner' this.nickname = 'Console' diff --git a/util/chatparse_console.js b/util/chatparse_console.js index c9a3584..fe0ad2e 100644 --- a/util/chatparse_console.js +++ b/util/chatparse_console.js @@ -1,9 +1,6 @@ -const _lang = require('minecraft-data')('1.20.2').language; const settings = require('../settings.json'); -const lang = Object.create(null) // Without constructor function -for (const i in _lang) { - lang[i] = _lang[i] -} +const lang = require('./mc_lang.js'); + const _consoleColors = require("./consolecolors.json"); let consoleColors; let consoleColors24; @@ -14,6 +11,7 @@ if(_consoleColors[settings.terminalMode]){ 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 ""; @@ -29,6 +27,7 @@ const hexColorParser = (color) => { } return out + `38;2;${redChannel};${greenChannel};${blueChannel}m` } + const processColor = (col, rcol) => { let out; if (col === 'reset') { @@ -40,6 +39,7 @@ const processColor = (col, rcol) => { } return out } + const parse = function (_data, l = 0, resetColor = consoleColors.reset) { if (l >= 12) { return ['', '', ''] @@ -55,18 +55,15 @@ const parse = function (_data, l = 0, resetColor = consoleColors.reset) { data = _data } let nkt = false - const out = { - console: "", // Console formatting using ANSI escape codes for colors - plain: "" // Plain formatting with no colors - } + let out = "" if (data['']) { data.text = data[''] nkt = true } if (data.color) { - out.console += processColor(data.color, resetColor) + out += processColor(data.color, resetColor) } else { - out.console += resetColor + out += resetColor } if (data.text) { let _text = data.text @@ -74,36 +71,29 @@ const parse = function (_data, l = 0, resetColor = consoleColors.reset) { _text = _text.toString() } if (nkt) { - out.console += resetColor + out += resetColor } - out.console += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format - out.plain += _text + out += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format } 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') if (lang[trans] !== undefined) { trans = lang[trans].replace(/%%/g, '\ue123') - trans2 = lang[trans2].replace(/%%/g, '\ue123') } for (const i in data.with) { 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')) - trans2 = trans2.replace(/%s/, j2.plain.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')) + trans = trans.replace(/%s/, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) + trans = trans.replaceAll(`%${+i + 1}$s`, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) } - out.console += 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') + out += trans.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s') } if (data.extra) { for (const i in data.extra) { const parsed = parse(data.extra[i], l, data.color ? processColor(data.color, resetColor) : resetColor) - out.console += parsed.console - out.plain += parsed.plain + out += parsed } } - out.console += resetColor + out += resetColor return out } const parse2 = function (_data, l, resetColor) { @@ -111,10 +101,7 @@ const parse2 = function (_data, l, resetColor) { return parse(_data) } catch (e) { console.error(e) - return { - 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) - } + 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) } } module.exports = parse2 diff --git a/util/chatparse_plain.js b/util/chatparse_plain.js index c9a3584..c8523ec 100644 --- a/util/chatparse_plain.js +++ b/util/chatparse_plain.js @@ -1,120 +1,59 @@ -const _lang = require('minecraft-data')('1.20.2').language; -const settings = require('../settings.json'); -const lang = Object.create(null) // Without constructor function -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) { +const lang = require('./mc_lang.js'); + +const parse = function (_data, l = 0) { if (l >= 12) { return ['', '', ''] } let data if (typeof _data === 'string') { - data = { text: _data, color: 'reset' } + data = { text: _data } } else if (typeof _data === 'number') { - data = { text: _data + '', color: 'reset' } + data = { text: _data + '' } } else if (_data.constructor === Array) { - data = { extra: _data, color: 'reset' } + data = { extra: _data } } else { data = _data } let nkt = false - const out = { - console: "", // Console formatting using ANSI escape codes for colors - plain: "" // Plain formatting with no colors - } + const out = "" if (data['']) { data.text = data[''] nkt = true } - if (data.color) { - out.console += processColor(data.color, resetColor) - } else { - out.console += resetColor - } if (data.text) { let _text = data.text if (typeof _text === 'number') { _text = _text.toString() } - if (nkt) { - out.console += resetColor - } - out.console += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format - out.plain += _text + out += _text } 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') if (lang[trans] !== undefined) { trans = lang[trans].replace(/%%/g, '\ue123') trans2 = lang[trans2].replace(/%%/g, '\ue123') } for (const i in data.with) { - 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')) - trans2 = trans2.replace(/%s/, j2.plain.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')) + const j2 = parse(data.with[i], l + 1) + trans2 = trans2.replace(/%s/, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) + trans2 = trans2.replaceAll(`%${+i + 1}$s`, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125')) } - out.console += 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') + out += trans2.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s') } if (data.extra) { for (const i in data.extra) { - const parsed = parse(data.extra[i], l, data.color ? processColor(data.color, resetColor) : resetColor) - out.console += parsed.console - out.plain += parsed.plain + const parsed = parse(data.extra[i], l) + out += parsed } } - out.console += resetColor return out } -const parse2 = function (_data, l, resetColor) { +const parse2 = function (_data, l) { try { return parse(_data) } catch (e) { console.error(e) - return { - 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) - } + return 'An error occured while parsing a message. See console for more information. JSON that caused the error: ' + JSON.stringify(_data) } } module.exports = parse2 diff --git a/util/mc_lang.js b/util/mc_lang.js new file mode 100644 index 0000000..67f656d --- /dev/null +++ b/util/mc_lang.js @@ -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 \ No newline at end of file