PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
DyeColorIdMap.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
26use pocketmine\block\utils\DyeColor;
28use pocketmine\utils\SingletonTrait;
29use function spl_object_id;
30
31final class DyeColorIdMap{
32 use SingletonTrait;
34 use IntSaveIdMapTrait {
35 register as registerInt;
36 }
37
42 private array $itemIdToEnum = [];
43
48 private array $enumToItemId = [];
49
50 private function __construct(){
51 foreach(DyeColor::cases() as $case){
52 [$colorId, $dyeItemId] = match($case){
53 DyeColor::WHITE => [0, ItemTypeNames::WHITE_DYE],
54 DyeColor::ORANGE => [1, ItemTypeNames::ORANGE_DYE],
55 DyeColor::MAGENTA => [2, ItemTypeNames::MAGENTA_DYE],
56 DyeColor::LIGHT_BLUE => [3, ItemTypeNames::LIGHT_BLUE_DYE],
57 DyeColor::YELLOW => [4, ItemTypeNames::YELLOW_DYE],
58 DyeColor::LIME => [5, ItemTypeNames::LIME_DYE],
59 DyeColor::PINK => [6, ItemTypeNames::PINK_DYE],
60 DyeColor::GRAY => [7, ItemTypeNames::GRAY_DYE],
61 DyeColor::LIGHT_GRAY => [8, ItemTypeNames::LIGHT_GRAY_DYE],
62 DyeColor::CYAN => [9, ItemTypeNames::CYAN_DYE],
63 DyeColor::PURPLE => [10, ItemTypeNames::PURPLE_DYE],
64 DyeColor::BLUE => [11, ItemTypeNames::BLUE_DYE],
65 DyeColor::BROWN => [12, ItemTypeNames::BROWN_DYE],
66 DyeColor::GREEN => [13, ItemTypeNames::GREEN_DYE],
67 DyeColor::RED => [14, ItemTypeNames::RED_DYE],
68 DyeColor::BLACK => [15, ItemTypeNames::BLACK_DYE],
69 };
70
71 $this->register($colorId, $dyeItemId, $case);
72 }
73 }
74
75 private function register(int $id, string $itemId, DyeColor $color) : void{
76 $this->registerInt($id, $color);
77 $this->itemIdToEnum[$itemId] = $color;
78 $this->enumToItemId[spl_object_id($color)] = $itemId;
79 }
80
81 public function toInvertedId(DyeColor $color) : int{
82 return ~$this->toId($color) & 0xf;
83 }
84
85 public function toItemId(DyeColor $color) : string{
86 return $this->enumToItemId[spl_object_id($color)];
87 }
88
89 public function fromInvertedId(int $id) : ?DyeColor{
90 return $this->fromId(~$id & 0xf);
91 }
92
93 public function fromItemId(string $itemId) : ?DyeColor{
94 return $this->itemIdToEnum[$itemId] ?? null;
95 }
96}