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 {
|
|
||||||
const val ZWSP: Char = '\u200b'
|
|
||||||
|
|
||||||
//Inserts a zero-width space at index 1 in the string'
|
object Util {
|
||||||
@JvmStatic
|
const val ZWSP: Char = '\u200b'
|
||||||
fun antiping(str: String): String {
|
|
||||||
return str[0].toString()+ZWSP+str.substring(1)
|
//Inserts a zero-width space at index 1 in the string'
|
||||||
}
|
|
||||||
|
fun String.antiping(): String {
|
||||||
|
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,8 +42,10 @@ object MessageHandler {
|
||||||
var rcvQueue = ConcurrentLinkedQueue<ApiMessage>()
|
var rcvQueue = ConcurrentLinkedQueue<ApiMessage>()
|
||||||
|
|
||||||
fun transmit(msg: ApiMessage) {
|
fun transmit(msg: ApiMessage) {
|
||||||
CivilEngineering.logger.debug("Transmitting: " + msg)
|
if (connected) {
|
||||||
transmitMessage(msg)
|
CivilEngineering.logger.debug("Transmitting: " + msg)
|
||||||
|
transmitMessage(msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
|
@ -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,24 +64,34 @@ 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) {
|
||||||
//open a connection
|
try {
|
||||||
val client = HttpClients.createDefault()
|
//open a connection
|
||||||
val post = HttpPost(cfg!!.connect.url + "/api/message")
|
val client = HttpClients.createDefault()
|
||||||
|
val post = HttpPost(cfg!!.connect.url + "/api/message")
|
||||||
|
|
||||||
post.entity = StringEntity(message.encode(), ContentType.APPLICATION_JSON)
|
post.entity = StringEntity(message.encode(), ContentType.APPLICATION_JSON)
|
||||||
post.authorize()
|
post.authorize()
|
||||||
|
|
||||||
val response = client.execute(post)
|
val response = client.execute(post)
|
||||||
val code = response.statusLine.statusCode
|
val code = response.statusLine.statusCode
|
||||||
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