try to: fix transmit crash and polish noping
This commit is contained in:
parent
f05de5188c
commit
c4754dd8c7
|
@ -1,13 +1,13 @@
|
||||||
package civilengineering
|
package civilengineering
|
||||||
|
|
||||||
class Util {
|
|
||||||
companion object {
|
object Util {
|
||||||
const val ZWSP: Char = '\u200b'
|
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'
|
||||||
@JvmStatic
|
|
||||||
fun antiping(str: String): String {
|
fun String.antiping(): String {
|
||||||
return str[0].toString()+ZWSP+str.substring(1)
|
return this[0].toString()+ZWSP+this.substring(1)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -13,7 +13,8 @@ import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
|
||||||
|
|
||||||
object MessageHandler {
|
object MessageHandler {
|
||||||
|
private var connected = false
|
||||||
|
private var sendErrors = 0
|
||||||
fun HttpRequestBase.authorize() {
|
fun HttpRequestBase.authorize() {
|
||||||
if (cfg!!.connect.authToken.isNotEmpty() && getHeaders("Authorization").isEmpty())
|
if (cfg!!.connect.authToken.isNotEmpty() && getHeaders("Authorization").isEmpty())
|
||||||
setHeader("Authorization", "Bearer " + cfg!!.connect.authToken)
|
setHeader("Authorization", "Bearer " + cfg!!.connect.authToken)
|
||||||
|
@ -41,9 +42,11 @@ object MessageHandler {
|
||||||
var rcvQueue = ConcurrentLinkedQueue<ApiMessage>()
|
var rcvQueue = ConcurrentLinkedQueue<ApiMessage>()
|
||||||
|
|
||||||
fun transmit(msg: ApiMessage) {
|
fun transmit(msg: ApiMessage) {
|
||||||
|
if (connected) {
|
||||||
CivilEngineering.logger.debug("Transmitting: " + msg)
|
CivilEngineering.logger.debug("Transmitting: " + msg)
|
||||||
transmitMessage(msg)
|
transmitMessage(msg)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
CivilEngineering.logger.info("Closing bridge connection...")
|
CivilEngineering.logger.info("Closing bridge connection...")
|
||||||
|
@ -51,6 +54,7 @@ object MessageHandler {
|
||||||
streamConnection.close()
|
streamConnection.close()
|
||||||
|
|
||||||
CivilEngineering.logger.info("Bridge connection closed!")
|
CivilEngineering.logger.info("Bridge connection closed!")
|
||||||
|
connected = false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun start(): Boolean {
|
fun start(): Boolean {
|
||||||
|
@ -60,13 +64,14 @@ object MessageHandler {
|
||||||
if (!streamConnection.isAlive) {
|
if (!streamConnection.isAlive) {
|
||||||
streamConnection.start()
|
streamConnection.start()
|
||||||
// MessageHandler.transmit(ApiMessage(text="bridge connected", username="Server"))
|
// 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) {
|
private fun transmitMessage(message: ApiMessage) {
|
||||||
|
try {
|
||||||
//open a connection
|
//open a connection
|
||||||
val client = HttpClients.createDefault()
|
val client = HttpClients.createDefault()
|
||||||
val post = HttpPost(cfg!!.connect.url + "/api/message")
|
val post = HttpPost(cfg!!.connect.url + "/api/message")
|
||||||
|
@ -79,6 +84,15 @@ object MessageHandler {
|
||||||
if (code != 200) {
|
if (code != 200) {
|
||||||
CivilEngineering.logger.error("Server returned $code for $post")
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package civilengineering.eventhandlers
|
package civilengineering.eventhandlers
|
||||||
|
|
||||||
|
import civilengineering.Util.antiping
|
||||||
import civilengineering.CivilEngineering
|
import civilengineering.CivilEngineering
|
||||||
import civilengineering.CivilEngineeringConfig
|
import civilengineering.CivilEngineeringConfig
|
||||||
import civilengineering.Util
|
import civilengineering.Util
|
||||||
|
@ -13,10 +14,11 @@ class AdvancementEventHandler {
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
fun handleAdvancements(event: AdvancementEvent) {
|
fun handleAdvancements(event: AdvancementEvent) {
|
||||||
if(cfg!!.relay.advancements && event.advancement.display != null) {
|
if(cfg!!.relay.advancements && event.advancement.display != null) {
|
||||||
val zwsp: Char = '\u200B'
|
val player = event.entityPlayer.name.antiping()
|
||||||
var player: String = Util.antiping(event.entityPlayer.name)
|
val content = event.advancement.displayText.unformattedText
|
||||||
|
MessageHandler.transmit(ApiMessage(username = "Server",
|
||||||
MessageHandler.transmit(ApiMessage("Server",player+" has earned the advancement "+event.advancement.displayText.unformattedText))
|
text = "$player has earned the advancement $content"
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -31,7 +31,10 @@ class CommandEventHandler {
|
||||||
}
|
}
|
||||||
message = message.trim { it <= ' ' }
|
message = message.trim { it <= ' ' }
|
||||||
|
|
||||||
MessageHandler.transmit(ApiMessage(event.sender.name,message))
|
MessageHandler.transmit(ApiMessage(
|
||||||
|
username = event.sender.name,
|
||||||
|
text = message
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package civilengineering.eventhandlers
|
package civilengineering.eventhandlers
|
||||||
|
|
||||||
import civilengineering.Util
|
import civilengineering.Util.antiping
|
||||||
import civilengineering.bridge.ApiMessage
|
import civilengineering.bridge.ApiMessage
|
||||||
import civilengineering.bridge.MessageHandler
|
import civilengineering.bridge.MessageHandler
|
||||||
import civilengineering.cfg
|
import civilengineering.cfg
|
||||||
|
@ -15,7 +15,7 @@ class DeathEventHandler {
|
||||||
val entity = event.entityLiving
|
val entity = event.entityLiving
|
||||||
if (entity is EntityPlayer) {
|
if (entity is EntityPlayer) {
|
||||||
var message: String = entity.getCombatTracker().deathMessage.unformattedText
|
var message: String = entity.getCombatTracker().deathMessage.unformattedText
|
||||||
MessageHandler.transmit(ApiMessage(username = "Server", text = Util.antiping(message)))
|
MessageHandler.transmit(ApiMessage(username = "Server", text = message.antiping()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue