PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
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 public string $filteredMessage = "";
45
46 private static function messageOnly(int $type, string $message) : self{
47 $result = new self;
48 $result->type = $type;
49 $result->message = $message;
50 return $result;
51 }
52
56 private static function baseTranslation(int $type, string $key, array $parameters) : self{
57 $result = new self;
58 $result->type = $type;
59 $result->needsTranslation = true;
60 $result->message = $key;
61 $result->parameters = $parameters;
62 return $result;
63 }
64
65 public static function raw(string $message) : self{
66 return self::messageOnly(self::TYPE_RAW, $message);
67 }
68
72 public static function translation(string $key, array $parameters = []) : self{
73 return self::baseTranslation(self::TYPE_TRANSLATION, $key, $parameters);
74 }
75
76 public static function popup(string $message) : self{
77 return self::messageOnly(self::TYPE_POPUP, $message);
78 }
79
83 public static function translatedPopup(string $key, array $parameters = []) : self{
84 return self::baseTranslation(self::TYPE_POPUP, $key, $parameters);
85 }
86
90 public static function jukeboxPopup(string $key, array $parameters = []) : self{
91 return self::baseTranslation(self::TYPE_JUKEBOX_POPUP, $key, $parameters);
92 }
93
94 public static function tip(string $message) : self{
95 return self::messageOnly(self::TYPE_TIP, $message);
96 }
97
98 protected function decodePayload(PacketSerializer $in) : void{
99 $this->type = $in->getByte();
100 $this->needsTranslation = $in->getBool();
101 switch($this->type){
102 case self::TYPE_CHAT:
103 case self::TYPE_WHISPER:
105 case self::TYPE_ANNOUNCEMENT:
106 $this->sourceName = $in->getString();
107 case self::TYPE_RAW:
108 case self::TYPE_TIP:
109 case self::TYPE_SYSTEM:
110 case self::TYPE_JSON_WHISPER:
111 case self::TYPE_JSON:
112 case self::TYPE_JSON_ANNOUNCEMENT:
113 $this->message = $in->getString();
114 break;
115
116 case self::TYPE_TRANSLATION:
117 case self::TYPE_POPUP:
118 case self::TYPE_JUKEBOX_POPUP:
119 $this->message = $in->getString();
120 $count = $in->getUnsignedVarInt();
121 for($i = 0; $i < $count; ++$i){
122 $this->parameters[] = $in->getString();
123 }
124 break;
125 }
126
127 $this->xboxUserId = $in->getString();
128 $this->platformChatId = $in->getString();
129 $this->filteredMessage = $in->getString();
130 }
131
132 protected function encodePayload(PacketSerializer $out) : void{
133 $out->putByte($this->type);
134 $out->putBool($this->needsTranslation);
135 switch($this->type){
136 case self::TYPE_CHAT:
137 case self::TYPE_WHISPER:
139 case self::TYPE_ANNOUNCEMENT:
140 $out->putString($this->sourceName);
141 case self::TYPE_RAW:
142 case self::TYPE_TIP:
143 case self::TYPE_SYSTEM:
144 case self::TYPE_JSON_WHISPER:
145 case self::TYPE_JSON:
146 case self::TYPE_JSON_ANNOUNCEMENT:
147 $out->putString($this->message);
148 break;
149
150 case self::TYPE_TRANSLATION:
151 case self::TYPE_POPUP:
152 case self::TYPE_JUKEBOX_POPUP:
153 $out->putString($this->message);
154 $out->putUnsignedVarInt(count($this->parameters));
155 foreach($this->parameters as $p){
156 $out->putString($p);
157 }
158 break;
159 }
160
161 $out->putString($this->xboxUserId);
162 $out->putString($this->platformChatId);
163 $out->putString($this->filteredMessage);
164 }
165
166 public function handle(PacketHandlerInterface $handler) : bool{
167 return $handler->handleText($this);
168 }
169}
static translation(string $key, array $parameters=[])
static jukeboxPopup(string $key, array $parameters=[])
handle(PacketHandlerInterface $handler)
static translatedPopup(string $key, array $parameters=[])