PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
DoubleChestInventory.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\inventory;
23 
30 
33  private $left;
35  private $right;
36 
37  public function __construct(Chest $left, Chest $right){
38  $this->left = $left->getRealInventory();
39  $this->right = $right->getRealInventory();
40  BaseInventory::__construct($this, InventoryType::get(InventoryType::DOUBLE_CHEST));
41  }
42 
43  public function getInventory(){
44  return $this;
45  }
46 
47  public function getHolder(){
48  return $this->left->getHolder();
49  }
50 
51  public function getItem($index){
52  return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->right->getSize());
53  }
54 
55  public function setItem($index, Item $item, $source = null){
56  return $index < $this->left->getSize() ? $this->left->setItem($index, $item, $source) : $this->right->setItem($index - $this->right->getSize(), $item, $source);
57  }
58 
59  public function clear($index, $source = null){
60  return $index < $this->left->getSize() ? $this->left->clear($index, $source) : $this->right->clear($index - $this->right->getSize(), $source);
61  }
62 
63  public function getContents(){
64  $contents = [];
65  for($i = 0; $i < $this->getSize(); ++$i){
66  $contents[$i] = $this->getItem($i);
67  }
68 
69  return $contents;
70  }
71 
75  public function setContents(array $items){
76  if(count($items) > $this->size){
77  $items = array_slice($items, 0, $this->size, true);
78  }
79 
80  parent::setContents($items);
81 
82  $leftItems = array_slice($items, 0, $this->left->getSize(), true);
83  $this->left->setContents($leftItems);
84  if(count($items) > $this->left->getSize()){
85  $rightItems = array_slice($items, $this->left->getSize() - 1, $this->right->getSize(), true);
86  $this->right->setContents($rightItems);
87  }
88  }
89 
90  public function onOpen(Player $who){
91  parent::onOpen($who);
92 
93  if(count($this->getViewers()) === 1){
94  $pk = new TileEventPacket();
95  $pk->x = $this->right->getHolder()->getX();
96  $pk->y = $this->right->getHolder()->getY();
97  $pk->z = $this->right->getHolder()->getZ();
98  $pk->case1 = 1;
99  $pk->case2 = 2;
100  if(($level = $this->right->getHolder()->getLevel()) instanceof Level){
101  Server::broadcastPacket($level->getUsingChunk($this->right->getHolder()->getX() >> 4, $this->right->getHolder()->getZ() >> 4), $pk);
102  }
103  }
104  }
105 
106  public function onClose(Player $who){
107  if(count($this->getViewers()) === 1){
108  $pk = new TileEventPacket();
109  $pk->x = $this->right->getHolder()->getX();
110  $pk->y = $this->right->getHolder()->getY();
111  $pk->z = $this->right->getHolder()->getZ();
112  $pk->case1 = 1;
113  $pk->case2 = 0;
114  if(($level = $this->right->getHolder()->getLevel()) instanceof Level){
115  Server::broadcastPacket($level->getUsingChunk($this->right->getHolder()->getX() >> 4, $this->right->getHolder()->getZ() >> 4), $pk);
116  }
117  }
118  parent::onClose($who);
119  }
120 
124  public function getLeftSide(){
125  return $this->left;
126  }
127 
131  public function getRightSide(){
132  return $this->right;
133  }
134 }
__construct(InventoryHolder $holder, InventoryType $type, array $items=[], $overrideSize=null, $overrideTitle=null)
static broadcastPacket(array $players, DataPacket $packet)
Definition: Server.php:1725
setItem($index, Item $item, $source=null)