PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
ItemIdMetaUpgrader.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
26use function ksort;
27use const SORT_NUMERIC;
28
33
38 private array $idMetaUpgradeSchemas = [];
39
44 public function __construct(
45 array $idMetaUpgradeSchemas,
46 ){
47 foreach($idMetaUpgradeSchemas as $schema){
48 $this->addSchema($schema);
49 }
50 }
51
52 public function addSchema(ItemIdMetaUpgradeSchema $schema) : void{
53 if(isset($this->idMetaUpgradeSchemas[$schema->getSchemaId()])){
54 throw new \InvalidArgumentException("Already have a schema with priority " . $schema->getSchemaId());
55 }
56 $this->idMetaUpgradeSchemas[$schema->getSchemaId()] = $schema;
57 ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC);
58 }
59
64 public function getSchemas() : array{ return $this->idMetaUpgradeSchemas; }
65
69 public function upgrade(string $id, int $meta) : array{
70 $newId = $id;
71 $newMeta = $meta;
72 foreach($this->idMetaUpgradeSchemas as $schema){
73 if(($remappedMetaId = $schema->remapMeta($newId, $newMeta)) !== null){
74 $newId = $remappedMetaId;
75 $newMeta = 0;
76 }elseif(($renamedId = $schema->renameId($newId)) !== null){
77 $newId = $renamedId;
78 }
79 }
80
81 return [$newId, $newMeta];
82 }
83}