1: <?php
2: namespace aliuly\common;
3: use pocketmine\item\Item;
4: use pocketmine\Player;
5:
6: 7: 8:
9: abstract class InvUtils {
10: 11: 12: 13:
14: static public function clearInventory(Player $target) {
15: if ($target->isCreative() || $target->isSpectator()) return;
16: $target->getInventory()->clearAll();
17: }
18: 19: 20: 21:
22: static public function clearHotBar(Player $target) {
23: $inv = $target->getInventory();
24: for ($i=0;$i < $inv->getHotbarSize(); $i++) {
25: $inv->setHotbarSlotIndex($i,-1);
26: }
27:
28: $inv->sendContents($target);
29: }
30: 31: 32: 33: 34: 35:
36: static public function rmInvItem(Player $target, Item $item, $count = null) {
37: $k = 0;
38: foreach ($target->getInventory()->getContents() as $slot => &$inv) {
39: if ($inv->getId() != $item->getId()) continue;
40: if ($count !== null) {
41: if ($inv->getCount() > $count) {
42: $k += $count;
43: $inv->setCount($inv->getCount()-$count);
44: $target->getInventory()->setItem($slot,clone $inv);
45: break;
46: }
47: $count -= $inv->getCount();
48: }
49: $k += $inv->getCount();
50: $target->getInventory()->clear($slot);
51: if ($count === 0) break;
52: }
53: $target->getInventory()->sendContents($target);
54: return $k;
55: }
56: 57: 58: 59: 60: 61:
62: static public function countInvItem(Player $target,Item $item) {
63: $k = 0;
64: foreach ($target->getInventory()->getContents() as $slot => &$inv) {
65: if ($inv->getId() == $item->getId()) $k += $inv->getCount();
66: }
67: return $k;
68: }
69:
70: }
71: