PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Cactus.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\block;
23 
34 
35 class Cactus extends Transparent{
36 
37  protected $id = self::CACTUS;
38 
39  public function __construct($meta = 0){
40  $this->meta = $meta;
41  }
42 
43  public function getHardness(){
44  return 2;
45  }
46 
47  public function hasEntityCollision(){
48  return true;
49  }
50 
51  public function getName(){
52  return "Cactus";
53  }
54 
55  protected function recalculateBoundingBox(){
56 
57  return new AxisAlignedBB(
58  $this->x + 0.0625,
59  $this->y,
60  $this->z + 0.0625,
61  $this->x + 0.9375,
62  $this->y + 1,
63  $this->z + 0.9375
64  );
65  }
66 
67  public function onEntityCollide(Entity $entity){
68  $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1);
69  $entity->attack($ev->getFinalDamage(), $ev);
70  }
71 
72  public function onUpdate($type){
73  if($type === Level::BLOCK_UPDATE_NORMAL){
74  $down = $this->getSide(0);
75  if($down->getId() !== self::SAND and $down->getId() !== self::CACTUS){
76  $this->getLevel()->useBreakOn($this);
77  }else{
78  for($side = 2; $side <= 5; ++$side){
79  $b = $this->getSide($side);
80  if(!$b->isFlowable){
81  $this->getLevel()->useBreakOn($this);
82  }
83  }
84  }
85  }elseif($type === Level::BLOCK_UPDATE_RANDOM){
86  if($this->getSide(0)->getId() !== self::CACTUS){
87  if($this->meta == 0x0F){
88  for($y = 1; $y < 3; ++$y){
89  $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z));
90  if($b->getId() === self::AIR){
91  Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($b, new Cactus()));
92  if(!$ev->isCancelled()){
93  $this->getLevel()->setBlock($b, $ev->getNewState(), true);
94  }
95  }
96  }
97  $this->meta = 0;
98  $this->getLevel()->setBlock($this, $this);
99  }else{
100  ++$this->meta;
101  $this->getLevel()->setBlock($this, $this);
102  }
103  }
104  }
105 
106  return false;
107  }
108 
109  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
110  $down = $this->getSide(0);
111  if($down->getId() === self::SAND or $down->getId() === self::CACTUS){
112  $block0 = $this->getSide(2);
113  $block1 = $this->getSide(3);
114  $block2 = $this->getSide(4);
115  $block3 = $this->getSide(5);
116  if($block0->isTransparent() === true and $block1->isTransparent() === true and $block2->isTransparent() === true and $block3->isTransparent() === true){
117  $this->getLevel()->setBlock($this, $this, true);
118 
119  return true;
120  }
121  }
122 
123  return false;
124  }
125 
126  public function getDrops(Item $item){
127  return [
128  [$this->id, 0, 1],
129  ];
130  }
131 }
attack($damage, $source=EntityDamageEvent::CAUSE_MAGIC)
static getInstance()
Definition: Server.php:1444
getSide($side, $step=1)
Definition: Block.php:1028