|
| 1 | +package net.stonebound.simpleircbridge.simpleircbridge; |
| 2 | + |
| 3 | +import com.electronwill.nightconfig.core.ConfigSpec; |
| 4 | +import com.electronwill.nightconfig.core.UnmodifiableConfig; |
| 5 | +import com.electronwill.nightconfig.core.file.CommentedFileConfig; |
| 6 | +import com.electronwill.nightconfig.core.file.FileNotFoundAction; |
| 7 | +import dev.architectury.platform.Platform; |
| 8 | +import net.minecraft.server.MinecraftServer; |
| 9 | +import net.stonebound.simpleircbridge.utils.CommentedConfigSpec; |
| 10 | + |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public final class Config { |
| 16 | + public static final CommentedConfigSpec commonSpec; |
| 17 | + public static CommentedFileConfig commonConfig; |
| 18 | + |
| 19 | + private static final Path commonPath = Platform.getConfigFolder().resolve("simpleircbridge-common.toml"); |
| 20 | + |
| 21 | + private Config() { |
| 22 | + } |
| 23 | + |
| 24 | + public static Boolean ircFormatting = true; |
| 25 | + public static Boolean mcFormatting = true; |
| 26 | + public static String nick = "bridgebot"; |
| 27 | + public static String password = "true"; |
| 28 | + public static String hostname = "localhost"; |
| 29 | + public static Integer port = 6667; |
| 30 | + public static String channel = "#general"; |
| 31 | + public static Boolean tls = true; |
| 32 | + public static String username = "bridgebot"; |
| 33 | + |
| 34 | + public static String realname = "SimpleIRCBridge"; |
| 35 | + public static Boolean timestop = true; |
| 36 | + |
| 37 | + static { |
| 38 | + System.setProperty("nightconfig.preserveInsertionOrder", "true"); |
| 39 | + |
| 40 | + commonSpec = new CommentedConfigSpec(); |
| 41 | + |
| 42 | + commonSpec.comment("ircFormatting", "Whether minecraft formatting should be converted to IRC formatting"); |
| 43 | + commonSpec.define("ircFormatting", Config.ircFormatting); |
| 44 | + |
| 45 | + commonSpec.comment("mcFormatting", "Whether IRC formatting should be converted to Minecraft formatting"); |
| 46 | + commonSpec.define("mcFormatting", Config.mcFormatting); |
| 47 | + |
| 48 | + commonSpec.comment("nick", "The nickname that the relay bot will use, remember the 9 char limit"); |
| 49 | + commonSpec.define("nick", Config.nick); |
| 50 | + |
| 51 | + commonSpec.comment("password", "#IRC Server password (if any), it should be obvious but this is probably accessable to other mods"); |
| 52 | + commonSpec.define("password", Config.password); |
| 53 | + |
| 54 | + commonSpec.comment("hostname", "Hostname or IP address of your IRC server"); |
| 55 | + commonSpec.define("hostname", Config.hostname); |
| 56 | + |
| 57 | + commonSpec.comment("port", "Port of the IRC server to connect to. Common values: 6697 for TLS/SSL; 6667 for plaintext connections"); |
| 58 | + commonSpec.defineInRange("port", Config.port, 1025, 65535); |
| 59 | + |
| 60 | + commonSpec.comment("channel", "IRC channel to relay into"); |
| 61 | + commonSpec.define("channel", Config.channel); |
| 62 | + |
| 63 | + commonSpec.comment("tls", "Whether TLS/SSL is enabled. Set to 'false' for a plaintext connection"); |
| 64 | + commonSpec.define("tls", Config.tls); |
| 65 | + |
| 66 | + commonSpec.comment("username", "The username/ident that the relay bot will use"); |
| 67 | + commonSpec.define("username", Config.username); |
| 68 | + |
| 69 | + commonSpec.comment("realname", "The realname/gecos that the relay bot will use"); |
| 70 | + commonSpec.define("realname", Config.realname); |
| 71 | + |
| 72 | + commonSpec.comment("timestop", "Sends a message to the bridge for each player that was online at server closing, as the normal PLAYERQUIT event does not fire"); |
| 73 | + commonSpec.define("timestop", Config.timestop); |
| 74 | + } |
| 75 | + |
| 76 | + private static final FileNotFoundAction MAKE_DIRECTORIES_AND_FILE = (file, configFormat) -> { |
| 77 | + Files.createDirectories(file.getParent()); |
| 78 | + Files.createFile(file); |
| 79 | + configFormat.initEmptyFile(file); |
| 80 | + return false; |
| 81 | + }; |
| 82 | + |
| 83 | + private static CommentedFileConfig buildFileConfig(Path path) { |
| 84 | + return CommentedFileConfig.builder(path) |
| 85 | + .onFileNotFound(MAKE_DIRECTORIES_AND_FILE) |
| 86 | + .preserveInsertionOrder() |
| 87 | + .build(); |
| 88 | + } |
| 89 | + |
| 90 | + private static void saveConfig(UnmodifiableConfig config, CommentedConfigSpec spec, Path path) { |
| 91 | + try (CommentedFileConfig fileConfig = buildFileConfig(path)) { |
| 92 | + fileConfig.putAll(config); |
| 93 | + spec.correct(fileConfig); |
| 94 | + fileConfig.save(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static void save() { |
| 99 | + if (commonConfig != null) { |
| 100 | + saveConfig(commonConfig, commonSpec, commonPath); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static void serverStarting(MinecraftServer server) { |
| 105 | + try (CommentedFileConfig config = buildFileConfig(commonPath)) { |
| 106 | + config.load(); |
| 107 | + commonSpec.correct(config, Config::correctionListener); |
| 108 | + config.save(); |
| 109 | + commonConfig = config; |
| 110 | + sync(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public static void serverStopping(MinecraftServer server) { |
| 115 | + commonConfig = null; |
| 116 | + } |
| 117 | + |
| 118 | + private static void correctionListener(ConfigSpec.CorrectionAction action, List<String> path, Object incorrectValue, |
| 119 | + Object correctedValue) { |
| 120 | + String key = String.join(".", path); |
| 121 | + switch (action) { |
| 122 | + case ADD: |
| 123 | + SimpleIRCBridgeCommon.logger.warn("Config key {} missing -> added default value.", key); |
| 124 | + break; |
| 125 | + case REMOVE: |
| 126 | + SimpleIRCBridgeCommon.logger.warn("Config key {} not defined -> removed from config.", key); |
| 127 | + break; |
| 128 | + case REPLACE: |
| 129 | + SimpleIRCBridgeCommon.logger.warn("Config key {} not valid -> replaced with default value.", key); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public static void sync() { |
| 134 | + if (commonConfig != null) { |
| 135 | + Config.ircFormatting = commonConfig.<Boolean>get("ircFormatting"); |
| 136 | + Config.mcFormatting = commonConfig.<Boolean>get("mcFormatting"); |
| 137 | + Config.nick = commonConfig.<String>get("nick"); |
| 138 | + Config.password = commonConfig.<String>get("password"); |
| 139 | + Config.hostname = commonConfig.<String>get("hostname"); |
| 140 | + Config.port = commonConfig.<Integer>get("port"); |
| 141 | + Config.channel = commonConfig.<String>get("channel"); |
| 142 | + Config.tls = commonConfig.<Boolean>get("tls"); |
| 143 | + Config.username = commonConfig.<String>get("username"); |
| 144 | + Config.realname = commonConfig.<String>get("realname"); |
| 145 | + Config.timestop = commonConfig.<Boolean>get("timestop"); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments