PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
block/Bed.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 
29 
30 class Bed extends Transparent{
31 
32  protected $id = self::BED_BLOCK;
33 
34  public function __construct($meta = 0){
35  $this->meta = $meta;
36  }
37 
38  public function canBeActivated(){
39  return true;
40  }
41 
42  public function getHardness(){
43  return 1;
44  }
45 
46  public function getName(){
47  return "Bed Block";
48  }
49 
50  protected function recalculateBoundingBox(){
51  return new AxisAlignedBB(
52  $this->x,
53  $this->y,
54  $this->z,
55  $this->x + 1,
56  $this->y + 0.5625,
57  $this->z + 1
58  );
59  }
60 
61  public function onActivate(Item $item, Player $player = null){
62 
63  $time = $this->getLevel()->getTime() % Level::TIME_FULL;
64 
65  $isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE);
66 
67  if($player instanceof Player and !$isNight){
68  $pk = new ChatPacket();
69  $pk->message = "You can only sleep at night";
70  $player->dataPacket($pk);
71 
72  return true;
73  }
74 
75  $blockNorth = $this->getSide(2); //Gets the blocks around them
76  $blockSouth = $this->getSide(3);
77  $blockEast = $this->getSide(5);
78  $blockWest = $this->getSide(4);
79  if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed
80  $b = $this;
81  }else{ //Bottom Part of Bed
82  if($blockNorth->getId() === $this->id and ($blockNorth->meta & 0x08) === 0x08){
83  $b = $blockNorth;
84  }elseif($blockSouth->getId() === $this->id and ($blockSouth->meta & 0x08) === 0x08){
85  $b = $blockSouth;
86  }elseif($blockEast->getId() === $this->id and ($blockEast->meta & 0x08) === 0x08){
87  $b = $blockEast;
88  }elseif($blockWest->getId() === $this->id and ($blockWest->meta & 0x08) === 0x08){
89  $b = $blockWest;
90  }else{
91  if($player instanceof Player){
92  $pk = new ChatPacket();
93  $pk->message = "This bed is incomplete";
94  $player->dataPacket($pk);
95  }
96 
97  return true;
98  }
99  }
100 
101  if($player instanceof Player and $player->sleepOn($b) === false){
102  $pk = new ChatPacket();
103  $pk->message = "This bed is occupied";
104  $player->dataPacket($pk);
105  }
106 
107  return true;
108  }
109 
110  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
111  $down = $this->getSide(0);
112  if($down->isTransparent() === false){
113  $faces = [
114  0 => 3,
115  1 => 4,
116  2 => 2,
117  3 => 5,
118  ];
119  $d = $player instanceof Player ? $player->getDirection() : 0;
120  $next = $this->getSide($faces[(($d + 3) % 4)]);
121  $downNext = $this->getSide(0);
122  if($next->canBeReplaced() === true and $downNext->isTransparent() === false){
123  $meta = (($d + 3) % 4) & 0x03;
124  $this->getLevel()->setBlock($block, Block::get($this->id, $meta), true, true);
125  $this->getLevel()->setBlock($next, Block::get($this->id, $meta | 0x08), true, true);
126 
127  return true;
128  }
129  }
130 
131  return false;
132  }
133 
134  public function onBreak(Item $item){
135  $blockNorth = $this->getSide(2); //Gets the blocks around them
136  $blockSouth = $this->getSide(3);
137  $blockEast = $this->getSide(5);
138  $blockWest = $this->getSide(4);
139 
140  if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed
141  if($blockNorth->getId() === $this->id and $blockNorth->meta !== 0x08){ //Checks if the block ID and meta are right
142  $this->getLevel()->setBlock($blockNorth, new Air(), true, true);
143  }elseif($blockSouth->getId() === $this->id and $blockSouth->meta !== 0x08){
144  $this->getLevel()->setBlock($blockSouth, new Air(), true, true);
145  }elseif($blockEast->getId() === $this->id and $blockEast->meta !== 0x08){
146  $this->getLevel()->setBlock($blockEast, new Air(), true, true);
147  }elseif($blockWest->getId() === $this->id and $blockWest->meta !== 0x08){
148  $this->getLevel()->setBlock($blockWest, new Air(), true, true);
149  }
150  }else{ //Bottom Part of Bed
151  if($blockNorth->getId() === $this->id and ($blockNorth->meta & 0x08) === 0x08){
152  $this->getLevel()->setBlock($blockNorth, new Air(), true, true);
153  }elseif($blockSouth->getId() === $this->id and ($blockSouth->meta & 0x08) === 0x08){
154  $this->getLevel()->setBlock($blockSouth, new Air(), true, true);
155  }elseif($blockEast->getId() === $this->id and ($blockEast->meta & 0x08) === 0x08){
156  $this->getLevel()->setBlock($blockEast, new Air(), true, true);
157  }elseif($blockWest->getId() === $this->id and ($blockWest->meta & 0x08) === 0x08){
158  $this->getLevel()->setBlock($blockWest, new Air(), true, true);
159  }
160  }
161  $this->getLevel()->setBlock($this, new Air(), true, true);
162 
163  return true;
164  }
165 
166  public function getDrops(Item $item){
167  return [
168  [Item::BED, 0, 1],
169  ];
170  }
171 
172 }
sleepOn(Vector3 $pos)
Definition: Player.php:796
getSide($side, $step=1)
Definition: Block.php:1028
static get($id, $meta=0, Position $pos=null)
Definition: Block.php:782