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