restructure configuration, fix connection errors, register events properly on 1.7.10

This commit is contained in:
NikkyAI 2018-02-18 04:50:58 +01:00
parent 23c65bd4e1
commit 7fd8b31474
23 changed files with 160 additions and 122 deletions

View File

@ -11,6 +11,7 @@ import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.* import net.minecraftforge.fml.common.event.*
import org.apache.logging.log4j.Level import org.apache.logging.log4j.Level
import org.apache.logging.log4j.Logger import org.apache.logging.log4j.Logger
import java.io.File
lateinit var logger: Logger lateinit var logger: Logger
@ -33,7 +34,7 @@ object MatterLink : IMatterLink() {
logger = event.modLog logger = event.modLog
logger.info("Building bridge!") logger.info("Building bridge!")
cfg = MatterLinkConfig(event.suggestedConfigurationFile) cfg = MatterLinkConfig(event.modConfigurationDirectory)
} }
@Mod.EventHandler @Mod.EventHandler
@ -41,11 +42,6 @@ object MatterLink : IMatterLink() {
this.registerBridgeCommands() this.registerBridgeCommands()
} }
@Mod.EventHandler
fun serverAboutToStart(event: FMLServerAboutToStartEvent) {
// MessageHandler.start(clear = true)
}
@Mod.EventHandler @Mod.EventHandler
fun serverStarting(event: FMLServerStartingEvent) { fun serverStarting(event: FMLServerStartingEvent) {
logger.debug("Registering server commands") logger.debug("Registering server commands")
@ -70,10 +66,10 @@ object MatterLink : IMatterLink() {
} }
override fun log(level: String, formatString: String, vararg data: Any) = override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO), formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender
override val mcVersion: String = MCVERSION override val mcVersion: String = MCVERSION
override val modVersion: String = MODVERSION override val modVersion: String = MODVERSION
} }

View File

@ -5,10 +5,10 @@ import matterlink.config.cfg
import net.minecraftforge.common.config.Configuration import net.minecraftforge.common.config.Configuration
import java.io.File import java.io.File
class MatterLinkConfig(val file: File) : BaseConfig() { class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) {
init { init {
logger.info("Reading bridge blueprints... from {}", file) logger.info("Reading bridge blueprints... from {}", cfgDirectory)
val config = Configuration(file) val config = Configuration(mainCfgFile)
load( load(
getBoolean = config::getBoolean, getBoolean = config::getBoolean,
@ -18,9 +18,8 @@ class MatterLinkConfig(val file: File) : BaseConfig() {
getStringList = config::getStringList, getStringList = config::getStringList,
addCustomCategoryComment = config::addCustomCategoryComment addCustomCategoryComment = config::addCustomCategoryComment
) )
if (config.hasChanged()) config.save() if (config.hasChanged()) config.save()
} }
override fun load() = MatterLinkConfig(file) override fun load() = MatterLinkConfig(baseCfgDir)
} }

View File

@ -48,7 +48,7 @@ object MatterlinkCommandSender : IMinecraftCommandSender, ICommandSender {
override fun sendMessage(@Nonnull component: ITextComponent?) { override fun sendMessage(@Nonnull component: ITextComponent?) {
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg!!.relay.systemUser, username = cfg.relay.systemUser,
text = component!!.unformattedComponentText text = component!!.unformattedComponentText
)) ))

View File

@ -11,6 +11,7 @@ import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.* import net.minecraftforge.fml.common.event.*
import org.apache.logging.log4j.Level import org.apache.logging.log4j.Level
import org.apache.logging.log4j.Logger import org.apache.logging.log4j.Logger
import java.io.File
lateinit var logger: Logger lateinit var logger: Logger
@ -33,17 +34,12 @@ object MatterLink : IMatterLink() {
logger = event.modLog logger = event.modLog
logger.info("Building bridge!") logger.info("Building bridge!")
cfg = MatterLinkConfig(event.suggestedConfigurationFile) cfg = MatterLinkConfig(event.modConfigurationDirectory)
} }
@Mod.EventHandler @Mod.EventHandler
fun init(event: FMLInitializationEvent) { fun init(event: FMLInitializationEvent) {
this.registerBridgeCommands() this.registerBridgeCommands()
}
@Mod.EventHandler
fun serverAboutToStart(event: FMLServerAboutToStartEvent) {
// MessageHandler.start(clear = true)
} }
@Mod.EventHandler @Mod.EventHandler
@ -70,7 +66,7 @@ object MatterLink : IMatterLink() {
} }
override fun log(level: String, formatString: String, vararg data: Any) = override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO), formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender

