Muqsit / InvMenu

A PocketMine-MP virion to create and manage virtual inventories!
https://poggit.pmmp.io/ci/Muqsit/InvMenu/~
GNU General Public License v3.0
201 stars 75 forks source link

Inventory Transaction #65

Closed HimmelKreis4865 closed 4 years ago

HimmelKreis4865 commented 4 years ago

Hello,

would it be possible to set for e.g only one item as read-only? Or maybe 2, 3, 4 up to 5? Like you can drop items there but there are some items you can't get out of the inventory?

Muqsit commented 4 years ago

You can do that but not directly through the API.

$x = Item::get(Item::DIAMOND_SWORD);
$y = Item::get(Item::IRON_SWORD);
$z = Item::get(Item::GOLD_SWORD);

foreach([$x, $y] as $readonly_item){ // making $x and $y read-only
    $nbt = $readonly_item->getNamedTag();
    $nbt->setByte("ReadOnly", 1); // set "ReadOnly" tag on item
    $readonly_item->setNamedTag($nbt);
}

$menu->getInventory()->addItem($x, $y, $z);
$menu->setListener(function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
    if($itemClicked->getNamedTag()->hasTag("ReadOnly")){ // cancel transaction for items with "ReadOnly" tag
        return false;
    }
    return true;
});

You could wrap InvMenu over with your own API that extends it. InvMenu provides just the required data to work with.