Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Last active March 11, 2018 12:55
Show Gist options
  • Save Muqsit/8154dac28a85c71c46f6b42a0d55df41 to your computer and use it in GitHub Desktop.
Save Muqsit/8154dac28a85c71c46f6b42a0d55df41 to your computer and use it in GitHub Desktop.
<?php
namespace imt;
use muqsit\invmenu\{InvMenu, InvMenuHandler};
use pocketmine\block\Block;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\Listener;
use pocketmine\item\Item;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\Task;
use pocketmine\Server;
use pocketmine\utils\TextFormat as TF;
class Main extends PluginBase implements Listener {
/** @var InvMenu */
private $menu;
public function onEnable() : void
{
if (!InvMenuHandler::isRegistered()) {
InvMenuHandler::register($this);
}
$this->menu = InvMenu::create(InvMenu::TYPE_HOPPER)
->readonly()
->setListener([$this, 'handleClicking'])
->sessionize();
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function handleClicking(Player $player, Item $itemClicked) : bool
{
$player->sendMessage(TF::RED.'Hi '.$itemClicked->getName());
return true;
}
public function onBlockPlace(BlockPlaceEvent $event) : void
{
if ($event->getBlock()->getId() === Block::GLASS) {
$player = $event->getPlayer();
$this->menu->send($player);
$player->getServer()->getScheduler()->scheduleRepeatingTask(new class($this->menu->getInventory($player)) extends Task {
/** @var \pocketmine\inventory\BaseInventory */
public $inventory;
public function __construct($inventory)
{
$this->inventory = $inventory;
}
public function onRun(int $tick) : void
{
if (empty($this->inventory->getViewers())) {
Server::getInstance()->getScheduler()->cancelTask($this->getTaskId());
return;
}
$this->inventory->setContents([Item::get(mt_rand(256, 511)), Item::get(mt_rand(256, 511)), Item::get(mt_rand(256, 511)), Item::get(mt_rand(256, 511)), Item::get(mt_rand(256, 511))]);
}
}, 20);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment