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