Created
April 9, 2015 18:53
-
-
Save shoghicp/24ef70a73959437f8b78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
nice