Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
2
Indexable
Never
<?php

namespace MyPlugin;

use pocketmine\{Player, Server};
use pocketmine\event\{Listener, player\PlayerJoinEvent};
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\{Task, TaskHandler};
use pocketmine\network\mcpe\protocol\{BossEventPacket, LevelEventPacket};

class BossBarTask extends Task {
    /** @var string[] */
    private $messages;
    /** @var int */
    private $index = 0;

    public function __construct(array $messages) {
        $this->messages = $messages;
    }

    public function onRun(int $currentTick) : void {
        $this->index = ($this->index + 1) % count($this->messages);
        $message = $this->messages[$this->index];
        foreach (Server::getInstance()->getOnlinePlayers() as $player) {
            $pk = new BossEventPacket();
            $pk->bossEid = 1;
            $pk->eventType = BossEventPacket::TYPE_SHOW;
            $pk->title = $message;
            $pk->healthPercent = 1;
            $pk->color = BossEventPacket::COLOR_GREEN;
            $pk->overlay = BossEventPacket::OVERLAY_PROGRESS;
            $player->sendDataPacket($pk);
            $pk = new LevelEventPacket();
            $pk->evid = LevelEventPacket::EVENT_SOUND_ORB;
            $pk->data = 0;
            $pk->position = $player->getPosition();
            $player->sendDataPacket($pk);
        }
    }
}

class MyPlugin extends PluginBase implements Listener {

    const MESSAGES = [
        "§aДобро пожаловать, %player%!",
        "§eСкоро будет рестарт сервера!",
        "§6Следующий ивент пройдёт через 30 минут",
        "§cОбратите внимание на наши правила"
    ];

    /** @var TaskHandler|null */
    private $taskHandler = null;

    public function onEnable(){
        $this->getLogger()->info("Привет, Владос!");
        $this->getServer()->getPluginManager()->registerEvents($this, $this);
        $this->taskHandler = $this->getScheduler()->scheduleRepeatingTask(new BossBarTask(self::MESSAGES), 20);
    }
    public function onJoin(PlayerJoinEvent $event){
        $player = $event->getPlayer();
        $message = str_replace("%player%", $player->getName(), self::MESSAGES[mt_rand(0, count(self::MESSAGES) - 1)]);
        $this->sendBossBar($player, $message);
    }
    public function onDisable() {
        $this->taskHandler?->cancel();
        $this->taskHandler = null;
    }
    public function sendBossBar(Player $player, string $message): void {
        $pk = new BossEventPacket();
        $pk->bossEid = 1;
        $pk->eventType = BossEventPacket::TYPE_SHOW;
        $pk->title = $message;
        $pk->healthPercent = 1;
        $pk->color = BossEventPacket::COLOR_GREEN;
        $pk->overlay = BossEventPacket::OVERLAY_PROGRESS;
        $player->sendDataPacket($pk);

        $pk = LevelEventPacket::create(LevelEventPacket::EVENT_SOUND_ORB, $player->getPosition());
        $player->sendDataPacket($pk);
    }
}