diff --git a/resources/BackgroundMusic Pack/manifest.json b/BackgroundMusic Pack/manifest.json similarity index 100% rename from resources/BackgroundMusic Pack/manifest.json rename to BackgroundMusic Pack/manifest.json diff --git a/resources/BackgroundMusic Pack/pack_icon.png b/BackgroundMusic Pack/pack_icon.png similarity index 100% rename from resources/BackgroundMusic Pack/pack_icon.png rename to BackgroundMusic Pack/pack_icon.png diff --git a/resources/BackgroundMusic Pack/sounds/C418Sweden.ogg b/BackgroundMusic Pack/sounds/C418Sweden.ogg similarity index 100% rename from resources/BackgroundMusic Pack/sounds/C418Sweden.ogg rename to BackgroundMusic Pack/sounds/C418Sweden.ogg diff --git a/resources/BackgroundMusic Pack/sounds/LICENSE b/BackgroundMusic Pack/sounds/LICENSE similarity index 100% rename from resources/BackgroundMusic Pack/sounds/LICENSE rename to BackgroundMusic Pack/sounds/LICENSE diff --git a/resources/BackgroundMusic Pack/sounds/sound_definitions.json b/BackgroundMusic Pack/sounds/sound_definitions.json similarity index 100% rename from resources/BackgroundMusic Pack/sounds/sound_definitions.json rename to BackgroundMusic Pack/sounds/sound_definitions.json diff --git a/src/NhanAZ/BackgroundMusic/Main.php b/src/NhanAZ/BackgroundMusic/Main.php index e433f96..e1ec037 100644 --- a/src/NhanAZ/BackgroundMusic/Main.php +++ b/src/NhanAZ/BackgroundMusic/Main.php @@ -4,22 +4,50 @@ namespace NhanAZ\BackgroundMusic; +use NhanAZ\BackgroundMusic\task\DownloadTask; +use NhanAZ\BackgroundMusic\task\GetInfoTask; use NhanAZ\libBedrock\ResourcePackManager; use pocketmine\event\Listener; -use pocketmine\plugin\PluginBase; -use pocketmine\scheduler\ClosureTask; use pocketmine\event\player\PlayerJoinEvent; use pocketmine\network\mcpe\protocol\PlaySoundPacket; +use pocketmine\plugin\PluginBase; +use pocketmine\scheduler\ClosureTask; +use pocketmine\utils\SingletonTrait; +use Symfony\Component\Filesystem\Path; +use Phar; class Main extends PluginBase implements Listener { + use SingletonTrait; + + const PACK_URL = "https://api.github.com/repos/jacob3105/BackgroundMusic/contents/BackgroundMusic%20Pack"; + + protected function onLoad(): void { + self::setInstance($this); + $pluginPath = (($phar = Phar::running()) === "") ? $this->getFile() : $phar; + define("RESOURCE_PACK_PATH", Path::join($pluginPath, "resources", "BackgroundMusic Pack")); + if (!is_dir(RESOURCE_PACK_PATH)) { + @mkdir(RESOURCE_PACK_PATH); + $this->getLogger()->info("Downloading BackgroundMusic Pack..."); + $this->getServer()->getAsyncPool()->submitTask(new GetInfoTask(self::PACK_URL)); + } else { + try { + ResourcePackManager::registerResourcePack($this); + } catch (\Exception) { + @rmdir(RESOURCE_PACK_PATH); + } + } + } + protected function onEnable(): void { $this->getServer()->getPluginManager()->registerEvents($this, $this); - ResourcePackManager::registerResourcePack($this); } protected function onDisable(): void { - ResourcePackManager::unRegisterResourcePack($this); + try { + ResourcePackManager::unRegisterResourcePack($this); + } catch (\Exception) { + } } public function onJoin(PlayerJoinEvent $event): void { @@ -37,6 +65,7 @@ public function onJoin(PlayerJoinEvent $event): void { if ($player->isOnline()) { $player->getNetworkSession()->sendDataPacket($packet); } - }), 4400); /** 3m40s = 220s * 20 tick = 4400 tick */ + }), 4400); + /** 3m40s = 220s * 20 tick = 4400 tick */ } } diff --git a/src/NhanAZ/BackgroundMusic/task/DownloadTask.php b/src/NhanAZ/BackgroundMusic/task/DownloadTask.php new file mode 100644 index 0000000..f1be1a3 --- /dev/null +++ b/src/NhanAZ/BackgroundMusic/task/DownloadTask.php @@ -0,0 +1,47 @@ +setResult(Internet::getURL($this->url)); + } + + public function onCompletion(): void { + if (!$this->getResult() instanceof InternetRequestResult) { + return; + } + $content = $this->getResult()->getBody(); + if (($phar = Phar::running()) === "") { // Plugin folder + file_put_contents($this->path, $content); + } else { + $phar = new \PharData($phar); + $phar->addFromString($this->path, $content); + $phar->compress(Phar::GZ); // wtf poggit?? + } + if ($this->finish) { + Server::getInstance()->getLogger()->debug("Downloaded BackgroundMusic Pack!"); + ResourcePackManager::registerResourcePack(Main::getInstance()); + } + } +} diff --git a/src/NhanAZ/BackgroundMusic/task/GetInfoTask.php b/src/NhanAZ/BackgroundMusic/task/GetInfoTask.php new file mode 100644 index 0000000..adbb4ad --- /dev/null +++ b/src/NhanAZ/BackgroundMusic/task/GetInfoTask.php @@ -0,0 +1,56 @@ +setResult(Internet::getURL($this->url)); + } + + public function onCompletion(): void { + $result = $this->getResult(); + if (!$result instanceof InternetRequestResult) { + return; + } + if ($result->getCode() !== 200) { // Error + @unlink(Path::join(RESOURCE_PACK_PATH)); + return; + } + $contents = json_decode($result->getBody(), true); + $endFile = end($contents); + foreach ($contents as $content) { + if ($content["type"] === "dir") { + $realPath = Path::join(RESOURCE_PACK_PATH, "..", $content["path"]); + if (!is_dir($realPath)) { + @mkdir($realPath); + } + Server::getInstance()->getAsyncPool()->submitTask(new GetInfoTask($content["url"])); + } else { + $path = Path::join(RESOURCE_PACK_PATH, "..", $content["path"]); + Server::getInstance()->getAsyncPool()->submitTask( + new DownloadTask( + $content["download_url"], + $path, + $endFile["type"] !== "dir" && $content["download_url"] === $endFile["download_url"] + )); + } + } + } +} \ No newline at end of file