49 public const BREW_TIME_TICKS = 400;
51 private const TAG_BREW_TIME =
"BrewTime";
52 private const TAG_BREW_TIME_PE =
"CookTime";
53 private const TAG_MAX_FUEL_TIME =
"FuelTotal";
54 private const TAG_REMAINING_FUEL_TIME =
"Fuel";
55 private const TAG_REMAINING_FUEL_TIME_PE =
"FuelAmount";
59 private int $brewTime = 0;
60 private int $maxFuelTime = 0;
61 private int $remainingFuelTime = 0;
64 parent::__construct($world, $pos);
66 $this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange(
static function(
Inventory $unused) use ($world, $pos) :
void{
71 public function readSaveData(
CompoundTag $nbt) :
void{
72 $this->loadName($nbt);
73 $this->loadItems($nbt);
75 $this->brewTime = $nbt->getShort(self::TAG_BREW_TIME, $nbt->getShort(self::TAG_BREW_TIME_PE, 0));
76 $this->maxFuelTime = $nbt->getShort(self::TAG_MAX_FUEL_TIME, 0);
77 $this->remainingFuelTime = $nbt->getByte(self::TAG_REMAINING_FUEL_TIME, $nbt->getShort(self::TAG_REMAINING_FUEL_TIME_PE, 0));
78 if($this->maxFuelTime === 0){
79 $this->maxFuelTime = $this->remainingFuelTime;
81 if($this->remainingFuelTime === 0){
82 $this->maxFuelTime = $this->remainingFuelTime = $this->brewTime = 0;
87 $this->saveName($nbt);
88 $this->saveItems($nbt);
90 $nbt->
setShort(self::TAG_BREW_TIME_PE, $this->brewTime);
91 $nbt->
setShort(self::TAG_MAX_FUEL_TIME, $this->maxFuelTime);
92 $nbt->
setShort(self::TAG_REMAINING_FUEL_TIME_PE, $this->remainingFuelTime);
96 $this->addNameSpawnData($nbt);
98 $nbt->
setShort(self::TAG_BREW_TIME_PE, $this->brewTime);
99 $nbt->
setShort(self::TAG_MAX_FUEL_TIME, $this->maxFuelTime);
100 $nbt->
setShort(self::TAG_REMAINING_FUEL_TIME_PE, $this->remainingFuelTime);
103 public function getDefaultName() : string{
104 return
"Brewing Stand";
107 public function close() : void{
109 $this->inventory->removeAllViewers();
115 public function getInventory() : BrewingStandInventory{
116 return $this->inventory;
119 public function getRealInventory() : BrewingStandInventory{
120 return $this->inventory;
123 private function checkFuel(Item $item) : void{
124 $ev = new BrewingFuelUseEvent($this);
125 if(!$item->equals(VanillaItems::BLAZE_POWDER(),
true,
false)){
130 if($ev->isCancelled()){
135 $this->inventory->setItem(BrewingStandInventory::SLOT_FUEL, $item);
137 $this->maxFuelTime = $this->remainingFuelTime = $ev->getFuelTime();
144 private function getBrewableRecipes() : array{
145 $ingredient = $this->inventory->getItem(BrewingStandInventory::SLOT_INGREDIENT);
146 if($ingredient->isNull()){
151 $craftingManager = $this->position->getWorld()->getServer()->getCraftingManager();
152 foreach([BrewingStandInventory::SLOT_BOTTLE_LEFT, BrewingStandInventory::SLOT_BOTTLE_MIDDLE, BrewingStandInventory::SLOT_BOTTLE_RIGHT] as $slot){
153 $input = $this->inventory->getItem($slot);
154 if($input->isNull()){
158 if(($recipe = $craftingManager->matchBrewingRecipe($input, $ingredient)) !==
null){
159 $recipes[$slot] = $recipe;
166 public function onUpdate() : bool{
171 $this->timings->startTiming();
173 $prevBrewTime = $this->brewTime;
174 $prevRemainingFuelTime = $this->remainingFuelTime;
175 $prevMaxFuelTime = $this->maxFuelTime;
179 $fuel = $this->inventory->getItem(BrewingStandInventory::SLOT_FUEL);
180 $ingredient = $this->inventory->getItem(BrewingStandInventory::SLOT_INGREDIENT);
182 $recipes = $this->getBrewableRecipes();
183 $canBrew = count($recipes) !== 0;
185 if($this->remainingFuelTime <= 0 && $canBrew){
186 $this->checkFuel($fuel);
189 if($this->remainingFuelTime > 0){
191 if($this->brewTime === 0){
192 $this->brewTime = self::BREW_TIME_TICKS;
193 --$this->remainingFuelTime;
198 if($this->brewTime <= 0){
199 $anythingBrewed =
false;
200 foreach($recipes as $slot => $recipe){
201 $input = $this->inventory->getItem($slot);
202 $output = $recipe->getResultFor($input);
203 if($output ===
null){
207 $ev =
new BrewItemEvent($this, $slot, $input, $output, $recipe);
209 if($ev->isCancelled()){
213 $this->inventory->setItem($slot, $ev->getResult());
214 $anythingBrewed =
true;
218 $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5),
new PotionFinishBrewingSound());
222 $this->inventory->setItem(BrewingStandInventory::SLOT_INGREDIENT, $ingredient);
232 $this->brewTime = $this->remainingFuelTime = $this->maxFuelTime = 0;
235 $viewers = array_map(fn(Player $p) => $p->getNetworkSession()->getInvManager(), $this->inventory->getViewers());
236 foreach($viewers as $v){
240 if($prevBrewTime !== $this->brewTime){
241 $v->syncData($this->inventory, ContainerSetDataPacket::PROPERTY_BREWING_STAND_BREW_TIME, $this->brewTime);
243 if($prevRemainingFuelTime !== $this->remainingFuelTime){
244 $v->syncData($this->inventory, ContainerSetDataPacket::PROPERTY_BREWING_STAND_FUEL_AMOUNT, $this->remainingFuelTime);
246 if($prevMaxFuelTime !== $this->maxFuelTime){
247 $v->syncData($this->inventory, ContainerSetDataPacket::PROPERTY_BREWING_STAND_FUEL_TOTAL, $this->maxFuelTime);
251 $this->timings->stopTiming();