PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
RuntimeDataSizeCalculator.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\data\runtime;
25
26use pocketmine\block\utils\BrewingStandSlot;
28use function count;
29use function log;
30
32 use LegacyRuntimeEnumDescriberTrait;
33
34 private int $bits = 0;
35
36 protected function addBits(int $bits) : void{
37 $this->bits += $bits;
38 }
39
40 public function getBitsUsed() : int{
41 return $this->bits;
42 }
43
44 public function int(int $bits, int &$value) : void{
45 $this->addBits($bits);
46 }
47
51 public function boundedInt(int $bits, int $min, int $max, int &$value) : void{
52 $currentBits = $this->bits;
53 $this->boundedIntAuto($min, $max, $value);
54 $actualBits = $this->bits - $currentBits;
55 if($actualBits !== $bits){
56 throw new \InvalidArgumentException("Bits should be $actualBits for the given bounds, but received $bits. Use boundedIntAuto() for automatic bits calculation.");
57 }
58 }
59
60 public function boundedIntAuto(int $min, int $max, int &$value) : void{
61 $this->addBits(((int) log($max - $min, 2)) + 1);
62 }
63
64 public function bool(bool &$value) : void{
65 $this->addBits(1);
66 }
67
68 public function horizontalFacing(int &$facing) : void{
69 $this->addBits(2);
70 }
71
72 public function facingFlags(array &$faces) : void{
73 $this->addBits(count(Facing::ALL));
74 }
75
76 public function horizontalFacingFlags(array &$faces) : void{
77 $this->addBits(count(Facing::HORIZONTAL));
78 }
79
80 public function facing(int &$facing) : void{
81 $this->addBits(3);
82 }
83
84 public function facingExcept(int &$facing, int $except) : void{
85 $this->facing($facing);
86 }
87
88 public function axis(int &$axis) : void{
89 $this->addBits(2);
90 }
91
92 public function horizontalAxis(int &$axis) : void{
93 $this->addBits(1);
94 }
95
96 public function wallConnections(array &$connections) : void{
97 $this->addBits(7);
98 }
99
100 public function brewingStandSlots(array &$slots) : void{
101 $this->addBits(count(BrewingStandSlot::cases()));
102 }
103
104 public function railShape(int &$railShape) : void{
105 $this->addBits(4);
106 }
107
108 public function straightOnlyRailShape(int &$railShape) : void{
109 $this->addBits(3);
110 }
111
112 public function enum(\UnitEnum &$case) : void{
113 $metadata = RuntimeEnumMetadata::from($case);
114 $this->addBits($metadata->bits);
115 }
116
117 public function enumSet(array &$set, array $allCases) : void{
118 $this->addBits(count($allCases));
119 }
120}
boundedInt(int $bits, int $min, int $max, int &$value)