PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
PlayerInventory.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
22declare(strict_types=1);
23
24namespace pocketmine\inventory;
25
30
32
33 protected Human $holder;
34 protected int $itemInHandIndex = 0;
35
40 protected ObjectSet $heldItemIndexChangeListeners;
41
42 public function __construct(Human $player){
43 $this->holder = $player;
44 $this->heldItemIndexChangeListeners = new ObjectSet();
45 parent::__construct(36);
46 }
47
48 public function isHotbarSlot(int $slot) : bool{
49 return $slot >= 0 && $slot < $this->getHotbarSize();
50 }
51
55 private function throwIfNotHotbarSlot(int $slot) : void{
56 if(!$this->isHotbarSlot($slot)){
57 throw new \InvalidArgumentException("$slot is not a valid hotbar slot index (expected 0 - " . ($this->getHotbarSize() - 1) . ")");
58 }
59 }
60
66 public function getHotbarSlotItem(int $hotbarSlot) : Item{
67 $this->throwIfNotHotbarSlot($hotbarSlot);
68 return $this->getItem($hotbarSlot);
69 }
70
74 public function getHeldItemIndex() : int{
75 return $this->itemInHandIndex;
76 }
77
85 public function setHeldItemIndex(int $hotbarSlot) : void{
86 $this->throwIfNotHotbarSlot($hotbarSlot);
87
88 $oldIndex = $this->itemInHandIndex;
89 $this->itemInHandIndex = $hotbarSlot;
90
91 foreach($this->heldItemIndexChangeListeners as $callback){
92 $callback($oldIndex);
93 }
94 }
95
100 public function getHeldItemIndexChangeListeners() : ObjectSet{ return $this->heldItemIndexChangeListeners; }
101
105 public function getItemInHand() : Item{
106 return $this->getHotbarSlotItem($this->itemInHandIndex);
107 }
108
112 public function setItemInHand(Item $item) : void{
113 $this->setItem($this->getHeldItemIndex(), $item);
114 }
115
119 public function getHotbarSize() : int{
120 return 9;
121 }
122
123 public function getHolder() : Human{
124 return $this->holder;
125 }
126}