PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
block/Chest.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 
32 use pocketmine\tile\Chest as TileChest;
34 
35 class Chest extends Transparent{
36 
37  protected $id = self::CHEST;
38 
39  public function __construct($meta = 0){
40  $this->meta = $meta;
41  }
42 
43  public function canBeActivated(){
44  return true;
45  }
46 
47  public function getHardness(){
48  return 15;
49  }
50 
51  public function getName(){
52  return "Chest";
53  }
54 
55  protected function recalculateBoundingBox(){
56  return new AxisAlignedBB(
57  $this->x + 0.0625,
58  $this->y,
59  $this->z + 0.0625,
60  $this->x + 0.9375,
61  $this->y + 0.9475,
62  $this->z + 0.9375
63  );
64  }
65 
66  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
67  $faces = [
68  0 => 4,
69  1 => 2,
70  2 => 5,
71  3 => 3,
72  ];
73 
74  $chest = null;
75  $this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0];
76 
77  for($side = 2; $side <= 5; ++$side){
78  if(($this->meta === 4 or $this->meta === 5) and ($side === 4 or $side === 5)){
79  continue;
80  }elseif(($this->meta === 3 or $this->meta === 2) and ($side === 2 or $side === 3)){
81  continue;
82  }
83  $c = $this->getSide($side);
84  if($c instanceof Chest and $c->getDamage() === $this->meta){
85  $tile = $this->getLevel()->getTile($c);
86  if($tile instanceof TileChest and !$tile->isPaired()){
87  $chest = $tile;
88  break;
89  }
90  }
91  }
92 
93  $this->getLevel()->setBlock($block, $this, true, true);
94  $nbt = new Compound(false, [
95  new Enum("Items", []),
96  new String("id", Tile::CHEST),
97  new Int("x", $this->x),
98  new Int("y", $this->y),
99  new Int("z", $this->z)
100  ]);
101  $nbt->Items->setTagType(NBT::TAG_Compound);
102  $tile = Tile::createTile("Chest", $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
103 
104  if($chest instanceof TileChest and $tile instanceof TileChest){
105  $chest->pairWith($tile);
106  $tile->pairWith($chest);
107  }
108 
109  return true;
110  }
111 
112  public function onBreak(Item $item){
113  $t = $this->getLevel()->getTile($this);
114  if($t instanceof TileChest){
115  $t->unpair();
116  }
117  $this->getLevel()->setBlock($this, new Air(), true, true);
118 
119  return true;
120  }
121 
122  public function onActivate(Item $item, Player $player = null){
123  if($player instanceof Player){
124  $top = $this->getSide(1);
125  if($top->isTransparent() !== true){
126  return true;
127  }
128 
129  $t = $this->getLevel()->getTile($this);
130  $chest = null;
131  if($t instanceof TileChest){
132  $chest = $t;
133  }else{
134  $nbt = new Compound(false, [
135  new Enum("Items", []),
136  new String("id", Tile::CHEST),
137  new Int("x", $this->x),
138  new Int("y", $this->y),
139  new Int("z", $this->z)
140  ]);
141  $nbt->Items->setTagType(NBT::TAG_Compound);
142  $chest = Tile::createTile("Chest", $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
143  }
144 
145 
146  if($player->isCreative()){
147  return true;
148  }
149  $player->addWindow($chest->getInventory());
150  }
151 
152  return true;
153  }
154 
155  public function getDrops(Item $item){
156  return [
157  [$this->id, 0, 1],
158  ];
159  }
160 }
getSide($side, $step=1)
Definition: Block.php:1028
static createTile($type, FullChunk $chunk, Compound $nbt,...$args)
Definition: Tile.php:74