PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
18use function count;
19
21 public const NETWORK_ID = ProtocolInfo::TEXT_PACKET;
22
23 public const TYPE_RAW = 0;
24 public const TYPE_CHAT = 1;
25 public const TYPE_TRANSLATION = 2;
26 public const TYPE_POPUP = 3;
27 public const TYPE_JUKEBOX_POPUP = 4;
28 public const TYPE_TIP = 5;
29 public const TYPE_SYSTEM = 6;
30 public const TYPE_WHISPER = 7;
31 public const TYPE_ANNOUNCEMENT = 8;
32 public const TYPE_JSON_WHISPER = 9;
33 public const TYPE_JSON = 10;
34 public const TYPE_JSON_ANNOUNCEMENT = 11;
35
36 public int $type;
37 public bool $needsTranslation = false;
38 public string $sourceName;
39 public string $message;
41 public array $parameters = [];
42 public string $xboxUserId = "";
43 public string $platformChatId = "";
44
45 private static function messageOnly(int $type, string $message) : self{
46 $result = new self;
47 $result->type = $type;
48 $result->message = $message;
49 return $result;
50 }
51
55 private static function baseTranslation(int $type, string $key, array $parameters) : self{
56 $result = new self;
57 $result->type = $type;
58 $result->needsTranslation = true;
59 $result->message = $key;
60 $result->parameters = $parameters;
61 return $result;
62 }
63
64 public static function raw(string $message) : self{
65 return self::messageOnly(self::TYPE_RAW, $message);
66 }
67
71 public static function translation(string $key, array $parameters = []) : self{
72 return self::baseTranslation(self::TYPE_TRANSLATION, $key, $parameters);
73 }
74
75 public static function popup(string $message) : self{
76 return self::messageOnly(self::TYPE_POPUP, $message);
77 }
78
82 public static function translatedPopup(string $key, array $parameters = []) : self{
83 return self::baseTranslation(self::TYPE_POPUP, $key, $parameters);
84 }
85
89 public static function jukeboxPopup(string $key, array $parameters = []) : self{
90 return self::baseTranslation(self::TYPE_JUKEBOX_POPUP, $key, $parameters);
91 }
92
93 public static function tip(string $message) : self{
94 return self::messageOnly(self::TYPE_TIP, $message);
95 }
96
97 protected function decodePayload(PacketSerializer $in) : void{
98 $this->type = $in->getByte();
99 $this->needsTranslation = $in->getBool();
100 switch($this->type){
101 case self::TYPE_CHAT:
102 case self::TYPE_WHISPER:
104 case self::TYPE_ANNOUNCEMENT:
105 $this->sourceName = $in->getString();
106 case self::TYPE_RAW:
107 case self::TYPE_TIP:
108 case self::TYPE_SYSTEM:
109 case self::TYPE_JSON_WHISPER:
110 case self::TYPE_JSON:
111 case self::TYPE_JSON_ANNOUNCEMENT:
112 $this->message = $in->getString();
113 break;
114
115 case self::TYPE_TRANSLATION:
116 case self::TYPE_POPUP:
117 case self::TYPE_JUKEBOX_POPUP:
118 $this->message = $in->getString();
119 $count = $in->getUnsignedVarInt();
120 for($i = 0; $i < $count; ++$i){
121 $this->parameters[] = $in->getString();
122 }
123 break;
124 }
125
126 $this->xboxUserId = $in->getString();
127 $this->platformChatId = $in->getString();
128 }
129
130 protected function encodePayload(PacketSerializer $out) : void{
131 $out->putByte($this->type);
132 $out->putBool($this->needsTranslation);
133 switch($this->type){
134 case self::TYPE_CHAT:
135 case self::TYPE_WHISPER:
137 case self::TYPE_ANNOUNCEMENT:
138 $out->putString($this->sourceName);
139 case self::TYPE_RAW:
140 case self::TYPE_TIP:
141 case self::TYPE_SYSTEM:
142 case self::TYPE_JSON_WHISPER:
143 case self::TYPE_JSON:
144 case self::TYPE_JSON_ANNOUNCEMENT:
145 $out->putString($this->message);
146 break;
147
148 case self::TYPE_TRANSLATION:
149 case self::TYPE_POPUP:
150 case self::TYPE_JUKEBOX_POPUP:
151 $out->putString($this->message);
152 $out->putUnsignedVarInt(count($this->parameters));
153 foreach($this->parameters as $p){
154 $out->putString($p);
155 }
156 break;
157 }
158
159 $out->putString($this->xboxUserId);
160 $out->putString($this->platformChatId);
161 }
162
163 public function handle(PacketHandlerInterface $handler) : bool{
164 return $handler->handleText($this);
165 }
166}
static translation(string $key, array $parameters=[])
Definition: TextPacket.php:71
static jukeboxPopup(string $key, array $parameters=[])
Definition: TextPacket.php:89
handle(PacketHandlerInterface $handler)
Definition: TextPacket.php:163
static translatedPopup(string $key, array $parameters=[])
Definition: TextPacket.php:82