View File

@ -5,10 +5,10 @@ import matterlink.config.cfg
import net.minecraftforge.common.config.Configuration import net.minecraftforge.common.config.Configuration
import java.io.File import java.io.File
class MatterLinkConfig(val file: File) : BaseConfig() { class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) {
init { init {
logger.info("Reading bridge blueprints... from {}", file) logger.info("Reading bridge blueprints... from {}", cfgDirectory)
val config = Configuration(file) val config = Configuration(mainCfgFile)
load( load(
getBoolean = config::getBoolean, getBoolean = config::getBoolean,
@ -18,9 +18,8 @@ class MatterLinkConfig(val file: File) : BaseConfig() {
getStringList = config::getStringList, getStringList = config::getStringList,
addCustomCategoryComment = config::addCustomCategoryComment addCustomCategoryComment = config::addCustomCategoryComment
) )
if (config.hasChanged()) config.save() if (config.hasChanged()) config.save()
} }
override fun load() = MatterLinkConfig(file) override fun load() = MatterLinkConfig(baseCfgDir)
} }

View File

@ -48,7 +48,7 @@ object MatterlinkCommandSender : IMinecraftCommandSender, ICommandSender {
override fun sendMessage(@Nonnull component: ITextComponent?) { override fun sendMessage(@Nonnull component: ITextComponent?) {
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg!!.relay.systemUser, username = cfg.relay.systemUser,
text = component!!.unformattedComponentText text = component!!.unformattedComponentText
)) ))

View File

@ -52,7 +52,7 @@ object EventHandler {
fun commandEvent(e: CommandEvent) { fun commandEvent(e: CommandEvent) {
val sender = when { val sender = when {
e.sender is EntityPlayer -> e.sender.name e.sender is EntityPlayer -> e.sender.name
e.sender is DedicatedServer -> cfg!!.relay.systemUser e.sender is DedicatedServer -> cfg.relay.systemUser
e.sender is TileEntityCommandBlock -> "CommandBlock" e.sender is TileEntityCommandBlock -> "CommandBlock"
else -> return else -> return
} }

View File

@ -11,6 +11,7 @@ import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.* import net.minecraftforge.fml.common.event.*
import org.apache.logging.log4j.Level import org.apache.logging.log4j.Level
import org.apache.logging.log4j.Logger import org.apache.logging.log4j.Logger
import java.io.File
lateinit var logger: Logger lateinit var logger: Logger
@ -33,7 +34,7 @@ object MatterLink : IMatterLink() {
logger = event.modLog logger = event.modLog
logger.info("Building bridge!") logger.info("Building bridge!")
cfg = MatterLinkConfig(event.suggestedConfigurationFile) cfg = MatterLinkConfig(event.modConfigurationDirectory)
} }
@Mod.EventHandler @Mod.EventHandler
@ -70,7 +71,7 @@ object MatterLink : IMatterLink() {
} }
override fun log(level: String, formatString: String, vararg data: Any) = override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO), formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender
override val mcVersion: String = MCVERSION override val mcVersion: String = MCVERSION

View File

