PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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 $type = $this->potionItem instanceof Potion || $this->potionItem instanceof SplashPotion ? $this->potionItem->getType() : null;
80 $nbt->setShort(self::TAG_POTION_ID, $type === null ? self::POTION_ID_NONE : PotionTypeIdMap::getInstance()->toId($type));
81
82 if($this->customWaterColor !== null){
83 $nbt->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($this->customWaterColor->toARGB()));
84 }
85 }
86
87 public function readSaveData(CompoundTag $nbt) : void{
88 $containerType = $nbt->getShort(self::TAG_POTION_CONTAINER_TYPE, self::POTION_CONTAINER_TYPE_NONE);
89 $potionId = $nbt->getShort(self::TAG_POTION_ID, self::POTION_ID_NONE);
90 if($containerType !== self::POTION_CONTAINER_TYPE_NONE && $potionId !== self::POTION_ID_NONE){
91 $potionType = PotionTypeIdMap::getInstance()->fromId($potionId);
92 if($potionType === null){
93 throw new SavedDataLoadingException("Unknown potion type ID $potionId");
94 }
95 $this->potionItem = match($containerType){
96 self::POTION_CONTAINER_TYPE_NORMAL => VanillaItems::POTION()->setType($potionType),
97 self::POTION_CONTAINER_TYPE_SPLASH => VanillaItems::SPLASH_POTION()->setType($potionType),
98 self::POTION_CONTAINER_TYPE_LINGERING => VanillaItems::LINGERING_POTION()->setType($potionType),
99 default => throw new SavedDataLoadingException("Invalid potion container type ID $containerType")
100 };
101 }else{
102 $this->potionItem = null;
103 }
104
105 $this->customWaterColor = ($customColorTag = $nbt->getTag(self::TAG_CUSTOM_COLOR)) instanceof IntTag ? Color::fromARGB(Binary::unsignInt($customColorTag->getValue())) : null;
106 }
107
108 protected function writeSaveData(CompoundTag $nbt) : void{
109 $nbt->setShort(self::TAG_POTION_CONTAINER_TYPE, match($this->potionItem?->getTypeId()){
110 ItemTypeIds::POTION => self::POTION_CONTAINER_TYPE_NORMAL,
111 ItemTypeIds::SPLASH_POTION => self::POTION_CONTAINER_TYPE_SPLASH,
112 ItemTypeIds::LINGERING_POTION => self::POTION_CONTAINER_TYPE_LINGERING,
113 null => self::POTION_CONTAINER_TYPE_NONE,
114 default => throw new AssumptionFailedError("Unexpected potion item type")
115 });
116
117 $type = $this->potionItem instanceof Potion || $this->potionItem instanceof SplashPotion ? $this->potionItem->getType() : null;
118 $nbt->setShort(self::TAG_POTION_ID, $type === null ? self::POTION_ID_NONE : PotionTypeIdMap::getInstance()->toId($type));
119
120 if($this->customWaterColor !== null){
121 $nbt->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($this->customWaterColor->toARGB()));
122 }
123 }
124
125 public function getRenderUpdateBugWorkaroundStateProperties(Block $block) : array{
126 if($block instanceof FillableCauldron){
127 $realFillLevel = $block->getFillLevel();
128 return [BlockStateNames::FILL_LEVEL => new IntTag($realFillLevel === FillableCauldron::MAX_FILL_LEVEL ? FillableCauldron::MIN_FILL_LEVEL : $realFillLevel + 1)];
129 }
130
131 return [];
132 }
133}
getRenderUpdateBugWorkaroundStateProperties(Block $block)
addAdditionalSpawnData(CompoundTag $nbt)
setInt(string $name, int $value)
setShort(string $name, int $value)