Skip to content
This repository has been archived by the owner on May 30, 2018. It is now read-only.

Commit

Permalink
Optimized Updater :D Fixed typo with ClearInventory (Blame @64FF00 fo…
Browse files Browse the repository at this point in the history
…r that typo :P )
  • Loading branch information
iksaku committed May 6, 2015
1 parent 3ffdaeb commit 30158fc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/EssentialsPE/Commands/ClearInventory.php
Expand Up @@ -47,7 +47,7 @@ public function execute(CommandSender $sender, $alias, array $args){
return false;
}
$player->getInventory()->clearAll();
$sender->sendMessage(TextFormat::AQUA . $player->getDisplayName() . ($player->getDisplayName(), -1, 1) === "s" ? "'" : "'s") . " inventory was cleared");
$sender->sendMessage(TextFormat::AQUA . $player->getDisplayName() . (substr($player->getDisplayName(), -1, 1) === "s" ? "'" : "'s") . " inventory was cleared");
$player->sendMessage(TextFormat::AQUA . "Your inventory was cleared");
break;
default:
Expand Down
3 changes: 2 additions & 1 deletion src/EssentialsPE/Loader.php
Expand Up @@ -1981,6 +1981,7 @@ public function fetchEssentialsPEUpdate($install = false){
if($this->updaterTask !== null && $this->updaterTask->isRunning()){
return false;
}
$this->getServer()->getLogger()->debug(TextFormat::YELLOW . "Running EssentialsPE's UpdateFetchTask");
$this->getServer()->getScheduler()->scheduleAsyncTask($task = new UpdateFetchTask($this->getUpdateBuild(), $install));
$this->updaterTask = $task;
return true;
Expand All @@ -2002,7 +2003,7 @@ public function scheduleUpdaterTask(){
*/
public function broadcastUpdateAvailability($message){
if($this->getConfig()->getNested("updater.warn-console")){
$this->getLogger()->info($message);
$this->getServer()->getLogger()->info($message);
}
if($this->getConfig()->getNested("updater.warn-players")){
foreach($this->getServer()->getOnlinePlayers() as $p){
Expand Down
1 change: 0 additions & 1 deletion src/EssentialsPE/Tasks/Updater/AutoFetchCallerTask.php
Expand Up @@ -6,7 +6,6 @@
use pocketmine\utils\TextFormat;

class AutoFetchCallerTask extends BaseTask{

public function __construct(Loader $plugin){
parent::__construct($plugin);
}
Expand Down
38 changes: 2 additions & 36 deletions src/EssentialsPE/Tasks/Updater/UpdateFetchTask.php
Expand Up @@ -34,26 +34,17 @@ public function onRun(){
$r["downloadURL"] = "http://forums.pocketmine.net/plugins/essentialspe.886/download?version=" . $i["current_version_id"];
break;
case "beta":
foreach($i as $release){
if($release["prerelease"] === true){
$i = $release;
break;
}
}
$i = $i[0]; // Grab the latest version from Github releases... Doesn't matter if it's Beta or Stable :3
$r["version"] = substr($i["name"], 13);
$r["downloadURL"] = $i["assets"][0]["browser_download_url"];
break;
/*case "development":
// IDK How to do this :(
break;*/
}
$this->setResult($r);
}

public function onCompletion(Server $server){
/** @var Loader $esspe */
$esspe = $server->getPluginManager()->getPlugin("EssentialsPE");
$esspe->getServer()->getLogger()->debug(TextFormat::YELLOW . "Running EssentialsPE's UpdateFetchTask");
if($esspe->getDescription()->getVersion() < ($v = $this->getResult()["version"])){
$continue = true;
$message = TextFormat::AQUA . "[EssentialsPE]" . TextFormat::GREEN . " A new " . TextFormat::YELLOW . $this->build . TextFormat::GREEN . " version of EssentialsPE found! Version: " . TextFormat::YELLOW . $v . TextFormat::GREEN . ($this->install !== true ? "" : ", " . TextFormat::LIGHT_PURPLE . "Installing...");
Expand All @@ -63,32 +54,7 @@ public function onCompletion(Server $server){
}
$esspe->broadcastUpdateAvailability($message);
if($continue && $this->install){
$this->install($server);
$esspe->broadcastUpdateAvailability($server->getLogger()->info(TextFormat::AQUA . "[EssentialsPE]" . TextFormat::YELLOW . " Successfully updated to version " . TextFormat::GREEN . $v . TextFormat::YELLOW . ". To start using the new features, please fully restart your server."));
$server->getScheduler()->scheduleAsyncTask(new UpdateInstallTask($esspe, $this->getResult()["downloadURL"], $server->getPluginPath(), $v));
}
$esspe->scheduleUpdaterTask();
}

public function install(Server $server){
$url = $this->getResult()["downloadURL"];
if(file_exists($server->getPluginPath() . "EssentialsPE.phar")){
unlink($server->getPluginPath() . "EssentialsPE.phar");
}
$file = fopen($server->getPluginPath() . "EssentialsPE.phar", 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
file_put_contents($server->getPluginPath() . "EssentialsPE.phar", curl_exec($ch));
curl_close($ch);
fclose($file);
}
}
52 changes: 52 additions & 0 deletions src/EssentialsPE/Tasks/Updater/UpdateInstallTask.php
@@ -0,0 +1,52 @@
<?php
namespace EssentialsPE\Tasks\Updater;

use EssentialsPE\Loader;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\TextFormat;

class UpdateInstallTask extends AsyncTask{
/** @var string */
private $url;
/** @var string */
private $pluginPath;
/** @var string */
private $newVersion;
/** @var Loader */
private $plugin;

public function __construct(Loader $plugin, $url, $pluginPath, $newVersion){
$this->url = $url;
$this->pluginPath = $pluginPath;
$this->newVersion = $newVersion;
$this->plugin = $plugin;
}

public function onRun(){
if(file_exists($this->pluginPath . "EssentialsPE.phar")){
unlink($this->pluginPath . "EssentialsPE.phar");
}
$file = fopen($this->pluginPath . "EssentialsPE.phar", 'w+');
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
file_put_contents($this->pluginPath . "EssentialsPE.phar", curl_exec($ch));
curl_close($ch);
fclose($file);
}

public function onCompletion(Server $server){
$server->getLogger()->info(TextFormat::AQUA . "[EssentialsPE]" . TextFormat::YELLOW . " Successfully updated to version " . TextFormat::GREEN . $this->newVersion . TextFormat::YELLOW . ". To start using the new features, please fully restart your server.");
$this->plugin->scheduleUpdaterTask();
}
}

0 comments on commit 30158fc

Please sign in to comment.