Added config check

2.x-1.16
szymon 2020-07-14 14:42:46 +02:00
parent d13bc5256c
commit 888d56c3bb
2 changed files with 24 additions and 7 deletions

View File

@ -87,10 +87,24 @@ public class ConfigHandler {
public Set<String> playerBlacklist = new HashSet<>(); public Set<String> playerBlacklist = new HashSet<>();
@Comment("\nFormat of date&time used to name backup files.\n" + @Comment("\nFormat of date&time used to name backup files.\n" +
"Remember not to use '#' symbol and any other character that is not allowed by your operating system such as:\n" + "Remember not to use '#' symbol or any other character that is not allowed by your operating system such as:\n" +
"':', '\\', etc\n") "':', '\\', etc...\n" +
"For more info: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html\n")
public String dateTimeFormat = "dd.MM.yyyy_HH-mm-ss"; public String dateTimeFormat = "dd.MM.yyyy_HH-mm-ss";
public Optional<String> sanitize() {
if(compressionCoreCountLimit > Runtime.getRuntime().availableProcessors())
return Optional.of("compressionCoreCountLimit is too big! Your system only has: " + Runtime.getRuntime().availableProcessors() + " cores!");
try {
DateTimeFormatter.ofPattern(dateTimeFormat);
} catch (IllegalArgumentException e) {
return Optional.of("dateTimeFormat is wrong!\n" + e.getMessage() + "\n See: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html");
}
return Optional.empty();
}
public enum ArchiveFormat { public enum ArchiveFormat {
ZIP(".zip"), ZIP(".zip"),
GZIP(".tar.gz"), GZIP(".tar.gz"),

View File

@ -23,7 +23,6 @@ import io.github.cottonmc.cotton.config.ConfigManager;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.commands.BlacklistCommand; import net.szum123321.textile_backup.commands.BlacklistCommand;
@ -34,6 +33,7 @@ import net.szum123321.textile_backup.core.BackupScheduler;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.Optional;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -50,12 +50,15 @@ public class TextileBackup implements ModInitializer {
public void onInitialize() { public void onInitialize() {
config = ConfigManager.loadConfig(ConfigHandler.class); config = ConfigManager.loadConfig(ConfigHandler.class);
registerCommands(); Optional<String> errorMessage = config.sanitize();
ServerTickEvents.END_SERVER_TICK.register(scheduler::tick); if(errorMessage.isPresent()) {
LOGGER.fatal("TextileBackup config file has wrong settings! \n" + errorMessage.get());
System.exit(1);
} }
private void registerCommands(){ ServerTickEvents.END_SERVER_TICK.register(scheduler::tick);
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register( CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(
LiteralArgumentBuilder.<ServerCommandSource>literal("backup") LiteralArgumentBuilder.<ServerCommandSource>literal("backup")
.requires((ctx) -> { .requires((ctx) -> {