Load world or get it from loaded worlds list if it is already loaded @param filename world file name @return world or null @throws java.lang.Exception throws Exception if world could not be read
(String filename)
| 62 | * @throws java.lang.Exception throws Exception if world could not be read |
| 63 | */ |
| 64 | public static World getWorld(String filename) throws Exception{ |
| 65 | World ret = null; |
| 66 | |
| 67 | // find world in loaded worlds list |
| 68 | for(World world: loadedWorlds){ |
| 69 | if(world.getWorldFile() != null && |
| 70 | world.getWorldFile().getFilename().equals(filename)){ |
| 71 | ret = world; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // if world not loaded already |
| 77 | if(ret == null){ |
| 78 | /* TODO: find correct file format if a new format gets implemented |
| 79 | * in future |
| 80 | */ |
| 81 | WorldFile worldFile = new WorldFileDefault(filename); |
| 82 | if(worldFile.canRead()){ |
| 83 | ret = worldFile.readFile(); |
| 84 | worldFile.backup(); |
| 85 | register(ret); |
| 86 | } else { |
| 87 | throw new Exception("Could not read world file: invalid format"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates and registers a new world |