PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
BlockStateDictionaryEntry.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\network\mcpe\convert;
25
31use function count;
32use function ksort;
33use const SORT_STRING;
34
40 private static array $uniqueRawStates = [];
41
42 private string $rawStateProperties;
43
47 public function __construct(
48 private string $stateName,
49 array $stateProperties,
50 private int $meta
51 ){
52 $rawStateProperties = self::encodeStateProperties($stateProperties);
53 $this->rawStateProperties = self::$uniqueRawStates[$rawStateProperties] ??= $rawStateProperties;
54 }
55
56 public function getStateName() : string{ return $this->stateName; }
57
58 public function getRawStateProperties() : string{ return $this->rawStateProperties; }
59
60 public function generateStateData() : BlockStateData{
61 return new BlockStateData(
62 $this->stateName,
63 self::decodeStateProperties($this->rawStateProperties),
64 BlockStateData::CURRENT_VERSION
65 );
66 }
67
68 public function getMeta() : int{ return $this->meta; }
69
73 public static function decodeStateProperties(string $rawProperties) : array{
74 if($rawProperties === ""){
75 return [];
76 }
77 return (new LittleEndianNbtSerializer())->read($rawProperties)->mustGetCompoundTag()->getValue();
78 }
79
83 public static function encodeStateProperties(array $properties) : string{
84 if(count($properties) === 0){
85 return "";
86 }
87 //TODO: make a more efficient encoding - NBT will do for now, but it's not very compact
88 ksort($properties, SORT_STRING);
89 $tag = new CompoundTag();
90 foreach($properties as $k => $v){
91 $tag->setTag($k, $v);
92 }
93 return (new LittleEndianNbtSerializer())->write(new TreeRoot($tag));
94 }
95}
static decodeStateProperties(string $rawProperties)
__construct(private string $stateName, array $stateProperties, private int $meta)
static encodeStateProperties(array $properties)