Skip to content

Commit f519614

Browse files
committed
Fix join messages and /spawn on join on 1.19+.
1 parent b530855 commit f519614

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

src/minecraft/packets/chat.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { concatPacketData, PacketDataTypes } from '../packet'
2+
import { protocolMap, writeVarInt } from '../utils'
3+
4+
export const makeChatMessagePacket = (
5+
msg: string,
6+
protocolVersion: number,
7+
msgSalt?: Buffer
8+
): [number, Buffer] => {
9+
const is119 = protocolVersion >= protocolMap[1.19]
10+
const is1191 = protocolVersion >= protocolMap['1.19.1']
11+
if (!is119) {
12+
return [0x03, concatPacketData([msg])]
13+
} else {
14+
const id = msg.startsWith('/')
15+
? is1191
16+
? 0x04
17+
: 0x03
18+
: is1191
19+
? 0x05
20+
: 0x04
21+
const timestamp = Buffer.alloc(8)
22+
timestamp.writeIntBE(Date.now(), 2, 6) // writeBigInt64BE(BigInt(Date.now()))
23+
const salt = msgSalt ?? Buffer.alloc(8)
24+
// TODO-1.19: Send signature(s), preview chat, last seen messages and last received message if possible.
25+
const data: PacketDataTypes[] = [
26+
msg.startsWith('/') ? msg.substring(1) : msg,
27+
timestamp,
28+
salt,
29+
writeVarInt(0),
30+
false
31+
]
32+
if (is1191) data.push(writeVarInt(0), writeVarInt(0))
33+
return [id, concatPacketData(data)]
34+
}
35+
}

src/screens/chat/ChatScreen.tsx

+5-34
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ import {
4141
ClickEvent,
4242
ColorMap
4343
} from '../../minecraft/chatToJsx'
44-
import { protocolMap, writeVarInt } from '../../minecraft/utils'
45-
import { concatPacketData, PacketDataTypes } from '../../minecraft/packet'
44+
import { makeChatMessagePacket } from '../../minecraft/packets/chat'
4645
import TextField from '../../components/TextField'
4746
import Text from '../../components/Text'
4847

@@ -254,38 +253,10 @@ const ChatScreen = ({ navigation, route }: Props) => {
254253
if (msg.startsWith('/') && saveHistory) {
255254
setCommandHistory(ch => ch.concat([msg]))
256255
}
257-
const is119 =
258-
connection.connection.options.protocolVersion >= protocolMap[1.19]
259-
const is1191 =
260-
connection.connection.options.protocolVersion >= protocolMap['1.19.1']
261-
if (!is119) {
262-
connection.connection
263-
.writePacket(0x03, concatPacketData([msg]))
264-
.catch(handleError(addMessage, sendMessageError))
265-
} else {
266-
const id = msg.startsWith('/')
267-
? is1191
268-
? 0x04
269-
: 0x03
270-
: is1191
271-
? 0x05
272-
: 0x04
273-
const timestamp = Buffer.alloc(8)
274-
timestamp.writeIntBE(Date.now(), 2, 6) // writeBigInt64BE(BigInt(Date.now()))
275-
const salt = connection.connection.msgSalt ?? Buffer.alloc(8)
276-
// TODO-1.19: Send signature(s), preview chat, last seen messages and last received message if possible.
277-
const data: PacketDataTypes[] = [
278-
msg.startsWith('/') ? msg.substring(1) : msg,
279-
timestamp,
280-
salt,
281-
writeVarInt(0),
282-
false
283-
]
284-
if (is1191) data.push(writeVarInt(0), writeVarInt(0))
285-
connection.connection
286-
.writePacket(id, concatPacketData(data))
287-
.catch(handleError(addMessage, sendMessageError))
288-
}
256+
const protocolVersion = connection.connection.options.protocolVersion
257+
connection.connection
258+
.writePacket(...makeChatMessagePacket(msg, protocolVersion))
259+
.catch(handleError(addMessage, sendMessageError))
289260
}
290261

291262
const handleClickEvent = useCallback(

src/screens/chat/packetHandler.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import React from 'react'
22
import { Status } from './ChatScreen'
33
import { MinecraftChat, parseValidJson } from '../../minecraft/chatToJsx'
44
import { ServerConnection } from '../../minecraft/connection'
5-
import { concatPacketData, Packet } from '../../minecraft/packet'
5+
import { Packet } from '../../minecraft/packet'
66
import { protocolMap, readVarInt, writeVarInt } from '../../minecraft/utils'
7+
import { makeChatMessagePacket } from '../../minecraft/packets/chat'
78

89
export const enderChatPrefix = '\u00A74[\u00A7cEnderChat\u00A74] \u00A7c'
910
export const parseMessageError = 'An error occurred when parsing chat.'
@@ -94,13 +95,15 @@ export const packetHandler =
9495
statusRef.current = 'CONNECTED'
9596
const joinMessageToSend = joinMessage.substring(0, charLimit).trim()
9697
if (sendJoinMessage && joinMessageToSend) {
98+
const protocolVer = connection.options.protocolVersion
9799
connection
98-
.writePacket(0x03, concatPacketData([joinMessageToSend]))
100+
.writePacket(...makeChatMessagePacket(joinMessageToSend, protocolVer))
99101
.catch(handleError(addMessage, sendMessageError))
100102
}
101103
if (sendSpawnCommand) {
104+
const protocolVer = connection.options.protocolVersion
102105
connection
103-
.writePacket(0x03, concatPacketData(['/spawn']))
106+
.writePacket(...makeChatMessagePacket('/spawn', protocolVer))
104107
.catch(handleError(addMessage, sendMessageError))
105108
}
106109
}

0 commit comments

Comments
 (0)