22declare(strict_types=1);
24namespace pocketmine\updater;
32use
function strtolower;
38 protected string $endpoint;
40 private \Logger $logger;
42 public function __construct(
Server $server,
string $endpoint){
44 $this->logger = new \PrefixedLogger($server->getLogger(),
"Update Checker");
45 $this->endpoint =
"http://$endpoint/api/";
47 if($server->getConfigGroup()->getPropertyBool(YmlServerProperties::AUTO_UPDATER_ENABLED,
true)){
52 public function checkUpdateError(
string $error) :
void{
53 $this->logger->debug(
"Async update check failed due to \"$error\"");
63 if($this->
server->getConfigGroup()->getPropertyBool(YmlServerProperties::AUTO_UPDATER_ON_UPDATE_WARN_CONSOLE,
true)){
67 if(!VersionInfo::IS_DEVELOPMENT_BUILD && $this->
getChannel() !==
"stable"){
68 $this->showChannelSuggestionStable();
69 }elseif(VersionInfo::IS_DEVELOPMENT_BUILD && $this->
getChannel() ===
"stable"){
70 $this->showChannelSuggestionBeta();
79 return $this->updateInfo !== null;
86 if($this->updateInfo === null){
89 $newVersion =
new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
91 "Your version of " . $this->
server->getName() .
" is out of date. Version " . $newVersion->getFullVersion(
true) .
" was released on " . date(
"D M j h:i:s Y", $this->updateInfo->date)
94 $messages[] =
"Details: " . $this->updateInfo->details_url;
95 $messages[] =
"Download: " . $this->updateInfo->download_url;
97 $this->printConsoleMessage($messages, \LogLevel::WARNING);
100 protected function showChannelSuggestionStable() : void{
101 $this->printConsoleMessage([
102 "You're running a Stable build, but you're receiving update notifications for " . ucfirst($this->getChannel()) .
" builds.",
103 "To get notified about new Stable builds only, change 'preferred-channel' in your pocketmine.yml to 'stable'."
107 protected function showChannelSuggestionBeta() : void{
108 $this->printConsoleMessage([
109 "You're running a Beta build, but you're receiving update notifications for Stable builds.",
110 "To get notified about new Beta or Development builds, change 'preferred-channel' in your pocketmine.yml to 'beta' or 'development'."
118 foreach($lines as $line){
119 $this->logger->log($logLevel, $line);
127 return $this->updateInfo;
134 $this->
server->getAsyncPool()->submitTask(new
UpdateCheckTask($this, $this->endpoint, $this->getChannel()));
143 $newVersion =
new VersionString($updateInfo->base_version, $updateInfo->is_dev, $updateInfo->build);
144 }
catch(\InvalidArgumentException $e){
146 $this->logger->debug(
"Assuming no update because \"" . $e->getMessage() .
"\"");
150 if($currentVersion->getBuild() > 0 && $currentVersion->compare($newVersion) > 0){
151 $this->updateInfo = $updateInfo;
153 $this->logger->debug(
"API reported version is an older version or the same version (" . $newVersion->getFullVersion() .
"), not showing notification");
161 return strtolower($this->
server->getConfigGroup()->getPropertyString(
YmlServerProperties::AUTO_UPDATER_PREFERRED_CHANNEL,
"stable"));
168 return $this->endpoint;
checkUpdate(UpdateInfo $updateInfo)
printConsoleMessage(array $lines, string $logLevel=\LogLevel::INFO)
checkUpdateCallback(UpdateInfo $updateInfo)