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");
}
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);
}
}
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);
}
}
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();
}
}
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);
}
}
}
}
}
Aggregations