Hi today I was wondering if it is possible to use PHP(The API) to delete and duplicate the world folders. If it is possible could I see some code so I can test it out. Any response is appreciated, Cheers, -Calrizer
In that snippet from Legion what is the purpose of this: PHP: foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($from)) as $file){ To get the $file object?
The line iterates over all files on the given directory (including those in subdirectories) (directories and "." and ".." are returned as well, so remember to prevent them). $file is an SplFileInfo object, although we can use it as a string directly. The following lines check if $file represents a file, and edits its old path into its supposedly new path, attempt to create parent directories (recursively) if not already existing, and copy contents from old to new.
So I would use that to 'scan' my "worlds" directory to find the desired world, then once it returns $file I can then delete or duplicate.
I tried using your function but it keeps returning this error, any ideas? [WARNING] RuntimeException: "unlink(villages.dat): No such file or directory"
No problem, but I'm having trouble with the copy feature. Once I delete the world; which works, I replace it with a new clean copy of the world. But when I go on the world all the blocks that I placed on the old world are still there. He's my code: PHP: $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/BB-1/data"); $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/arena/DIM-1"); $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/arena/DIM1"); $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/arena/playerdata"); $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/arena/region"); $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/arena/stats"); $this->plugin->dir_delete($this->plugin->getServer()->getDataPath() . "worlds/arena");//Duplicating$this->plugin->dir_copy($this->plugin->getServer()->getDataPath() . "worlds/cleanworlds", $this->plugin->getServer()->getDataPath() . "worlds"); $this->plugin->dir_copy($this->plugin->getServer()->getDataPath() . "worlds/cleanworlds/arena", $this->plugin->getServer()->getDataPath() . "worlds/arena"); Do you think I need to unload the level before I delete it?
First unload the level. Then delete the whole folder. My function scans the folder recursively, so you can simply pass the parent folder.
Yep that seems to be working now, now time to stop all my repeating tasks shouting at me that the level is unloaded and it can find players! XD Cheers for your help.
Oops I spoke too soon, now I'm getting this: [WARNING] InvalidArgumentException: "trim() expects parameter 1 to be string, object given" (E_WARNING) in "/src/pocketmine/Server__64bit" at line 1040 I think it's coming from the $includePath = ltrim(substr($file, strlen($from)), "\\/"); on the copy directory function. The first parameter for this ($file) is the result of your reclusive parsing combined with ($from) which is: $this->plugin->getServer()->getDataPath() . "worlds/cleanworlds". Any sense?
Cast $file to a string. It is an SplFileInfo, but we can use it as a string directly in most cases. In this case, just cast it to string.
I casted the $file to $filestr using: PHP: $filestr = (string) $file; And it's still returning the same error. Do you think I should try to cast $to to a string because this also uses the 'trim' function?
I know weird right! Do you think you could show me a simple way of deleting a world and then duplicating a world with the same name from a folder within 'worlds' without using the function from Leigon?
The function is the simplest way... Unless you are sure of your operating system and run native commands (like `rm -r $folder` `cp -r $from $to`).
What about this? PHP: private function extractWorld($zipPath, $worldName){ $zip = new \ZipArchive; $errId = $zip->open($zipPath); // TODO: if errored, check $errId $zip->extractTo($this->getServer()->getDataPath() . "worlds/$worldName/"); $zip->close(); }
I use Mac OS X for testing and Linux for release so would 'rm -r' work for both these operating systems?