diff --git a/src/main/kotlin/civilengineering/Util.kt b/src/main/kotlin/civilengineering/Util.kt index 8a07453..ec98f07 100644 --- a/src/main/kotlin/civilengineering/Util.kt +++ b/src/main/kotlin/civilengineering/Util.kt @@ -1,13 +1,13 @@ package civilengineering -class Util { - companion object { - const val ZWSP: Char = '\u200b' - //Inserts a zero-width space at index 1 in the string' - @JvmStatic - fun antiping(str: String): String { - return str[0].toString()+ZWSP+str.substring(1) - } +object Util { + const val ZWSP: Char = '\u200b' + + //Inserts a zero-width space at index 1 in the string' + + fun String.antiping(): String { + return this[0].toString()+ZWSP+this.substring(1) } + } \ No newline at end of file diff --git a/src/main/kotlin/civilengineering/bridge/MessageHandler.kt b/src/main/kotlin/civilengineering/bridge/MessageHandler.kt index 62ef6bb..e223539 100644 --- a/src/main/kotlin/civilengineering/bridge/MessageHandler.kt +++ b/src/main/kotlin/civilengineering/bridge/MessageHandler.kt @@ -13,7 +13,8 @@ import java.util.concurrent.ConcurrentLinkedQueue object MessageHandler { - + private var connected = false + private var sendErrors = 0 fun HttpRequestBase.authorize() { if (cfg!!.connect.authToken.isNotEmpty() && getHeaders("Authorization").isEmpty()) setHeader("Authorization", "Bearer " + cfg!!.connect.authToken) @@ -41,8 +42,10 @@ object MessageHandler { var rcvQueue = ConcurrentLinkedQueue() fun transmit(msg: ApiMessage) { - CivilEngineering.logger.debug("Transmitting: " + msg) - transmitMessage(msg) + if (connected) { + CivilEngineering.logger.debug("Transmitting: " + msg) + transmitMessage(msg) + } } fun stop() { @@ -51,6 +54,7 @@ object MessageHandler { streamConnection.close() CivilEngineering.logger.info("Bridge connection closed!") + connected = false } fun start(): Boolean { @@ -60,24 +64,34 @@ object MessageHandler { if (!streamConnection.isAlive) { streamConnection.start() // MessageHandler.transmit(ApiMessage(text="bridge connected", username="Server")) - return true + connected = true + return connected } - return false + return connected } - @Throws(IOException::class) private fun transmitMessage(message: ApiMessage) { - //open a connection - val client = HttpClients.createDefault() - val post = HttpPost(cfg!!.connect.url + "/api/message") + try { + //open a connection + val client = HttpClients.createDefault() + val post = HttpPost(cfg!!.connect.url + "/api/message") - post.entity = StringEntity(message.encode(), ContentType.APPLICATION_JSON) - post.authorize() + post.entity = StringEntity(message.encode(), ContentType.APPLICATION_JSON) + post.authorize() - val response = client.execute(post) - val code = response.statusLine.statusCode - if (code != 200) { - CivilEngineering.logger.error("Server returned $code for $post") + val response = client.execute(post) + val code = response.statusLine.statusCode + if (code != 200) { + CivilEngineering.logger.error("Server returned $code for $post") + } + sendErrors = 0 + } catch (e: IOException) { + CivilEngineering.logger.error("sending message caused $e") + sendErrors++ + if(sendErrors > 5) { + CivilEngineering.logger.error("caught too many errors, closing bridge") + stop() + } } } } diff --git a/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt b/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt index 787c949..548f0d2 100644 --- a/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt +++ b/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt @@ -1,5 +1,6 @@ package civilengineering.eventhandlers +import civilengineering.Util.antiping import civilengineering.CivilEngineering import civilengineering.CivilEngineeringConfig import civilengineering.Util @@ -13,10 +14,11 @@ class AdvancementEventHandler { @SubscribeEvent fun handleAdvancements(event: AdvancementEvent) { if(cfg!!.relay.advancements && event.advancement.display != null) { - val zwsp: Char = '\u200B' - var player: String = Util.antiping(event.entityPlayer.name) - - MessageHandler.transmit(ApiMessage("Server",player+" has earned the advancement "+event.advancement.displayText.unformattedText)) + val player = event.entityPlayer.name.antiping() + val content = event.advancement.displayText.unformattedText + MessageHandler.transmit(ApiMessage(username = "Server", + text = "$player has earned the advancement $content" + )) } } } \ No newline at end of file diff --git a/src/main/kotlin/civilengineering/eventhandlers/CommandEventHandler.kt b/src/main/kotlin/civilengineering/eventhandlers/CommandEventHandler.kt index 8ea2a4f..914fea1 100644 --- a/src/main/kotlin/civilengineering/eventhandlers/CommandEventHandler.kt +++ b/src/main/kotlin/civilengineering/eventhandlers/CommandEventHandler.kt @@ -31,7 +31,10 @@ class CommandEventHandler { } message = message.trim { it <= ' ' } - MessageHandler.transmit(ApiMessage(event.sender.name,message)) + MessageHandler.transmit(ApiMessage( + username = event.sender.name, + text = message + )) } } } diff --git a/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt b/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt index cea641e..e08cb4a 100644 --- a/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt +++ b/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt @@ -1,6 +1,6 @@ package civilengineering.eventhandlers -import civilengineering.Util +import civilengineering.Util.antiping import civilengineering.bridge.ApiMessage import civilengineering.bridge.MessageHandler import civilengineering.cfg @@ -15,7 +15,7 @@ class DeathEventHandler { val entity = event.entityLiving if (entity is EntityPlayer) { var message: String = entity.getCombatTracker().deathMessage.unformattedText - MessageHandler.transmit(ApiMessage(username = "Server", text = Util.antiping(message))) + MessageHandler.transmit(ApiMessage(username = "Server", text = message.antiping())) } } }