deduplicate configuration code

This commit is contained in:
NikkyAI 2018-02-11 21:12:56 +01:00
parent 440ab5ac9f
commit f69eac719c
5 changed files with 195 additions and 379 deletions

View File

@ -1,124 +1,20 @@
package matterlink
import matterlink.config.IMatterLinkConfig
import matterlink.config.BaseConfig
import matterlink.config.cfg
import net.minecraftforge.common.config.Configuration
import java.io.File
import java.util.regex.Pattern
class MatterLinkConfig(file: File) : IMatterLinkConfig() {
class MatterLinkConfig(file: File) : BaseConfig() {
init {
logger.info("Reading bridge blueprints... from {}", file)
val config = Configuration(file)
config.addCustomCategoryComment(CATEGORY_RELAY_OPTIONS, "Relay options")
relay = RelayOptions(
systemUser = config.getString(
"systemUser",
CATEGORY_RELAY_OPTIONS,
relay.systemUser,
"Name of the server user (used by death and advancement messages and the /say command)"
),
deathEvents = config.getBoolean(
"deathEvents",
CATEGORY_RELAY_OPTIONS,
relay.deathEvents,
"Relay player death messages"
),
advancements = config.getBoolean(
"advancements",
CATEGORY_RELAY_OPTIONS,
relay.advancements,
"Relay player advancements"
),
joinLeave = config.getBoolean(
"joinLeave",
CATEGORY_RELAY_OPTIONS,
relay.joinLeave,
"Relay when a player joins or leaves the game"
)
)
config.addCustomCategoryComment(CATEGORY_COMMAND, "User commands")
command = CommandOptions(
enable = config.getBoolean(
"enable",
CATEGORY_COMMAND,
command.enable,
"Enable MC bridge commands"
),
prefix = config.getString(
"prefix",
CATEGORY_COMMAND,
command.prefix,
"Prefix for MC bridge commands. Accepts a single character (not alphanumeric or /)",
Pattern.compile("^[^0-9A-Za-z/]$")
)
)
config.addCustomCategoryComment(CATEGORY_FORMATTING_INCOMING, "Gateway -> Server" +
"Formatting options: " +
"Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}")
formatting = FormattingOptions(
chat = config.getString(
"chat",
CATEGORY_FORMATTING_INCOMING,
formatting.chat,
"Generic chat event, just talking"
),
joinLeave = config.getString(
"joinLeave",
CATEGORY_FORMATTING_INCOMING,
formatting.joinLeave,
"Join and leave events from other gateways"
),
action = config.getString(
"action",
CATEGORY_FORMATTING_INCOMING,
formatting.action,
"User actions (/me) sent by users from other gateways"
)
)
config.addCustomCategoryComment(CATEGORY_FORMATTING_JOIN_LEAVE, "Server -> Gateway" +
"Formatting options: " +
"Available variables: {username}, {username:antiping}")
formattingJoinLeave = FormattingJoinLeave(
joinServer = config.getString(
"joinServer",
CATEGORY_FORMATTING_JOIN_LEAVE,
formattingJoinLeave.joinServer,
"user join message sent to other gateways, available variables: {username}, {username:antiping}"
),
leaveServer = config.getString(
"leaveServer",
CATEGORY_FORMATTING_JOIN_LEAVE,
formattingJoinLeave.leaveServer,
"user leave message sent to other gateways, available variables: {username}, {username:antiping}"
)
)
config.addCustomCategoryComment(CATEGORY_CONNECTION, "Connection settings")
connect = ConnectOptions(
url = config.getString(
"connectURL",
CATEGORY_CONNECTION,
connect.url,
"The URL or IP address of the bridge server"
),
authToken = config.getString(
"authToken",
CATEGORY_CONNECTION,
connect.authToken,
"Auth token used to connect to the bridge server"
),
gateway = config.getString(
"gateway",
CATEGORY_CONNECTION,
connect.gateway,
"MatterBridge gateway"
)
load(
getBoolean = config::getBoolean,
getString = config::getString,
getStringValidated = config::getString,
addCustomCategoryComment = config::addCustomCategoryComment
)
if (config.hasChanged()) config.save()

View File

@ -1,124 +1,20 @@
package matterlink
import matterlink.config.IMatterLinkConfig
import matterlink.config.BaseConfig
import matterlink.config.cfg
import net.minecraftforge.common.config.Configuration
import java.io.File
import java.util.regex.Pattern
class MatterLinkConfig(file: File) : IMatterLinkConfig() {
class MatterLinkConfig(file: File) : BaseConfig() {
init {
logger.info("Reading bridge blueprints... from {}", file)
val config = Configuration(file)
config.addCustomCategoryComment(CATEGORY_RELAY_OPTIONS, "Relay options")
relay = RelayOptions(
systemUser = config.getString(
"systemUser",
CATEGORY_RELAY_OPTIONS,
relay.systemUser,
"Name of the server user (used by death and advancement messages and the /say command)"
),
deathEvents = config.getBoolean(
"deathEvents",
CATEGORY_RELAY_OPTIONS,
relay.deathEvents,
"Relay player death messages"
),
advancements = config.getBoolean(
"advancements",
CATEGORY_RELAY_OPTIONS,
relay.advancements,
"Relay player advancements"
),
joinLeave = config.getBoolean(
"joinLeave",
CATEGORY_RELAY_OPTIONS,
relay.joinLeave,
"Relay when a player joins or leaves the game"
)
)
config.addCustomCategoryComment(CATEGORY_COMMAND, "User commands")
command = CommandOptions(
enable = config.getBoolean(
"enable",
CATEGORY_COMMAND,
command.enable,
"Enable MC bridge commands"
),
prefix = config.getString(
"prefix",
CATEGORY_COMMAND,
command.prefix,
"Prefix for MC bridge commands. Accepts a single character (not alphanumeric or /)",
Pattern.compile("^[^0-9A-Za-z/]$")
)
)
config.addCustomCategoryComment(CATEGORY_FORMATTING_INCOMING, "Gateway -> Server" +
"Formatting options: " +
"Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}")
formatting = FormattingOptions(
chat = config.getString(
"chat",
CATEGORY_FORMATTING_INCOMING,
formatting.chat,
"Generic chat event, just talking"
),
joinLeave = config.getString(
"joinLeave",
CATEGORY_FORMATTING_INCOMING,
formatting.joinLeave,
"Join and leave events from other gateways"
),
action = config.getString(
"action",
CATEGORY_FORMATTING_INCOMING,
formatting.action,
"User actions (/me) sent by users from other gateways"
)
)
config.addCustomCategoryComment(CATEGORY_FORMATTING_JOIN_LEAVE, "Server -> Gateway" +
"Formatting options: " +
"Available variables: {username}, {username:antiping}")
formattingJoinLeave = FormattingJoinLeave(
joinServer = config.getString(
"joinServer",
CATEGORY_FORMATTING_JOIN_LEAVE,
formattingJoinLeave.joinServer,
"user join message sent to other gateways, available variables: {username}, {username:antiping}"
),
leaveServer = config.getString(
"leaveServer",
CATEGORY_FORMATTING_JOIN_LEAVE,
formattingJoinLeave.leaveServer,
"user leave message sent to other gateways, available variables: {username}, {username:antiping}"
)
)
config.addCustomCategoryComment(CATEGORY_CONNECTION, "Connection settings")
connect = ConnectOptions(
url = config.getString(
"connectURL",
CATEGORY_CONNECTION,
connect.url,
"The URL or IP address of the bridge server"
),
authToken = config.getString(
"authToken",
CATEGORY_CONNECTION,
connect.authToken,
"Auth token used to connect to the bridge server"
),
gateway = config.getString(
"gateway",
CATEGORY_CONNECTION,
connect.gateway,
"MatterBridge gateway"
)
load(
getBoolean = config::getBoolean,
getString = config::getString,
getStringValidated = config::getString,
addCustomCategoryComment = config::addCustomCategoryComment
)
if (config.hasChanged()) config.save()

View File

@ -1,124 +1,20 @@
package matterlink
import matterlink.config.IMatterLinkConfig
import matterlink.config.BaseConfig
import matterlink.config.cfg
import net.minecraftforge.common.config.Configuration
import java.io.File
import java.util.regex.Pattern
class MatterLinkConfig(file: File) : IMatterLinkConfig() {
class MatterLinkConfig(file: File) : BaseConfig() {
init {
logger.info("Reading bridge blueprints... from {}", file)
val config = Configuration(file)
config.addCustomCategoryComment(CATEGORY_RELAY_OPTIONS, "Relay options")
relay = RelayOptions(
systemUser = config.getString(
"systemUser",
CATEGORY_RELAY_OPTIONS,
relay.systemUser,
"Name of the server user (used by death and advancement messages and the /say command)"
),
deathEvents = config.getBoolean(
"deathEvents",
CATEGORY_RELAY_OPTIONS,
relay.deathEvents,
"Relay player death messages"
),
advancements = config.getBoolean(
"advancements",
CATEGORY_RELAY_OPTIONS,
relay.advancements,
"Relay player advancements"
),
joinLeave = config.getBoolean(
"joinLeave",
CATEGORY_RELAY_OPTIONS,
relay.joinLeave,
"Relay when a player joins or leaves the game"
)
)
config.addCustomCategoryComment(CATEGORY_COMMAND, "User commands")
command = CommandOptions(
enable = config.getBoolean(
"enable",
CATEGORY_COMMAND,
command.enable,
"Enable MC bridge commands"
),
prefix = config.getString(
"prefix",
CATEGORY_COMMAND,
command.prefix,
"Prefix for MC bridge commands. Accepts a single character (not alphanumeric or /)",
Pattern.compile("^[^0-9A-Za-z/]$")
)
)
config.addCustomCategoryComment(CATEGORY_FORMATTING_INCOMING, "Gateway -> Server" +
"Formatting options: " +
"Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}")
formatting = FormattingOptions(
chat = config.getString(
"chat",
CATEGORY_FORMATTING_INCOMING,
formatting.chat,
"Generic chat event, just talking"
),
joinLeave = config.getString(
"joinLeave",
CATEGORY_FORMATTING_INCOMING,
formatting.joinLeave,
"Join and leave events from other gateways"
),
action = config.getString(
"action",
CATEGORY_FORMATTING_INCOMING,
formatting.action,
"User actions (/me) sent by users from other gateways"
)
)
config.addCustomCategoryComment(CATEGORY_FORMATTING_JOIN_LEAVE, "Server -> Gateway" +
"Formatting options: " +
"Available variables: {username}, {username:antiping}")
formattingJoinLeave = FormattingJoinLeave(
joinServer = config.getString(
"joinServer",
CATEGORY_FORMATTING_JOIN_LEAVE,
formattingJoinLeave.joinServer,
"user join message sent to other gateways, available variables: {username}, {username:antiping}"
),
leaveServer = config.getString(
"leaveServer",
CATEGORY_FORMATTING_JOIN_LEAVE,
formattingJoinLeave.leaveServer,
"user leave message sent to other gateways, available variables: {username}, {username:antiping}"
)
)
config.addCustomCategoryComment(CATEGORY_CONNECTION, "Connection settings")
connect = ConnectOptions(
url = config.getString(
"connectURL",
CATEGORY_CONNECTION,
connect.url,
"The URL or IP address of the bridge server"
),
authToken = config.getString(
"authToken",
CATEGORY_CONNECTION,
connect.authToken,
"Auth token used to connect to the bridge server"
),
gateway = config.getString(
"gateway",
CATEGORY_CONNECTION,
connect.gateway,
"MatterBridge gateway"
)
load(
getBoolean = config::getBoolean,
getString = config::getString,
getStringValidated = config::getString,
addCustomCategoryComment = config::addCustomCategoryComment
)
if (config.hasChanged()) config.save()

View File

@ -0,0 +1,174 @@
package matterlink.config
import matterlink.instance
import java.util.regex.Pattern
var cfg: BaseConfig? = null
abstract class BaseConfig {
companion object {
private val CATEGORY_RELAY_OPTIONS = "relay"
private val CATEGORY_FORMATTING_INCOMING = "formatting"
private val CATEGORY_FORMATTING_JOIN_LEAVE = "formatting_join_leave"
private val CATEGORY_CONNECTION = "connection"
private val CATEGORY_COMMAND = "command"
}
var relay: RelayOptions = RelayOptions()
var connect: ConnectOptions = ConnectOptions()
var formatting: FormattingOptions = FormattingOptions()
var formattingJoinLeave: FormattingJoinLeave = FormattingJoinLeave()
var command: CommandOptions = CommandOptions()
data class RelayOptions(
var systemUser: String = "Server",
var deathEvents: Boolean = true,
var advancements: Boolean = true,
var joinLeave: Boolean = true
)
data class FormattingOptions(
var chat: String = "<{username}> {text}",
var joinLeave: String = "§6-- {username} {text}",
var action: String = "§5* {username} {text}"
)
data class FormattingJoinLeave(
var joinServer: String = "{username:antiping} has connected to the server",
var leaveServer: String = "{username:antiping} has disconnected from the server"
)
data class ConnectOptions(
var url: String = "http://localhost:4242",
var authToken: String = "",
var gateway: String = "minecraft"
)
data class CommandOptions(
var prefix: String = "$",
var 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,
addCustomCategoryComment: (key: String, comment: String) -> Unit
) {
var category = CATEGORY_RELAY_OPTIONS
addCustomCategoryComment(CATEGORY_RELAY_OPTIONS, "Relay options")
relay = RelayOptions(
systemUser = getString(
"systemUser",
category,
relay.systemUser,
"Name of the server user (used by death and advancement messages and the /say command)"
),
deathEvents = getBoolean(
"deathEvents",
category,
relay.deathEvents,
"Relay player death messages"
),
advancements = getBoolean(
"advancements",
category,
relay.advancements,
"Relay player advancements"
),
joinLeave = getBoolean(
"joinLeave",
category,
relay.joinLeave,
"Relay when a player joins or leaves the game"
)
)
category = CATEGORY_COMMAND
addCustomCategoryComment(CATEGORY_COMMAND, "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 = CATEGORY_FORMATTING_INCOMING
addCustomCategoryComment(CATEGORY_FORMATTING_INCOMING, "Gateway -> Server" +
"Formatting options: " +
"Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}")
formatting = FormattingOptions(
chat = getString(
"chat",
category,
formatting.chat,
"Generic chat event, just talking"
),
joinLeave = getString(
"joinLeave",
category,
formatting.joinLeave,
"Join and leave events from other gateways"
),
action = getString(
"action",
category,
formatting.action,
"User actions (/me) sent by users from other gateways"
)
)
category = CATEGORY_FORMATTING_JOIN_LEAVE
addCustomCategoryComment(CATEGORY_FORMATTING_JOIN_LEAVE, "Server -> Gateway" +
"Formatting options: " +
"Available variables: {username}, {username:antiping}")
formattingJoinLeave = FormattingJoinLeave(
joinServer = getString(
"joinServer",
category,
formattingJoinLeave.joinServer,
"user join message sent to other gateways, available variables: {username}, {username:antiping}"
),
leaveServer = getString(
"leaveServer",
category,
formattingJoinLeave.leaveServer,
"user leave message sent to other gateways, available variables: {username}, {username:antiping}"
)
)
category = CATEGORY_CONNECTION
addCustomCategoryComment(CATEGORY_CONNECTION, "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"
)
)
}
}

View File

@ -1,46 +0,0 @@
package matterlink.config
var cfg: IMatterLinkConfig? = null
abstract class IMatterLinkConfig {
protected val CATEGORY_RELAY_OPTIONS = "relay"
protected val CATEGORY_FORMATTING_INCOMING = "formatting"
protected val CATEGORY_FORMATTING_JOIN_LEAVE = "formatting_join_leave"
protected val CATEGORY_CONNECTION = "connection"
protected val CATEGORY_COMMAND = "command"
var relay: RelayOptions = RelayOptions()
var connect: ConnectOptions = ConnectOptions()
var formatting: FormattingOptions = FormattingOptions()
var formattingJoinLeave: FormattingJoinLeave = FormattingJoinLeave()
var command: CommandOptions = CommandOptions()
data class RelayOptions(
val systemUser: String = "Server",
val deathEvents: Boolean = true,
val advancements: Boolean = true,
val joinLeave: Boolean = true
)
data class FormattingOptions(
val chat: String = "<{username}> {text}",
val joinLeave: String = "§6-- {username} {text}",
val action: String = "§5* {username} {text}"
)
data class FormattingJoinLeave(
val joinServer: String = "{username:antiping} has connected to the server",
val leaveServer: String = "{username:antiping} has disconnected from the server"
)
data class ConnectOptions(
val url: String = "http://localhost:4242",
val authToken: String = "",
val gateway: String = "minecraft"
)
data class CommandOptions(
val prefix: String = "$",
val enable: Boolean = true
)
}