PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
SpawnEgg.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\item;
23 
33 
34 class SpawnEgg extends Item{
35  public function __construct($meta = 0, $count = 1){
36  parent::__construct(self::SPAWN_EGG, $meta, $count, "Spawn Egg");
37  }
38 
39  public function canBeActivated(){
40  return true;
41  }
42 
43  public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
44  $entity = null;
45  $chunk = $level->getChunk($block->getX() >> 4, $block->getZ() >> 4);
46 
47  if(!($chunk instanceof FullChunk)){
48  return false;
49  }
50 
51  $nbt = new Compound("", [
52  "Pos" => new Enum("Pos", [
53  new Double("", $block->getX() + 0.5),
54  new Double("", $block->getY()),
55  new Double("", $block->getZ() + 0.5)
56  ]),
57  "Motion" => new Enum("Motion", [
58  new Double("", 0),
59  new Double("", 0),
60  new Double("", 0)
61  ]),
62  "Rotation" => new Enum("Rotation", [
63  new Float("", lcg_value() * 360),
64  new Float("", 0)
65  ]),
66  ]);
67 
68  $entity = Entity::createEntity($this->meta, $chunk, $nbt);
69 
70  if($entity instanceof Entity){
71  if($player->isSurvival()){
72  --$this->count;
73  }
74  $entity->spawnToAll();
75 
76  return true;
77  }
78 
79  return false;
80  }
81 }
getChunk($x, $z, $create=false)
Definition: Level.php:1732
static createEntity($type, FullChunk $chunk, Compound $nbt,...$args)
Definition: Entity.php:239