Skip to content

Commit

Permalink
fix: don't send chat message twice on 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Nov 5, 2024
1 parent a932afb commit 1f02a6d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/blockRenames.ts
Original file line number Diff line number Diff line change
@@ -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 =>
// [
Expand Down
23 changes: 14 additions & 9 deletions src/lib/modules/chat.ts
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) => {
Expand All @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`
}

0 comments on commit 1f02a6d

Please sign in to comment.