Skip to content

Commit da0046f

Browse files
committed
Make the TextPacket dummy strings hack a bit less shitty
1 parent 4110fb5 commit da0046f

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

src/TextPacket.php

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ class TextPacket extends DataPacket implements ClientboundPacket, ServerboundPac
2828
private const CATEGORY_AUTHORED_MESSAGE = 1;
2929
private const CATEGORY_MESSAGE_WITH_PARAMETERS = 2;
3030

31+
private const CATEGORY_DUMMY_STRINGS = [
32+
self::CATEGORY_MESSAGE_ONLY => [
33+
'raw',
34+
'tip',
35+
'systemMessage',
36+
'textObjectWhisper',
37+
'textObjectAnnouncement',
38+
'textObject'
39+
],
40+
self::CATEGORY_AUTHORED_MESSAGE => [
41+
'chat',
42+
'whisper',
43+
'announcement'
44+
],
45+
self::CATEGORY_MESSAGE_WITH_PARAMETERS => [
46+
'translate',
47+
'popup',
48+
'jukeboxPopup',
49+
]
50+
];
51+
3152
public const TYPE_RAW = 0;
3253
public const TYPE_CHAT = 1;
3354
public const TYPE_TRANSLATION = 2;
@@ -107,25 +128,12 @@ protected function decodePayload(ByteBufferReader $in) : void{
107128
$this->needsTranslation = CommonTypes::getBool($in);
108129

109130
$category = Byte::readUnsigned($in);
110-
switch($category){
111-
case self::CATEGORY_MESSAGE_ONLY:
112-
$this->assertString(CommonTypes::getString($in), 'raw');
113-
$this->assertString(CommonTypes::getString($in), 'tip');
114-
$this->assertString(CommonTypes::getString($in), 'systemMessage');
115-
$this->assertString(CommonTypes::getString($in), 'textObjectWhisper');
116-
$this->assertString(CommonTypes::getString($in), 'textObjectAnnouncement');
117-
$this->assertString(CommonTypes::getString($in), 'textObject');
118-
break;
119-
case self::CATEGORY_AUTHORED_MESSAGE:
120-
$this->assertString(CommonTypes::getString($in), 'chat');
121-
$this->assertString(CommonTypes::getString($in), 'whisper');
122-
$this->assertString(CommonTypes::getString($in), 'announcement');
123-
break;
124-
case self::CATEGORY_MESSAGE_WITH_PARAMETERS:
125-
$this->assertString(CommonTypes::getString($in), 'translate');
126-
$this->assertString(CommonTypes::getString($in), 'popup');
127-
$this->assertString(CommonTypes::getString($in), 'jukeboxPopup');
128-
break;
131+
$expectedDummyStrings = self::CATEGORY_DUMMY_STRINGS[$category] ?? throw new PacketDecodeException("Unknown category ID $category");
132+
foreach($expectedDummyStrings as $k => $expectedDummyString){
133+
$actual = CommonTypes::getString($in);
134+
if($expectedDummyString !== $actual){
135+
throw new PacketDecodeException("Dummy string mismatch for category $category at position $k: expected $expectedDummyString, got $actual");
136+
}
129137
}
130138

131139
$this->type = Byte::readUnsigned($in);
@@ -173,37 +181,27 @@ protected function decodePayload(ByteBufferReader $in) : void{
173181
protected function encodePayload(ByteBufferWriter $out) : void{
174182
CommonTypes::putBool($out, $this->needsTranslation);
175183

176-
if(match($this->type){
184+
$category = match ($this->type) {
177185
self::TYPE_RAW,
178186
self::TYPE_TIP,
179187
self::TYPE_SYSTEM,
180188
self::TYPE_JSON_WHISPER,
181189
self::TYPE_JSON_ANNOUNCEMENT,
182-
self::TYPE_JSON => true,
183-
default => false,
184-
}){
185-
Byte::writeUnsigned($out, self::CATEGORY_MESSAGE_ONLY);
186-
CommonTypes::putString($out, 'raw');
187-
CommonTypes::putString($out, 'tip');
188-
CommonTypes::putString($out, 'systemMessage');
189-
CommonTypes::putString($out, 'textObjectWhisper');
190-
CommonTypes::putString($out, 'textObjectAnnouncement');
191-
CommonTypes::putString($out, 'textObject');
192-
}elseif(match($this->type){
190+
self::TYPE_JSON => self::CATEGORY_MESSAGE_ONLY,
191+
193192
self::TYPE_CHAT,
194193
self::TYPE_WHISPER,
195-
self::TYPE_ANNOUNCEMENT => true,
196-
default => false,
197-
}){
198-
Byte::writeUnsigned($out, self::CATEGORY_AUTHORED_MESSAGE);
199-
CommonTypes::putString($out, 'chat');
200-
CommonTypes::putString($out, 'whisper');
201-
CommonTypes::putString($out, 'announcement');
202-
}else{
203-
Byte::writeUnsigned($out, self::CATEGORY_MESSAGE_WITH_PARAMETERS);
204-
CommonTypes::putString($out, 'translate');
205-
CommonTypes::putString($out, 'popup');
206-
CommonTypes::putString($out, 'jukeboxPopup');
194+
self::TYPE_ANNOUNCEMENT => self::CATEGORY_AUTHORED_MESSAGE,
195+
196+
self::TYPE_TRANSLATION,
197+
self::TYPE_POPUP,
198+
self::TYPE_JUKEBOX_POPUP => self::CATEGORY_MESSAGE_WITH_PARAMETERS,
199+
200+
default => throw new \LogicException("Invalid TextPacket type: $this->type")
201+
};
202+
Byte::writeUnsigned($out, $category);
203+
foreach(self::CATEGORY_DUMMY_STRINGS[$category] as $dummyString){
204+
CommonTypes::putString($out, $dummyString);
207205
}
208206

209207
Byte::writeUnsigned($out, $this->type);

0 commit comments

Comments
 (0)