PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
entity/object/EndCrystal.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\entity\object;
25
42
43class EndCrystal extends Entity implements Explosive{
44 private const TAG_SHOWBASE = "ShowBottom"; //TAG_Byte
45
46 private const TAG_BLOCKTARGET_X = "BlockTargetX"; //TAG_Int
47 private const TAG_BLOCKTARGET_Y = "BlockTargetY"; //TAG_Int
48 private const TAG_BLOCKTARGET_Z = "BlockTargetZ"; //TAG_Int
49
50 public function getNetworkTypeId() : string{ return EntityIds::ENDER_CRYSTAL; }
51
52 protected bool $showBase = false;
53 protected ?Vector3 $beamTarget = null;
54
55 private bool $primed = false;
56
57 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(2.0, 2.0); }
58
59 protected function getInitialDragMultiplier() : float{ return 1.0; }
60
61 protected function getInitialGravity() : float{ return 0.0; }
62
63 public function isFireProof() : bool{
64 return true;
65 }
66
67 public function getPickedItem() : ?Item{
68 return VanillaItems::END_CRYSTAL();
69 }
70
71 public function showBase() : bool{
72 return $this->showBase;
73 }
74
75 public function setShowBase(bool $showBase) : void{
76 $this->showBase = $showBase;
77 $this->networkPropertiesDirty = true;
78 }
79
80 public function getBeamTarget() : ?Vector3{
81 return $this->beamTarget;
82 }
83
84 public function setBeamTarget(?Vector3 $beamTarget) : void{
85 $this->beamTarget = $beamTarget;
86 $this->networkPropertiesDirty = true;
87 }
88
89 public function attack(EntityDamageEvent $source) : void{
90 parent::attack($source);
91 if(
92 $source->getCause() !== EntityDamageEvent::CAUSE_VOID &&
93 !$source->isCancelled()
94 ){
95 $this->primed = true;
96 }
97 }
98
99 protected function initEntity(CompoundTag $nbt) : void{
100 parent::initEntity($nbt);
101
102 $this->setMaxHealth(1);
103 $this->setHealth(1);
104
105 $this->setShowBase($nbt->getByte(self::TAG_SHOWBASE, 0) === 1);
106
107 if(
108 ($beamXTag = $nbt->getTag(self::TAG_BLOCKTARGET_X)) instanceof IntTag &&
109 ($beamYTag = $nbt->getTag(self::TAG_BLOCKTARGET_Y)) instanceof IntTag &&
110 ($beamZTag = $nbt->getTag(self::TAG_BLOCKTARGET_Z)) instanceof IntTag
111 ){
112 $this->setBeamTarget(new Vector3($beamXTag->getValue(), $beamYTag->getValue(), $beamZTag->getValue()));
113 }
114 }
115
116 public function saveNBT() : CompoundTag{
117 $nbt = parent::saveNBT();
118
119 $nbt->setByte(self::TAG_SHOWBASE, $this->showBase ? 1 : 0);
120 if($this->beamTarget !== null){
121 $nbt->setInt(self::TAG_BLOCKTARGET_X, $this->beamTarget->getFloorX());
122 $nbt->setInt(self::TAG_BLOCKTARGET_Y, $this->beamTarget->getFloorY());
123 $nbt->setInt(self::TAG_BLOCKTARGET_Z, $this->beamTarget->getFloorZ());
124 }
125 return $nbt;
126 }
127
128 protected function onDeathUpdate(int $tickDiff) : bool{
129 if($this->primed){
130 $this->explode();
131 }
132 return true;
133 }
134
135 public function explode() : void{
136 $ev = new EntityPreExplodeEvent($this, 6);
137 $ev->call();
138 if(!$ev->isCancelled()){
139 $explosion = new Explosion($this->getPosition(), $ev->getRadius(), $this, $ev->getFireChance());
140 if($ev->isBlockBreaking()){
141 $explosion->explodeA();
142 }
143 $explosion->explodeB();
144 }
145 }
146
147 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
148 parent::syncNetworkData($properties);
149
150 $properties->setGenericFlag(EntityMetadataFlags::SHOWBASE, $this->showBase);
151 $properties->setBlockPos(EntityMetadataProperties::BLOCK_TARGET, BlockPosition::fromVector3($this->beamTarget ?? Vector3::zero()));
152 }
153}