Skip to content

Commit

Permalink
load only the worlds you need
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyutsuki committed Nov 16, 2023
1 parent f548a9c commit 060cb78
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/jp/mcbe/fuyutsuki/Texter/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
use jp\mcbe\fuyutsuki\Texter\task\CheckUpdateTask;
use jp\mcbe\fuyutsuki\Texter\util\dependencies\Dependencies;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\ClosureTask;
use pocketmine\utils\TextFormat;
use pocketmine\utils\VersionString;
use function array_key_last;
use function explode;
use function file_exists;
use function glob;
use function is_dir;
use function mkdir;
use function str_starts_with;
Expand All @@ -67,6 +67,7 @@ public function onLoad(): void {
self::setPrefix();
$this->loadResources();
$this->registerCommands();
$this->convertOldFloatingTexts();
$this->loadFloatingTexts();
$this->checkUpdate();
}
Expand Down Expand Up @@ -121,7 +122,7 @@ private function registerCommands() {
$this->getLogger()->info(($isCanUse ? TextFormat::GREEN : TextFormat::RED) . $message);
}

private function loadFloatingTexts() {
private function convertOldFloatingTexts() {
$floatingTextDir = $this->getDataFolder() . FloatingTextData::FLOATING_TEXT_DIRECTORY;
if (!file_exists($floatingTextDir)) {
mkdir($floatingTextDir, 0755, true);
Expand All @@ -142,17 +143,18 @@ private function loadFloatingTexts() {
$uftFile = new OldFloatingTextData($this, $dir, OldFloatingTextData::FILE_UFT);
$uftFile->convert();
}
}

$worldsPath = $this->findWorldsPath();
foreach ($worldsPath as $worldPath) {
$folderName = $this->getFileName($worldPath);
$floatingTextData = FloatingTextData::getInstance($folderName);
if ($floatingTextData === null) {
$floatingTextData = new FloatingTextData($this, $folderName);
}
$floatingTextData->generateFloatingTexts($this);
$this->getLogger()->debug("Loaded FloatingText file: $folderName.json");
}
private function loadFloatingTexts() {
$this->getScheduler()->scheduleDelayedTask(
new ClosureTask(function () {
$defaultWorldFolderName = $this->getServer()->getWorldManager()->getDefaultWorld()->getFolderName();
$floatingTextData = new FloatingTextData($this, $defaultWorldFolderName);
$floatingTextData->generateFloatingTexts($this);
$this->getLogger()->debug("Loaded FloatingText file: $defaultWorldFolderName.json");
}),
$this->isDebug() ? 5 * 20 : 1
);
}

public function checkUpdate() {
Expand Down Expand Up @@ -227,22 +229,13 @@ private function unlinkRecursive(string $dir): bool {
return rmdir($dir);
}

private function getFileName(string $path): string {
$exploded = explode(DIRECTORY_SEPARATOR, $path);
return $exploded[array_key_last($exploded)];
}

private function getFileExtension(string $path): string {
$exploded = explode(".", $path);
return $exploded[array_key_last($exploded)];
}

private function getWorldsPath(): string {
return $this->getServer()->getDataPath() . "worlds" . DIRECTORY_SEPARATOR;
}

private function findWorldsPath(): array {
return glob($this->getWorldsPath() . "*");
private function isDebug(): bool {
return Main::canLoadDependencyFromComposer() && class_exists(Dependencies::PMFORMS);
}

private function isPhar(): bool {
Expand Down

0 comments on commit 060cb78

Please sign in to comment.