22declare(strict_types=1);
24namespace pocketmine\data\bedrock;
26use
function array_key_exists;
27use
function spl_object_id;
32trait IntSaveIdMapTrait{
38 private array $idToEnum = [];
44 private array $enumToId = [];
49 protected function getRuntimeId(
object $enum) : int{
51 return spl_object_id($enum);
57 public function register(
int $saveId,
object $enum) : void{
58 $this->idToEnum[$saveId] = $enum;
59 $this->enumToId[$this->getRuntimeId($enum)] = $saveId;
65 public function fromId(
int $id) : ?object{
67 return $this->idToEnum[$id] ?? null;
73 public function toId(
object $enum) : int{
74 $runtimeId = $this->getRuntimeId($enum);
75 if(!array_key_exists($runtimeId, $this->enumToId)){
77 throw new \InvalidArgumentException(
"Object does not have a mapped save ID");
79 return $this->enumToId[$runtimeId];