PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
R12ItemIdToBlockIdMap.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\data\bedrock\item\upgrade;
25
28use pocketmine\utils\SingletonTrait;
30use Symfony\Component\Filesystem\Path;
31use function is_array;
32use function is_string;
33use function json_decode;
34use function mb_strtolower;
35use const JSON_THROW_ON_ERROR;
36use const pocketmine\BEDROCK_ITEM_UPGRADE_SCHEMA_PATH;
37
46 use SingletonTrait;
47
48 private static function make() : self{
49 $map = json_decode(
50 Filesystem::fileGetContents(Path::join(BEDROCK_ITEM_UPGRADE_SCHEMA_PATH, '1.12.0_item_id_to_block_id_map.json')),
51 associative: true,
52 flags: JSON_THROW_ON_ERROR
53 );
54 if(!is_array($map)){
55 throw new AssumptionFailedError("Invalid blockitem ID mapping table, expected array as root type");
56 }
57
58 $builtMap = [];
59 foreach($map as $itemId => $blockId){
60 if(!is_string($itemId)){
61 throw new AssumptionFailedError("Invalid blockitem ID mapping table, expected string as key");
62 }
63 if(!is_string($blockId)){
64 throw new AssumptionFailedError("Invalid blockitem ID mapping table, expected string as value");
65 }
66 $builtMap[$itemId] = $blockId;
67 }
68
69 return new self($builtMap);
70 }
71
76 private array $itemToBlock = [];
81 private array $blockToItem = [];
82
87 public function __construct(array $itemToBlock){
88 foreach(Utils::stringifyKeys($itemToBlock) as $itemId => $blockId){
89 $this->itemToBlock[mb_strtolower($itemId, 'US-ASCII')] = $blockId;
90 $this->blockToItem[mb_strtolower($blockId, 'US-ASCII')] = $itemId;
91 }
92 }
93
94 public function itemIdToBlockId(string $itemId) : ?string{
95 return $this->itemToBlock[mb_strtolower($itemId, 'US-ASCII')] ?? null;
96 }
97
98 public function blockIdToItemId(string $blockId) : ?string{
99 //we don't need this for any functionality, but it might be nice to have for debugging
100 return $this->blockToItem[mb_strtolower($blockId, 'US-ASCII')] ?? null;
101 }
102}