PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
tile/Cauldron.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
41
42final class Cauldron extends Spawnable{
43
44 private const POTION_CONTAINER_TYPE_NONE = -1;
45 private const POTION_CONTAINER_TYPE_NORMAL = 0;
46 private const POTION_CONTAINER_TYPE_SPLASH = 1;
47 private const POTION_CONTAINER_TYPE_LINGERING = 2;
48
49 private const POTION_ID_NONE = -1;
50
51 private const TAG_POTION_ID = "PotionId"; //TAG_Short
52 private const TAG_POTION_CONTAINER_TYPE = "PotionType"; //TAG_Short
53 private const TAG_CUSTOM_COLOR = "CustomColor"; //TAG_Int
54
55 private ?Item $potionItem = null;
56 private ?Color $customWaterColor = null;
57
58 public function getPotionItem() : ?Item{ return $this->potionItem; }
59
60 public function setPotionItem(?Item $potionItem) : void{
61 $this->potionItem = $potionItem;
62 }
63
64 public function getCustomWaterColor() : ?Color{ return $this->customWaterColor; }
65
66 public function setCustomWaterColor(?Color $customWaterColor) : void{
67 $this->customWaterColor = $customWaterColor;
68 }
69
70 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
71 $nbt->setShort(self::TAG_POTION_CONTAINER_TYPE, match($this->potionItem?->getTypeId()){
72 ItemTypeIds::POTION => self::POTION_CONTAINER_TYPE_NORMAL,
73 ItemTypeIds::SPLASH_POTION => self::POTION_CONTAINER_TYPE_SPLASH,
74 ItemTypeIds::LINGERING_POTION => self::POTION_CONTAINER_TYPE_LINGERING,
75 null => self::POTION_CONTAINER_TYPE_NONE,
76 default => throw new AssumptionFailedError("Unexpected potion item type")
77 });
78
79 //TODO: lingering potion
80 $type = $this->potionItem instanceof Potion || $this->potionItem instanceof SplashPotion ? $this->potionItem->getType() : null;
81 $nbt->setShort(self::TAG_POTION_ID, $type === null ? self::POTION_ID_NONE : PotionTypeIdMap::getInstance()->toId($type));
82
83 if($this->customWaterColor !== null){
84 $nbt->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($this->customWaterColor->toARGB()));
85 }
86 }
87
88 public function readSaveData(CompoundTag $nbt) : void{
89 $containerType = $nbt->getShort(self::TAG_POTION_CONTAINER_TYPE, self::POTION_CONTAINER_TYPE_NONE);
90 $potionId = $nbt->getShort(self::TAG_POTION_ID, self::POTION_ID_NONE);
91 if($containerType !== self::POTION_CONTAINER_TYPE_NONE && $potionId !== self::POTION_ID_NONE){
92 $potionType = PotionTypeIdMap::getInstance()->fromId($potionId);
93 if($potionType === null){
94 throw new SavedDataLoadingException("Unknown potion type ID $potionId");
95 }
96 $this->potionItem = match($containerType){
97 self::POTION_CONTAINER_TYPE_NORMAL => VanillaItems::POTION()->setType($potionType),
98 self::POTION_CONTAINER_TYPE_SPLASH => VanillaItems::SPLASH_POTION()->setType($potionType),
99 self::POTION_CONTAINER_TYPE_LINGERING => throw new SavedDataLoadingException("Not implemented"),
100 default => throw new SavedDataLoadingException("Invalid potion container type ID $containerType")
101 };
102 }else{
103 $this->potionItem = null;
104 }
105
106 $this->customWaterColor = ($customColorTag = $nbt->getTag(self::TAG_CUSTOM_COLOR)) instanceof IntTag ? Color::fromARGB(Binary::unsignInt($customColorTag->getValue())) : null;
107 }
108
109 protected function writeSaveData(CompoundTag $nbt) : void{
110 $nbt->setShort(self::TAG_POTION_CONTAINER_TYPE, match($this->potionItem?->getTypeId()){
111 ItemTypeIds::POTION => self::POTION_CONTAINER_TYPE_NORMAL,
112 ItemTypeIds::SPLASH_POTION => self::POTION_CONTAINER_TYPE_SPLASH,
113 ItemTypeIds::LINGERING_POTION => self::POTION_CONTAINER_TYPE_LINGERING,
114 null => self::POTION_CONTAINER_TYPE_NONE,
115 default => throw new AssumptionFailedError("Unexpected potion item type")
116 });
117
118 //TODO: lingering potion
119 $type = $this->potionItem instanceof Potion || $this->potionItem instanceof SplashPotion ? $this->potionItem->getType() : null;
120 $nbt->setShort(self::TAG_POTION_ID, $type === null ? self::POTION_ID_NONE : PotionTypeIdMap::getInstance()->toId($type));
121
122 if($this->customWaterColor !== null){
123 $nbt->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($this->customWaterColor->toARGB()));
124 }
125 }
126
127 public function getRenderUpdateBugWorkaroundStateProperties(Block $block) : array{
128 if($block instanceof FillableCauldron){
129 $realFillLevel = $block->getFillLevel();
130 return [BlockStateNames::FILL_LEVEL => new IntTag($realFillLevel === FillableCauldron::MAX_FILL_LEVEL ? FillableCauldron::MIN_FILL_LEVEL : $realFillLevel + 1)];
131 }
132
133 return [];
134 }
135}
getRenderUpdateBugWorkaroundStateProperties(Block $block)
addAdditionalSpawnData(CompoundTag $nbt)
static fromARGB(int $code)
Definition: Color.php:101
setInt(string $name, int $value)
setShort(string $name, int $value)