PocketMine-MP 5.41.2 git-32e7cb3539fcad5780de7092eb6b02f074516e74
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 $item->pop();
73 return true;
74 }
75
76 if($this->charges > self::MIN_CHARGES){
77 if($player === null){
78 return false;
79 }
80
81 $ev = new PlayerRespawnAnchorUseEvent($player, $this, PlayerRespawnAnchorUseEvent::ACTION_EXPLODE);
82 $ev->call();
83 if($ev->isCancelled()){
84 return false;
85 }
86
87 switch($ev->getAction()){
88 case PlayerRespawnAnchorUseEvent::ACTION_EXPLODE:
89 $this->explode($player);
90 return true;
91
92 case PlayerRespawnAnchorUseEvent::ACTION_SET_SPAWN:
93 if($player->getSpawn() !== null && $player->getSpawn()->equals($this->position)){
94 return true;
95 }
96
97 $player->setSpawn($this->position);
98 $this->position->getWorld()->addSound($this->position, new RespawnAnchorSetSpawnSound());
99 $player->sendMessage(KnownTranslationFactory::tile_respawn_anchor_respawnSet()->prefix(TextFormat::GRAY));
100 return true;
101 }
102 }
103 return false;
104 }
105
106 private function explode(?Player $player) : void{
107 $ev = new BlockPreExplodeEvent($this, 5, $player);
108 $ev->setIncendiary(true);
109
110 $ev->call();
111 if($ev->isCancelled()){
112 return;
113 }
114
115 $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR());
116
117 $explosion = new Explosion(Position::fromObject($this->position->add(0.5, 0.5, 0.5), $this->position->getWorld()), $ev->getRadius(), $this);
118 $explosion->setFireChance($ev->getFireChance());
119
120 if($ev->isBlockBreaking()){
121 $explosion->explodeA();
122 }
123 $explosion->explodeB();
124 }
125}
describeBlockOnlyState(RuntimeDataDescriber $w)
onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition Item.php:431