2222namespace pocketmine \entity ;
2323
2424
25+ use pocketmine \event \entity \EntityDamageEvent ;
26+ use pocketmine \level \Explosion ;
27+ use pocketmine \nbt \tag \Byte ;
2528use pocketmine \nbt \tag \String ;
29+ use pocketmine \network \protocol \AddEntityPacket ;
30+ use pocketmine \network \protocol \SetEntityMotionPacket ;
31+ use pocketmine \Player ;
2632
2733class PrimedTNT extends Entity implements Explosive{
34+ const NETWORK_ID = 65 ;
35+
36+ public $ width = 0.98 ;
37+ public $ length = 0.98 ;
38+ public $ height = 0.98 ;
39+
40+ protected $ gravity = 0.04 ;
41+ protected $ drag = 0.02 ;
42+
43+ protected $ fuse ;
44+
45+ public $ canCollide = false ;
46+
2847 protected function initEntity (){
2948 $ this ->namedtag ->id = new String ("id " , "PrimedTNT " );
49+ if (isset ($ this ->namedtag ->Fuse )){
50+ $ this ->fuse = $ this ->namedtag ["Fuse " ];
51+ }else {
52+ $ this ->fuse = 80 ;
53+ }
54+ }
55+
56+
57+ public function canCollideWith (Entity $ entity ){
58+ return false ;
59+ }
60+
61+ public function getData (){
62+ return [
63+ 16 => ["type " => 0 , "value " => $ this ->fuse ],
64+ ];
65+ }
66+
67+ public function saveNBT (){
68+ parent ::saveNBT ();
69+ $ this ->namedtag ->Fuse = new Byte ("Fuse " , $ this ->fuse );
70+ }
71+
72+ public function onUpdate (){
73+
74+ if ($ this ->closed ){
75+ return false ;
76+ }
77+
78+ $ this ->timings ->startTiming ();
79+
80+ $ this ->entityBaseTick ();
81+
82+ if (!$ this ->dead ){
83+
84+ $ this ->motionY -= $ this ->gravity ;
85+
86+ $ this ->move ($ this ->motionX , $ this ->motionY , $ this ->motionZ );
87+
88+ $ friction = 1 - $ this ->drag ;
89+
90+ $ this ->motionX *= $ friction ;
91+ $ this ->motionY *= $ friction ;
92+ $ this ->motionZ *= $ friction ;
93+
94+ $ this ->updateMovement ();
95+
96+ if ($ this ->onGround ){
97+ $ this ->motionY *= -0.5 ;
98+ $ this ->motionX *= 0.7 ;
99+ $ this ->motionZ *= 0.7 ;
100+ }
101+
102+ if ($ this ->fuse -- <= 0 ){
103+ $ this ->kill ();
104+ $ this ->explode ();
105+ }else {
106+ $ this ->sendMetadata ($ this ->getViewers ());
107+ }
108+
109+ }
110+
111+
112+ return !$ this ->onGround or ($ this ->motionX == 0 and $ this ->motionY == 0 and $ this ->motionZ == 0 );
113+ }
114+
115+ public function attack ($ damage , $ source = EntityDamageEvent::CAUSE_MAGIC ){
116+
117+ }
118+
119+ public function heal ($ amount ){
120+
121+ }
122+
123+ public function explode (){
124+ (new Explosion ($ this , 4 , $ this ))->explode ();
125+ }
126+
127+ public function spawnTo (Player $ player ){
128+ $ pk = new AddEntityPacket ();
129+ $ pk ->type = PrimedTNT::NETWORK_ID ;
130+ $ pk ->eid = $ this ->getID ();
131+ $ pk ->x = $ this ->x ;
132+ $ pk ->y = $ this ->y ;
133+ $ pk ->z = $ this ->z ;
134+ $ pk ->did = 0 ;
135+ $ player ->dataPacket ($ pk );
136+
137+ $ pk = new SetEntityMotionPacket ();
138+ $ pk ->entities = [
139+ [$ this ->getID (), $ this ->motionX , $ this ->motionY , $ this ->motionZ ]
140+ ];
141+ $ player ->dataPacket ($ pk );
142+
143+ parent ::spawnTo ($ player );
30144 }
31145}
0 commit comments