Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created July 9, 2020 05:39
Show Gist options
  • Save PJZ9n/6f2554f4eb7426470acf26da05ca282a to your computer and use it in GitHub Desktop.
Save PJZ9n/6f2554f4eb7426470acf26da05ca282a to your computer and use it in GitHub Desktop.
ワールドコピーのテスト
<?php
public function onEnable(): void
{
$name = "world-" . uniqid();
$this->copyWorld("world", $name);
$this->getServer()->loadLevel($name);
var_dump($this->getServer()->getLevelByName($name)->getId());
$this->func("test", function () {
$closure = function () {
//
};
});
}
public function func($v1, $v2)
{
//
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool
{
return parent::onCommand($sender, $command, $label, $args); // TODO: Change the autogenerated stub
}
/**
* ワールドをコピーする
*
* @param string $from コピー元ワールド名(ディレクトリ名)
* @param string $to コピー先ワールド名(ディレクトリ名)
*/
public function copyWorld(string $from, string $to): void
{
/** @var Plugin $this */
$server = $this->getServer();
$baseWorldPath = $server->getDataPath() . "worlds/";
$fromPath = $baseWorldPath . $from . "/";
$toPath = $baseWorldPath . $to . "/";
if (!file_exists($fromPath)) throw new \RuntimeException("コピー元フォルダが存在しません。");
if (file_exists($toPath)) throw new \RuntimeException("コピー先フォルダが既に存在します。");
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$fromPath,
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME
),
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $pathName) {
//コピー先パスに置換
$replacedPathName = str_replace($fromPath, $toPath, $pathName);
//ディレクトリ作成
@mkdir(dirname($replacedPathName), 0755, true);
//コピー実行
copy($pathName, $replacedPathName);
//表示
$this->getLogger()->debug("copy: " . $pathName . " =>" . $replacedPathName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment