Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/NhanAZ/BackgroundMusic/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 */
}
}
47 changes: 47 additions & 0 deletions src/NhanAZ/BackgroundMusic/task/DownloadTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace NhanAZ\BackgroundMusic\task;

use NhanAZ\BackgroundMusic\Main;
use NhanAZ\libBedrock\ResourcePackManager;
use Phar;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetRequestResult;

class DownloadTask extends AsyncTask {

public function __construct(
private string $url,
private string $path,
private bool $finish
) {}

/**
* @return void
*/
public function onRun(): void {
$this->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());
}
}
}
56 changes: 56 additions & 0 deletions src/NhanAZ/BackgroundMusic/task/GetInfoTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace NhanAZ\BackgroundMusic\task;

use NhanAZ\BackgroundMusic\Main;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetRequestResult;
use Symfony\Component\Filesystem\Path;

class GetInfoTask extends AsyncTask {

public function __construct(
private string $url
) {}

/**
* @return void
*/
public function onRun(): void {
$this->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"]
));
}
}
}
}