PocketMine-MP 5.41.2 git-e73f84662e3363c758c5cd2380d3130518867133
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 $item->pop();
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}
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockOnlyState(RuntimeDataDescriber $w)
pop(int $count=1)
Definition Item.php:427