Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created June 23, 2018 18:00
Show Gist options
  • Save Muqsit/68b6217d5e3c7c01396fd0c8046cf859 to your computer and use it in GitHub Desktop.
Save Muqsit/68b6217d5e3c7c01396fd0c8046cf859 to your computer and use it in GitHub Desktop.
Sends viewer count of a double chest and it's pairs.
<?php
/**
* @name DoubleChestDebug
* @main muqsit\dcdcd\Main
* @version 1.0
* @api 3.0.0
*/
namespace muqsit\dcdcd {
use pocketmine\plugin\PluginBase;
use pocketmine\event\inventory\InventoryOpenEvent;
use pocketmine\event\Listener;
use pocketmine\inventory\DoubleChestInventory;
use pocketmine\scheduler\Task;
class Main extends PluginBase implements Listener {
public function onEnable() : void
{
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onInventoryOpen(InventoryOpenEvent $event) : void
{
$inventory = $event->getInventory();
if ($inventory instanceof DoubleChestInventory) {
$this->getScheduler()->scheduleRepeatingTask(new ViewerCountTask($this, $inventory), 1);
}
}
}
class ViewerCountTask extends Task {
/** @var Main */
private $plugin;
/** @var DoubleChestInventory */
private $inventory;
public function __construct(Main $plugin, DoubleChestInventory $inventory)
{
$this->plugin = $plugin;
$this->inventory = $inventory;
}
public function onRun(int $tick) : void
{
$viewers = count($this->inventory->getViewers());
$viewers_l = count($this->inventory->getLeftSide()->getViewers());
$viewers_r = count($this->inventory->getRightSide()->getViewers());
var_dump([
"viewers" => $viewers,
"viewers_l" => $viewers_l,
"viewers_r" => $viewers_r
]);
if ($viewers === 0 && $viewers_l === 0 && $viewers_r === 0) {
$this->plugin->getScheduler()->cancelTask($this->getTaskId());
echo "Dropped " . get_class($this) . " due to 0 viewers" . PHP_EOL;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment