Skip to content

Instantly share code, notes, and snippets.

@shoghicp
Created April 9, 2015 18:53
Show Gist options
  • Save shoghicp/24ef70a73959437f8b78 to your computer and use it in GitHub Desktop.
Save shoghicp/24ef70a73959437f8b78 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;
}
}
Copy link

ghost commented Oct 20, 2016

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment