PocketMine-MP 5.15.1 git-ed158f8a1b0cfe334ac5f45febc0f633602014f2
tile/FlowerPot.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\block\tile;
25
38
43class FlowerPot extends Spawnable{
44 private const TAG_ITEM = "item";
45 private const TAG_ITEM_DATA = "mData";
46 private const TAG_PLANT_BLOCK = "PlantBlock";
47
48 private ?Block $plant = null;
49
50 public function readSaveData(CompoundTag $nbt) : void{
51 $blockStateData = null;
52
53 $blockDataUpgrader = GlobalBlockStateHandlers::getUpgrader();
54 if(($itemIdTag = $nbt->getTag(self::TAG_ITEM)) instanceof ShortTag && ($itemMetaTag = $nbt->getTag(self::TAG_ITEM_DATA)) instanceof IntTag){
55 try{
56 $blockStateData = $blockDataUpgrader->upgradeIntIdMeta($itemIdTag->getValue(), $itemMetaTag->getValue());
58 throw new SavedDataLoadingException("Error loading legacy flower pot item data: " . $e->getMessage(), 0, $e);
59 }
60 }elseif(($plantBlockTag = $nbt->getCompoundTag(self::TAG_PLANT_BLOCK)) !== null){
61 try{
62 $blockStateData = $blockDataUpgrader->upgradeBlockStateNbt($plantBlockTag);
64 throw new SavedDataLoadingException("Error loading " . self::TAG_PLANT_BLOCK . " tag for flower pot: " . $e->getMessage(), 0, $e);
65 }
66 }
67
68 if($blockStateData !== null){
69 try{
70 $blockStateId = GlobalBlockStateHandlers::getDeserializer()->deserialize($blockStateData);
72 throw new SavedDataLoadingException("Error deserializing plant for flower pot: " . $e->getMessage(), 0, $e);
73 }
74 $this->setPlant(RuntimeBlockStateRegistry::getInstance()->fromStateId($blockStateId));
75 }
76 }
77
78 protected function writeSaveData(CompoundTag $nbt) : void{
79 if($this->plant !== null){
80 $nbt->setTag(self::TAG_PLANT_BLOCK, GlobalBlockStateHandlers::getSerializer()->serialize($this->plant->getStateId())->toNbt());
81 }
82 }
83
84 public function getPlant() : ?Block{
85 return $this->plant !== null ? clone $this->plant : null;
86 }
87
88 public function setPlant(?Block $plant) : void{
89 if($plant === null || $plant instanceof Air){
90 $this->plant = null;
91 }else{
92 $this->plant = clone $plant;
93 }
94 }
95
96 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
97 if($this->plant !== null){
98 $nbt->setTag(self::TAG_PLANT_BLOCK, TypeConverter::getInstance()->getBlockTranslator()->internalIdToNetworkStateData($this->plant->getStateId())->toNbt());
99 }
100 }
101
102 public function getRenderUpdateBugWorkaroundStateProperties(Block $block) : array{
103 return [BlockStateNames::UPDATE_BIT => new ByteTag(1)];
104 }
105}
getRenderUpdateBugWorkaroundStateProperties(Block $block)
addAdditionalSpawnData(CompoundTag $nbt)
setTag(string $name, Tag $tag)