Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Feb 17, 2020
1 parent fbc2e04 commit 7411623
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/DaPigGuy/PiggyBackpacks/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DaPigGuy\PiggyBackpacks;

use muqsit\invmenu\inventory\InvMenuInventory;
use muqsit\invmenu\InvMenu;
use pocketmine\event\inventory\InventoryTransactionEvent;
use pocketmine\event\Listener;
Expand Down Expand Up @@ -70,8 +71,9 @@ public function onInteract(PlayerInteractEvent $event): void
$backpack->getInventory()->setItem($i, $slot);
}

$backpack->setListener(function (Player $player) use ($backpack): bool {
$updateClosure = (function (Player $player, $secondArgument) use ($backpack) {
$backpackItem = $player->getInventory()->getItemInHand();
if ($secondArgument instanceof InvMenuInventory) $backpackItem->removeNamedTagEntry("Opened");
$backpackItem->setNamedTagEntry(new ListTag("Contents", array_map(function (Item $item) {
return $item->nbtSerialize();
}, array_filter($backpack->getInventory()->getContents(true), function (Item $item) {
Expand All @@ -81,16 +83,8 @@ public function onInteract(PlayerInteractEvent $event): void
return true;
});

$backpack->setInventoryCloseListener(function (Player $player) use ($backpack): void {
$backpackItem = $player->getInventory()->getItemInHand();
$backpackItem->removeNamedTagEntry("Opened");
$backpackItem->setNamedTagEntry(new ListTag("Contents", array_map(function (Item $item) {
return $item->nbtSerialize();
}, array_filter($backpack->getInventory()->getContents(true), function (Item $item) {
return $item->getId() !== Item::INVISIBLE_BEDROCK || $item->getNamedTagEntry("BackpackSlot") === null;
}))));
$player->getInventory()->setItemInHand($backpackItem);
});
$backpack->setListener($updateClosure);
$backpack->setInventoryCloseListener($updateClosure);

$item->setNamedTagEntry(new ByteTag("Opened", 1));
$player->getInventory()->setItemInHand($item);
Expand Down

0 comments on commit 7411623

Please sign in to comment.