PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
ClientboundDebugRendererPacket.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\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
22
24 public const NETWORK_ID = ProtocolInfo::CLIENTBOUND_DEBUG_RENDERER_PACKET;
25
26 public const TYPE_CLEAR = 1;
27 public const TYPE_ADD_CUBE = 2;
28
29 private int $type;
30
31 //TODO: if more types are added, we'll probably want to make a separate data type and interfaces
32 private string $text;
33 private Vector3 $position;
34 private float $red;
35 private float $green;
36 private float $blue;
37 private float $alpha;
38 private int $durationMillis;
39
40 private static function base(int $type) : self{
41 $result = new self;
42 $result->type = $type;
43 return $result;
44 }
45
46 public static function clear() : self{ return self::base(self::TYPE_CLEAR); }
47
48 public static function addCube(string $text, Vector3 $position, float $red, float $green, float $blue, float $alpha, int $durationMillis) : self{
49 $result = self::base(self::TYPE_ADD_CUBE);
50 $result->text = $text;
51 $result->position = $position;
52 $result->red = $red;
53 $result->green = $green;
54 $result->blue = $blue;
55 $result->alpha = $alpha;
56 $result->durationMillis = $durationMillis;
57 return $result;
58 }
59
60 public function getType() : int{ return $this->type; }
61
62 public function getText() : string{ return $this->text; }
63
64 public function getPosition() : Vector3{ return $this->position; }
65
66 public function getRed() : float{ return $this->red; }
67
68 public function getGreen() : float{ return $this->green; }
69
70 public function getBlue() : float{ return $this->blue; }
71
72 public function getAlpha() : float{ return $this->alpha; }
73
74 public function getDurationMillis() : int{ return $this->durationMillis; }
75
76 protected function decodePayload(ByteBufferReader $in) : void{
77 $this->type = LE::readUnsignedInt($in);
78
79 switch($this->type){
80 case self::TYPE_CLEAR:
81 //NOOP
82 break;
83 case self::TYPE_ADD_CUBE:
84 $this->text = CommonTypes::getString($in);
85 $this->position = CommonTypes::getVector3($in);
86 $this->red = LE::readFloat($in);
87 $this->green = LE::readFloat($in);
88 $this->blue = LE::readFloat($in);
89 $this->alpha = LE::readFloat($in);
90 $this->durationMillis = LE::readUnsignedLong($in);
91 break;
92 default:
93 throw new PacketDecodeException("Unknown type " . $this->type);
94 }
95 }
96
97 protected function encodePayload(ByteBufferWriter $out) : void{
98 LE::writeUnsignedInt($out, $this->type);
99
100 switch($this->type){
101 case self::TYPE_CLEAR:
102 //NOOP
103 break;
104 case self::TYPE_ADD_CUBE:
105 CommonTypes::putString($out, $this->text);
106 CommonTypes::putVector3($out, $this->position);
107 LE::writeFloat($out, $this->red);
108 LE::writeFloat($out, $this->green);
109 LE::writeFloat($out, $this->blue);
110 LE::writeFloat($out, $this->alpha);
111 LE::writeUnsignedLong($out, $this->durationMillis);
112 break;
113 default:
114 throw new \InvalidArgumentException("Unknown type " . $this->type);
115 }
116 }
117
118 public function handle(PacketHandlerInterface $handler) : bool{
119 return $handler->handleClientboundDebugRenderer($this);
120 }
121}