PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
ItemTagToIdMap.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;
25
28use pocketmine\utils\SingletonTrait;
30use function array_keys;
31use function gettype;
32use function is_array;
33use function is_string;
34use function json_decode;
35use const JSON_THROW_ON_ERROR;
36
42final class ItemTagToIdMap{
43 use SingletonTrait;
44
45 private static function make() : self{
46 $map = json_decode(Filesystem::fileGetContents(BedrockDataFiles::ITEM_TAGS_JSON), true, flags: JSON_THROW_ON_ERROR);
47 if(!is_array($map)){
48 throw new AssumptionFailedError("Invalid item tag map, expected array");
49 }
50 $cleanMap = [];
51 foreach($map as $tagName => $ids){
52 if(!is_string($tagName)){
53 throw new AssumptionFailedError("Invalid item tag name $tagName, expected string as key");
54 }
55 if(!is_array($ids)){
56 throw new AssumptionFailedError("Invalid item tag $tagName, expected array of IDs as value");
57 }
58 $cleanIds = [];
59 foreach($ids as $id){
60 if(!is_string($id)){
61 throw new AssumptionFailedError("Invalid item tag $tagName, expected string as ID, got " . gettype($id));
62 }
63 $cleanIds[] = $id;
64 }
65 $cleanMap[$tagName] = $cleanIds;
66 }
67
68 return new self($cleanMap);
69 }
70
75 private array $tagToIdsMap = [];
76
81 public function __construct(
82 array $tagToIds
83 ){
84 foreach(Utils::stringifyKeys($tagToIds) as $tag => $ids){
85 foreach($ids as $id){
86 $this->tagToIdsMap[$tag][$id] = true;
87 }
88 }
89 }
90
95 public function getIdsForTag(string $tag) : array{
96 return array_keys($this->tagToIdsMap[$tag] ?? []);
97 }
98
99 public function tagContainsId(string $tag, string $id) : bool{
100 return isset($this->tagToIdsMap[$tag][$id]);
101 }
102
103 public function addIdToTag(string $tag, string $id) : void{
104 $this->tagToIdsMap[$tag][$id] = true;
105 }
106}
static fileGetContents(string $fileName, bool $useIncludePath=false, $context=null, int $offset=0, ?int $length=null)
Definition: Filesystem.php:305
static stringifyKeys(array $array)
Definition: Utils.php:605