Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created February 8, 2022 17:38
Show Gist options
  • Save Muqsit/8e738a77e3b823f06d10cf6721e82fd8 to your computer and use it in GitHub Desktop.
Save Muqsit/8e738a77e3b823f06d10cf6721e82fd8 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
/**
* @name InvMenuListenerTest
* @main muqsit\imtest\InvMenuListenerTest
* @version 0.0.1
* @api 3.0.0
*/
namespace muqsit\imtest{
use muqsit\invmenu\InvMenu;
use muqsit\invmenu\InvMenuHandler;
use muqsit\invmenu\transaction\DeterministicInvMenuTransaction;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginCommand;
use pocketmine\item\Item;
use pocketmine\item\ItemIds;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
final class InvMenuListenerTest extends PluginBase{
public function onEnable() : void{
if(!InvMenuHandler::isRegistered()){
InvMenuHandler::register($this);
}
$command = new PluginCommand("imtest", $this);
$command->setExecutor($this);
$this->getServer()->getCommandMap()->register($this->getName(), $command);
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
if($sender instanceof Player){
new Gui($sender);
}
return true;
}
}
final class Gui{
public function __construct(Player $player){
echo "Lottery Construct";
$this->chest = InvMenu::create(InvMenu::TYPE_CHEST);
$this->chest->setName("Click the diamond!");
$this->chest->setListener(InvMenu::readonly(function(DeterministicInvMenuTransaction $transaction) : void{
echo "This Listener Runs";
$player = $transaction->getPlayer();
$itemTakenOut = $transaction->getOut();/*$transaction->getItemClicked();*/ // or $transaction->getOut();
if($itemTakenOut->getId() === ItemIds::DIAMOND){
$player->removeWindow($transaction->getAction()->getInventory());
// on PocketMine-MP API 3, use:
// $player->removeWindow($transaction->getAction()->getInventory());
$player->sendMessage("Hello, world!");
}
}));
$this->chest->getInventory()->addItem(Item::get(ItemIds::DIAMOND));
/** @var Player $player */
$this->chest->send($player);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment