PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
entity/Item.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 
28 use pocketmine\item\Item as ItemItem;
36 
37 class Item extends Entity{
38  const NETWORK_ID = 64;
39 
40  protected $owner = null;
41  protected $thrower = null;
42  protected $pickupDelay = 0;
44  protected $item;
45 
46  public $width = 0.25;
47  public $length = 0.25;
48  public $height = 0.25;
49  protected $gravity = 0.04;
50  protected $drag = 0.02;
51 
52  public $canCollide = false;
53 
54  protected function initEntity(){
55  $this->setMaxHealth(5);
56  $this->setHealth($this->namedtag["Health"]);
57  if(isset($this->namedtag->Age)){
58  $this->age = $this->namedtag["Age"];
59  }
60  if(isset($this->namedtag->PickupDelay)){
61  $this->pickupDelay = $this->namedtag["PickupDelay"];
62  }
63  if(isset($this->namedtag->Owner)){
64  $this->owner = $this->namedtag["Owner"];
65  }
66  if(isset($this->namedtag->Thrower)){
67  $this->thrower = $this->namedtag["Thrower"];
68  }
69  $this->item = ItemItem::get($this->namedtag->Item["id"], $this->namedtag->Item["Damage"], $this->namedtag->Item["Count"]);
70 
71 
72  $this->server->getPluginManager()->callEvent(new ItemSpawnEvent($this));
73  }
74 
75  public function onUpdate($currentTick){
76  if($this->closed !== false){
77  return false;
78  }
79 
80  $tickDiff = max(1, $currentTick - $this->lastUpdate);
81  $this->lastUpdate = $currentTick;
82 
83  $this->timings->startTiming();
84 
85  $hasUpdate = $this->entityBaseTick($tickDiff);
86 
87  if(!$this->dead){
88 
89  if($this->pickupDelay > 0 and $this->pickupDelay < 32767){ //Infinite delay
90  $this->pickupDelay -= $tickDiff;
91  }
92 
93  $this->motionY -= $this->gravity;
94 
95  $this->keepMovement = $this->checkObstruction($this->x, ($this->boundingBox->minY + $this->boundingBox->maxY) / 2, $this->z);
96  $this->move($this->motionX, $this->motionY, $this->motionZ);
97 
98  $friction = 1 - $this->drag;
99 
100  if($this->onGround and ($this->motionX != 0 or $this->motionZ != 0)){
101  $friction = $this->getLevel()->getBlock(new Vector3($this->getFloorX(), $this->getFloorY() - 1, $this->getFloorZ()))->getFrictionFactor() * $friction;
102  }
103 
104  $this->motionX *= $friction;
105  $this->motionY *= 1 - $this->drag;
106  $this->motionZ *= $friction;
107 
108  $this->updateMovement();
109 
110  if($this->onGround){
111  $this->motionY *= -0.5;
112  }
113 
114  if($this->age > 6000){
115  $this->server->getPluginManager()->callEvent($ev = new ItemDespawnEvent($this));
116  if($ev->isCancelled()){
117  $this->age = 0;
118  }else{
119  $this->kill();
120  $hasUpdate = true;
121  }
122  }
123 
124  }
125 
126  $this->timings->stopTiming();
127 
128  return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0;
129  }
130 
131  public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){
132  if($source instanceof EntityDamageEvent){
133  $this->server->getPluginManager()->callEvent($source);
134  $damage = $source->getFinalDamage();
135  if($source->isCancelled()){
136  return;
137  }
138  }
139  $this->setLastDamageCause($source);
140  $this->setHealth($this->getHealth() - $damage);
141  }
142 
143  public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){
144 
145  }
146 
147  public function saveNBT(){
148  parent::saveNBT();
149  $this->namedtag->Item = new Compound("Item", [
150  "id" => new Short("id", $this->item->getId()),
151  "Damage" => new Short("Damage", $this->item->getDamage()),
152  "Count" => new Byte("Count", $this->item->getCount())
153  ]);
154  $this->namedtag->Health = new Short("Health", $this->getHealth());
155  $this->namedtag->Age = new Short("Age", $this->age);
156  $this->namedtag->PickupDelay = new Short("PickupDelay", $this->pickupDelay);
157  if($this->owner !== null){
158  $this->namedtag->Owner = new String("Owner", $this->owner);
159  }
160  if($this->thrower !== null){
161  $this->namedtag->Thrower = new String("Thrower", $this->thrower);
162  }
163  }
164 
165  public function getData(){
166  $flags = 0;
167  $flags |= $this->fireTicks > 0 ? 1 : 0;
168 
169  return [
170  0 => ["type" => 0, "value" => $flags]
171  ];
172  }
173 
177  public function getItem(){
178  return $this->item;
179  }
180 
181  public function canCollideWith(Entity $entity){
182  return false;
183  }
184 
188  public function getPickupDelay(){
189  return $this->pickupDelay;
190  }
191 
195  public function setPickupDelay($delay){
196  $this->pickupDelay = $delay;
197  }
198 
202  public function getOwner(){
203  return $this->owner;
204  }
205 
209  public function setOwner($owner){
210  $this->owner = $owner;
211  }
212 
216  public function getThrower(){
217  return $this->thrower;
218  }
219 
223  public function setThrower($thrower){
224  $this->thrower = $thrower;
225  }
226 
227  public function spawnTo(Player $player){
228  $pk = new AddItemEntityPacket();
229  $pk->eid = $this->getId();
230  $pk->x = $this->x;
231  $pk->y = $this->y;
232  $pk->z = $this->z;
233  $pk->yaw = $this->yaw;
234  $pk->pitch = $this->pitch;
235  $pk->roll = 0;
236  $pk->item = $this->getItem();
237  $player->dataPacket($pk);
238 
239  $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
240 
241  parent::spawnTo($player);
242  }
243 }
dataPacket(DataPacket $packet, $needACK=false)
Definition: Player.php:745