@@ -15,6 +15,35 @@ export const sendMessageError = 'Failed to send message to server!'
15
15
export const healthMessage =
16
16
enderChatPrefix + "You're losing health! \u00A7b%prev \u00A7f-> \u00A7c%new"
17
17
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
+
18
47
export const packetHandler =
19
48
(
20
49
healthRef : React . MutableRefObject < number | null > ,
@@ -25,10 +54,7 @@ export const packetHandler =
25
54
joinMessage : string ,
26
55
sendJoinMessage : boolean ,
27
56
sendSpawnCommand : boolean ,
28
- handleError : (
29
- addMsg : ( text : MinecraftChat ) => void ,
30
- translated : string
31
- ) => ( error : unknown ) => any ,
57
+ handleError : HandleError ,
32
58
charLimit : number
33
59
) =>
34
60
( packet : Packet ) => {
@@ -58,19 +84,13 @@ export const packetHandler =
58
84
( packet . id === 0x0e && ! is117 ) ||
59
85
( packet . id === 0x0f && is117 && ! is119 )
60
86
) {
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 )
74
94
} else if (
75
95
/* Player Chat Message (clientbound) */
76
96
( packet . id === 0x30 && is119 && ! is1191 ) ||
@@ -120,28 +140,6 @@ export const packetHandler =
120
140
} catch ( e ) {
121
141
handleError ( addMessage , parseMessageError ) ( e )
122
142
}
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
- }
145
143
} else if (
146
144
/* Open Window */
147
145
( packet . id === 0x2e && ! is119 ) ||
@@ -157,20 +155,30 @@ export const packetHandler =
157
155
. catch ( handleError ( addMessage , inventoryCloseError ) )
158
156
} else if (
159
157
/* Death Combat Event */
160
- ( packet . id === 0x32 && ! is117 ) ||
158
+ ( packet . id === 0x31 && ! is117 ) ||
161
159
( packet . id === 0x35 && is117 && ! is119 ) ||
162
160
( packet . id === 0x33 && is119 && ! is1191 ) ||
163
161
( packet . id === 0x36 && is1191 )
164
162
) {
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 )
169
171
const deathMessage = parseValidJson (
170
- packet . data . slice ( jsonOffset , jsonOffset + chatLen ) . toString ( 'utf8' )
172
+ data . slice ( chatViLength , chatViLength + chatLen ) . toString ( 'utf8' )
171
173
)
172
174
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
+ }
174
182
// Automatically respawn.
175
183
// LOW-TODO: Should this be manual, or a dialog, like MC?
176
184
connection // Client Status
0 commit comments