PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
ItemDeserializer.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;
25
35use function min;
36
37final class ItemDeserializer{
42 private array $deserializers = [];
43
44 public function __construct(
45 private BlockStateDeserializer $blockStateDeserializer
46 ){
48 }
49
53 public function map(string $id, \Closure $deserializer) : void{
54 if(isset($this->deserializers[$id])){
55 throw new \InvalidArgumentException("Deserializer is already assigned for \"$id\"");
56 }
57 $this->deserializers[$id] = $deserializer;
58 }
59
63 public function mapBlock(string $id, \Closure $deserializer) : void{
64 $this->map($id, fn(Data $data) => $deserializer($data)->asItem());
65 }
66
70 public function deserializeType(Data $data) : Item{
71 if(($blockData = $data->getBlock()) !== null){
72 //TODO: this is rough duct tape; we need a better way to deal with this
73 try{
74 $block = $this->blockStateDeserializer->deserialize($blockData);
76 throw new UnsupportedItemTypeException($e->getMessage(), 0, $e);
78 throw new ItemTypeDeserializeException("Failed to deserialize item data: " . $e->getMessage(), 0, $e);
79 }
80
81 //TODO: worth caching this or not?
82 return RuntimeBlockStateRegistry::getInstance()->fromStateId($block)->asItem();
83 }
84 $id = $data->getName();
85 if(!isset($this->deserializers[$id])){
86 throw new UnsupportedItemTypeException("No deserializer found for ID $id");
87 }
88
89 return ($this->deserializers[$id])($data);
90 }
91
95 public function deserializeStack(SavedItemStackData $data) : Item{
96 $itemStack = $this->deserializeType($data->getTypeData());
97
98 $itemStack->setCount($data->getCount());
99 if(($tagTag = $data->getTypeData()->getTag()) !== null){
100 try{
101 $itemStack->setNamedTag(clone $tagTag);
102 }catch(NbtException $e){
103 throw new ItemTypeDeserializeException("Invalid item saved NBT: " . $e->getMessage(), 0, $e);
104 }
105 }
106
107 //TODO: this hack is necessary to get legacy tools working - we need a better way to handle this kind of stuff
108 if($itemStack instanceof Durable && $itemStack->getDamage() === 0 && ($damage = $data->getTypeData()->getMeta()) > 0){
109 $itemStack->setDamage(min($damage, $itemStack->getMaxDurability()));
110 }
111
112 //TODO: canDestroy, canPlaceOn, wasPickedUp are currently unused
113
114 return $itemStack;
115 }
116}
mapBlock(string $id, \Closure $deserializer)
map(string $id, \Closure $deserializer)
setCount(int $count)
Definition Item.php:418