@ -5,10 +5,10 @@ import matterlink.config.cfg
import net.minecraftforge.common.config.Configuration import net.minecraftforge.common.config.Configuration
import java.io.File import java.io.File
class MatterLinkConfig(val file: File) : BaseConfig() { class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) {
init { init {
logger.info("Reading bridge blueprints... from {}", file) logger.info("Reading bridge blueprints... from {}", cfgDirectory)
val config = Configuration(file) val config = Configuration(mainCfgFile)
load( load(
getBoolean = config::getBoolean, getBoolean = config::getBoolean,
@ -21,5 +21,5 @@ class MatterLinkConfig(val file: File) : BaseConfig() {
if (config.hasChanged()) config.save() if (config.hasChanged()) config.save()
} }
override fun load() = MatterLinkConfig(file) override fun load() = MatterLinkConfig(baseCfgDir)
} }

View File

@ -39,7 +39,7 @@ object MatterlinkCommandSender : IMinecraftCommandSender, ICommandSender {
override fun sendMessage(@Nonnull component: ITextComponent?) { override fun sendMessage(@Nonnull component: ITextComponent?) {
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg!!.relay.systemUser, username = cfg.relay.systemUser,
text = component!!.unformattedComponentText text = component!!.unformattedComponentText
)) ))

View File

@ -1,5 +1,6 @@
package matterlink package matterlink
import cpw.mods.fml.common.FMLCommonHandler
import cpw.mods.fml.common.Mod import cpw.mods.fml.common.Mod
import cpw.mods.fml.common.event.* import cpw.mods.fml.common.event.*
import matterlink.command.CommandMatterlink import matterlink.command.CommandMatterlink
@ -28,10 +29,12 @@ class MatterLink : IMatterLink() {
@Mod.EventHandler @Mod.EventHandler
fun preInit(event: FMLPreInitializationEvent) { fun preInit(event: FMLPreInitializationEvent) {
MinecraftForge.EVENT_BUS.register(EventHandler) MinecraftForge.EVENT_BUS.register(EventHandler)
FMLCommonHandler.instance().bus().register(EventHandler)
logger = event.modLog logger = event.modLog
logger.info("Building bridge!") logger.info("Building bridge!")
cfg = MatterLinkConfig(event.suggestedConfigurationFile) cfg = MatterLinkConfig(event.modConfigurationDirectory)
} }
@Mod.EventHandler @Mod.EventHandler
@ -63,10 +66,10 @@ class MatterLink : IMatterLink() {
} }
override fun log(level: String, formatString: String, vararg data: Any) = override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO), formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender
override val mcVersion: String = MCVERSION override val mcVersion: String = MCVERSION
override val modVersion: String = MODVERSION override val modVersion: String = MODVERSION
} }

View File

@ -4,10 +4,10 @@ import matterlink.config.BaseConfig
import net.minecraftforge.common.config.Configuration import net.minecraftforge.common.config.Configuration
import java.io.File import java.io.File
class MatterLinkConfig(val file: File) : BaseConfig() { class MatterLinkConfig(val baseCfgDir: File) : BaseConfig(baseCfgDir) {
init { init {
logger.info("Reading bridge blueprints... from {}", file) logger.info("Reading bridge blueprints... from {}", cfgDirectory)
val config = Configuration(file) val config = Configuration(mainCfgFile)
load( load(
getBoolean = config::getBoolean, getBoolean = config::getBoolean,
@ -17,9 +17,8 @@ class MatterLinkConfig(val file: File) : BaseConfig() {
getStringList = config::getStringList, getStringList = config::getStringList,
addCustomCategoryComment = config::addCustomCategoryComment addCustomCategoryComment = config::addCustomCategoryComment
) )
if (config.hasChanged()) config.save() if (config.hasChanged()) config.save()
} }
override fun load() = MatterLinkConfig(file) override fun load() = MatterLinkConfig(baseCfgDir)
} }

View File

@ -5,6 +5,7 @@ import matterlink.bridge.command.BridgeCommandRegistry
import matterlink.command.IMinecraftCommandSender import matterlink.command.IMinecraftCommandSender
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.update.UpdateChecker import matterlink.update.UpdateChecker
import java.io.File
lateinit var instance: IMatterLink lateinit var instance: IMatterLink
@ -12,21 +13,24 @@ abstract class IMatterLink {
abstract val mcVersion: String abstract val mcVersion: String
abstract val modVersion: String abstract val modVersion: String
lateinit var cfgDir: File
abstract var commandSender: IMinecraftCommandSender abstract var commandSender: IMinecraftCommandSender
abstract fun wrappedSendToPlayers(msg: String) abstract fun wrappedSendToPlayers(msg: String)
abstract fun wrappedPlayerList(): Array<String> abstract fun wrappedPlayerList(): Array<String>
private var updateChecked: Boolean = false private var firstRun: Boolean = true
fun connect() { fun connect() {
MessageHandler.start(clear = true) MessageHandler.start(clear = true, firstRun = firstRun)
if (!updateChecked && cfg.update.enable) { if (firstRun && cfg.update.enable) {
Thread(UpdateChecker()).start() Thread(UpdateChecker()).start()
updateChecked = true
} }
firstRun = false
} }
fun disconnect() { fun disconnect() {

View File

@ -1,12 +1,16 @@
package matterlink package matterlink
import java.io.PrintWriter
import java.io.StringWriter
private const val ZWSP: Char = '\u200b' private const val ZWSP: Char = '\u200b'
//Inserts a zero-width space at index 1 in the string' //Inserts a zero-width space at index 1 in the string'
fun String.antiping(): String { val String.antiping: String
return this[0].toString() + ZWSP + this.substring(1) get() {
} return this[0].toString() + ZWSP + this.substring(1)
}
fun String.mapFormat(env: Map<String, String>): String { fun String.mapFormat(env: Map<String, String>): String {
var result = this var result = this
@ -17,3 +21,10 @@ fun String.mapFormat(env: Map<String, String>): String {
} }
return result return result
} }
val Exception.stackTraceString: String
get() {
val sw = StringWriter()
this.printStackTrace(PrintWriter(sw))
return sw.toString()
}

View File

@ -44,7 +44,7 @@ data class ApiMessage(
"{gateway}" to gateway, "{gateway}" to gateway,
"{channel}" to channel, "{channel}" to channel,
"{protocol}" to protocol, "{protocol}" to protocol,
"{username:antiping}" to username.antiping() "{username:antiping}" to username.antiping
) )
) )

