PocketMine-MP 5.28.3 git-d5a1007c80fcee27feb2251cf5dcf1ad5a59a85c
Loading...
Searching...
No Matches
RespawnAnchor.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
39
40final class RespawnAnchor extends Opaque{
41 private const MIN_CHARGES = 0;
42 private const MAX_CHARGES = 4;
43
44 private int $charges = self::MIN_CHARGES;
45
46 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
47 $w->boundedIntAuto(self::MIN_CHARGES, self::MAX_CHARGES, $this->charges);
48 }
49
50 public function getCharges() : int{
51 return $this->charges;
52 }
53
55 public function setCharges(int $charges) : self{
56 if($charges < self::MIN_CHARGES || $charges > self::MAX_CHARGES){
57 throw new \InvalidArgumentException("Charges must be between " . self::MIN_CHARGES . " and " . self::MAX_CHARGES . ", given: $charges");
58 }
59 $this->charges = $charges;
60 return $this;
61 }
62
63 public function getLightLevel() : int{
64 return $this->charges > 0 ? ($this->charges * 4) - 1 : 0;
65 }
66
67 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
68 if($item->getTypeId() === ItemTypeIds::fromBlockTypeId(BlockTypeIds::GLOWSTONE) && $this->charges < self::MAX_CHARGES){
69 $this->position->getWorld()->setBlock($this->position, $this->setCharges($this->charges + 1));
70 $this->position->getWorld()->addSound($this->position, new RespawnAnchorChargeSound());
71 return true;
72 }
73
74 if($this->charges > self::MIN_CHARGES){
75 if($player === null){
76 return false;
77 }
78
79 $ev = new PlayerRespawnAnchorUseEvent($player, $this, PlayerRespawnAnchorUseEvent::ACTION_EXPLODE);
80 $ev->call();
81 if($ev->isCancelled()){
82 return false;
83 }
84
85 switch($ev->getAction()){
86 case PlayerRespawnAnchorUseEvent::ACTION_EXPLODE:
87 $this->explode($player);
88 return false;
89
90 case PlayerRespawnAnchorUseEvent::ACTION_SET_SPAWN:
91 if($player->getSpawn() !== null && $player->getSpawn()->equals($this->position)){
92 return true;
93 }
94
95 $player->setSpawn($this->position);
96 $this->position->getWorld()->addSound($this->position, new RespawnAnchorSetSpawnSound());
97 $player->sendMessage(KnownTranslationFactory::tile_respawn_anchor_respawnSet()->prefix(TextFormat::GRAY));
98 return true;
99 }
100 }
101 return false;
102 }
103
104 private function explode(?Player $player) : void{
105 $ev = new BlockPreExplodeEvent($this, 5, $player);
106 $ev->setIncendiary(true);
107
108 $ev->call();
109 if($ev->isCancelled()){
110 return;
111 }
112
113 $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR());
114
115 $explosion = new Explosion(Position::fromObject($this->position->add(0.5, 0.5, 0.5), $this->position->getWorld()), $ev->getRadius(), $this);
116 $explosion->setFireChance($ev->getFireChance());
117
118 if($ev->isBlockBreaking()){
119 $explosion->explodeA();
120 }
121 $explosion->explodeB();
122 }
123}
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockOnlyState(RuntimeDataDescriber $w)