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

How to reset a world

Comments in 'Plugin Development' started by AndrewBit, Sep 30, 2015.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I'm trying to code a minigame right now, but I need know how to reset a full world as well

    Can anyone tell me the code? :')
  2. PianoPlayer38
    Offline

    PianoPlayer38 New Member

    Joined:
    Sep 29, 2015
    Posts:
    3
    Minecraft User:
    Ment97
    That's the only thing coded by @MCG76 that is propably the best. See MCG plugins with world resetting.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Create an archive of the world. Unload level, delete level, extract archive, reload level.
  4. MrGenga
    Offline

    MrGenga Active Member

    Joined:
    Jul 24, 2015
    Posts:
    75
    Minecraft User:
    MrGenga
    You can save world blocks in the text file, and create function, to load level from this text file.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Very laggy. And it is not as easy as you think.
    Taha_The_Hacker and hoyinm14mc like this.
  6. TheMaks
    Offline

    TheMaks Active Member

    Joined:
    Jul 19, 2015
    Posts:
    58
    Minecraft User:
    TheMaks
    Example code pos
  7. MrGenga
    Offline

    MrGenga Active Member

    Joined:
    Jul 24, 2015
    Posts:
    75
    Minecraft User:
    MrGenga
    Use this in the AsyncTask, because PocketMine-MP disallow to change, read files(not Config class) from out of AsyncTask.
    Code:
    Server::getInstance()->unloadLevel(Server::getInstance()->getLevelByName("example"));
    $archive = new \ZipArchive;
    $archive->open($this->getDataFolder() . "example.zip");
    $archive->extractTo(Server::getInstance()->getDataPath()."worlds/");
    $archive->close();
    Server::getInstance()->loadLevel(Server::getInstance()->getLevelByName("example"));
    
  8. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    public function reset() {
    //tp everybody to another world temporary
            
    $this->getServer()->loadLevel("Redirect");
            foreach(
    $this->getServer()->getOnlinePlayers() as $p){
                
    $p->getInventory()->clearAll();
                
    $p->teleport($this->getServer()->getLevelByName("Redirect")->getSafeSpawn());
            }
    //replace world
            
    $this->getServer()->unloadLevel($this->getServer()->getLevelByName("WorldToReset"));
            
    $path $this->getServer()->getDataPath();
            
    $this->recurse_copy($path."worlds/WorldBackup",$path."worlds/WorldToReset");
            
    $this->getServer()->loadLevel("WorldToReset");
    //tp everybody back to the world
            
    foreach($this->getServer()->getOnlinePlayers() as $p){
                
    $p->teleport($this->getServer()->getLevelByName("WorldToReset")->getSafeSpawn());
            }
        }
       
        public function 
    recurse_copy($src,$dst) {
        
    $dir opendir($src);
        @
    mkdir($dst);
        while(
    false !== ( $file readdir($dir)) ) {
            if (( 
    $file != '.' ) && ( $file != '..' )) {
                if ( 
    is_dir($src '/' $file) ) {
                    
    $this->recurse_copy($src '/' $file,$dst '/' $file);
                }
                else {
                    
    copy($src '/' $file,$dst '/' $file);
                }
            }
        }
        
    closedir($dir);
        } 
    AndrewBit likes this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Text file? Terrible idea.
    PHP:
    public static function doBackup($backupPath$worldPath){
      
    $zip = new \ZipArchive;
      
    $zip->open($backupPath, \ZipArchive::CREATE);
      foreach(new \
    RecursiveIteratorIterator(new \RecursiveDirectoryIterator($worldPath)) as $file){
        if(
    is_file($file)) $zip->addFile($filestr_replace("\\""/"ltrim(substr($filestrlen($worldPath)), "/\\")));
      }
    }
    public function 
    resetLevel(Server $server$levelName$backupPath){
      
    $lv $server->getLevelByName($levelName);
      if(
    $lv === null) return false;
      
    $server->unloadLevel($lv);
      
    $worldPath $server->getDataPath() . "worlds/" $levelName;
      
    self::file_delDir($worldPath);
      
    mkdir($worldPath);
      
    $zip = new \ZipArchive;
      
    $zip->open($backupPath);
      
    $zip->extractTo($worldPath);
      
    $server->loadLevel($levelName);
    }
    public static function 
    file_delDir($dir){
      
    $dir rtrim($dir"/\\") . "/";
      foreach(
    scandir($dir) as $file){
        if(
    $file === "." or $file === ".."){
          continue;
        }
        
    $path $dir $file;
        if(
    is_dir($path)){
          
    self::dir_delete($path);
        }else{
          
    unlink($path);
        }
      }
      
    rmdir($dir);
    }
  10. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Better question yet, how would you reset chunks in a world that is still loaded (if your server only has one world.

    Would you just save the chunk files, unload the chunk and then replace the chunk files?
  11. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    Better;)
  12. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    maybe if i disable level saving so i can only unload and load it right?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Search the forums. @Falk has posted something pretty useful.
  14. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    It looks pretty good! Can you explain with comments how it works? There are somethings that I really didn't understand
  15. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Can you explain how recurse_copy function works?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Is there even such function? :p
    PHP:
    public static function doBackup($backupPath$worldPath){
      
    $zip = new \ZipArchive// initialize a zip object
      
    $zip->open($backupPath, \ZipArchive::CREATE); // create the zip file at the desired path
      
    foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($worldPath)) as $file){ // loop through the world directory recursively
        
    if(is_file($file)) // don't add directories
          
    $zip->addFile($filestr_replace("\\""/"ltrim(substr($filestrlen($worldPath)), "/\\"))); // add file into zip.
    // The second parameter if the path of the file inside the zip.
    // The str_replace is to replace the windows file separator \ into the common /.
    // substr calculates the local path by removing the directory we are looping through.
      
    }
    }
    public function 
    resetLevel(Server $server$levelName$backupPath){
      
    // attempt to unload level
      
    $lv $server->getLevelByName($levelName);
      if(
    $lv === null) return false;
      
    $server->unloadLevel($lv);
      
    // delete worlds
      
    $worldPath $server->getDataPath() . "worlds/" $levelName;
      
    self::file_delDir($worldPath);
      
    // make new world directory
      
    mkdir($worldPath);
      
    // extract zip file into world path
      
    $zip = new \ZipArchive;
      
    $zip->open($backupPath);
      
    $zip->extractTo($worldPath);
      
    // reload level
      
    $server->loadLevel($levelName);
    }
    public static function 
    file_delDir($dir){
      
    // clean the directory name to ensure it is suffixed with /
      
    $dir rtrim($dir"/\\") . "/";
      
    // Loop through the files and directories in this directory, NOT recursively
      
    foreach(scandir($dir) as $file){
        
    // ignore these two evil sources of bugs
        
    if($file === "." or $file === ".."){
          continue;
        }
        
    // calculate the real path of the file
        
    $path $dir $file;
        
    // use recursive functions go delete the child directory cleanly
        
    if(is_dir($path)){
          
    self::dir_delete($path);
        }else{
          
    // otherwise, simply delete the file.
          
    unlink($path);
        }
      }
      
    // remove directory. This function only works when the directory is empty.
      
    rmdir($dir);
    }
  17. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    It don't work, we can't be teleport to the world after reseting.

    I use ManyWorld and it say "No world with the name .......... exists!"
  18. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    ManyWorlds might load the worlds into an array when it's enabled so it isn't constantly checking the worlds folder (Not sure in this, I haven't used ManyWorlds or looked through it's source). I'd suggest you just make your own plugin to teleport players between worlds as, it can eliminate other issues you may come across.
  19. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    I've correct the bug now it work:)

Share This Page

Advertisement