@@ -15,6 +15,35 @@ export const sendMessageError = 'Failed to send message to server!'
1515export const healthMessage =
1616 enderChatPrefix + "You're losing health! \u00A7b%prev \u00A7f-> \u00A7c%new"
1717
18+ type HandleError = (
19+ addMsg : ( text : MinecraftChat ) => void ,
20+ translated : string
21+ ) => ( error : unknown ) => any
22+
23+ const handleSystemMessage = (
24+ packet : Packet ,
25+ addMessage : ( text : MinecraftChat ) => void ,
26+ handleError : HandleError ,
27+ is1191 : boolean
28+ ) => {
29+ try {
30+ const [ chatLength , chatVarIntLength ] = readVarInt ( packet . data )
31+ const chatJson = packet . data
32+ . slice ( chatVarIntLength , chatVarIntLength + chatLength )
33+ . toString ( 'utf8' )
34+ // TODO: Support position 2 (action bar), true (action bar) and sender for disableChat/blocked players.
35+ // TODO-1.19: 3 say command, 4 msg command, 5 team msg command, 6 emote command, 7 tellraw command, also in Player Chat Message.
36+ const position = packet . data . readInt8 ( chatVarIntLength + chatLength )
37+ if ( ! is1191 && ( position === 0 || position === 1 ) ) {
38+ addMessage ( parseValidJson ( chatJson ) )
39+ } else if ( is1191 && ! position ) {
40+ addMessage ( parseValidJson ( chatJson ) )
41+ }
42+ } catch ( e ) {
43+ handleError ( addMessage , parseMessageError ) ( e )
44+ }
45+ }
46+
1847export const packetHandler =
1948 (
2049 healthRef : React . MutableRefObject < number | null > ,
@@ -25,10 +54,7 @@ export const packetHandler =
2554 joinMessage : string ,
2655 sendJoinMessage : boolean ,
2756 sendSpawnCommand : boolean ,
28- handleError : (
29- addMsg : ( text : MinecraftChat ) => void ,
30- translated : string
31- ) => ( error : unknown ) => any ,
57+ handleError : HandleError ,
3258 charLimit : number
3359 ) =>
3460 ( packet : Packet ) => {
@@ -58,19 +84,13 @@ export const packetHandler =
5884 ( packet . id === 0x0e && ! is117 ) ||
5985 ( packet . id === 0x0f && is117 && ! is119 )
6086 ) {
61- try {
62- const [ chatLength , chatVarIntLength ] = readVarInt ( packet . data )
63- const chatJson = packet . data
64- . slice ( chatVarIntLength , chatVarIntLength + chatLength )
65- . toString ( 'utf8' )
66- const position = packet . data . readInt8 ( chatVarIntLength + chatLength )
67- // TODO: Support position 2 (also in 0x5f 1.19 packet) and sender for disableChat/blocked players.
68- if ( position === 0 || position === 1 ) {
69- addMessage ( parseValidJson ( chatJson ) )
70- }
71- } catch ( e ) {
72- handleError ( addMessage , parseMessageError ) ( e )
73- }
87+ handleSystemMessage ( packet , addMessage , handleError , is1191 )
88+ } else if (
89+ /* System Chat Message (clientbound) */
90+ ( packet . id === 0x5f && is119 && ! is1191 ) ||
91+ ( packet . id === 0x62 && is1191 )
92+ ) {
93+ handleSystemMessage ( packet , addMessage , handleError , is1191 )
7494 } else if (
7595 /* Player Chat Message (clientbound) */
7696 ( packet . id === 0x30 && is119 && ! is1191 ) ||
@@ -120,28 +140,6 @@ export const packetHandler =
120140 } catch ( e ) {
121141 handleError ( addMessage , parseMessageError ) ( e )
122142 }
123- } else if (
124- /* System Chat Message (clientbound) */
125- ( packet . id === 0x5f && is119 && ! is1191 ) ||
126- ( packet . id === 0x62 && is1191 )
127- ) {
128- try {
129- const [ chatLength , chatVarIntLength ] = readVarInt ( packet . data )
130- const chatJson = packet . data
131- . slice ( chatVarIntLength , chatVarIntLength + chatLength )
132- . toString ( 'utf8' )
133- // As of 1.19.1, this is a boolean which says if this is an action bar or not.
134- const position = packet . data . readInt8 ( chatVarIntLength + chatLength )
135- // TODO-1.19 - 3: say command, 4: msg command, 5: team msg command, 6: emote command, 7: tellraw command
136- // Also in Player Chat Message.
137- if ( is119 && ( position === 0 || position === 1 ) ) {
138- addMessage ( parseValidJson ( chatJson ) )
139- } else if ( is1191 && ! position ) {
140- addMessage ( parseValidJson ( chatJson ) )
141- }
142- } catch ( e ) {
143- handleError ( addMessage , parseMessageError ) ( e )
144- }
145143 } else if (
146144 /* Open Window */
147145 ( packet . id === 0x2e && ! is119 ) ||
@@ -157,20 +155,30 @@ export const packetHandler =
157155 . catch ( handleError ( addMessage , inventoryCloseError ) )
158156 } else if (
159157 /* Death Combat Event */
160- ( packet . id === 0x32 && ! is117 ) ||
158+ ( packet . id === 0x31 && ! is117 ) ||
161159 ( packet . id === 0x35 && is117 && ! is119 ) ||
162160 ( packet . id === 0x33 && is119 && ! is1191 ) ||
163161 ( packet . id === 0x36 && is1191 )
164162 ) {
165- const [ , playerIdLen ] = readVarInt ( packet . data )
166- const offset = playerIdLen + 4 // Entity ID
167- const [ chatLen , chatVarIntLength ] = readVarInt ( packet . data , offset )
168- const jsonOffset = offset + chatVarIntLength
163+ let data = packet . data
164+ if ( ! is117 ) {
165+ const [ action , actionLen ] = readVarInt ( data )
166+ if ( action !== 2 ) return
167+ data = data . slice ( actionLen )
168+ }
169+ data = data . slice ( readVarInt ( data ) [ 1 ] + 4 ) // Remove Player/Entity ID
170+ const [ chatLen , chatViLength ] = readVarInt ( data )
169171 const deathMessage = parseValidJson (
170- packet . data . slice ( jsonOffset , jsonOffset + chatLen ) . toString ( 'utf8' )
172+ data . slice ( chatViLength , chatViLength + chatLen ) . toString ( 'utf8' )
171173 )
172174 addMessage ( deathRespawnMessage )
173- if ( deathMessage ?. text || deathMessage ?. extra ) addMessage ( deathMessage )
175+ if (
176+ ( typeof deathMessage === 'string' && deathMessage . trim ( ) ) ||
177+ deathMessage ?. text ||
178+ deathMessage ?. extra ?. length > 0
179+ ) {
180+ addMessage ( deathMessage )
181+ }
174182 // Automatically respawn.
175183 // LOW-TODO: Should this be manual, or a dialog, like MC?
176184 connection // Client Status
0 commit comments