First look at flattened ID specialisation for block serializers

in the future we should be able to unify these, similarly to simple mappings.
unifying blocks with states will, however, be considerably more work.

only color benefits from this so far
This commit is contained in:
Dylan K. Taylor
2025-08-15 22:02:12 +01:00
parent 1e8612cfc8
commit e89523ce66
4 changed files with 377 additions and 448 deletions

View File

@ -0,0 +1,78 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\data\bedrock\block\convert;
use pocketmine\data\bedrock\block\BlockStateDeserializeException;
use function spl_object_id;
/**
* @phpstan-template TEnum of \UnitEnum
*/
class StringEnumMap{
/**
* @var string[]
* @phpstan-var array<int, string>
*/
private array $enumToValue = [];
/**
* @var \UnitEnum[]
* @phpstan-var array<string, TEnum>
*/
private array $valueToEnum = [];
/**
* @phpstan-param class-string<TEnum> $class
* @phpstan-param \Closure(TEnum) : string $mapper
*/
public function __construct(
private string $class,
\Closure $mapper
){
foreach($class::cases() as $case){
$string = $mapper($case);
$this->valueToEnum[$string] = $case;
$this->enumToValue[spl_object_id($case)] = $string;
}
}
/**
* @phpstan-param TEnum $enum
*/
public function enumToValue(\UnitEnum $enum) : string{
return $this->enumToValue[spl_object_id($enum)];
}
public function valueToEnum(string $string) : ?\UnitEnum{
return $this->valueToEnum[$string] ?? throw new BlockStateDeserializeException("No $this->class enum mapping for \"$string\"");
}
/**
* @return \UnitEnum[]
* @phpstan-return array<string, TEnum>
*/
public function getValueToEnum() : array{
return $this->valueToEnum;
}
}