PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
ItemTypeDictionary.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol\serializer;
16
18use function array_key_exists;
19
21
26 private array $intToStringIdMap = [];
31 private array $stringToIntMap = [];
32
37 public function __construct(private array $itemTypes){
38 foreach($this->itemTypes as $type){
39 $this->stringToIntMap[$type->getStringId()] = $type->getNumericId();
40 $this->intToStringIdMap[$type->getNumericId()] = $type->getStringId();
41 }
42 }
43
48 public function getEntries() : array{
49 return $this->itemTypes;
50 }
51
52 public function fromStringId(string $stringId) : int{
53 if(!array_key_exists($stringId, $this->stringToIntMap)){
54 throw new \InvalidArgumentException("Unmapped string ID \"$stringId\"");
55 }
56 return $this->stringToIntMap[$stringId];
57 }
58
59 public function fromIntId(int $intId) : string{
60 if(!array_key_exists($intId, $this->intToStringIdMap)){
61 throw new \InvalidArgumentException("Unmapped int ID $intId");
62 }
63 return $this->intToStringIdMap[$intId];
64 }
65}