how can I make a hud that counts down>>? I tried this on repeatingtask PHP: $this->seconds = 60;$this->player->sendPopup("TImer: ".$this->seconds);$this->seconds--;$this->player is defined as the player
here is an example: PHP: class Timer extends PluginTask{ private $seconds = 60; private $players = []; /** * Timer constructor. * @param Plugin $owner * @param array $players */ public function __construct(Plugin $owner, array $players) { parent::__construct($owner); $this->players = $players; } /** * @param int $currentTick */ public function onRun(int $currentTick) { --$this->seconds; foreach ($this->players as $player){ if ($player instanceof Player){ $player->sendPopup("Timer: " . $this->seconds); } } }}
PHP: /*** @param int $int* @return string*/public static function intToString(int $int) : string{ $mins = floor($int / 60); $seconds = floor($int % 60); return (($m < 10 ? "0" : "") . $m . ":" . ($s < 10 ? "0" : "") . $s);}// self::intToString(60); will return "1:00"
Here is what you want to do: on the Main class add this function: PHP: public function teleportPlayer(Player $player){ new TeleportTask($this, $player);} and when you want to teleport a player execute it , ! remember to add the Task class ! PHP: class TeleportTask extends PluginTask{ private $time = 60, $player; /** * TeleportTask constructor. * @param Plugin $owner * @param Player $player */ public function __construct(Plugin $owner, Player $player) { parent::__construct($owner); $this->setPlayer($player); $this->setHandler($owner->getServer()->getScheduler()->scheduleRepeatingTask($this, 20)); } /** * @param int $currentTick */ public function onRun(int $currentTick) { $this->setTime($this->getTime() - 1); if($this->getTime() == 0){ $this->getPlayer()->teleport($this->getPlayer()->getLevel()->getSpawnLocation(), 0, 0); $this->getHandler()->cancel(); } else { $this->getPlayer()->sendPopup("You will be teleported to spawn in " . $this->getTime() . " second(s)..."); } } /** * @return int */ public function getTime() : int { return $this->time; } /** * @param int $time */ public function setTime(int $time) { $this->time = $time; } /** * @return Player */ public function getPlayer() : Player { return $this->player; } /** * @param Player $player */ public function setPlayer(Player $player) { $this->player = $player; }}