diff --git a/src/blockRenames.ts b/src/blockRenames.ts index d153229a..2dd49621 100644 --- a/src/blockRenames.ts +++ b/src/blockRenames.ts @@ -1,13 +1,9 @@ import itemBlockRenames from './itemBlockRenames.json' +import { versionToNumber } from './utils' // postflatenning // todo regen 1.13 the flatenning data -const versionToNumber = (ver: string) => { - const [x, y = '0', z = '0'] = ver.split('.') - return +`${x.padStart(2, '0')}${(parseInt(y).toString().padStart(2, '0'))}${parseInt(z).toString().padStart(2, '0')}` -} - // const allRenamesMapFromLatest = Object.fromEntries( // ['blocks', 'items'].map(x => // [ diff --git a/src/lib/modules/chat.ts b/src/lib/modules/chat.ts index 943dece5..69ad009a 100644 --- a/src/lib/modules/chat.ts +++ b/src/lib/modules/chat.ts @@ -1,3 +1,5 @@ +import { versionToNumber } from '../../utils' + export const server = function (serv: Server) { serv.broadcast = (message, { whitelist = serv.players, blacklist = [], system = false }: any = {}) => { if (whitelist.type === 'player') whitelist = [whitelist] @@ -127,7 +129,7 @@ export const server = function (serv: Server) { } } -export const player = function (player: Player, serv: Server) { +export const player = function (player: Player, serv: Server, settings: Options) { const chatHandler = ({ message }: { message: string }) => { if (message[0] === '/') { player.behavior('command', { command: message.slice(1) }, ({ command }) => player.handleCommand(command)) @@ -157,11 +159,17 @@ export const player = function (player: Player, serv: Server) { chatHandler({ message: `/${command}` }) }) - player.chat = message => { + const sendChat = (message, isSystem) => { if (typeof message === 'string') message = serv.parseClassic(message) - player._client.write('chat', { message: JSON.stringify(message), position: 0, sender: '0' }) - // 1.19+ - player._client.write('systemChat', { formattedMessage: JSON.stringify(message), position: 0, sender: '0' }) + if (versionToNumber(settings.version) >= versionToNumber('1.19')) { + player._client.write('systemChat', { message: JSON.stringify(message), position: isSystem ? 2 : 0, sender: '0' }) + } else { + player._client.write('chat', { message: JSON.stringify(message), position: isSystem ? 2 : 0, sender: '0' }) + } + } + + player.chat = message => { + sendChat(message, false) } player.emptyChat = (count = 1) => { @@ -171,10 +179,7 @@ export const player = function (player: Player, serv: Server) { } player.system = message => { - if (typeof message === 'string') message = serv.parseClassic(message) - player._client.write('chat', { message: JSON.stringify(message), position: 2, sender: '0' }) - // 1.19+ - player._client.write('systemChat', { formattedMessage: JSON.stringify(message), position: 2, sender: '0' }) + sendChat(message, true) } } declare global { diff --git a/src/utils/index.ts b/src/utils/index.ts index 45947129..aaa3e7c2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -13,7 +13,13 @@ export class ViewRect { this.z1 = cz + viewDistance } - contains(x, z) { + contains (x, z) { return this.x0 <= x && x <= this.x1 && this.z0 <= z && z <= this.z1 } } + + +export const versionToNumber = (ver: string) => { + const [x, y = '0', z = '0'] = ver.split('.') + return +`${x.padStart(2, '0')}${(parseInt(y).toString().padStart(2, '0'))}${parseInt(z).toString().padStart(2, '0')}` +}