PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
FallingSand.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 
22 namespace pocketmine\entity;
23 
24 
29 use pocketmine\item\Item as ItemItem;
35 
36 class FallingSand extends Entity{
37  const NETWORK_ID = 66;
38 
39  public $width = 0.98;
40  public $length = 0.98;
41  public $height = 0.98;
42 
43  protected $gravity = 0.04;
44  protected $drag = 0.02;
45  protected $blockId = 0;
46  protected $damage;
47 
48  public $canCollide = false;
49 
50  protected function initEntity(){
51  if(isset($this->namedtag->TileID)){
52  $this->blockId = $this->namedtag["TileID"];
53  }elseif(isset($this->namedtag->Tile)){
54  $this->blockId = $this->namedtag["Tile"];
55  $this->namedtag["TileID"] = new Int("TileID", $this->blockId);
56  }
57 
58  if(isset($this->namedtag->Data)){
59  $this->damage = $this->namedtag["Data"];
60  }
61 
62  if($this->blockId === 0){
63  $this->close();
64  }
65  }
66 
67  public function canCollideWith(Entity $entity){
68  return false;
69  }
70 
71  public function getData(){
72  return [];
73  }
74 
75  public function onUpdate($currentTick){
76 
77  if($this->closed){
78  return false;
79  }
80 
81  $this->timings->startTiming();
82 
83  $tickDiff = max(1, $currentTick - $this->lastUpdate);
84  $this->lastUpdate = $currentTick;
85 
86  $hasUpdate = $this->entityBaseTick($tickDiff);
87 
88  if(!$this->dead){
89  if($this->ticksLived === 1){
90  $block = $this->level->getBlock($pos = (new Vector3($this->x, $this->y, $this->z))->floor());
91  if($block->getId() != $this->blockId){
92  $this->kill();
93  return true;
94  }
95  $this->level->setBlock($pos, Block::get(0), true);
96 
97  }
98 
99  $this->motionY -= $this->gravity;
100 
101  $this->move($this->motionX, $this->motionY, $this->motionZ);
102 
103  $friction = 1 - $this->drag;
104 
105  $this->motionX *= $friction;
106  $this->motionY *= 1 - $this->drag;
107  $this->motionZ *= $friction;
108 
109  $pos = (new Vector3($this->x, $this->y, $this->z))->floor();
110 
111  if($this->onGround){
112  $this->kill();
113  $block = $this->level->getBlock($pos);
114  if(!$block->isFullBlock){
115  $this->getLevel()->dropItem($this, ItemItem::get($this->getBlock(), $this->getDamage(), 1));
116  }else{
117  $this->server->getPluginManager()->callEvent($ev = new EntityBlockChangeEvent($this, $block, Block::get($this->getBlock(), $this->getDamage())));
118  if(!$ev->isCancelled()){
119  $this->getLevel()->setBlock($pos, $ev->getTo(), true);
120  }
121  }
122  $hasUpdate = true;
123  }
124 
125  $this->updateMovement();
126  }
127 
128  return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0;
129  }
130 
131  public function getBlock(){
132  return $this->blockId;
133  }
134 
135  public function getDamage(){
136  return $this->damage;
137  }
138 
139  public function saveNBT(){
140  $this->namedtag->TileID = new Int("TileID", $this->blockId);
141  $this->namedtag->Data = new Byte("Data", $this->damage);
142  }
143 
144  public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){
145 
146  }
147 
148  public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){
149 
150  }
151 
152  public function spawnTo(Player $player){
153  $pk = new AddEntityPacket();
154  $pk->type = FallingSand::NETWORK_ID;
155  $pk->eid = $this->getId();
156  $pk->x = $this->x;
157  $pk->y = $this->y;
158  $pk->z = $this->z;
159  $pk->did = -($this->getBlock() | $this->getDamage() << 0x10);
160  $player->dataPacket($pk);
161 
162  $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
163 
164  parent::spawnTo($player);
165  }
166 }
dataPacket(DataPacket $packet, $needACK=false)
Definition: Player.php:745
static get($id, $meta=0, Position $pos=null)
Definition: Block.php:782