115 lines
5.6 KiB
Java
115 lines
5.6 KiB
Java
package lostcave.deathchests.util;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.util.List;
|
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
public class Config {
|
|
|
|
public static final String config_file_name = "deathchests.txt";
|
|
public static File configDir = null;
|
|
|
|
public static boolean debug = false;
|
|
public static int maxDistance = 50;
|
|
public static boolean allowOtherPlayers = true;
|
|
public static String notAllowedMessage = "§4Cannot get items from another player's death chest.";
|
|
public static boolean giveObituary = true;
|
|
public static String dropMessage = "§4A death chest could not be placed for this death. Your items have been dropped.";
|
|
public static String locationMessage = "§eThe death chest for this death is at %s in dimension %s.";
|
|
|
|
private static final String config_header = "# The format of this file is strictly `option=value` (no spaces).\n# Lines starting with # and empty lines are ignored.";
|
|
|
|
public static void writeConfig() {
|
|
String configOut = config_header;
|
|
configOut += "\n\n# Enable debug output?\n";
|
|
configOut += "debug=";
|
|
configOut += debug ? "true" : "false";
|
|
configOut += "\n\n# The maximum search radius for finding a suitable location for placing the death chest\n";
|
|
configOut += "maxDistance=";
|
|
configOut += Integer.toString(maxDistance);
|
|
configOut += "\n\n# Allow players other than the owner to get the items?\n";
|
|
configOut += "allowOtherPlayers=";
|
|
configOut += allowOtherPlayers ? "true" : "false";
|
|
configOut += "\n\n# The message to display when trying to get items from another player's death chest and allowOtherPlayers=false\n";
|
|
configOut += "notAllowedMessage=";
|
|
configOut += notAllowedMessage;
|
|
configOut += "\n\n# Whether to give respawning players an obituary item\n";
|
|
configOut += "giveObituary=";
|
|
configOut += giveObituary ? "true" : "false";
|
|
configOut += "\n\n# The message to display when no suitable location for a death chest could be found and the items have been dropped\n";
|
|
configOut += "dropMessage=";
|
|
configOut += dropMessage;
|
|
configOut += "\n\n# How to display the death chest location\n";
|
|
configOut += "locationMessage=";
|
|
configOut += locationMessage;
|
|
|
|
File configFile = new File(configDir, config_file_name);
|
|
try {
|
|
Files.write(configFile.toPath(), configOut.getBytes());
|
|
} catch (IOException e) {
|
|
System.err.println("Failed to write to file at " + configFile.toString() + ":");
|
|
e.printStackTrace();
|
|
System.err.println("Proceeding as if nothing happened...");
|
|
}
|
|
}
|
|
|
|
public static void loadConfig() {
|
|
List<String> configData = null;
|
|
File configFile = new File(configDir, config_file_name);
|
|
if (configFile.exists()) {
|
|
try {
|
|
configData = Files.readAllLines(configFile.toPath());
|
|
for (int i = 0; i < configData.size(); i++) {
|
|
if (configData.get(i).length() != 0 && configData.get(i).charAt(0) != '#') {
|
|
int equalsSign = configData.get(i).indexOf('=');
|
|
switch (configData.get(i).substring(0, equalsSign)) {
|
|
case ("debug"): {
|
|
debug = configData.get(i).substring(equalsSign + 1).equals("true") ? true : false;
|
|
break;
|
|
}
|
|
case ("maxDistance"): {
|
|
maxDistance = Integer.parseInt(configData.get(i).substring(equalsSign + 1));
|
|
break;
|
|
}
|
|
case ("allowOtherPlayers"): {
|
|
allowOtherPlayers = configData.get(i).substring(equalsSign + 1).equals("true") ? true : false;
|
|
break;
|
|
}
|
|
case ("notAllowedMessage"): {
|
|
notAllowedMessage = configData.get(i).substring(equalsSign+1);
|
|
break;
|
|
}
|
|
case ("giveObituary"): {
|
|
giveObituary = configData.get(i).substring(equalsSign + 1).equals("true") ? true : false;
|
|
break;
|
|
}
|
|
case ("dropMessage"): {
|
|
dropMessage = configData.get(i).substring(equalsSign+1);
|
|
break;
|
|
}
|
|
case ("locationMessage"): {
|
|
locationMessage = configData.get(i).substring(equalsSign+1);
|
|
break;
|
|
}
|
|
default: {
|
|
System.err.println("Failed parsing config entry: " + configData.get(i));
|
|
FMLCommonHandler.instance().exitJava(1, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
System.err.println("An error was encountered while trying to read the file at " + configFile.toString() + ":");
|
|
e.printStackTrace();
|
|
System.err.println("Proceeding with default configuration...");
|
|
}
|
|
} else {
|
|
// Skip reading config and set up initial config file with default values
|
|
writeConfig();
|
|
}
|
|
}
|
|
}
|