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