Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Deleting and Duplicating Worlds

Comments in 'Plugin Development' started by Calrizer, Jul 16, 2015.

  1. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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
    Last edited: Jul 16, 2015
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  3. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  5. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But if you delete, your folders aren't deleted. You must recursively remove folders one by one.
    Calrizer likes this.
  7. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Ok cheers, thanks for the help!
  8. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Oh sorry, I should have used unlink($path) instead of unlink($file). Thanks for pointing it out.
    Calrizer likes this.
  10. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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?
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    First unload the level.
    Then delete the whole folder. My function scans the folder recursively, so you can simply pass the parent folder.
  12. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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.
  13. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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?
    Last edited: Jul 17, 2015
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  15. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Wait, it is trim, but I didn't use it. I just used ltrim.
  17. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    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?
    Legoboy0215 likes this.
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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`).
  19. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    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();
        }
  20. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    I use Mac OS X for testing and Linux for release so would 'rm -r' work for both these operating systems?

Share This Page

Advertisement