PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
tile/BrewingStand.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
41use function array_map;
42use function count;
43
44class BrewingStand extends Spawnable implements ContainerTile, Nameable{
45 use NameableTrait {
46 addAdditionalSpawnData as addNameSpawnData;
47 }
49
50 public const BREW_TIME_TICKS = 400; // Brew time in ticks
51
52 private const TAG_BREW_TIME = "BrewTime"; //TAG_Short
53 private const TAG_BREW_TIME_PE = "CookTime"; //TAG_Short
54 private const TAG_MAX_FUEL_TIME = "FuelTotal"; //TAG_Short
55 private const TAG_REMAINING_FUEL_TIME = "Fuel"; //TAG_Byte
56 private const TAG_REMAINING_FUEL_TIME_PE = "FuelAmount"; //TAG_Short
57
58 private Inventory $inventory;
59
60 private int $brewTime = 0;
61 private int $maxFuelTime = 0;
62 private int $remainingFuelTime = 0;
63
64 public function __construct(World $world, Vector3 $pos){
65 parent::__construct($world, $pos);
66 $this->inventory = new SimpleInventory(5);
67 $this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange(static function(Inventory $unused) use ($world, $pos) : void{
68 $world->scheduleDelayedBlockUpdate($pos, 1);
69 }));
70 }
71
72 public function readSaveData(CompoundTag $nbt) : void{
73 $this->loadName($nbt);
74 $this->loadItems($nbt);
75
76 $this->brewTime = $nbt->getShort(self::TAG_BREW_TIME, $nbt->getShort(self::TAG_BREW_TIME_PE, 0));
77 $this->maxFuelTime = $nbt->getShort(self::TAG_MAX_FUEL_TIME, 0);
78 $this->remainingFuelTime = $nbt->getByte(self::TAG_REMAINING_FUEL_TIME, $nbt->getShort(self::TAG_REMAINING_FUEL_TIME_PE, 0));
79 if($this->maxFuelTime === 0){
80 $this->maxFuelTime = $this->remainingFuelTime;
81 }
82 if($this->remainingFuelTime === 0){
83 $this->maxFuelTime = $this->remainingFuelTime = $this->brewTime = 0;
84 }
85 }
86
87 protected function writeSaveData(CompoundTag $nbt) : void{
88 $this->saveName($nbt);
89 $this->saveItems($nbt);
90
91 $nbt->setShort(self::TAG_BREW_TIME_PE, $this->brewTime);
92 $nbt->setShort(self::TAG_MAX_FUEL_TIME, $this->maxFuelTime);
93 $nbt->setShort(self::TAG_REMAINING_FUEL_TIME_PE, $this->remainingFuelTime);
94 }
95
96 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
97 $this->addNameSpawnData($nbt);
98
99 $nbt->setShort(self::TAG_BREW_TIME_PE, $this->brewTime);
100 $nbt->setShort(self::TAG_MAX_FUEL_TIME, $this->maxFuelTime);
101 $nbt->setShort(self::TAG_REMAINING_FUEL_TIME_PE, $this->remainingFuelTime);
102 }
103
104 public function getDefaultName() : string{
105 return "Brewing Stand";
106 }
107
108 public function close() : void{
109 if(!$this->closed){
110 $this->inventory->removeAllWindows();
111
112 parent::close();
113 }
114 }
115
116 public function getInventory() : Inventory{
117 return $this->inventory;
118 }
119
120 public function getRealInventory() : Inventory{
121 return $this->inventory;
122 }
123
124 private function checkFuel(Item $item) : void{
125 $ev = new BrewingFuelUseEvent($this);
126 if(!$item->equals(VanillaItems::BLAZE_POWDER(), true, false)){
127 $ev->cancel();
128 }
129
130 $ev->call();
131 if($ev->isCancelled()){
132 return;
133 }
134
135 $item->pop();
136 $this->inventory->setItem(BrewingStandInventoryWindow::SLOT_FUEL, $item);
137
138 $this->maxFuelTime = $this->remainingFuelTime = $ev->getFuelTime();
139 }
140
145 private function getBrewableRecipes() : array{
146 $ingredient = $this->inventory->getItem(BrewingStandInventoryWindow::SLOT_INGREDIENT);
147 if($ingredient->isNull()){
148 return [];
149 }
150
151 $recipes = [];
152 $craftingManager = $this->position->getWorld()->getServer()->getCraftingManager();
153 foreach([BrewingStandInventoryWindow::SLOT_BOTTLE_LEFT, BrewingStandInventoryWindow::SLOT_BOTTLE_MIDDLE, BrewingStandInventoryWindow::SLOT_BOTTLE_RIGHT] as $slot){
154 $input = $this->inventory->getItem($slot);
155 if($input->isNull()){
156 continue;
157 }
158
159 if(($recipe = $craftingManager->matchBrewingRecipe($input, $ingredient)) !== null){
160 $recipes[$slot] = $recipe;
161 }
162 }
163
164 return $recipes;
165 }
166
167 public function onUpdate() : bool{
168 if($this->closed){
169 return false;
170 }
171
172 $this->timings->startTiming();
173
174 $prevBrewTime = $this->brewTime;
175 $prevRemainingFuelTime = $this->remainingFuelTime;
176 $prevMaxFuelTime = $this->maxFuelTime;
177
178 $ret = false;
179
180 $fuel = $this->inventory->getItem(BrewingStandInventoryWindow::SLOT_FUEL);
181 $ingredient = $this->inventory->getItem(BrewingStandInventoryWindow::SLOT_INGREDIENT);
182
183 $recipes = $this->getBrewableRecipes();
184 $canBrew = count($recipes) !== 0;
185
186 if($this->remainingFuelTime <= 0 && $canBrew){
187 $this->checkFuel($fuel);
188 }
189
190 if($this->remainingFuelTime > 0){
191 if($canBrew){
192 if($this->brewTime === 0){
193 $this->brewTime = self::BREW_TIME_TICKS;
194 --$this->remainingFuelTime;
195 }
196
197 --$this->brewTime;
198
199 if($this->brewTime <= 0){
200 $anythingBrewed = false;
201 foreach($recipes as $slot => $recipe){
202 $input = $this->inventory->getItem($slot);
203 $output = $recipe->getResultFor($input);
204 if($output === null){
205 continue;
206 }
207
208 $ev = new BrewItemEvent($this, $slot, $input, $output, $recipe);
209 $ev->call();
210 if($ev->isCancelled()){
211 continue;
212 }
213
214 $this->inventory->setItem($slot, $ev->getResult());
215 $anythingBrewed = true;
216 }
217
218 if($anythingBrewed){
219 $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new PotionFinishBrewingSound());
220 }
221
222 $ingredient->pop();
223 $this->inventory->setItem(BrewingStandInventoryWindow::SLOT_INGREDIENT, $ingredient);
224
225 $this->brewTime = 0;
226 }else{
227 $ret = true;
228 }
229 }else{
230 $this->brewTime = 0;
231 }
232 }else{
233 $this->brewTime = $this->remainingFuelTime = $this->maxFuelTime = 0;
234 }
235
236 $viewers = array_map(fn(Player $p) => $p->getNetworkSession()->getInvManager(), $this->inventory->getViewers());
237 foreach($viewers as $v){
238 if($v === null){
239 continue;
240 }
241 if($prevBrewTime !== $this->brewTime){
242 $v->syncData($this->inventory, ContainerSetDataPacket::PROPERTY_BREWING_STAND_BREW_TIME, $this->brewTime);
243 }
244 if($prevRemainingFuelTime !== $this->remainingFuelTime){
245 $v->syncData($this->inventory, ContainerSetDataPacket::PROPERTY_BREWING_STAND_FUEL_AMOUNT, $this->remainingFuelTime);
246 }
247 if($prevMaxFuelTime !== $this->maxFuelTime){
248 $v->syncData($this->inventory, ContainerSetDataPacket::PROPERTY_BREWING_STAND_FUEL_TOTAL, $this->maxFuelTime);
249 }
250 }
251
252 $this->timings->stopTiming();
253
254 return $ret;
255 }
256}
setShort(string $name, int $value)
scheduleDelayedBlockUpdate(Vector3 $pos, int $delay)
Definition World.php:1467