Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated how islands are stored and generated #4

Merged
merged 1 commit into from
Feb 20, 2022
Merged
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
1 change: 1 addition & 0 deletions resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ messages:
create: "You have successfully created your SkyBlock."
visit: "You are currently visiting {NAME}'s Island.."
delete: 'Deleting your SkyBlock Island..'
default-world: "You can not set the default world of the server as a skyblock world"
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo
$world->requestChunkPopulation($chunkX, $chunkZ, new PlayerChunkLoader($world->getSpawnLocation()))->onCompletion(
function () use ($world, $sender, $id, $player, $args){
$spawnLocation = $world->getSpawnLocation();
$world->setBlock($spawnLocation, VanillaBlocks::BEDROCK());
$sender->teleport(Position::fromObject($spawnLocation->up(), $world));
SkyBlocksPM::getInstance()->getSkyBlockManager()->createSkyBlock($id, $player, $args['name'], $world);
}, function () {
Expand Down
47 changes: 26 additions & 21 deletions src/Vecnavium/SkyBlocksPM/generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ class Generator
public function setIslandWorld(Player $player): void
{
$world = $player->getWorld();

$world->setSpawnLocation($player->getPosition());

$worldPath = SkyBlocksPM::getInstance()->getServer()->getDataPath() . "worlds" . DIRECTORY_SEPARATOR . $world->getFolderName();
$zip = new ZipArchive();
$islandZip = SkyBlocksPM::getInstance()->getDataFolder() . "island.zip";

if (is_file($islandZip))
unlink($islandZip);

$zip->open($islandZip, ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($world->getDisplayName() === SkyBlocksPM::getInstance()->getServer()->getWorldManager()->getDefaultWorld()->getDisplayName())
{
$player->sendMessage(SkyBlocksPM::getInstance()->getMessages()->getMessage('default-world'));
return;
}

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($worldPath)), RecursiveIteratorIterator::LEAVES_ONLY);

$player->teleport(SkyBlocksPM::getInstance()->getServer()->getWorldManager()->getDefaultWorld()->getSpawnLocation());
SkyBlocksPM::getInstance()->getServer()->getWorldManager()->unloadWorld($world);

/** @var SplFileInfo $file */
foreach ($files as $file)
{
if (!$file->isFile())
continue;
$filePath = $file->getPath() . DIRECTORY_SEPARATOR . $file->getBasename();
$localPath = substr($filePath, strlen(SkyBlocksPM::getInstance()->getServer()->getDataPath() . 'worlds' . DIRECTORY_SEPARATOR . $world->getFolderName()));
$zip->addFile($filePath, $localPath);
@mkdir(SkyBlocksPM::getInstance()->getDataFolder() . "cache/island/db");
copy($filePath, SkyBlocksPM::getInstance()->getDataFolder() . "cache/island/" . $localPath);
}

$zip->close();
}

/**
Expand All @@ -59,20 +59,25 @@ public function setIslandWorld(Player $player): void
*/
public function generateIsland(Player $player, string $folderName)
{
$zip = SkyBlocksPM::getInstance()->getDataFolder() . 'island.zip';

if (!is_file($zip))
{
$player->sendMessage("Default islands is not selected yet, please use the command '/is setworld' to set the default island world");
return;
}
$path = SkyBlocksPM::getInstance()->getDataFolder() . "cache/island";
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($path)), RecursiveIteratorIterator::LEAVES_ONLY);

$zipArchive = new ZipArchive();
$zipArchive->open($zip);
$path = SkyBlocksPM::getInstance()->getServer()->getDataPath() . "worlds" . DIRECTORY_SEPARATOR . $folderName;
@mkdir($path);
$zipArchive->extractTo($path);
$zipArchive->close();
@mkdir($path . "/db");

/** @var SplFileInfo $file */
foreach ($files as $file)
{
$filePath = $file->getPath() . DIRECTORY_SEPARATOR . $file->getBasename();
$localPath = substr($filePath, strlen(SkyBlocksPM::getInstance()->getDataFolder() . 'cache/island'));
if ($file->isDir())
{
@mkdir($path . $localPath);
continue;
}
copy($filePath, $path . DIRECTORY_SEPARATOR . $localPath);
}

SkyBlocksPM::getInstance()->getServer()->getWorldManager()->loadWorld($folderName);
$world = SkyBlocksPM::getInstance()->getServer()->getWorldManager()->getWorldByName($folderName);
Expand Down
2 changes: 1 addition & 1 deletion src/Vecnavium/SkyBlocksPM/skyblock/SkyBlockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getSkyBlockByWorld(World $world): ?SkyBlock
{
foreach ($this->SkyBlocks as $SkyBlock)
{
if ($SkyBlock->getWorld() == $world->getDisplayName())
if ($SkyBlock->getWorld() == $world->getFolderName())
return $SkyBlock;
}
return null;
Expand Down