Search in sources :

Example 1 with WorldFile

use of mudmap2.backend.WorldFileReader.WorldFile in project mudmap2 by Neop.

the class WorldTab method save.

/**
 * Saves the changes in the world
 */
public void save() {
    if (!worldPanel.isPassive()) {
        WorldFile worldFile = getWorld().getWorldFile();
        if (worldFile == null) {
            if (filename == null || filename.isEmpty() || (new File(filename)).exists()) {
                // get new filename
                int ret;
                do {
                    JFileChooser fileChooser = new JFileChooser(Environment.getWorldsDir());
                    ret = fileChooser.showSaveDialog(this);
                    if (ret == JFileChooser.APPROVE_OPTION) {
                        filename = fileChooser.getSelectedFile().getAbsolutePath();
                    } else {
                        ret = JOptionPane.showConfirmDialog(this, "No file chosen: " + getWorld().getName() + " will not be saved!", "Saving world", JOptionPane.OK_CANCEL_OPTION);
                        if (ret == JOptionPane.OK_OPTION)
                            return;
                    }
                } while (ret != JFileChooser.APPROVE_OPTION || ret != JOptionPane.OK_OPTION);
            }
            worldFile = new WorldFileDefault(filename);
            getWorld().setWorldFile(worldFile);
        }
        // set meta data writer
        if (worldFile.getWorldFileType() == WorldFileType.JSON) {
            WorldFileJSON wfj = null;
            if (worldFile instanceof WorldFileJSON) {
                wfj = (WorldFileJSON) worldFile;
            } else if (worldFile instanceof WorldFileDefault) {
                wfj = (WorldFileJSON) ((WorldFileDefault) worldFile).getWorldFile();
            }
            if (wfj != null)
                wfj.setMetaGetter(this);
        }
        // write world file
        try {
            worldFile.writeFile(getWorld());
            WorldManager.putWorld(worldFile.getFilename(), getWorld());
        } catch (IOException ex) {
            Logger.getLogger(WorldTab.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(getParent(), "Could not save world file " + worldFile.getFilename(), "Saving world file", JOptionPane.ERROR_MESSAGE);
        }
    }
    showMessage("World saved");
}
Also used : JFileChooser(javax.swing.JFileChooser) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) IOException(java.io.IOException) WorldFileJSON(mudmap2.backend.WorldFileReader.current.WorldFileJSON) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) File(java.io.File) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault)

Example 2 with WorldFile

use of mudmap2.backend.WorldFileReader.WorldFile 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)

Example 3 with WorldFile

use of mudmap2.backend.WorldFileReader.WorldFile in project mudmap2 by Neop.

the class WorldFileList method readWorldList.

/**
 * Get available worlds from worlds file
 */
public static void readWorldList() {
    try {
        // read from available worlds file
        BufferedReader reader = new BufferedReader(new FileReader(Environment.getAvailableWorldsFile()));
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.startsWith("f ")) {
                // world file entry
                String file = line.substring(2).trim();
                WorldFileDefault worldFile = new WorldFileDefault(file);
                if (worldFile.canRead()) {
                    // is world file
                    String name;
                    try {
                        // get world name
                        name = worldFile.readWorldName();
                    } catch (Exception ex) {
                        Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
                        // use file name if world name not found
                        name = file.substring(file.lastIndexOf('/') + 1);
                    }
                    // add found world to list
                    availableWorlds.put(file, name);
                }
            }
        }
    } catch (FileNotFoundException ex) {
    } catch (IOException ex) {
        Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with WorldFile

use of mudmap2.backend.WorldFileReader.WorldFile in project mudmap2 by Neop.

the class WorldFileDefaultTest method testSetWorldFile.

/**
 * Test of setWorldFile method, of class WorldFileDefault.
 */
@Test
public void testSetWorldFile() {
    System.out.println("setWorldFile");
    try {
        // getPlace access to private member
        Field field = WorldFileDefault.class.getDeclaredField("worldFile");
        field.setAccessible(true);
        WorldFileDefault instance = new WorldFileDefault(null);
        assertNotNull(field.get(instance));
        WorldFile worldFile = new WorldFileJSON(null);
        instance = new WorldFileDefault(null);
        instance.setWorldFile(worldFile);
        assertEquals(worldFile, field.get(instance));
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
        Logger.getLogger(WorldFileDefaultTest.class.getName()).log(Level.SEVERE, null, ex);
        fail();
    }
}
Also used : Field(java.lang.reflect.Field) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) Test(org.junit.Test)

Example 5 with WorldFile

use of mudmap2.backend.WorldFileReader.WorldFile in project mudmap2 by Neop.

the class WorldFileList method readDirectory.

/**
 * Get available worlds from filesystem
 */
public static void readDirectory() {
    // get file list
    File dir = new File(Environment.getWorldsDir());
    File[] fileList = dir.listFiles();
    if (fileList != null) {
        // find world files in file list
        for (File file : fileList) {
            // exclude meta and backup files
            if (!file.getName().equals("worlds") && !file.getName().endsWith("_meta") && !file.getName().endsWith(".backup") && !file.getName().endsWith(".bak") && file.isFile() && file.canRead()) {
                // check if file is world file
                WorldFileDefault worldFile = new WorldFileDefault(file.getPath());
                try {
                    if (worldFile.canRead()) {
                        // is world file
                        String name = worldFile.readWorldName();
                        availableWorlds.put(file.getPath(), name);
                    }
                } catch (FileNotFoundException ex) {
                } catch (Exception ex) {
                    Logger.getLogger(WorldFileList.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

WorldFileDefault (mudmap2.backend.WorldFileReader.current.WorldFileDefault)6 IOException (java.io.IOException)5 WorldFile (mudmap2.backend.WorldFileReader.WorldFile)5 FileNotFoundException (java.io.FileNotFoundException)4 File (java.io.File)3 WorldFileJSON (mudmap2.backend.WorldFileReader.current.WorldFileJSON)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JFileChooser (javax.swing.JFileChooser)1 World (mudmap2.backend.World)1 Test (org.junit.Test)1