Hello, I have this code: main: PHP: public function onMove(PlayerMoveEvent $event){ $level = $this->getServer()->getDefaultLevel(); $player = $event->getPlayer(); $x = $player->getX(); $y = $player->getY(); $z = $player->getZ(); $IBlock = $level->getBlock(new Vector3($x, $y-2, $z)); $GBlock = $level->getBlock(new Vector3($x, $y-1, $z)); if($IBlock->getId() == 42) { if($GBlock->getId() == 41) { $this->seconds = time(); $this->getServer()->getScheduler()->scheduleRepeatingTask(new TimerTask($this, $player), 20); } } }} TimerTask: PHP: public function onRun($currentTick){ $this->secondsElapsed = time(); $sec = $this->secondsElapsed; $timer = $sec - $this->getOwner()->seconds; $millis = $timer / 10; $nanos = $millis / 10; $this->player->sendPopup(TextFormat::YELLOW . "§eParkour Timer:§f " . $timer. ":" . $millis . "" . $nanos . ""); }} I want to display the time in seconds, milliseconds and nanoseconds but this shows me a popup which displays this: edit: changed $secs in the popup to $timer but still not working
You're really-over complicating this. All you need to do is store a seconds property(i.e. public/protected/private $secondsElapsed) and increment it until they're done. Just store the task in an array under the player's name or UUID and retrieve/stop the task once they finish the course. You can use the function gmdate/date to format the numbers into a time string for you. In your case, you'd use gmdate/date("i:s", $secondsVariable), where "i" means the minutes and "s" is the seconds. Note that gmdate/date don't have an option for nanoseconds nor milliseconds that I know of. Here's how I would do the task(Don't mind the type-hinting, I did this all on Sublime off of the top of my head): PHP: <?phpuse pocketmine\scheduler\PluginTask;use pocketmine\Player;class TimerTask extends PluginTask { /** @var int */ private $secondsElapsed = 0; /** @var Player */ private $player; public function __construct(YourMainClassHere $plugin, Player $player) { parent::__construct($plugin); $this->player = $player; } /** * @return Player */ public function getPlayer(): Player { return $this->player; } /** * @var $currentTick */ public function onRun($currentTick) { $this->secondsElapsed++; $this->getPlayer()->sendPopup("§eParkour Timer:§f " . gmdate("i:s", $this->secondsElapsed)); }}
this didn't work I got 00:1 (1 being the second, 00 didn't change) However I did come up with this but I'm unsure if it's the correct way of doing it but this does work just that the time in which the digits .00 (millisecond and nanosecond) update seems a bit choppy as sometimes they'll go fast and sometimes slow down but I think this is just the laggy server I'm testing this on since I usually experience lagback on this server. plus I always get this message in the console (not only after I added this timer but b4 as well): Code: [16:20:30] [Server thread/WARNING]: Can't keep up! Is the server overloaded? anyway here's what I've done: PHP: <?phpnamespace timer;use pocketmine\scheduler\PluginTask;use pocketmine\utils\TextFormat;use pocketmine\Player;class TimerTask extends PluginTask{ private $player; public function __construct($plugin, Player $player){ $this->plugin = $plugin; $this->player = $player; parent::__construct($plugin); } public function onRun($currentTick){ $this->secondsElapsed = time(); $sec = $this->secondsElapsed; $sec = microtime(true); $timer = $sec - $this->getOwner()->seconds; $time = round($timer, 2); $this->player->sendPopup("§6Parkour Timer:§f " . $time . ""); }}
Make sure to cancel the tasks after they're done, otherwise you'll keep all of those tasks running and that's no bueno.
My guesses for why it wouldn't work: You are never creating that class instance ("new TimerTask") anywhere (or else you will be getting an error whenever you create it. You are running on a very old version of PMMP (the Task::onRun() function is now strict-typed). Maybe that isn't your exact code?
are you asking why this isn't working? PHP: public function onRun($currentTick){ $this->secondsElapsed = time(); $sec = $this->secondsElapsed; $timer = $sec - $this->getOwner()->seconds; $millis = $timer / 10; $nanos = $millis / 10; $this->player->sendPopup(TextFormat::YELLOW . "§eParkour Timer:§f " . $timer. ":" . $millis . "" . $nanos . ""); }}