Load World from specific File

Title says it all, is it possible to load a world from a file/path denoting its home directory?

Since I don’t think sponge has a method for this, I would imagine you would do this by using Files.copy() and placing the world files in your plugins resources folder. Like so

Path newWorldDir = new File(Sponge.getGame().getSavesDirectory() + "customworldname").toPath();
Path worldDir = new File("src/main/resources/worlds/yourworld/").toPath();
try{
    Files.copy(newWorldDir, worldDir, StandardCopyOption.REPLACE_EXISTING);
}catch(IOException e){
    e.printStackTrace();
}

Have not tested this but this is how I think you could do it.Like you might have to create the new world directory before you create the path? but again you should test this out first.

As @intronate67 mentioned, there is no way to load a world other than from minecraft’s saves directory.

If you are running on a unix/linux system you could also create a symlink to the world you want instead, meaning you won’t have 2 copies. This is assuming that its located in disk, not in your plugin’s resources.

Path link = Sponge.getGame().getSavesDirectory().resolve("custom_world");
Path target = ...;
try {
    Files.createSymbolicLink(newLink, target);
} catch (IOException x) {
    System.err.println(x);
} catch (UnsupportedOperationException x) {
    // Some file systems do not support symbolic links.
    System.err.println(x);
}
  1. Thanks for the potential solutions!
  2. No, @ZephireNZ, I’m not running unix, and I don’t want it to be OS dependent
  3. I think I’ll just be copying files async then, thanks for your help though

You should use Sponge.getGame().getSavesDirectory().resolve("customworldname");
instead of new File(Sponge.getGame().getSavesDirectory() + "customworldname").toPath();
and Paths.get("src/main/resources/worlds/yourworld/");
instead of new File("src/main/resources/worlds/yourworld/").toPath();

1 Like