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

Commit

Permalink
Prevent use while stacked + NBT Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Oct 18, 2019
1 parent 7db2078 commit 3a0e3c7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/DaPigGuy/PiggyBackpacks/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
use pocketmine\item\Item;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
use pocketmine\utils\TextFormat;
use pocketmine\utils\UUID;

/**
* Class EventListener
Expand Down Expand Up @@ -40,7 +44,19 @@ public function onInteract(PlayerInteractEvent $event): void
$player = $event->getPlayer();
$item = $player->getInventory()->getItemInHand();
if ($item->getId() === Item::CHEST && ($size = $item->getNamedTagEntry("Size")) !== null) {
if ($item->getCount() > 1) {
$player->sendTip(TextFormat::RED . "Backpacks can not be opened while stacked.");
return;
}
if ($item->getNamedTagEntry("Contents") === null) $item->setNamedTagEntry(new ListTag("Contents"));
if ($item->getNamedTagEntry("UUID") === null) {
$item->setNamedTagEntry(new StringTag("UUID", UUID::fromRandom()->toString()));
$item->setNamedTagEntry(new ListTag("Creator", [
new StringTag("Name", $player->getName()),
new StringTag("XUID", $player->getXuid())
]));
$item->setNamedTagEntry(new IntTag("Timestamp", time()));
}

$backpack = InvMenu::create($size->getValue() > 27 ? InvMenu::TYPE_DOUBLE_CHEST : InvMenu::TYPE_CHEST);
$backpack->setName($item->getName());
Expand Down

0 comments on commit 3a0e3c7

Please sign in to comment.