PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
32use function count;
33use function ksort;
34use const SORT_STRING;
35
41 private static array $uniqueRawStates = [];
42
43 private string $rawStateProperties;
44
49 public function __construct(
50 private string $stateName,
51 array $stateProperties,
52 private int $meta
53 ){
54 $rawStateProperties = self::encodeStateProperties($stateProperties);
55 $this->rawStateProperties = self::$uniqueRawStates[$rawStateProperties] ??= $rawStateProperties;
56 }
57
58 public function getStateName() : string{ return $this->stateName; }
59
60 public function getRawStateProperties() : string{ return $this->rawStateProperties; }
61
62 public function generateStateData() : BlockStateData{
63 return new BlockStateData(
64 $this->stateName,
65 self::decodeStateProperties($this->rawStateProperties),
66 BlockStateData::CURRENT_VERSION
67 );
68 }
69
70 public function getMeta() : int{ return $this->meta; }
71
75 public static function decodeStateProperties(string $rawProperties) : array{
76 if($rawProperties === ""){
77 return [];
78 }
79 return (new LittleEndianNbtSerializer())->read($rawProperties)->mustGetCompoundTag()->getValue();
80 }
81
86 public static function encodeStateProperties(array $properties) : string{
87 if(count($properties) === 0){
88 return "";
89 }
90 //TODO: make a more efficient encoding - NBT will do for now, but it's not very compact
91 ksort($properties, SORT_STRING);
92 $tag = new CompoundTag();
93 foreach(Utils::stringifyKeys($properties) as $k => $v){
94 $tag->setTag($k, $v);
95 }
96 return (new LittleEndianNbtSerializer())->write(new TreeRoot($tag));
97 }
98}
static decodeStateProperties(string $rawProperties)
__construct(private string $stateName, array $stateProperties, private int $meta)
static encodeStateProperties(array $properties)