From f05de5188c618bb5eb1181ce5b9d554d4646e712 Mon Sep 17 00:00:00 2001 From: DaMachinator Date: Tue, 23 Jan 2018 11:16:17 -0500 Subject: [PATCH] Abstract antiping into its own function --- src/main/kotlin/civilengineering/Util.kt | 13 +++++++++++++ .../eventhandlers/AdvancementEventHandler.kt | 6 +++--- .../eventhandlers/DeathEventHandler.kt | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/civilengineering/Util.kt diff --git a/src/main/kotlin/civilengineering/Util.kt b/src/main/kotlin/civilengineering/Util.kt new file mode 100644 index 0000000..8a07453 --- /dev/null +++ b/src/main/kotlin/civilengineering/Util.kt @@ -0,0 +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) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt b/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt index 4fdbbf7..787c949 100644 --- a/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt +++ b/src/main/kotlin/civilengineering/eventhandlers/AdvancementEventHandler.kt @@ -2,6 +2,7 @@ package civilengineering.eventhandlers import civilengineering.CivilEngineering import civilengineering.CivilEngineeringConfig +import civilengineering.Util import civilengineering.bridge.ApiMessage import civilengineering.bridge.MessageHandler import civilengineering.cfg @@ -13,9 +14,8 @@ class AdvancementEventHandler { fun handleAdvancements(event: AdvancementEvent) { if(cfg!!.relay.advancements && event.advancement.display != null) { val zwsp: Char = '\u200B' - var player: String = event.entityPlayer.name - player = player[0].toString()+zwsp+player.substring(1) //antiping - //toString is necessary, otherwise the JVM thinks we're trying to do integer addition + var player: String = Util.antiping(event.entityPlayer.name) + MessageHandler.transmit(ApiMessage("Server",player+" has earned the advancement "+event.advancement.displayText.unformattedText)) } } diff --git a/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt b/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt index 415bc0e..cea641e 100644 --- a/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt +++ b/src/main/kotlin/civilengineering/eventhandlers/DeathEventHandler.kt @@ -1,5 +1,6 @@ package civilengineering.eventhandlers +import civilengineering.Util import civilengineering.bridge.ApiMessage import civilengineering.bridge.MessageHandler import civilengineering.cfg @@ -14,8 +15,7 @@ class DeathEventHandler { val entity = event.entityLiving if (entity is EntityPlayer) { var message: String = entity.getCombatTracker().deathMessage.unformattedText - message = message[0].toString() + '\u200b' + message.substring(1) //antiping - MessageHandler.transmit(ApiMessage(username = "Server", text = message)) + MessageHandler.transmit(ApiMessage(username = "Server", text = Util.antiping(message))) } } }