Untitled

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

namespace MyPlugin;

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

class BossBarTask extends Task {
    private $plugin;
    private $timer = 0;

    public function __construct(PluginBase $plugin) {
        $this->plugin = $plugin;
    }

    public function onRun(int $currentTick) : void {
        $this->timer++;
        if($this->timer >= 10) {
            foreach ($this->plugin->getServer()->getOnlinePlayers() as $player) {
                $message = str_replace("%player%", $player->getName(), MyPlugin::MESSAGES[array_rand(MyPlugin::MESSAGES)]);
                $this->plugin->sendBossBar($player, $message);
            }
            $this->timer = 0;
        } else {
            foreach ($this->plugin->getServer()->getOnlinePlayers() as $player) {
                $pk = new BossEventPacket();
                $pk->bossEid = 1;
                $pk->eventType = BossEventPacket::TYPE_HEALTH_PERCENT;
                $pk->healthPercent = 1;
                $player->sendDataPacket($pk);
            }
        }
    }
}

class MyPlugin extends PluginBase implements Listener {

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

    public function onEnable(){
        $this->getLogger()->info("Привет, Владос!");
        $this->getServer()->getPluginManager()->registerEvents($this, $this);
        $this->getScheduler()->scheduleRepeatingTask(new BossBarTask($this), 20);
    }

    public function onJoin(PlayerJoinEvent $event){
        $player = $event->getPlayer();
        $message = str_replace("%player%", $player->getName(), self::MESSAGES[array_rand(self::MESSAGES)]);
        $this->sendBossBar($player, $message);
    }

    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 = new LevelEventPacket();
        $pk->evid = LevelEventPacket::EVENT_SOUND_ORB;
        $pk->data = 0;
        $pk->position = $player->getPosition();
        $player->sendDataPacket($pk);
    }
}