From f284a82b27a457082a39eb2db30dff3b76f41894 Mon Sep 17 00:00:00 2001 From: Gallion Date: Thu, 8 Jan 2015 09:50:24 -0500 Subject: [PATCH 1/3] Delete WorldVerify.java --- .../MinecraftLandGenerator/WorldVerify.java | 111 ------------------ 1 file changed, 111 deletions(-) delete mode 100644 src/morlok8k/MinecraftLandGenerator/WorldVerify.java diff --git a/src/morlok8k/MinecraftLandGenerator/WorldVerify.java b/src/morlok8k/MinecraftLandGenerator/WorldVerify.java deleted file mode 100644 index 4e28eb8..0000000 --- a/src/morlok8k/MinecraftLandGenerator/WorldVerify.java +++ /dev/null @@ -1,111 +0,0 @@ -/* -####################################################################### -# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # -# Version 2, December 2004 # -# # -# Copyright (C) 2004 Sam Hocevar # -# # -# Everyone is permitted to copy and distribute verbatim or modified # -# copies of this license document, and changing it is allowed as long # -# as the name is changed. # -# # -# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # -# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # -# # -# 0. You just DO WHAT THE FUCK YOU WANT TO. # -# # -####################################################################### -*/ - -package morlok8k.MinecraftLandGenerator; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author morlok8k - */ -public class WorldVerify { - - /** - * - */ - static void verifyWorld() { - //TODO: element comment - - // verify that we ended up with a good server path, either from the file or from an argument. - final File file = new File(var.serverPath); - if (!file.exists() || !file.isDirectory()) { - Out.err("The server directory is invalid: " + var.serverPath); - return; - } - - try { - // read the name of the current world from the server.properties file - final BufferedReader props = - new BufferedReader(new FileReader(new File(var.serverPath + var.fileSeparator - + "server.properties"))); - String line; - while ((line = props.readLine()) != null) { - String property = ""; - String value = ""; - - int pos = line.indexOf('='); - - int end = line.lastIndexOf('#'); // comments, ignored lines - - if (end == -1) { // If we have no hash sign, then we read till the end of the line - end = line.length(); - - } - if (end <= (pos + 1)) { // If hash is before the '=', we may have an issue... it should be fine, cause we check for issues next, but lets make sure. - end = line.length(); - pos = -1; - } - - if (end == 0) { //hash is first char, meaning entire line is a comment - end = line.length(); - pos = 0; - } - - if (pos != -1) { - if (line.length() == 0) { - property = ""; - value = ""; - } else { - property = line.substring(0, pos).toLowerCase(); - value = line.substring(pos + 1); - } - - if (property.equals("level-name")) { - var.worldPath = var.serverPath + var.fileSeparator + value; - var.worldName = value; - } - - } - } - - props.close(); - - } catch (final FileNotFoundException ex) { - Out.err("Could not open " + var.serverPath + var.fileSeparator + "server.properties"); - return; - } catch (final IOException ex) { - Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); - return; - } - - final File level = new File(var.worldPath + var.fileSeparator + "level.dat"); - if (!level.exists() || !level.isFile()) { - Out.err("The currently-configured world does not exist."); - return; - } - - } -} From f368d9e8f4b64202d03ef22b04a9e95a7a0c7ebc Mon Sep 17 00:00:00 2001 From: Gallion Date: Thu, 8 Jan 2015 09:53:01 -0500 Subject: [PATCH 2/3] Create Setup.java Fixed a few things and moved the initial world generation to this file to make things more simple. --- .../MinecraftLandGenerator/Setup.java | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/morlok8k/MinecraftLandGenerator/Setup.java diff --git a/src/morlok8k/MinecraftLandGenerator/Setup.java b/src/morlok8k/MinecraftLandGenerator/Setup.java new file mode 100644 index 0000000..0a0ea1d --- /dev/null +++ b/src/morlok8k/MinecraftLandGenerator/Setup.java @@ -0,0 +1,158 @@ +/* +####################################################################### +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # +# Version 2, December 2004 # +# # +# Copyright (C) 2004 Sam Hocevar # +# # +# Everyone is permitted to copy and distribute verbatim or modified # +# copies of this license document, and changing it is allowed as long # +# as the name is changed. # +# # +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # +# # +# 0. You just DO WHAT THE FUCK YOU WANT TO. # +# # +####################################################################### +*/ + +package morlok8k.MinecraftLandGenerator; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.nio.Buffer; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author morlok8k + */ + +//TODO : Remove var.worldPath entirely because storing that is useless. + +public class Setup { + + static boolean doSetup() { + // Declare your stuff at the beginning, yo. + final File serverPathFile; + final BufferedReader serverPropertiesFile; + final File levelDat; + final File backupLevel; + String line = null; + + +//---------- Verify server path + serverPathFile = new File(var.serverPath); + + if (!serverPathFile.exists() || !serverPathFile.isDirectory()) { + /*FileNotFoundException fileException = + new FileNotFoundException("The server directory is invalid: " + var.serverPath); + throw fileException;*/ + Out.err("The server directory is invalid: " + var.serverPath); + return true; + } + + +//---------- Verify server.properties + try { + serverPropertiesFile = new BufferedReader(new FileReader(new File(var.serverPath + var.fileSeparator + + "server.properties"))); + } catch (IOException e) { + Out.err("Could not open the server.properties file."); + return true; + } + + +//---------- Set world name + try { + line = serverPropertiesFile.readLine(); + } + catch (IOException e) { + } + + while (line != null) { + if (line.contains("level-name")) { // Yep, that line contains it + if (line.contains("#")) { // There is a comment somewhere on the line + if (line.indexOf('#') > line.indexOf("level-name")) { // The comment isn't before level-name + if (!(line.indexOf('#') > line.indexOf('='))) { // In case only the name is commented out + var.worldName = line.substring(line.indexOf('=') + 1, line.indexOf('#')); // Read world name until start of comment + var.worldPath = var.serverPath + var.fileSeparator + var.worldName; + } + } + } else { // There is no comment on the line + var.worldName = line.substring(line.indexOf('=') + 1, line.length()); + var.worldPath = var.serverPath + var.fileSeparator + var.worldName; + } + } + try { + line = serverPropertiesFile.readLine(); + } + catch (IOException e) { + } + + } + if (var.worldName == null) { // If after all this we still don't have a proper world name, stop everything and throw an exception + /*NullPointerException noNameException = new NullPointerException("There is no world name defined in the server.properties file!"); + throw noNameException;*/ + Out.err("There is no world name defined in the server.properties file!"); + return true; + } + +//---------- Verify that the world exists and restore level_backup.dat if it exists. If not, start server once to create the world. + levelDat = new File(var.worldPath + var.fileSeparator + "level.dat"); + backupLevel = new File(var.worldPath + var.fileSeparator + "level_backup.dat"); + + // prepare our two ProcessBuilders + // minecraft = new ProcessBuilder(javaLine, "-Xms1024m", "-Xmx1024m", "-jar", jarFile, "nogui"); + var.minecraft = new ProcessBuilder(var.javaLine.split("\\s")); // is this always going to work? i don't know. (most likely yes) + var.minecraft.directory(new File(var.serverPath)); + var.minecraft.redirectErrorStream(true); + + if (levelDat.exists() && levelDat.isFile()) { + if (backupLevel.exists()) { + Out.err("There is a level_backup.dat file left over from a previous attempt that failed."); + Out.out("Resuming..."); + + //use resume data + final File serverLevel = new File(var.worldPath + var.fileSeparator + "level.dat"); + try { + Misc.copyFile(backupLevel, serverLevel); + } catch (final IOException e) { + e.printStackTrace(); + } + backupLevel.delete(); + + //return; + + FileRead.readArrayListCoordLog(var.worldPath + var.fileSeparator + var.logFile); // we read the .log just for any resume data, if any. + + System.gc(); //run the garbage collector - hopefully free up some memory! + + var.xRange = var.resumeX; + var.zRange = var.resumeZ; + + } + } else { + /*FileNotFoundException fileException = + new FileNotFoundException("The currently configured world does not exist.");*/ + Out.err("The currently configured world does not exist! Launching the server once to create it..."); + try { + var.minecraft = new ProcessBuilder(var.javaLine.split("\\s")); // is this always going to work? i don't know. (most likely yes) + var.minecraft.directory(new File(var.serverPath)); + var.minecraft.redirectErrorStream(true); + if (!(Server.runMinecraft())) { + Out.err("Huh oh! Something went wrong with the server! Exiting..."); + System.exit(1); // we got a warning or severe error + } + } + catch (IOException e){} + Out.err("World created! Starting world generation..."); + } + return false; + } +} From 135796849029f1938a952b2bd5324302f73cc995 Mon Sep 17 00:00:00 2001 From: Gallion Date: Thu, 8 Jan 2015 09:54:49 -0500 Subject: [PATCH 3/3] Update Main.java Updated along with Setup.java. MLG should now properly create the world when it does not exist. --- src/morlok8k/MinecraftLandGenerator/Main.java | 68 +++---------------- 1 file changed, 9 insertions(+), 59 deletions(-) diff --git a/src/morlok8k/MinecraftLandGenerator/Main.java b/src/morlok8k/MinecraftLandGenerator/Main.java index 7d406be..c6e3bf2 100644 --- a/src/morlok8k/MinecraftLandGenerator/Main.java +++ b/src/morlok8k/MinecraftLandGenerator/Main.java @@ -34,7 +34,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import morlok8k.MinecraftLandGenerator.GUI.MLG_GUI; - /** * * @author Corrodias, Morlok8k, pr0f1x @@ -44,7 +43,7 @@ public class Main { ////////////////////////////////////////////////////////// // REMINDER: Because I always forget/mix up languages: // - // "static" in java means "global" to this class // + // "static" in java means that there will only ever be ONE of it created, shared by all the instances of that class.// // "final" means "constant" // // public/private shows/hides between classes // ////////////////////////////////////////////////////////// @@ -143,49 +142,15 @@ public class Main { * */ private static void runCLI() { + final File backupLevel; + final File serverLevel; // Basic Program Initialization Startup.initialStart(); - boolean quit = false; - quit = Startup.programArguments(); - if (quit) { return; } - quit = Startup.confFile(); - if (quit) { return; } + if (Startup.programArguments()) { return; } + if (Startup.confFile()) { return; } + if (Setup.doSetup()) { return; } // Checks for server.properties, checks for world, creates world if needed, checks old data} - WorldVerify.verifyWorld(); - - { - final File backupLevel = - new File(var.worldPath + var.fileSeparator + "level_backup.dat"); - if (backupLevel.exists()) { - //err("There is a level_backup.dat file left over from a previous attempt that failed. You should go determine whether to keep the current level.dat" - // + " or restore the backup."); - //err("You most likely will want to restore the backup!"); - //Time.waitTenSec(false); - - Out.err("There is a level_backup.dat file left over from a previous attempt that failed."); - Out.out("Resuming..."); - - //use resume data - final File serverLevel = new File(var.worldPath + var.fileSeparator + "level.dat"); - try { - Misc.copyFile(backupLevel, serverLevel); - } catch (final IOException e) { - e.printStackTrace(); - } - backupLevel.delete(); - - //return; - - FileRead.readArrayListCoordLog(var.worldPath + var.fileSeparator + var.logFile); // we read the .log just for any resume data, if any. - - System.gc(); //run the garbage collector - hopefully free up some memory! - - var.xRange = var.resumeX; - var.zRange = var.resumeZ; - - } - } // ===================================================================== // PROCESSING @@ -197,22 +162,8 @@ public class Main { Out.out(""); - // prepare our two ProcessBuilders - // minecraft = new ProcessBuilder(javaLine, "-Xms1024m", "-Xmx1024m", "-jar", jarFile, "nogui"); - var.minecraft = new ProcessBuilder(var.javaLine.split("\\s")); // is this always going to work? i don't know. (most likely yes) - var.minecraft.directory(new File(var.serverPath)); - var.minecraft.redirectErrorStream(true); - try { - Out.out("Launching server once to make sure there is a world."); - - final long generationStartTimeTracking = System.currentTimeMillis(); //Start of time remaining calculations. - - final boolean serverLaunch = Server.runMinecraft(); - - if (!(serverLaunch)) { - System.exit(1); // we got a warning or severe error - } + final long generationStartTimeTracking = System.currentTimeMillis(); //Start of time remaining calcrror if ((var.xRange == 0) & (var.zRange == 0)) { //If the server is launched with an X and a Z of zero, then we just shutdown MLG after the initial launch. return; @@ -227,9 +178,8 @@ public class Main { Out.out(""); - final File serverLevel = new File(var.worldPath + var.fileSeparator + "level.dat"); - final File backupLevel = - new File(var.worldPath + var.fileSeparator + "level_backup.dat"); + serverLevel = new File(var.worldPath + var.fileSeparator + "level.dat"); + backupLevel = new File(var.worldPath + var.fileSeparator + "level_backup.dat"); Out.out("Backing up level.dat to level_backup.dat."); Misc.copyFile(serverLevel, backupLevel);