PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
TextPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\VarInt;
22use function count;
23
25 public const NETWORK_ID = ProtocolInfo::TEXT_PACKET;
26
27 public const TYPE_RAW = 0;
28 public const TYPE_CHAT = 1;
29 public const TYPE_TRANSLATION = 2;
30 public const TYPE_POPUP = 3;
31 public const TYPE_JUKEBOX_POPUP = 4;
32 public const TYPE_TIP = 5;
33 public const TYPE_SYSTEM = 6;
34 public const TYPE_WHISPER = 7;
35 public const TYPE_ANNOUNCEMENT = 8;
36 public const TYPE_JSON_WHISPER = 9;
37 public const TYPE_JSON = 10;
38 public const TYPE_JSON_ANNOUNCEMENT = 11;
39
40 public int $type;
41 public bool $needsTranslation = false;
42 public string $sourceName;
43 public string $message;
45 public array $parameters = [];
46 public string $xboxUserId = "";
47 public string $platformChatId = "";
48 public string $filteredMessage = "";
49
50 private static function messageOnly(int $type, string $message) : self{
51 $result = new self;
52 $result->type = $type;
53 $result->message = $message;
54 return $result;
55 }
56
60 private static function baseTranslation(int $type, string $key, array $parameters) : self{
61 $result = new self;
62 $result->type = $type;
63 $result->needsTranslation = true;
64 $result->message = $key;
65 $result->parameters = $parameters;
66 return $result;
67 }
68
69 public static function raw(string $message) : self{
70 return self::messageOnly(self::TYPE_RAW, $message);
71 }
72
76 public static function translation(string $key, array $parameters = []) : self{
77 return self::baseTranslation(self::TYPE_TRANSLATION, $key, $parameters);
78 }
79
80 public static function popup(string $message) : self{
81 return self::messageOnly(self::TYPE_POPUP, $message);
82 }
83
87 public static function translatedPopup(string $key, array $parameters = []) : self{
88 return self::baseTranslation(self::TYPE_POPUP, $key, $parameters);
89 }
90
94 public static function jukeboxPopup(string $key, array $parameters = []) : self{
95 return self::baseTranslation(self::TYPE_JUKEBOX_POPUP, $key, $parameters);
96 }
97
98 public static function tip(string $message) : self{
99 return self::messageOnly(self::TYPE_TIP, $message);
100 }
101
102 protected function decodePayload(ByteBufferReader $in) : void{
103 $this->type = Byte::readUnsigned($in);
104 $this->needsTranslation = CommonTypes::getBool($in);
105 switch($this->type){
106 case self::TYPE_CHAT:
107 case self::TYPE_WHISPER:
109 case self::TYPE_ANNOUNCEMENT:
110 $this->sourceName = CommonTypes::getString($in);
111 case self::TYPE_RAW:
112 case self::TYPE_TIP:
113 case self::TYPE_SYSTEM:
114 case self::TYPE_JSON_WHISPER:
115 case self::TYPE_JSON:
116 case self::TYPE_JSON_ANNOUNCEMENT:
117 $this->message = CommonTypes::getString($in);
118 break;
119
120 case self::TYPE_TRANSLATION:
121 case self::TYPE_POPUP:
122 case self::TYPE_JUKEBOX_POPUP:
123 $this->message = CommonTypes::getString($in);
124 $count = VarInt::readUnsignedInt($in);
125 for($i = 0; $i < $count; ++$i){
126 $this->parameters[] = CommonTypes::getString($in);
127 }
128 break;
129 }
130
131 $this->xboxUserId = CommonTypes::getString($in);
132 $this->platformChatId = CommonTypes::getString($in);
133 $this->filteredMessage = CommonTypes::getString($in);
134 }
135
136 protected function encodePayload(ByteBufferWriter $out) : void{
137 Byte::writeUnsigned($out, $this->type);
138 CommonTypes::putBool($out, $this->needsTranslation);
139 switch($this->type){
140 case self::TYPE_CHAT:
141 case self::TYPE_WHISPER:
143 case self::TYPE_ANNOUNCEMENT:
144 CommonTypes::putString($out, $this->sourceName);
145 case self::TYPE_RAW:
146 case self::TYPE_TIP:
147 case self::TYPE_SYSTEM:
148 case self::TYPE_JSON_WHISPER:
149 case self::TYPE_JSON:
150 case self::TYPE_JSON_ANNOUNCEMENT:
151 CommonTypes::putString($out, $this->message);
152 break;
153
154 case self::TYPE_TRANSLATION:
155 case self::TYPE_POPUP:
156 case self::TYPE_JUKEBOX_POPUP:
157 CommonTypes::putString($out, $this->message);
158 VarInt::writeUnsignedInt($out, count($this->parameters));
159 foreach($this->parameters as $p){
160 CommonTypes::putString($out, $p);
161 }
162 break;
163 }
164
165 CommonTypes::putString($out, $this->xboxUserId);
166 CommonTypes::putString($out, $this->platformChatId);
167 CommonTypes::putString($out, $this->filteredMessage);
168 }
169
170 public function handle(PacketHandlerInterface $handler) : bool{
171 return $handler->handleText($this);
172 }
173}
static translation(string $key, array $parameters=[])
static jukeboxPopup(string $key, array $parameters=[])
handle(PacketHandlerInterface $handler)
static translatedPopup(string $key, array $parameters=[])