Loading Worlds and Running Batch File.

Discussion in 'Plugin Development' started by nicholasntp, Apr 14, 2012.

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

    nicholasntp

    When the plugin enables, I want it to load a world (which will be the main world) from a zip/tar file (un zip/tar it and load it) and on disable, I want it to delete the world. Kind of like a restarted world every time you start the server. Thanks.

    ~ nicholasntp

    And also, when the plugin disables, aka server stops, I want it to launch a batch file. How would I do so?

    Anyone have anything? Especially for my second question?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  2. Offline

    ferrybig

    ou can register an thread at Runtime to run when the process "java" is shutting down
    you can use an processbuilder to launch other processes, like cmd
    You can use some methodes at server class to load worlds

    Is it just me, or is evryone getting 502 errors?
     
  3. Offline

    nicholasntp

    Well I know all of those things.... But can you like give me an example or something? And yes, We all are getting those errors.
     
  4. Offline

    ferrybig

    Code:java
    1. Runtime.addShutdownHook(new Thread(new Runnable(){public void run(){
    2. // runned on program exit, not bukkit thread safe
    3. }}));
     
  5. Offline

    nicholasntp

    ??? Anyone? I still don't know how to launch a bat, or un-tar a world and load it, etc.
     
  6. Offline

    SirTyler

    Code:java
    1. Process child = Runtime.getRuntime().exec("ls");
     
  7. Offline

    nicholasntp

    Okay great! Ill try this for a batch file... now all i need to do is the world stuff :)
     
  8. Offline

    nicholasntp

    So how could I load a world from a backup, (tar or zip), in some python or batch script?
     
  9. Offline

    ferrybig

    You can make your own java program, thats doing those file move/copy when its started from your script. I dont have mutch expirimense whit files and sutch, but I can make an example code mayby, but not now
     
  10. Offline

    nicholasntp

    An example or maybe even help is what Im looking for. Thanks :)
     
  11. Offline

    ferrybig

    try making your own stuff whit the things I given, that way you would learn more, I dont have anny examples from this

    I found an example at the internet whit google, mayby you can search the next time?
    Code:java
    1.  
    2. public static void copyFile(File source, File dest) throws IOException {
    3. if(!dest.exists()) {
    4. dest.createNewFile();
    5. }
    6. InputStream in = null;
    7. OutputStream out = null;
    8. try {
    9. in = new new FileInputStream(source);
    10. out = new FileOutputStream(dest);
    11.  
    12. // Transfer bytes from in to out
    13. byte[] buf = new byte[1024];
    14. int len;
    15. while ((len = in.read(buf)) > 0) {
    16. out.write(buf, 0, len);
    17. }
    18. }
    19. finally {
    20. if(in != null) {
    21. in.close();
    22. }
    23. if(out != null) {
    24. out.close();
    25. }
    26. }
    27. }
    28.  
    29.  
    EDIT: http://lmgtfy.com/?q=copy+file+java+site:http://www.javalobby.org

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  12. Offline

    nicholasntp

    Yes, but I need to unrar and stuff. Ill figure it out :p
     
  13. Offline

    Technius

    I think you can only do ZIP archives without any external libraries. The following code is untested. It might mess up while copying directories, but I'll mess around with it and see what I can come up with.
    Code:
    File file = new File("yourzip.zip");
    ZipFile zip = new ZipFile(file);
    File destination = new File("destination");
    InputStream is;
    OutputStream out;
    try
    {
        for(ZipEntry z:zip.entries)
        {
            byte buffer = new byte[1024];
            int len;
            is = zip.getEntry(z.getName());
            out = new OutputStream(destination);
            while((len = is.read(buf)) > 0)out.write(buf, 0, len);
            out.flush();
            out.close();
            in.close();
        }
    }
    catch(IOException ioe)
    {
        //error
    }
    
    I use code tags because of formatting issues... XenForo, fix syntax tags!
     
  14. Offline

    ferrybig

    its better to use finly tags around the zip file reading and that kin o stuff, just to be sure
     
Thread Status:
Not open for further replies.

Share This Page