Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@legoboy0215
Forked from shoghicp/MarqueeTask.php
Created March 1, 2016 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save legoboy0215/e50537f32cf355795d2d to your computer and use it in GitHub Desktop.
Save legoboy0215/e50537f32cf355795d2d to your computer and use it in GitHub Desktop.
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment