PocketMine-MP 5.25.1 git-694aa17cc916a954b10fe12721c81b1dc73eecd5
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 $this->deserializers[$id] = $deserializer;
55 }
56
63 public function getDeserializerForId(string $id) : ?\Closure{
64 return $this->deserializers[$id] ?? null;
65 }
66
70 public function mapBlock(string $id, \Closure $deserializer) : void{
71 $this->map($id, fn(Data $data) => $deserializer($data)->asItem());
72 }
73
77 public function deserializeType(Data $data) : Item{
78 if(($blockData = $data->getBlock()) !== null){
79 //TODO: this is rough duct tape; we need a better way to deal with this
80 try{
81 $block = $this->blockStateDeserializer->deserialize($blockData);
83 throw new UnsupportedItemTypeException($e->getMessage(), 0, $e);
85 throw new ItemTypeDeserializeException("Failed to deserialize item data: " . $e->getMessage(), 0, $e);
86 }
87
88 //TODO: worth caching this or not?
89 return RuntimeBlockStateRegistry::getInstance()->fromStateId($block)->asItem();
90 }
91 $id = $data->getName();
92 if(!isset($this->deserializers[$id])){
93 throw new UnsupportedItemTypeException("No deserializer found for ID $id");
94 }
95
96 return ($this->deserializers[$id])($data);
97 }
98
102 public function deserializeStack(SavedItemStackData $data) : Item{
103 $itemStack = $this->deserializeType($data->getTypeData());
104
105 $itemStack->setCount($data->getCount());
106 if(($tagTag = $data->getTypeData()->getTag()) !== null){
107 try{
108 $itemStack->setNamedTag(clone $tagTag);
109 }catch(NbtException $e){
110 throw new ItemTypeDeserializeException("Invalid item saved NBT: " . $e->getMessage(), 0, $e);
111 }
112 }
113
114 //TODO: this hack is necessary to get legacy tools working - we need a better way to handle this kind of stuff
115 if($itemStack instanceof Durable && $itemStack->getDamage() === 0 && ($damage = $data->getTypeData()->getMeta()) > 0){
116 $itemStack->setDamage(min($damage, $itemStack->getMaxDurability()));
117 }
118
119 //TODO: canDestroy, canPlaceOn, wasPickedUp are currently unused
120
121 return $itemStack;
122 }
123}
mapBlock(string $id, \Closure $deserializer)
map(string $id, \Closure $deserializer)
setCount(int $count)
Definition Item.php:418