DeathChests/src/main/java/lostcave/deathchests/util/Config.java

99 lines
4.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 = true;
public static boolean fixLog4J = true;
public static int maxDistance = 50;
public static boolean allowOtherPlayers = true;
public static String notAllowedMessage = "§4Cannot get items from another player's death chest.";
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# Enable Log4Shell counter measures? (this feature is currently not implemented)\n";
configOut += "fixLog4J=";
configOut += fixLog4J ? "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";
configOut += "notAllowedMessage=";
configOut += notAllowedMessage;
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 ("fixLog4J"): {
fixLog4J = 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;
}
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();
}
}
}