Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace shoghicp\MinecraftSimulator\task;
use pocketmine\Player;
use pocketmine\scheduler\PluginTask;
use shoghicp\MinecraftSimulator\Loader;
class MarqueeTask extends PluginTask{
const MAX_LEN = 35;
private $scheduler;
private $player;
private $nextString;
private $message;
private $index;
private $delay;
private $format;
public function __construct(Loader $plugin, Player $player, $message, $step = null, $format = ""){
parent::__construct($plugin);
$this->player = $player;
$this->scheduler = $plugin->getServer()->getScheduler();
$this->delay = $step === null ? (strlen($message) > self::MAX_LEN ? 2 : 3) : $step;
$this->message = str_repeat(" ", self::MAX_LEN) . $message . str_repeat(" ", self::MAX_LEN);
$this->index = 0;
$this->format = $format;
$this->scheduleNext();
}
public function onRun($currentTick){
if($this->message === null or !$this->player->isConnected()){
return;
}
$this->player->sendPopup($this->format . $this->nextString);
$this->scheduleNext();
}
private function scheduleNext(){
$entry = $this->nextEntry();
if($entry){
$this->scheduler->scheduleDelayedTask($this, $this->delay);
}else{
$this->message = null;
}
}
private function nextEntry(){
if($this->index > strlen($this->message)){
return false;
}
$this->nextString = substr($this->message, $this->index, self::MAX_LEN);
++$this->index;
return true;
}
}
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Oct 20, 2016

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.