PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(2.0, 2.0); }
56
57 protected function getInitialDragMultiplier() : float{ return 1.0; }
58
59 protected function getInitialGravity() : float{ return 0.0; }
60
61 public function isFireProof() : bool{
62 return true;
63 }
64
65 public function getPickedItem() : ?Item{
66 return VanillaItems::END_CRYSTAL();
67 }
68
69 public function showBase() : bool{
70 return $this->showBase;
71 }
72
73 public function setShowBase(bool $showBase) : void{
74 $this->showBase = $showBase;
75 $this->networkPropertiesDirty = true;
76 }
77
78 public function getBeamTarget() : ?Vector3{
79 return $this->beamTarget;
80 }
81
82 public function setBeamTarget(?Vector3 $beamTarget) : void{
83 $this->beamTarget = $beamTarget;
84 $this->networkPropertiesDirty = true;
85 }
86
87 public function attack(EntityDamageEvent $source) : void{
88 parent::attack($source);
89 if(
90 $source->getCause() !== EntityDamageEvent::CAUSE_VOID &&
91 !$this->isFlaggedForDespawn() &&
92 !$source->isCancelled()
93 ){
94 $this->flagForDespawn();
95 $this->explode();
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 public function explode() : void{
129 $ev = new EntityPreExplodeEvent($this, 6);
130 $ev->call();
131 if(!$ev->isCancelled()){
132 $explosion = new Explosion($this->getPosition(), $ev->getRadius(), $this);
133 if($ev->isBlockBreaking()){
134 $explosion->explodeA();
135 }
136 $explosion->explodeB();
137 }
138 }
139
140 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
141 parent::syncNetworkData($properties);
142
143 $properties->setGenericFlag(EntityMetadataFlags::SHOWBASE, $this->showBase);
144 $properties->setBlockPos(EntityMetadataProperties::BLOCK_TARGET, BlockPosition::fromVector3($this->beamTarget ?? Vector3::zero()));
145 }
146}