From 5eb79935ba8d802cb8b3e9dedbb423e26e55e3a0 Mon Sep 17 00:00:00 2001 From: nikky Date: Mon, 25 Jun 2018 02:28:30 +0200 Subject: [PATCH] switch main config to hjson --- .../src/main/kotlin/matterlink/MatterLink.kt | 3 +- .../kotlin/matterlink/MatterLinkConfig.kt | 24 - .../src/main/kotlin/matterlink/MatterLink.kt | 3 +- .../kotlin/matterlink/MatterLinkConfig.kt | 24 - .../src/main/kotlin/matterlink/MatterLink.kt | 13 +- .../kotlin/matterlink/MatterLinkConfig.kt | 24 - .../src/main/kotlin/matterlink/MatterLink.kt | 3 +- .../kotlin/matterlink/MatterLinkConfig.kt | 24 - Jankson | 2 +- core/src/main/kotlin/matterlink/Util.kt | 8 +- .../bridge/command/BridgeCommandRegistry.kt | 2 +- .../kotlin/matterlink/command/CommandCore.kt | 3 +- .../kotlin/matterlink/config/BaseConfig.kt | 593 ++++++++++-------- .../kotlin/matterlink/config/CommandConfig.kt | 30 +- .../matterlink/config/PermissionConfig.kt | 18 +- .../matterlink/handlers/DeathHandler.kt | 4 +- 16 files changed, 380 insertions(+), 398 deletions(-) delete mode 100644 1.10.2/src/main/kotlin/matterlink/MatterLinkConfig.kt delete mode 100644 1.11.2/src/main/kotlin/matterlink/MatterLinkConfig.kt delete mode 100644 1.12.2/src/main/kotlin/matterlink/MatterLinkConfig.kt delete mode 100644 1.7.10/src/main/kotlin/matterlink/MatterLinkConfig.kt diff --git a/1.10.2/src/main/kotlin/matterlink/MatterLink.kt b/1.10.2/src/main/kotlin/matterlink/MatterLink.kt index 310cd47..5b07cc5 100644 --- a/1.10.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.10.2/src/main/kotlin/matterlink/MatterLink.kt @@ -2,6 +2,7 @@ package matterlink import matterlink.command.CommandMatterlink import matterlink.command.MatterLinkCommandSender +import matterlink.config.BaseConfig import matterlink.config.cfg import net.minecraft.util.text.TextComponentString import net.minecraftforge.common.ForgeVersion @@ -35,7 +36,7 @@ object MatterLink : IMatterLink() { logger = event.modLog logger.info("Building bridge!") - cfg = MatterLinkConfig(event.modConfigurationDirectory) + cfg = BaseConfig(event.modConfigurationDirectory).load() } @Mod.EventHandler diff --git a/1.10.2/src/main/kotlin/matterlink/MatterLinkConfig.kt b/1.10.2/src/main/kotlin/matterlink/MatterLinkConfig.kt deleted file mode 100644 index a1d30cb..0000000 --- a/1.10.2/src/main/kotlin/matterlink/MatterLinkConfig.kt +++ /dev/null @@ -1,24 +0,0 @@ -package matterlink - -import matterlink.config.BaseConfig -import net.minecraftforge.common.config.Configuration -import java.io.File - -class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) { - init { - logger.info("Reading bridge blueprints... from {}", cfgDirectory) - val config = Configuration(mainCfgFile) - - load( - getBoolean = config::getBoolean, - getString = config::getString, - getStringValidated = config::getString, - getStringValidValues = config::getString, - getStringList = config::getStringList, - addCustomCategoryComment = config::addCustomCategoryComment - ) - if (config.hasChanged()) config.save() - } - - override fun load() = MatterLinkConfig(baseCfgDir) -} \ No newline at end of file diff --git a/1.11.2/src/main/kotlin/matterlink/MatterLink.kt b/1.11.2/src/main/kotlin/matterlink/MatterLink.kt index 494d40b..7aeb6cb 100644 --- a/1.11.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.11.2/src/main/kotlin/matterlink/MatterLink.kt @@ -2,6 +2,7 @@ package matterlink import matterlink.command.CommandMatterlink import matterlink.command.MatterLinkCommandSender +import matterlink.config.BaseConfig import matterlink.config.cfg import net.minecraft.util.text.TextComponentString import net.minecraftforge.common.ForgeVersion @@ -35,7 +36,7 @@ object MatterLink : IMatterLink() { logger = event.modLog logger.info("Building bridge!") - cfg = MatterLinkConfig(event.modConfigurationDirectory) + cfg = BaseConfig(event.modConfigurationDirectory).load() } @Mod.EventHandler diff --git a/1.11.2/src/main/kotlin/matterlink/MatterLinkConfig.kt b/1.11.2/src/main/kotlin/matterlink/MatterLinkConfig.kt deleted file mode 100644 index a1d30cb..0000000 --- a/1.11.2/src/main/kotlin/matterlink/MatterLinkConfig.kt +++ /dev/null @@ -1,24 +0,0 @@ -package matterlink - -import matterlink.config.BaseConfig -import net.minecraftforge.common.config.Configuration -import java.io.File - -class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) { - init { - logger.info("Reading bridge blueprints... from {}", cfgDirectory) - val config = Configuration(mainCfgFile) - - load( - getBoolean = config::getBoolean, - getString = config::getString, - getStringValidated = config::getString, - getStringValidValues = config::getString, - getStringList = config::getStringList, - addCustomCategoryComment = config::addCustomCategoryComment - ) - if (config.hasChanged()) config.save() - } - - override fun load() = MatterLinkConfig(baseCfgDir) -} \ No newline at end of file diff --git a/1.12.2/src/main/kotlin/matterlink/MatterLink.kt b/1.12.2/src/main/kotlin/matterlink/MatterLink.kt index d778362..f32c8a4 100644 --- a/1.12.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.12.2/src/main/kotlin/matterlink/MatterLink.kt @@ -2,12 +2,16 @@ package matterlink import matterlink.command.CommandMatterlink import matterlink.command.MatterLinkCommandSender +import matterlink.config.BaseConfig import matterlink.config.cfg import net.minecraft.util.text.TextComponentString import net.minecraftforge.common.ForgeVersion import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.Mod -import net.minecraftforge.fml.common.event.* +import net.minecraftforge.fml.common.event.FMLInitializationEvent +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent +import net.minecraftforge.fml.common.event.FMLServerStartingEvent +import net.minecraftforge.fml.common.event.FMLServerStoppingEvent import org.apache.logging.log4j.Level import org.apache.logging.log4j.Logger @@ -32,7 +36,7 @@ object MatterLink : IMatterLink() { logger = event.modLog logger.info("Building bridge!") - cfg = MatterLinkConfig(event.modConfigurationDirectory) + cfg = BaseConfig(event.modConfigurationDirectory).load() } @Mod.EventHandler @@ -40,11 +44,6 @@ object MatterLink : IMatterLink() { this.registerBridgeCommands() } - @Mod.EventHandler - fun serverAboutToStart(event: FMLServerAboutToStartEvent) { -// MessageHandlerInst.start(clear = true) - } - @Mod.EventHandler fun serverStarting(event: FMLServerStartingEvent) { log("DEBUG", "Registering server commands") diff --git a/1.12.2/src/main/kotlin/matterlink/MatterLinkConfig.kt b/1.12.2/src/main/kotlin/matterlink/MatterLinkConfig.kt deleted file mode 100644 index a1d30cb..0000000 --- a/1.12.2/src/main/kotlin/matterlink/MatterLinkConfig.kt +++ /dev/null @@ -1,24 +0,0 @@ -package matterlink - -import matterlink.config.BaseConfig -import net.minecraftforge.common.config.Configuration -import java.io.File - -class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) { - init { - logger.info("Reading bridge blueprints... from {}", cfgDirectory) - val config = Configuration(mainCfgFile) - - load( - getBoolean = config::getBoolean, - getString = config::getString, - getStringValidated = config::getString, - getStringValidValues = config::getString, - getStringList = config::getStringList, - addCustomCategoryComment = config::addCustomCategoryComment - ) - if (config.hasChanged()) config.save() - } - - override fun load() = MatterLinkConfig(baseCfgDir) -} \ No newline at end of file diff --git a/1.7.10/src/main/kotlin/matterlink/MatterLink.kt b/1.7.10/src/main/kotlin/matterlink/MatterLink.kt index 1fb0bdb..b0b080d 100644 --- a/1.7.10/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.7.10/src/main/kotlin/matterlink/MatterLink.kt @@ -8,6 +8,7 @@ import cpw.mods.fml.common.event.FMLServerStartingEvent import cpw.mods.fml.common.event.FMLServerStoppingEvent import matterlink.command.CommandMatterlink import matterlink.command.MatterLinkCommandSender +import matterlink.config.BaseConfig import matterlink.config.cfg import net.minecraft.server.MinecraftServer import net.minecraft.util.ChatComponentText @@ -37,7 +38,7 @@ class MatterLink : IMatterLink() { logger = event.modLog logger.info("Building bridge!") - cfg = MatterLinkConfig(event.modConfigurationDirectory) + cfg = BaseConfig(event.modConfigurationDirectory).load() } @Mod.EventHandler diff --git a/1.7.10/src/main/kotlin/matterlink/MatterLinkConfig.kt b/1.7.10/src/main/kotlin/matterlink/MatterLinkConfig.kt deleted file mode 100644 index a1d30cb..0000000 --- a/1.7.10/src/main/kotlin/matterlink/MatterLinkConfig.kt +++ /dev/null @@ -1,24 +0,0 @@ -package matterlink - -import matterlink.config.BaseConfig -import net.minecraftforge.common.config.Configuration -import java.io.File - -class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) { - init { - logger.info("Reading bridge blueprints... from {}", cfgDirectory) - val config = Configuration(mainCfgFile) - - load( - getBoolean = config::getBoolean, - getString = config::getString, - getStringValidated = config::getString, - getStringValidValues = config::getString, - getStringList = config::getStringList, - addCustomCategoryComment = config::addCustomCategoryComment - ) - if (config.hasChanged()) config.save() - } - - override fun load() = MatterLinkConfig(baseCfgDir) -} \ No newline at end of file diff --git a/Jankson b/Jankson index ce97d00..90b600e 160000 --- a/Jankson +++ b/Jankson @@ -1 +1 @@ -Subproject commit ce97d00016a58a4402d919155efa0fccec5ea9e6 +Subproject commit 90b600e2acdbba48a9a3c39d85a49a9ec24e8db1 diff --git a/core/src/main/kotlin/matterlink/Util.kt b/core/src/main/kotlin/matterlink/Util.kt index 25c0619..cdd5023 100644 --- a/core/src/main/kotlin/matterlink/Util.kt +++ b/core/src/main/kotlin/matterlink/Util.kt @@ -1,5 +1,6 @@ package matterlink +import blue.endless.jankson.JsonObject import matterlink.config.cfg import java.io.PrintWriter import java.io.StringWriter @@ -53,4 +54,9 @@ val Exception.stackTraceString: String val sw = StringWriter() this.printStackTrace(PrintWriter(sw)) return sw.toString() - } \ No newline at end of file + } + +inline fun JsonObject.getOrDefault(key: String, default: T, comment: String? = null): T { + instance.info("type: ${T::class.java.name} key: $key default: $default") + return putDefault(key, default, comment)!! +} diff --git a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt index 9841243..1253ef7 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt @@ -14,7 +14,7 @@ object BridgeCommandRegistry { fun handleCommand(input: ApiMessage): Boolean { if (!cfg.command.enable || input.text.isBlank()) return false - if (input.text[0] != cfg.command.prefix[0] || input.text.length < 2) return false + if (input.text[0] != cfg.command.prefix || input.text.length < 2) return false val cmd = input.text.substring(1).split(' ', ignoreCase = false, limit = 2) val args = if (cmd.size == 2) cmd[1] else "" diff --git a/core/src/main/kotlin/matterlink/command/CommandCore.kt b/core/src/main/kotlin/matterlink/command/CommandCore.kt index 817c360..ccbfb6f 100644 --- a/core/src/main/kotlin/matterlink/command/CommandCore.kt +++ b/core/src/main/kotlin/matterlink/command/CommandCore.kt @@ -2,6 +2,7 @@ package matterlink.command import matterlink.bridge.MessageHandlerInst import matterlink.bridge.command.BridgeCommandRegistry +import matterlink.config.baseCfg import matterlink.config.cfg object CommandCore { @@ -26,7 +27,7 @@ object CommandCore { "reload" -> { // if (MessageHandlerInst.connected) MessageHandlerInst.stop("Bridge restarting (reload command issued by console)") - cfg = cfg.load() + cfg = baseCfg.load() BridgeCommandRegistry.reloadCommands() // if (!MessageHandlerInst.connected) MessageHandlerInst.start("Bridge reconnected", false) diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index 9a1bbb7..1b2d128 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -1,26 +1,40 @@ package matterlink.config +import blue.endless.jankson.Jankson +import blue.endless.jankson.JsonObject +import blue.endless.jankson.impl.Marshaller +import blue.endless.jankson.impl.SyntaxError import matterlink.bridge.MessageHandlerInst +import matterlink.getOrDefault +import matterlink.instance +import matterlink.stackTraceString import java.io.File -import java.util.regex.Pattern +import java.io.FileNotFoundException -lateinit var cfg: BaseConfig +lateinit var cfg: BaseConfig.MatterLinkConfig +lateinit var baseCfg: BaseConfig -abstract class BaseConfig(rootDir: File) { +data class BaseConfig(val rootDir: File) { val cfgDirectory: File = rootDir.resolve("matterlink") - val mainCfgFile: File = cfgDirectory.resolve("matterlink.cfg") + val configFile: File = cfgDirectory.resolve("matterlink.hjson") + init { + instance.info("Reading bridge blueprints... from {}", rootDir) + baseCfg = this + } - var connect = ConnectOptions() - var debug = DebugOptions() - var incoming = IncomingOption() - var outgoing = OutgoingOptions() - var command = CommandOptions() - var update = UpdateOptions() + data class MatterLinkConfig( + val connect: ConnectOptions = ConnectOptions(), + val debug: DebugOptions = DebugOptions(), + val incoming: IncomingOptions = IncomingOptions(), + val outgoing: OutgoingOptions = OutgoingOptions(), + val command: CommandOptions = CommandOptions(), + val update: UpdateOptions = UpdateOptions() + ) data class CommandOptions( - val prefix: String = "!", + val prefix: Char = '!', val enable: Boolean = true ) @@ -36,10 +50,10 @@ abstract class BaseConfig(rootDir: File) { var logLevel: String = "INFO" ) - data class IncomingOption( + data class IncomingOptions( val chat: String = "<{username}> {text}", - val joinPart: String = "ยง6-- {username} {text}", - val action: String = "ยง5* {username} {text}", + val joinPart: String = "",//"ยง6-- {username} {text}", + val action: String = "",//"ยง5* {username} {text}", var stripColors: Boolean = true ) @@ -58,36 +72,36 @@ abstract class BaseConfig(rootDir: File) { data class DeathOptions( val enable: Boolean = true, val damageType: Boolean = true, - val damageTypeMapping: Map = mapOf( - "inFire" to "\uD83D\uDD25", //๐Ÿ”ฅ - "lightningBolt" to "\uD83C\uDF29", //๐ŸŒฉ - "onFire" to "\uD83D\uDD25", //๐Ÿ”ฅ - "lava" to "\uD83D\uDD25", //๐Ÿ”ฅ - "hotFloor" to "โ™จ๏ธ", - "inWall" to "", - "cramming" to "", - "drown" to "\uD83C\uDF0A", //๐ŸŒŠ - "starve" to "\uD83D\uDC80", //๐Ÿ’€ - "cactus" to "\uD83C\uDF35", //๐ŸŒต - "fall" to "\u2BEF๏ธ", //โฏฏ๏ธ - "flyIntoWall" to "\uD83D\uDCA8", //๐Ÿ’จ - "outOfWorld" to "\u2734", //โœด - "generic" to "\uD83D\uDC7B", //๐Ÿ‘ป - "magic" to "โœจ โšš", - "indirectMagic" to "โœจ โšš", - "wither" to "\uD83D\uDD71", //๐Ÿ•ฑ - "anvil" to "", - "fallingBlock" to "", - "dragonBreath" to "\uD83D\uDC32", //๐Ÿฒ - "fireworks" to "\uD83C\uDF86", //๐ŸŽ† + val damageTypeMapping: Map> = mapOf( + "inFire" to arrayOf("\uD83D\uDD25"), //๐Ÿ”ฅ + "lightningBolt" to arrayOf("\uD83C\uDF29"), //๐ŸŒฉ + "onFire" to arrayOf("\uD83D\uDD25"), //๐Ÿ”ฅ + "lava" to arrayOf("\uD83D\uDD25"), //๐Ÿ”ฅ + "hotFloor" to arrayOf("โ™จ๏ธ"), + "inWall" to arrayOf(), + "cramming" to arrayOf(), + "drown" to arrayOf("\uD83C\uDF0A"), //๐ŸŒŠ + "starve" to arrayOf("\uD83D\uDC80"), //๐Ÿ’€ + "cactus" to arrayOf("\uD83C\uDF35"), //๐ŸŒต + "fall" to arrayOf("\u2BEF๏ธ"), //โฏฏ๏ธ + "flyIntoWall" to arrayOf("\uD83D\uDCA8"), //๐Ÿ’จ + "outOfWorld" to arrayOf("\u2734"), //โœด + "generic" to arrayOf("\uD83D\uDC7B"), //๐Ÿ‘ป + "magic" to arrayOf("โœจ โšš"), + "indirectMagic" to arrayOf("โœจ โšš"), + "wither" to arrayOf("\uD83D\uDD71"), //๐Ÿ•ฑ + "anvil" to arrayOf(), + "fallingBlock" to arrayOf(), + "dragonBreath" to arrayOf("\uD83D\uDC32"), //๐Ÿฒ + "fireworks" to arrayOf("\uD83C\uDF86"), //๐ŸŽ† - "mob" to "\uD83D\uDC80", //๐Ÿ’€ - "player" to "\uD83D\uDDE1", //๐Ÿ—ก - "arrow" to "\uD83C\uDFF9", //๐Ÿน - "thrown" to "ๅฝกยฐ", - "thorns" to "\uD83C\uDF39", //๐ŸŒน - "explosion" to "\uD83D\uDCA3 \uD83D\uDCA5", //๐Ÿ’ฃ ๐Ÿ’ฅ - "explosion.player" to "\uD83D\uDCA3 \uD83D\uDCA5" //๐Ÿ’ฃ ๐Ÿ’ฅ + "mob" to arrayOf("\uD83D\uDC80"), //๐Ÿ’€ + "player" to arrayOf("\uD83D\uDDE1"), //๐Ÿ—ก + "arrow" to arrayOf("\uD83C\uDFF9"), //๐Ÿน + "thrown" to arrayOf("ๅฝกยฐ"), + "thorns" to arrayOf("\uD83C\uDF39"), //๐ŸŒน + "explosion" to arrayOf("\uD83D\uDCA3", "\uD83D\uDCA5"), //๐Ÿ’ฃ ๐Ÿ’ฅ + "explosion.player" to arrayOf("\uD83D\uDCA3", "\uD83D\uDCA5") //๐Ÿ’ฃ ๐Ÿ’ฅ ) ) @@ -101,228 +115,279 @@ abstract class BaseConfig(rootDir: File) { val enable: Boolean = true ) - protected fun load( - getBoolean: (key: String, category: String, default: Boolean, comment: String) -> Boolean, - getString: (key: String, category: String, default: String, comment: String) -> String, - getStringValidated: (key: String, category: String, default: String, comment: String, pattern: Pattern) -> String, - getStringValidValues: (key: String, category: String, default: String, comment: String, validValues: Array) -> String, - getStringList: (name: String, category: String, defaultValues: Array, comment: String) -> Array, - addCustomCategoryComment: (key: String, comment: String) -> Unit - ) { - - var category = "commands" - - addCustomCategoryComment(category, "User commands") - command = CommandOptions( - enable = getBoolean( - "enable", - category, - command.enable, - "Enable MC bridge commands" - ), - prefix = getStringValidated( - "prefix", - category, - command.prefix, - "Prefix for MC bridge commands. Accepts a single character (not alphanumeric or /)", - Pattern.compile("^[^0-9A-Za-z/]$") - ) - ) - - category = "connection" - addCustomCategoryComment(category, "Connection settings") - connect = ConnectOptions( - url = getString( - "connectURL", - category, - connect.url, - "The URL or IP address of the bridge server" - ), - authToken = getString( - "authToken", - category, - connect.authToken, - "Auth token used to connect to the bridge server" - ), - gateway = getString( - "gateway", - category, - connect.gateway, - "MatterBridge gateway" - ), - autoConnect = getBoolean( - "autoConnect", - category, - connect.autoConnect, - "Connect the relay on startup" - ) - ) - - category = "debug" - addCustomCategoryComment(category, "Options to help you figure out what happens and why, because computers can be silly") - debug = DebugOptions( - logLevel = getStringValidValues( - "logLevel", - category, - debug.logLevel, - "MatterLink log level", - arrayOf("INFO", "DEBUG", "TRACE") - ) - ) - - category = "incoming" - addCustomCategoryComment(category, "Gateway -> Server" + - "\nOptions all about receiving messages from the API" + - "\nFormatting options: " + - "\nAvailable variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}") - incoming = IncomingOption( - chat = getString( - "chat", - category, - incoming.chat, - "Generic chat event, just talking" - ), - joinPart = getString( - "joinPart", - category, - incoming.joinPart, - "Join and part events from other gateways" - ), - action = getString( - "action", - category, - incoming.action, - "User actions (/me) sent by users from other gateways" - ), - stripColors = getBoolean( - "stripColors", - category, - incoming.stripColors, - "strip colors from incoming text" - ) - ) - - category = "outgoing" - addCustomCategoryComment(category, "Server -> Gateway" + - "\nOptions all about sending messages to the API") - - outgoing = OutgoingOptions( - systemUser = getString( - "systemUser", - category, - outgoing.systemUser, - "Name of the server user (used by death and advancement messages and the /say command)" - ), - //outgoing events toggle - advancements = getBoolean( - "advancements", - category, - outgoing.advancements, - "Relay player achievements / advancements" - ), - announceConnect = getBoolean( - "announceConnect", - category, - outgoing.announceConnect, - "announce successful connection to the gateway" - ), - announceDisconnect = getBoolean( - "announceDisconnect", - category, - outgoing.announceConnect, - "announce intention to disconnect / reconnect" - ), - stripColors = getBoolean( - "stripColors", - category, - outgoing.stripColors, - "strip colors from nicknames and messages" - ) - ) - - category = "outgoing.death" - addCustomCategoryComment(category, "Death messages settings") - outgoing.death = DeathOptions( - - enable = getBoolean( - "enable", - category, - outgoing.death.enable, - "Relay player death messages" - ), - damageType = getBoolean( - "damageType", - category, - outgoing.death.damageType, - "Enable Damage type symbols on death messages" - ), - damageTypeMapping = getStringList( - "damageTypeMapping", - category, - outgoing.death.damageTypeMapping.map { entry -> - "${entry.key}=${entry.value}" - } - .toTypedArray(), - "Damage type mapping for death cause, " + - "\nseparate value and key with '=', " + - "\nseparate multiple values with spaces\n" - ).associate { - val key = it.substringBefore('=') - val value = it.substringAfter('=') - Pair(key, value) + companion object { + val jankson = Jankson + .builder() + .registerTypeAdapter(MatterLinkConfig::class.java) { + MatterLinkConfig( + command = it.getOrDefault( + "command", + CommandOptions(), + "User commands" + ), + connect = it.getOrDefault( + "connect", + ConnectOptions(), + "Connection Settings" + ), + debug = it.getOrDefault( + "debug", + DebugOptions(), + "Options to help you figure out what happens and why, because computers can be silly" + ), + incoming = it.getOrDefault( + "incoming", + IncomingOptions(), + """ + Gateway -> Server + Options all about receiving messages from the API + Formatting options: + Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping} + """.trimIndent() + ), + outgoing = it.getOrDefault( + "outgoing", + OutgoingOptions(), + """ + Server -> Gateway + Options all about sending messages to the API + """.trimIndent() + ), + update = it.getOrDefault( + "update", + UpdateOptions(), + "Update Settings" + ) + ) } - ) - - category = "outgoing.join&part" - addCustomCategoryComment(category, "relay join and part messages to the gatway" + - "\nFormatting options: " + - "\nAvailable variables: {username}, {username:antiping}") - outgoing.joinPart = JoinPartOptions( - enable = getBoolean( - "enable", - category, - outgoing.joinPart.enable, - "Relay when a player joins / parts the game" + - "\nany receiving end still needs to be configured with showJoinPart = true" + - "\nto display the messages" - ), - joinServer = getString( - "joinServer", - category, - outgoing.joinPart.joinServer, - "user join message sent to other gateways, available variables: {username}, {username:antiping}" - ), - partServer = getString( - "partServer", - category, - outgoing.joinPart.partServer, - "user part message sent to other gateways, available variables: {username}, {username:antiping}" - ) - ) - - - - category = "update" - addCustomCategoryComment(category, "Update Settings") - update = UpdateOptions( - enable = getBoolean( - "enable", - category, - update.enable, - "Enable Update checking" - ) - ) - - - MessageHandlerInst.config.url = connect.url - MessageHandlerInst.config.token = connect.authToken - MessageHandlerInst.config.gateway = connect.gateway - MessageHandlerInst.config.reconnectWait = connect.reconnectWait - - MessageHandlerInst.config.systemUser = outgoing.systemUser - MessageHandlerInst.config.announceConnect = outgoing.announceConnect - MessageHandlerInst.config.announceDisconnect = outgoing.announceDisconnect + .registerTypeAdapter(CommandOptions::class.java) { + with(CommandOptions()) { + CommandOptions( + enable = it.getOrDefault( + "enable", + enable, + "Enable MC bridge commands" + ), + prefix = it.getOrDefault( + "prefix", + prefix, + "Prefix for MC bridge commands. Accepts a single character (not alphanumeric or /)" + ) + ) + } + } + .registerTypeAdapter(ConnectOptions::class.java) { + with(ConnectOptions()) { + ConnectOptions( + url = it.getOrDefault( + "url", + url, + "The URL or IP address of the bridge server" + ), + authToken = it.getOrDefault( + "authToken", + authToken, + "Auth token used to connect to the bridge server" + ), + gateway = it.getOrDefault( + "gateway", + gateway, + "MatterBridge gateway" + ), + autoConnect = it.getOrDefault( + "autoConnect", + autoConnect, + "Connect the relay on startup" + ) + ) + } + } + .registerTypeAdapter(DebugOptions::class.java) { + with(DebugOptions()) { + DebugOptions( + logLevel = it.getOrDefault("loglevel", logLevel, "MatterLink log level") + ) + } + } + .registerTypeAdapter(IncomingOptions::class.java) { + with(IncomingOptions()) { + IncomingOptions( + chat = it.getOrDefault( + "chat", + chat, + "Generic chat event, just talking" + ), + joinPart = it.getOrDefault( + "joinPart", + joinPart, + "Join and part events from other gateways" + ), + action = it.getOrDefault( + "action", + action, + "User actions (/me) sent by users from other gateways" + ), + stripColors = it.getOrDefault( + "stripColors", + stripColors, + "strip colors from incoming text" + ) + ) + } + } + .registerTypeAdapter(OutgoingOptions::class.java) { + with(OutgoingOptions()) { + OutgoingOptions( + systemUser = it.getOrDefault( + "systemUser", + systemUser, + "Name of the server user (used by death and advancement messages and the /say command)" + ), + advancements = it.getOrDefault( + "advancements", + advancements, + "Relay player achievements / advancements" + ), + announceConnect = it.getOrDefault( + "announceConnect", + announceConnect, + "announce successful connection to the gateway" + ), + announceDisconnect = it.getOrDefault( + "announceDisconnect", + announceConnect, + "announce intention to disconnect / reconnect" + ), + stripColors = it.getOrDefault( + "stripColors", + stripColors, + "strip colors from nicknames and messages" + ), + death = it.getOrDefault( + "death", + DeathOptions(), + "Death messages settings" + ), + joinPart = it.getOrDefault( + "joinPart", + JoinPartOptions(), + "relay join and part messages to the gatway" + ) + ) + } + } + .registerTypeAdapter(DeathOptions::class.java) { jsonObj -> + with(DeathOptions()) { + DeathOptions( + enable = jsonObj.getOrDefault( + "enable", + enable, + "Relay player death messages" + ), + damageType = jsonObj.getOrDefault( + "damageType", + damageType, + "Enable Damage type symbols on death messages" + ), + damageTypeMapping = (jsonObj.getObject("damageTypeMapping") + ?: Marshaller.getFallback().serialize(damageTypeMapping) as JsonObject) + .let { + jsonObj.setComment( + "damageTypMapping", + "Damage type mapping for death cause" + ) + it.mapValues { (key, element) -> + it.getOrDefault(key, damageTypeMapping[key] ?: emptyArray(), key) + .apply { it[key] }.apply { + jsonObj["damageTypeMapping"] = it + } + } + } + ) + } + } + .registerTypeAdapter(JoinPartOptions:: + class.java) + { + with(JoinPartOptions()) { + JoinPartOptions( + enable = it.getOrDefault( + "enable", + enable, + "Relay when a player joins / parts the game" + + "\nany receiving end still needs to be configured with showJoinPart = true" + + "\nto display the messages" + ), + joinServer = it.getOrDefault( + "joinServer", + joinServer, + "user join message sent to other gateways, available variables: {username}, {username:antiping}" + ), + partServer = it.getOrDefault( + "partServer", + partServer, + "user part message sent to other gateways, available variables: {username}, {username:antiping}" + ) + ) + } + } + .registerTypeAdapter(UpdateOptions:: + class.java) + { + with(UpdateOptions()) { + UpdateOptions( + enable = it.getOrDefault( + "enable", + enable, + "Enable Update checking" + ) + ) + } + } + .build() } - abstract fun load(): BaseConfig + fun load(): MatterLinkConfig { + val jsonObject = try { + jankson.load(configFile) + } catch (e: SyntaxError) { + instance.error("error loading config: ${e.completeMessage}") + Marshaller.getFallback().serialize(MatterLinkConfig()) as JsonObject + } catch (e: FileNotFoundException) { + instance.error("creating config file $configFile") + configFile.createNewFile() + Marshaller.getFallback().serialize(MatterLinkConfig()) as JsonObject + } + instance.info("finished loading $jsonObject") + + val tmpCfg = try { + cfgDirectory.resolve("debug.matterlink.hjson").writeText(jsonObject.toJson(false, true)) + jankson.fromJson(jsonObject, MatterLinkConfig::class.java).apply { + configFile.writeText(jsonObject.toJson(true, true)) + instance.info("loaded config: $this") + } + } catch (e: SyntaxError) { + instance.error("error parsing config: ${e.completeMessage} ${e.stackTraceString}") + MatterLinkConfig() + } catch (e: IllegalStateException) { + instance.error(e.stackTraceString) + MatterLinkConfig() + } + + +// val defaultJsonObject = jankson.load("{}") +// jankson.fromJson(defaultJsonObject, MatterLinkConfig::class.java) +// val nonDefault = jsonObject.getDelta(defaultJsonObject) + + MessageHandlerInst.config.url = cfg.connect.url + MessageHandlerInst.config.token = cfg.connect.authToken + MessageHandlerInst.config.gateway = cfg.connect.gateway + MessageHandlerInst.config.reconnectWait = cfg.connect.reconnectWait + + MessageHandlerInst.config.systemUser = cfg.outgoing.systemUser + MessageHandlerInst.config.announceConnect = cfg.outgoing.announceConnect + MessageHandlerInst.config.announceDisconnect = cfg.outgoing.announceDisconnect + + return tmpCfg + } } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/config/CommandConfig.kt b/core/src/main/kotlin/matterlink/config/CommandConfig.kt index 6b94890..6a36113 100644 --- a/core/src/main/kotlin/matterlink/config/CommandConfig.kt +++ b/core/src/main/kotlin/matterlink/config/CommandConfig.kt @@ -3,7 +3,6 @@ package matterlink.config import blue.endless.jankson.Jankson import blue.endless.jankson.JsonObject import blue.endless.jankson.JsonPrimitive -import blue.endless.jankson.impl.Marshaller import blue.endless.jankson.impl.SyntaxError import matterlink.bridge.command.CommandType import matterlink.bridge.command.CustomCommand @@ -15,7 +14,7 @@ typealias CommandMap = MutableMap typealias DefaultCommands = Map> object CommandConfig { - private val configFile: File = cfg.cfgDirectory.resolve("commands.json") + private val configFile: File = baseCfg.cfgDirectory.resolve("commands.hjson") private val default: DefaultCommands = mapOf( "tps" to ("""Your run off the mill tps commands, change it to /sampler tps or /cofh tps if you like @@ -89,20 +88,14 @@ object CommandConfig { ) } } - .registerPrimitiveTypeAdapter(CommandType::class.java) {jsonObj -> - CommandType.valueOf(jsonObj.toString()) - } - .registerPrimitiveTypeAdapter(Regex::class.java) {jsonObj -> - jsonObj.toString().toRegex() + .registerPrimitiveTypeAdapter(Regex::class.java) { + it.toString().toRegex() } .build() - Marshaller.getFallback().registerSerializer(Regex::class.java) { + jankson.marshaller.registerSerializer(Regex::class.java) { JsonPrimitive(it.pattern) } - Marshaller.getFallback().registerSerializer(CommandType::class.java) { - JsonPrimitive(it.name) - } val jsonObject = try { jankson.load(configFile) @@ -117,8 +110,13 @@ object CommandConfig { commands.clear() jsonObject.forEach { key, element -> instance.trace("loading command '$key'") - val command = jankson.fromJson(element.toJson(), CustomCommand::class.java) - commands[key] = command + val command = jsonObject.get(CustomCommand::class.java, key) + if(command != null) + commands[key] = command + else { + instance.error("could not parse key: $key, value: '$element' as CustomCommand") + instance.error("skipping $key") + } } //apply defaults @@ -126,7 +124,7 @@ object CommandConfig { val command = commands[k] if (command == null || command.defaultCommand == true) { commands[k] = defCommand - val element = Marshaller.getFallback().serialize(defCommand) + val element = jankson.marshaller.serialize(defCommand) jsonObject.putDefault(k, element, comment) } } @@ -134,7 +132,9 @@ object CommandConfig { instance.debug("loaded jsonObj: $jsonObject") instance.debug("loaded commandMap: $commands") - configFile.writeText(jsonObject.toJson(true, true)) + val nonDefaultJsonObj = jsonObject.getDelta(jankson.marshaller.serialize(default.mapValues { it.value.second }) as JsonObject) + + configFile.writeText(nonDefaultJsonObj.toJson(true, true)) return true } diff --git a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt index c96ba5e..294350a 100644 --- a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt +++ b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt @@ -15,7 +15,7 @@ object PermissionConfig { .builder() .build() - private val configFile: File = cfg.cfgDirectory.resolve("permissions.json") + private val configFile: File = baseCfg.cfgDirectory.resolve("permissions.hjson") private val default = mapOf( "irc.esper" to mapOf( @@ -42,13 +42,17 @@ object PermissionConfig { } default.forEach { platform, userMap -> - val jsonUserMap = jsonObject.getOrDefault(platform, JsonObject()) as JsonObject - userMap.forEach { user, (powerlevel, comment) -> - instance.trace("loading platform: $platform user: $user powwerlevel: $powerlevel") - val element = Marshaller.getFallback().serialize(powerlevel) - jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() }) + val jsonUserMap = jsonObject.getOrDefault(platform, JsonObject()) + if(jsonUserMap is JsonObject) { + userMap.forEach { user, (powerlevel, comment) -> + instance.trace("loading platform: $platform user: $user powwerlevel: $powerlevel") + val element = Marshaller.getFallback().serialize(powerlevel) + jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() }) + } + jsonObject[platform] = jsonUserMap + } else { + instance.error("cannot parse platform: $platform , value: '$jsonUserMap' as Map, skipping") } - jsonObject[platform] = jsonUserMap } jsonObject.forEach { platform, jsonUserMap -> diff --git a/core/src/main/kotlin/matterlink/handlers/DeathHandler.kt b/core/src/main/kotlin/matterlink/handlers/DeathHandler.kt index 3538c35..ac42cc3 100644 --- a/core/src/main/kotlin/matterlink/handlers/DeathHandler.kt +++ b/core/src/main/kotlin/matterlink/handlers/DeathHandler.kt @@ -18,8 +18,8 @@ object DeathHandler { if (cfg.outgoing.death.enable) { var msg = deathMessage.stripColorOut.replace(player, player.stripColorOut.antiping) if (cfg.outgoing.death.damageType) { - val emojis = cfg.outgoing.death.damageTypeMapping[damageType]?.split(' ') - ?: listOf("\uD83D\uDC7B unknown type '$damageType'") + val emojis = cfg.outgoing.death.damageTypeMapping[damageType] + ?: arrayOf("\uD83D\uDC7B unknown type '$damageType'") val damageEmoji = emojis[random.nextInt(emojis.size)] msg += " $damageEmoji" }