View File

@ -2,11 +2,13 @@ package matterlink.bridge
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.instance import matterlink.instance
import matterlink.stackTraceString
import org.apache.http.client.methods.HttpGet import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpRequestBase import org.apache.http.client.methods.HttpRequestBase
import org.apache.http.impl.client.HttpClients import org.apache.http.impl.client.HttpClients
import java.io.InputStream import java.io.InputStream
import java.net.SocketException import java.net.SocketException
import java.net.UnknownHostException
import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.ConcurrentLinkedQueue
const val BUFFER_SIZE = 1000 const val BUFFER_SIZE = 1000
@ -20,12 +22,17 @@ fun HttpRequestBase.authorize() {
} }
class HttpStreamConnection(private val rcvQueue: ConcurrentLinkedQueue<ApiMessage>, class HttpStreamConnection(private val rcvQueue: ConcurrentLinkedQueue<ApiMessage>,
private val clear: Boolean = true private val clear: Boolean = true,
private val messageHandler: MessageHandler
) : Thread() { ) : Thread() {
var connected = false var connected = false
private set
var connecting = false var connecting = false
var enabled = true private set
var connectErrors = 0
var cancelled: Boolean = false
private set
init { init {
name = "MsgRcvThread" name = "MsgRcvThread"
@ -41,11 +48,12 @@ class HttpStreamConnection(private val rcvQueue: ConcurrentLinkedQueue<ApiMessag
connecting = false connecting = false
if (success) { if (success) {
instance.info("connected successfully") instance.info("connected successfully")
connectErrors = 0 messageHandler.connectErrors = 0
connected = true connected = true
} else { } else {
connectErrors++ messageHandler.connectErrors++
connected = false connected = false
instance.warn("connectErrors: ${messageHandler.connectErrors}")
} }
} }
@ -59,22 +67,31 @@ class HttpStreamConnection(private val rcvQueue: ConcurrentLinkedQueue<ApiMessag
authorize() authorize()
} }
private var cancelled: Boolean = false
override fun run() { override fun run() {
if (clear) {
val r = client.execute(clearGet)
r.entity.content.bufferedReader().forEachLine {
instance.debug("skipping $it")
}
}
try { try {
instance.info("Attemping to open Bridge Connection")
if (clear) {
val r = client.execute(clearGet)
r.entity.content.bufferedReader().forEachLine {
instance.debug("skipping $it")
}
}
val response = client.execute(get) val response = client.execute(get)
if (response.statusLine.statusCode != 200) { if (response.statusLine.statusCode != 200) {
instance.error("Bridge Connection rejected... status code ${response.statusLine.statusCode}") instance.error("Bridge Connection rejected... status code ${response.statusLine.statusCode}")
setSuccess(false) //TODO: pass message setSuccess(false) //TODO: pass message
onClose() onClose()
when (response.statusLine.statusCode) {
400 -> {
instance.warn("Missing token, please use /bridge reload after entering correct information")
messageHandler.enabled = false
}
401 -> {
instance.warn("Incorrect token, please use /bridge reload after entering correct information")
messageHandler.enabled = false
}
}
return return
} else { } else {
instance.debug("Bridge Connection accepted") instance.debug("Bridge Connection accepted")
@ -111,11 +128,15 @@ class HttpStreamConnection(private val rcvQueue: ConcurrentLinkedQueue<ApiMessag
content.close() content.close()
} catch (e: SocketException) { } catch (e: SocketException) {
instance.info("error {}", e) instance.error(e.stackTraceString)
if (!cancelled) { if (!cancelled) {
instance.error("Bridge Connection interrupted...") instance.error("Bridge Connection interrupted...")
setSuccess(false) setSuccess(false)
} }
} catch (e: UnknownHostException) {
instance.error(e.message ?: e.stackTraceString)
// instance.error(e.stackTraceString())
setSuccess(false)
} finally { } finally {
instance.debug("thread finished") instance.debug("thread finished")
onClose() onClose()
@ -124,23 +145,20 @@ class HttpStreamConnection(private val rcvQueue: ConcurrentLinkedQueue<ApiMessag
} }
fun open() { fun open() {
enabled = true if (!isAlive) {
if (!isAlive && cfg.connect.enable) {
connecting = true connecting = true
super.start() super.start()
// MessageHandler.transmit(ApiMessage(text="bridge connected", username="Server")) // MessageHandler.transmit(ApiMessage(text="bridge connected", username="Server"))
} }
if (isAlive) { if (isAlive) {
instance.info("Bridge Connection opened") instance.info("Bridge is connecting")
} }
} }
fun close() { fun close() {
instance.info("Closing bridge connection...") instance.info("Closing bridge connection...")
// MessageHandler.transmit(ApiMessage(text="bridge closing", username="Server")) // MessageHandler.transmit(ApiMessage(text="bridge closing", username="Server"))
try { try {
enabled = false
cancelled = true cancelled = true
get.abort() get.abort()
join() join()

View File

@ -11,6 +11,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
object MessageHandler { object MessageHandler {
private var sendErrors = 0 private var sendErrors = 0
var connectErrors = 0
private var streamConnection: HttpStreamConnection private var streamConnection: HttpStreamConnection
var rcvQueue = ConcurrentLinkedQueue<ApiMessage>() var rcvQueue = ConcurrentLinkedQueue<ApiMessage>()
private set private set
@ -23,11 +24,10 @@ object MessageHandler {
val connected get() = streamConnection.connected val connected get() = streamConnection.connected
private fun createThread(clear: Boolean = true): HttpStreamConnection { private fun createThread(clear: Boolean = true): HttpStreamConnection {
instance.info("Attempting to open bridge connection.")
instance.info("queue: $rcvQueue")
return HttpStreamConnection( return HttpStreamConnection(
rcvQueue, rcvQueue,
clear clear,
this
) )
} }
@ -38,12 +38,26 @@ object MessageHandler {
} }
} }
fun stop() = streamConnection.close() fun stop() {
enabled = false
streamConnection.close()
}
fun start(clear: Boolean = true) { var enabled: Boolean = false
if (!connected)
fun start(clear: Boolean = true, firstRun: Boolean = false) {
enabled = when {
firstRun -> cfg.connect.autoConnect
else -> true
}
if (!connected) {
streamConnection = createThread(clear) streamConnection = createThread(clear)
streamConnection.open() }
if (enabled) {
streamConnection.open()
}
} }
private fun transmitMessage(message: ApiMessage) { private fun transmitMessage(message: ApiMessage) {
@ -78,9 +92,9 @@ object MessageHandler {
} }
fun checkConnection(tick: Int) { fun checkConnection(tick: Int) {
if (streamConnection.enabled && tick % 20 == 0 && !streamConnection.connected && !streamConnection.connecting) { if (enabled && tick % 20 == 0 && !streamConnection.connected && !streamConnection.connecting) {
if (streamConnection.connectErrors > 5) { if (connectErrors > 5) {
instance.fatal("Caught too many errors, closing bridge") instance.fatal("Caught too many errors, closing bridge")
stop() stop()
return return

View File

@ -15,7 +15,7 @@ object PlayerListCommand : IBridgeCommand {
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg.relay.systemUser, username = cfg.relay.systemUser,
text = when { text = when {
playerList.isNotEmpty() -> "players: " + playerList.joinToString(" ") { it.antiping() } playerList.isNotEmpty() -> "players: " + playerList.joinToString(" ") { it.antiping }
else -> "No Players online" else -> "No Players online"
} }
)) ))

View File

@ -6,17 +6,19 @@ import matterlink.config.cfg
import matterlink.instance import matterlink.instance
object CommandCore { object CommandCore {
fun getName() : String { return "bridge" } fun getName() = "bridge"
fun getAliases() : List<String> { return listOf("BRIDGE","bridge") }
fun getUsage() : String { return "bridge <connect|disconnect|reload>"}
fun execute(args : Array<String>) : String { fun getAliases() = listOf("BRIDGE", "bridge")
fun getUsage() = "bridge <connect|disconnect|reload>"
fun execute(args: Array<String>): String {
val cmd = args[0].toLowerCase() val cmd = args[0].toLowerCase()
return when (cmd) { return when (cmd) {
"connect" -> { "connect" -> {
instance.connect() instance.connect()
"Bridge connected!" "Attempting bridge connection!"
} }
"disconnect" -> { "disconnect" -> {
instance.disconnect() instance.disconnect()

View File

@ -1,24 +1,21 @@
package matterlink.config package matterlink.config
import java.io.File
import java.util.regex.Pattern import java.util.regex.Pattern
lateinit var cfg: BaseConfig lateinit var cfg: BaseConfig
abstract class BaseConfig { abstract class BaseConfig(rootDir: File) {
companion object { companion object {
private val CATEGORY_RELAY_OPTIONS = "relay"
private val CATEGORY_FORMATTING_INCOMING = "formatting"
private val CATEGORY_JOIN_LEAVE = "join_leave"
private val CATEGORY_CONNECTION = "connection"
private val CATEGORY_COMMAND = "command"
private val CATEGORY_DEATH = "death"
private val CATEGORY_UPDATE = "update"
fun reload() { fun reload() {
cfg = cfg.load() cfg = cfg.load()
} }
} }
val cfgDirectory: File = rootDir.resolve("matterlink")
val mainCfgFile: File = cfgDirectory.resolve("matterlink.cfg")
var relay = RelayOptions() var relay = RelayOptions()
var connect = ConnectOptions() var connect = ConnectOptions()
var formatting = FormattingOptions() var formatting = FormattingOptions()
@ -50,14 +47,16 @@ abstract class BaseConfig {
var url: String = "http://localhost:4242", var url: String = "http://localhost:4242",
var authToken: String = "", var authToken: String = "",
var gateway: String = "minecraft", var gateway: String = "minecraft",
var enable: Boolean = true var autoConnect: Boolean = true
) )
data class CommandOptions( data class CommandOptions(
var prefix: String = "$", var prefix: String = "$",
var enable: Boolean = true, var enable: Boolean = true,
var commandMapping: Map<String, String> = mapOf( var commandMapping: Map<String, String> = mapOf(
"tps" to "forge tps" "tps" to "forge tps",
"list" to "list",
"seed" to "seed"
) )
) )
@ -97,10 +96,7 @@ abstract class BaseConfig {
"thrown" to "彡°", "thrown" to "彡°",
"thorns" to "\uD83C\uDF39", //🌹 "thorns" to "\uD83C\uDF39", //🌹
"explosion" to "\uD83D\uDCA3 \uD83D\uDCA5", //💣 💥 "explosion" to "\uD83D\uDCA3 \uD83D\uDCA5", //💣 💥
"explosion.player" to "\uD83D\uDCA3 \uD83D\uDCA5", //💣 💥 "explosion.player" to "\uD83D\uDCA3 \uD83D\uDCA5" //💣 💥
"electrocut" to "",
"radiation" to ""
) )
) )
@ -112,8 +108,8 @@ abstract class BaseConfig {
addCustomCategoryComment: (key: String, comment: String) -> Unit, addCustomCategoryComment: (key: String, comment: String) -> Unit,
getStringList: (name: String, category: String, defaultValues: Array<String>, comment: String) -> Array<String> getStringList: (name: String, category: String, defaultValues: Array<String>, comment: String) -> Array<String>
) { ) {
var category = CATEGORY_RELAY_OPTIONS var category = "relay"
addCustomCategoryComment(CATEGORY_RELAY_OPTIONS, "Relay options") addCustomCategoryComment(category, "Relay options")
relay = RelayOptions( relay = RelayOptions(
systemUser = getString( systemUser = getString(
"systemUser", "systemUser",
@ -136,8 +132,8 @@ abstract class BaseConfig {
) )
) )
category = CATEGORY_COMMAND category = "commands"
addCustomCategoryComment(CATEGORY_COMMAND, "User commands") addCustomCategoryComment(category, "User commands")
command = CommandOptions( command = CommandOptions(
enable = getBoolean( enable = getBoolean(
"enable", "enable",
@ -168,8 +164,8 @@ abstract class BaseConfig {
} }
) )
category = CATEGORY_FORMATTING_INCOMING category = "formatting"
addCustomCategoryComment(CATEGORY_FORMATTING_INCOMING, "Gateway -> Server" + addCustomCategoryComment(category, "Gateway -> Server" +
"Formatting options: " + "Formatting options: " +
"Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}") "Available variables: {username}, {text}, {gateway}, {channel}, {protocol}, {username:antiping}")
formatting = FormattingOptions( formatting = FormattingOptions(
@ -193,8 +189,8 @@ abstract class BaseConfig {
) )
) )
category = CATEGORY_JOIN_LEAVE category = "join_leave"
addCustomCategoryComment(CATEGORY_JOIN_LEAVE, "Server -> Gateway" + addCustomCategoryComment(category, "Server -> Gateway" +
"Formatting options: " + "Formatting options: " +
"Available variables: {username}, {username:antiping}") "Available variables: {username}, {username:antiping}")
joinLeave = FormattingJoinLeave( joinLeave = FormattingJoinLeave(
@ -226,8 +222,8 @@ abstract class BaseConfig {
) )
) )
category = CATEGORY_CONNECTION category = "connection"
addCustomCategoryComment(CATEGORY_CONNECTION, "Connection settings") addCustomCategoryComment(category, "Connection settings")
connect = ConnectOptions( connect = ConnectOptions(
url = getString( url = getString(
"connectURL", "connectURL",
@ -247,14 +243,14 @@ abstract class BaseConfig {
connect.gateway, connect.gateway,
"MatterBridge gateway" "MatterBridge gateway"
), ),
enable = getBoolean( autoConnect = getBoolean(
"enable", "autoConnect",
category, category,
connect.enable, connect.autoConnect,
"Enable the relay, it will not work if it is not enabled" "Connect the relay on startup"
) )
) )
category = CATEGORY_DEATH category = "death"
addCustomCategoryComment(category, "Death message settings") addCustomCategoryComment(category, "Death message settings")
death = DeathOptions( death = DeathOptions(
showDeath = getBoolean( showDeath = getBoolean(
@ -287,7 +283,7 @@ abstract class BaseConfig {
) )
category = CATEGORY_UPDATE category = "update"
addCustomCategoryComment(category, "Update Settings") addCustomCategoryComment(category, "Update Settings")
update = UpdateOptions( update = UpdateOptions(
enable = getBoolean( enable = getBoolean(

View File

@ -15,7 +15,7 @@ object DeathHandler {
damageType: String damageType: String
) { ) {
if (cfg.death.showDeath) { if (cfg.death.showDeath) {
var msg = deathMessage.replace(player, player.antiping()) var msg = deathMessage.replace(player, player.antiping)
if(cfg.death.showDamageType) { if(cfg.death.showDamageType) {
val emojis = cfg.death.damageTypeMapping[damageType]?.split(' ') ?: listOf("\uD83D\uDC7B unknown type '$damageType'") val emojis = cfg.death.damageTypeMapping[damageType]?.split(' ') ?: listOf("\uD83D\uDC7B unknown type '$damageType'")
val damageEmoji = emojis[random.nextInt(emojis.size)] val damageEmoji = emojis[random.nextInt(emojis.size)]

View File

@ -13,7 +13,7 @@ object JoinLeaveHandler {
val msg = cfg.joinLeave.joinServer.mapFormat( val msg = cfg.joinLeave.joinServer.mapFormat(
mapOf( mapOf(
"{username}" to player, "{username}" to player,
"{username:antiping}" to player.antiping() "{username:antiping}" to player.antiping
) )
) )
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
@ -29,7 +29,7 @@ object JoinLeaveHandler {
val msg = cfg.joinLeave.leaveServer.mapFormat( val msg = cfg.joinLeave.leaveServer.mapFormat(
mapOf( mapOf(
"{username}" to player, "{username}" to player,
"{username:antiping}" to player.antiping() "{username:antiping}" to player.antiping
) )
) )
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(

View File

@ -9,7 +9,7 @@ object ProgressHandler {
fun handleProgress(name: String, message: String, display: String) { fun handleProgress(name: String, message: String, display: String) {
if (!cfg.relay.advancements) return if (!cfg.relay.advancements) return
val usr = name.antiping() val usr = name.antiping
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg.relay.systemUser, username = cfg.relay.systemUser,
text = "$usr $message $display" text = "$usr $message $display"