PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
PotionCauldron.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;
25
26use pocketmine\block\tile\Cauldron as TileCauldron;
35use function assert;
36
37final class PotionCauldron extends FillableCauldron{
38 public const POTION_FILL_AMOUNT = 2;
39
40 private ?Item $potionItem = null;
41
42 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo){
43 parent::__construct($idInfo, $name, $typeInfo);
44 }
45
46 public function readStateFromWorld() : Block{
47 parent::readStateFromWorld();
48 $tile = $this->position->getWorld()->getTile($this->position);
49 $this->potionItem = $tile instanceof TileCauldron ? $tile->getPotionItem() : null;
50
51 return $this;
52 }
53
54 public function writeStateToWorld() : void{
55 parent::writeStateToWorld();
56 $tile = $this->position->getWorld()->getTile($this->position);
57 assert($tile instanceof TileCauldron);
58 $tile->setCustomWaterColor(null);
59 $tile->setPotionItem($this->potionItem);
60 }
61
62 public function getPotionItem() : ?Item{ return $this->potionItem === null ? null : clone $this->potionItem; }
63
65 public function setPotionItem(?Item $potionItem) : self{
66 if($potionItem !== null && !match($potionItem->getTypeId()){
67 ItemTypeIds::POTION,
68 ItemTypeIds::SPLASH_POTION,
69 ItemTypeIds::LINGERING_POTION => true,
70 default => false,
71 }){
72 throw new \InvalidArgumentException("Item must be a POTION, SPLASH_POTION or LINGERING_POTION");
73 }
74 $this->potionItem = $potionItem !== null ? (clone $potionItem)->setCount(1) : null;
75 return $this;
76 }
77
78 public function getFillSound() : Sound{
79 return new CauldronFillPotionSound();
80 }
81
82 public function getEmptySound() : Sound{
83 return new CauldronEmptyPotionSound();
84 }
85
89 protected function addFillLevelsOrMix(int $amount, Item $usedItem, Item $returnedItem, array &$returnedItems) : void{
90 if($this->potionItem !== null && !$usedItem->equals($this->potionItem, true, false)){
91 $this->mix($usedItem, $returnedItem, $returnedItems);
92 }else{
93 $this->addFillLevels($amount, $usedItem, $returnedItem, $returnedItems);
94 }
95 }
96
97 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
98 match($item->getTypeId()){
99 ItemTypeIds::LINGERING_POTION, ItemTypeIds::POTION, ItemTypeIds::SPLASH_POTION => $this->addFillLevelsOrMix(self::POTION_FILL_AMOUNT, $item, VanillaItems::GLASS_BOTTLE(), $returnedItems),
100 ItemTypeIds::GLASS_BOTTLE => $this->potionItem === null ? null : $this->removeFillLevels(self::POTION_FILL_AMOUNT, $item, clone $this->potionItem, $returnedItems),
101 ItemTypeIds::LAVA_BUCKET, ItemTypeIds::POWDER_SNOW_BUCKET, ItemTypeIds::WATER_BUCKET => $this->mix($item, VanillaItems::BUCKET(), $returnedItems),
102 //TODO: tipped arrows
103 default => null
104 };
105 return true;
106 }
107
108 public function onNearbyBlockChange() : void{
109 $world = $this->position->getWorld();
110 if($world->getBlock($this->position->up())->getTypeId() === BlockTypeIds::WATER){
111 $cauldron = VanillaBlocks::WATER_CAULDRON()->setFillLevel(FillableCauldron::MAX_FILL_LEVEL);
112 $world->setBlock($this->position, $cauldron);
113 $world->addSound($this->position->add(0.5, 0.5, 0.5), $cauldron->getFillSound());
114 }
115 }
116}
setPotionItem(?Item $potionItem)
addFillLevelsOrMix(int $amount, Item $usedItem, Item $returnedItem, array &$returnedItems)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
__construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo)