Can A Plugin Generate A Pre-created World Into the Server's World Files.

Discussion in 'Plugin Development' started by SnowGears, Nov 25, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    SnowGears

    Lets say I have a world that is just an island in the sky. This world is named "Island"

    When running the "island plugin", it would generate this custom world into the server's world files as "Island".

    How would I go about doing this?

    I was thinking maybe I could save the world file to my eclipse project and then transfer the data over to the server world files but Im not sure how to do this if this is the way to go.

    Can anyone help me with this please?
     
  2. Offline

    Zidkon

    I think the only problem is that the world is initialized when u are starting your server, before all the plugins all initialized, so here could be a problem.
     
  3. Offline

    SnowGears

    Hmm.. Im pretty sure other plugins I know of create a new world but I will have to find one specifically I guess.
     
  4. You could pack the world into your jar, then use Javas zip methods to extract it (if not already there) and then load it. You jar will get big through. So why exactly do you want that instead of giving the map as a separate download or writing a WorldGenerator that generates it on the fly?

    //EDIT: Zidkon The world is initialized as soon as minecraft knows about it. If no other plugin interferes this is the case when his plugin decides that it's time for, so this shouldn't be a problem.
     
  5. Offline

    The_Coder

    Just put: load: STARTUP in the plugin yml. That loads the plugin before the worlds.
     
    Tooner101 and Zidkon like this.
  6. Offline

    Zidkon

    Well the way the minecraft server loads or at least the messages I can read are something like first maps and then plugins, didn't know plugins can interfeer in map loading.

    But what do you think is the best way then, based on performance and stuff, is it better to pack the world and load it?, or generate structures on a empty world?
     
  7. Offline

    The_Coder

    Well it grabs all of the plugins first then loads the worlds an finally loads the plugins. Ether way you would need to make sure that the map would have the same name.
     
  8. Minecraft loads only worlds it knows about, a world called "Island" doesn't count to that. Also how do you think multiworld plugins like multiverse work? ;)
    Well, I would just create on-the-fly, so create a chunk whenever needed. All worlds are handled that way. ^^
     
  9. Offline

    SnowGears

    Well my plugin will create a separate arena map. Users will be able to warp to this map when they want to battle. Of course the server owner may replace this map with their own if they choose to but I want to have this map generated by default.

    I'm still not quite sure how to accomplish this after reading everyone's replies.
     
  10. Shhh, secret hint: I've been developing a plugin called "VirtualWorlds" for quite some time now, which is supposed to be an API for exactly that - and more. The idea is the same: you have a so-called snapshot (static world data) which only is read from and loaded into a virtual world. That means, the world has no actual representation as a file but only exists in memory - exactly what you need for short-term worlds like this.

    Huge benefit is that you can load a theoretically infinite amount of those worlds at the same time, all from the same snapshot.
    Still WIP, not released, though. Justed wanted to throw that out there, before you have to rewrite everything on your own ;)
     
  11. Offline

    Rprrr

    I'm not exactly sure if this is what the TS was looking for, but perhaps he wanted to know how copy, then paste a world from a certain folder and load that world. Well, like this:

    Code:
      public static void copyFolder(File src, File dest)
            throws IOException{
     
            if(src.isDirectory()){
     
                //if directory not exists, create it
                if(!dest.exists()){
                  dest.mkdir();
                  System.out.println("Directory copied from "
                                  + src + "  to " + dest);
                }
     
                //list all the directory contents
                String files[] = src.list();
     
                for (String file : files) {
                  //construct the src and dest file structure
                  File srcFile = new File(src, file);
                  File destFile = new File(dest, file);
                  //recursive copy
                  copyFolder(srcFile,destFile);
                }
     
            }else{
                //if file, then copy it
                //Use bytes stream to support all file types
                InputStream in = new FileInputStream(src);
                    OutputStream out = new FileOutputStream(dest);
     
                    byte[] buffer = new byte[1024];
     
                    int length;
                    //copy the file content in bytes
                    while ((length = in.read(buffer)) > 0){
                      out.write(buffer, 0, length);
                    }
     
                    in.close();
                    out.close();
                    System.out.println("File copied from " + src + " to " + dest);
            }
        }
       
        public static void replaceWorld(String worldname, String backupworldname){
           
            World world = Bukkit.getServer().getWorld(worldname);
            if(Bukkit.getServer().unloadWorld(world,false)) {
               
               
                // Delete, not sure if correct
                File tobereplaced = new File(plugin.getServer().getWorldContainer() + File.separator + worldname);
                tobereplaced.delete();
               
                File backup = new File(plugin.getServer().getWorldContainer() + File.separator + "worldbackups" +  File.separator + backupworldname);
               
                File folder = new File (plugin.getServer().getWorldContainer() + File.separator + worldname);
                try {
                    copyFolder(backup, folder);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               
                // Load new world
                new WorldCreator("crownconquest").createWorld();
           
            }
           
            else
            {
                Bukkit.getServer().getLogger().info("Could not unload " + worldname + " ! Failed to replace.");
            }
           
           
        }
       
        
     
    Tooner101 likes this.
  12. Offline

    SnowGears

    How would I get the location of the server's main folder where the worlds are stored?
     
  13. Offline

    Rprrr


    You can do that with getServer().getWorldContainer() :).
     
    Tooner101 likes this.
  14. Rprrr Now change that to extract the files from the jar instead of copying from a place from disc and it is what the TS wanted. ;)
    getServer().getWorlds().get(0).getWorldFolder().getParentFolder(); ;)

    //EDIT: For the world folder: What Rprrr told is probably better.

    //EDIT²: Tooner101 Another thing: If you create a custom world generator it would ofc. not overwrite already existing chunks, so a server admin could put his own world in. In any way you have to handle new chunks somehow, maybe just create empty ones?
     
Thread Status:
Not open for further replies.

Share This Page