Merge pull request #9 from Gallion/master
Now generates world when world doesn't exist
This commit is contained in:
commit
3f9b9565e6
Binary file not shown.
@ -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);
|
||||
|
@ -68,8 +68,7 @@ public class Misc {
|
||||
// ugh, sorry, this is an ugly hack
|
||||
|
||||
FileRead.readConf();
|
||||
WorldVerify.verifyWorld();
|
||||
|
||||
Setup.doSetup();
|
||||
final File level = new File(var.worldPath + var.fileSeparator + "level.dat");
|
||||
try {
|
||||
final Coordinates spawn = SpawnPoint.getSpawn(level);
|
||||
|
155
src/morlok8k/MinecraftLandGenerator/Setup.java
Normal file
155
src/morlok8k/MinecraftLandGenerator/Setup.java
Normal file
@ -0,0 +1,155 @@
|
||||
/*
|
||||
#######################################################################
|
||||
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE #
|
||||
# Version 2, December 2004 #
|
||||
# #
|
||||
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> #
|
||||
# #
|
||||
# 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.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author morlok8k
|
||||
*/
|
||||
|
||||
//TODO : Remove var.worldPath entirely because storing that is useless.
|
||||
|
||||
public class Setup {
|
||||
|
||||
static boolean doSetup() {
|
||||
final File serverPathFile;
|
||||
final BufferedReader serverPropertiesFile;
|
||||
final File levelDat;
|
||||
final File backupLevel;
|
||||
String line;
|
||||
|
||||
|
||||
//---------- 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) {
|
||||
return true;
|
||||
}
|
||||
|
||||
while (line != null) {
|
||||
if (line.contains("level-name")) { // Yep, that line contains it
|
||||
if (line.contains("#") && line.indexOf('=') < line.indexOf('#')) {
|
||||
// Apparently, "#slf dghdsf #gdcfggh" is a perfectly valid world name.
|
||||
// In the other cases, the line is commented or invalid.
|
||||
var.worldName = line.substring(line.indexOf('=') + 1);
|
||||
var.worldPath = var.serverPath + var.fileSeparator + var.worldName;
|
||||
} else { // There is no comment on the line
|
||||
var.worldName = line.substring(line.indexOf('=') + 1);
|
||||
var.worldPath = var.serverPath + var.fileSeparator + var.worldName;
|
||||
}
|
||||
}
|
||||
try {
|
||||
line = serverPropertiesFile.readLine();
|
||||
}
|
||||
catch (IOException e) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
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){
|
||||
return true;
|
||||
}
|
||||
Out.err("World created! Starting world generation...");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
/*
|
||||
#######################################################################
|
||||
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE #
|
||||
# Version 2, December 2004 #
|
||||
# #
|
||||
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> #
|
||||
# #
|
||||
# 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user