PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
PrimedTNT.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
41
42class PrimedTNT extends Entity implements Explosive{
43
44 private const TAG_FUSE = "Fuse"; //TAG_Short
45
46 public function getNetworkTypeId() : string{ return EntityIds::TNT; }
47
48 protected int $fuse;
49 protected bool $worksUnderwater = false;
50
51 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.98, 0.98); }
52
53 protected function getInitialDragMultiplier() : float{ return 0.02; }
54
55 protected function getInitialGravity() : float{ return 0.04; }
56
57 public function getFuse() : int{
58 return $this->fuse;
59 }
60
61 public function setFuse(int $fuse) : void{
62 if($fuse < 0 || $fuse > 32767){
63 throw new \InvalidArgumentException("Fuse must be in the range 0-32767");
64 }
65 $this->fuse = $fuse;
66 $this->networkPropertiesDirty = true;
67 }
68
69 public function worksUnderwater() : bool{ return $this->worksUnderwater; }
70
71 public function setWorksUnderwater(bool $worksUnderwater) : void{
72 $this->worksUnderwater = $worksUnderwater;
73 $this->networkPropertiesDirty = true;
74 }
75
76 public function attack(EntityDamageEvent $source) : void{
77 if($source->getCause() === EntityDamageEvent::CAUSE_VOID){
78 parent::attack($source);
79 }
80 }
81
82 protected function initEntity(CompoundTag $nbt) : void{
83 parent::initEntity($nbt);
84
85 $this->fuse = $nbt->getShort(self::TAG_FUSE, 80);
86 }
87
88 public function canCollideWith(Entity $entity) : bool{
89 return false;
90 }
91
92 public function saveNBT() : CompoundTag{
93 $nbt = parent::saveNBT();
94 $nbt->setShort(self::TAG_FUSE, $this->fuse);
95
96 return $nbt;
97 }
98
99 protected function entityBaseTick(int $tickDiff = 1) : bool{
100 if($this->closed){
101 return false;
102 }
103
104 $hasUpdate = parent::entityBaseTick($tickDiff);
105
106 if(!$this->isFlaggedForDespawn()){
107 $this->fuse -= $tickDiff;
108 $this->networkPropertiesDirty = true;
109
110 if($this->fuse <= 0){
111 $this->flagForDespawn();
112 $this->explode();
113 }
114 }
115
116 return $hasUpdate || $this->fuse >= 0;
117 }
118
119 public function explode() : void{
120 $ev = new EntityPreExplodeEvent($this, 4);
121 $ev->call();
122 if(!$ev->isCancelled()){
123 //TODO: deal with underwater TNT (underwater TNT treats water as if it has a blast resistance of 0)
124 $explosion = new Explosion(Position::fromObject($this->location->add(0, $this->size->getHeight() / 2, 0), $this->getWorld()), $ev->getRadius(), $this);
125 if($ev->isBlockBreaking()){
126 $explosion->explodeA();
127 }
128 $explosion->explodeB();
129 }
130 }
131
132 public function getPickedItem() : ?Item{
133 return VanillaBlocks::TNT()->setWorksUnderwater($this->worksUnderwater)->asItem();
134 }
135
136 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
137 parent::syncNetworkData($properties);
138
139 $properties->setGenericFlag(EntityMetadataFlags::IGNITED, true);
140 $properties->setInt(EntityMetadataProperties::VARIANT, $this->worksUnderwater ? 1 : 0);
141 $properties->setInt(EntityMetadataProperties::FUSE_LENGTH, $this->fuse);
142 }
143
144 public function getOffsetPosition(Vector3 $vector3) : Vector3{
145 return $vector3->add(0, 0.49, 0);
146 }
147}