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