Search in sources :

Example 1 with WorldManager

use of mudmap2.backend.WorldManager in project mudmap2 by Neop.

the class WorldFileList method writeWorldList.

/**
 * Saves the available worlds list
 * do this after writing the world files or new places won't appear in list
 */
public static void writeWorldList() {
    final String file = Environment.getWorldsDir() + "worlds";
    try {
        // open file
        if (!Environment.isDirectory(Environment.getWorldsDir()))
            Environment.createDirectory(Environment.getWorldsDir());
        try (PrintWriter outstream = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
            outstream.println("# MUD Map (v2) worlds file");
            outstream.println("ver " + META_FILE_VER_MAJOR + "." + META_FILE_VER_MINOR);
            for (Map.Entry<String, String> w : availableWorlds.entrySet()) {
                // check whether the file name in file equals the name in the list
                WorldFileDefault worldFile = new WorldFileDefault(w.getKey());
                String fw = null;
                try {
                    if (worldFile.canRead()) {
                        fw = worldFile.readWorldName();
                    }
                } catch (Exception ex) {
                    Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (fw != null) {
                    outstream.println("n " + fw);
                    String w_file = w.getKey();
                    outstream.println("f " + w_file);
                    if (w_file.startsWith(Environment.getWorldsDir())) {
                        w_file = w_file.substring(Environment.getWorldsDir().length());
                        outstream.println("g " + w_file);
                    }
                }
            }
        }
    } catch (IOException ex) {
        System.out.printf("Couldn't write worlds file " + file);
        Logger.getLogger(WorldManager.class.getName()).log(Level.INFO, null, ex);
        JOptionPane.showMessageDialog(null, "Could not write worlds list file " + file + ".\nYou might have to open your worlds manually from " + Environment.getWorldsDir(), "WorldManager", JOptionPane.WARNING_MESSAGE);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WorldFileDefault (mudmap2.backend.WorldFileReader.current.WorldFileDefault)1