PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
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 private int $bits = 0;
33
34 protected function addBits(int $bits) : void{
35 $this->bits += $bits;
36 }
37
38 public function getBitsUsed() : int{
39 return $this->bits;
40 }
41
42 public function int(int $bits, int &$value) : void{
43 $this->addBits($bits);
44 }
45
49 public function boundedInt(int $bits, int $min, int $max, int &$value) : void{
50 $currentBits = $this->bits;
51 $this->boundedIntAuto($min, $max, $value);
52 $actualBits = $this->bits - $currentBits;
53 if($actualBits !== $bits){
54 throw new \InvalidArgumentException("Bits should be $actualBits for the given bounds, but received $bits. Use boundedIntAuto() for automatic bits calculation.");
55 }
56 }
57
58 public function boundedIntAuto(int $min, int $max, int &$value) : void{
59 $this->addBits(((int) log($max - $min, 2)) + 1);
60 }
61
62 public function bool(bool &$value) : void{
63 $this->addBits(1);
64 }
65
66 public function horizontalFacing(int &$facing) : void{
67 $this->addBits(2);
68 }
69
70 public function facingFlags(array &$faces) : void{
71 $this->addBits(count(Facing::ALL));
72 }
73
74 public function horizontalFacingFlags(array &$faces) : void{
75 $this->addBits(count(Facing::HORIZONTAL));
76 }
77
78 public function facing(int &$facing) : void{
79 $this->addBits(3);
80 }
81
82 public function facingExcept(int &$facing, int $except) : void{
83 $this->facing($facing);
84 }
85
86 public function axis(int &$axis) : void{
87 $this->addBits(2);
88 }
89
90 public function horizontalAxis(int &$axis) : void{
91 $this->addBits(1);
92 }
93
94 public function wallConnections(array &$connections) : void{
95 $this->addBits(7);
96 }
97
98 public function brewingStandSlots(array &$slots) : void{
99 $this->addBits(count(BrewingStandSlot::cases()));
100 }
101
102 public function railShape(int &$railShape) : void{
103 $this->addBits(4);
104 }
105
106 public function straightOnlyRailShape(int &$railShape) : void{
107 $this->addBits(3);
108 }
109
110 public function enum(\UnitEnum &$case) : void{
111 $metadata = RuntimeEnumMetadata::from($case);
112 $this->addBits($metadata->bits);
113 }
114
115 public function enumSet(array &$set, array $allCases) : void{
116 $this->addBits(count($allCases));
117 }
118}
boundedInt(int $bits, int $min, int $max, int &$value)