From cac5c59f759aa85ffc7deaba6255bafeed900fd9 Mon Sep 17 00:00:00 2001 From: DaMachinator Date: Fri, 9 Feb 2018 13:06:53 -0500 Subject: [PATCH] Wrap forge event handlers --- src/main/kotlin/matterlink/EventWrapper.kt | 100 ++++++++++++++++++ src/main/kotlin/matterlink/MatterLink.kt | 17 ++- .../kotlin/matterlink/bridge/ApiMessage.kt | 5 + ...rverChatHelper.kt => ServerChatHandler.kt} | 10 +- .../eventhandlers/AdvancementEventHandler.kt | 22 ---- .../eventhandlers/ChatMessageHandler.kt | 18 ---- .../eventhandlers/CommandEventHandler.kt | 40 ------- .../eventhandlers/DeathEventHandler.kt | 26 ----- .../kotlin/matterlink/handlers/ChatHandler.kt | 15 +++ .../matterlink/handlers/CommandHandler.kt | 14 +++ .../matterlink/handlers/DeathHandler.kt | 21 ++++ .../JoinLeaveHandler.kt | 23 ++-- .../matterlink/handlers/ProgressHandler.kt | 18 ++++ 13 files changed, 197 insertions(+), 132 deletions(-) create mode 100644 src/main/kotlin/matterlink/EventWrapper.kt rename src/main/kotlin/matterlink/bridge/{ServerChatHelper.kt => ServerChatHandler.kt} (90%) delete mode 100644 src/main/kotlin/matterlink/eventhandlers/AdvancementEventHandler.kt delete mode 100644 src/main/kotlin/matterlink/eventhandlers/ChatMessageHandler.kt delete mode 100644 src/main/kotlin/matterlink/eventhandlers/CommandEventHandler.kt delete mode 100644 src/main/kotlin/matterlink/eventhandlers/DeathEventHandler.kt create mode 100644 src/main/kotlin/matterlink/handlers/ChatHandler.kt create mode 100644 src/main/kotlin/matterlink/handlers/CommandHandler.kt create mode 100644 src/main/kotlin/matterlink/handlers/DeathHandler.kt rename src/main/kotlin/matterlink/{eventhandlers => handlers}/JoinLeaveHandler.kt (51%) create mode 100644 src/main/kotlin/matterlink/handlers/ProgressHandler.kt diff --git a/src/main/kotlin/matterlink/EventWrapper.kt b/src/main/kotlin/matterlink/EventWrapper.kt new file mode 100644 index 0000000..ca38cbf --- /dev/null +++ b/src/main/kotlin/matterlink/EventWrapper.kt @@ -0,0 +1,100 @@ +package matterlink + +import matterlink.bridge.ServerChatHandler +import matterlink.bridge.USER_ACTION +import matterlink.handlers.* +import net.minecraft.command.server.CommandBroadcast +import net.minecraft.command.server.CommandEmote +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.server.dedicated.DedicatedServer +import net.minecraft.tileentity.TileEntityCommandBlock +import net.minecraftforge.event.CommandEvent +import net.minecraftforge.event.ServerChatEvent +import net.minecraftforge.event.entity.living.LivingDeathEvent +import net.minecraftforge.event.entity.player.AdvancementEvent +import net.minecraftforge.fml.common.Mod +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.minecraftforge.fml.common.gameevent.PlayerEvent +import net.minecraftforge.fml.common.gameevent.TickEvent + +//FORGE-DEPENDENT +@Mod.EventBusSubscriber +class EventWrapper { + companion object JavaIsDumb { + + //MC-VERSION & FORGE DEPENDENT + @SubscribeEvent + @JvmStatic + fun progressEvent(e: AdvancementEvent) { + if (e.advancement.display == null) return + val name = e.entityPlayer.name + val text = "has made the advancement ${e.advancement.displayText.unformattedText}" + ProgressHandler.handleProgress(name, text) + } + + //FORGE-DEPENDENT + @SubscribeEvent + @JvmStatic + fun chatEvent(e: ServerChatEvent) { + val user = e.username + val msg = e.message + ChatHandler.handleChat(user, msg) + } + + //FORGE-DEPENDENT + @SubscribeEvent + @JvmStatic + fun commandEvent(e: CommandEvent) { + val sender = + when { + e.sender is EntityPlayer -> e.sender.name + e.sender is DedicatedServer -> cfg!!.relay.systemUser + e.sender is TileEntityCommandBlock -> "CommandBlock" + else -> return + } + + if (e.command is CommandEmote || e.command is CommandBroadcast) { + val args = e.parameters.joinToString(" ") + val type = + when { + e.command is CommandEmote -> USER_ACTION + else -> "" + } + CommandHandler.handleCommand(sender, args, type) + } + } + + //FORGE-DEPENDENT + @SubscribeEvent + @JvmStatic + fun deathEvent(e: LivingDeathEvent) { + if (e.entityLiving is EntityPlayer) { + DeathHandler.handleDeath( + e.entityLiving.name, + e.entityLiving.combatTracker.deathMessage.unformattedText + ) + } + } + + //FORGE-DEPENDENT + @SubscribeEvent + @JvmStatic + fun joinEvent(e: PlayerEvent.PlayerLoggedInEvent) { + JoinLeaveHandler.handleJoin(e.player.name) + } + + //FORGE-DEPENDENT + @SubscribeEvent + @JvmStatic + fun leaveEvent(e: PlayerEvent.PlayerLoggedOutEvent) { + JoinLeaveHandler.handleLeave(e.player.name) + } + + //FORGE-DEPENDENT + @SubscribeEvent + @JvmStatic + fun serverTickEvent(e: TickEvent.ServerTickEvent) { + ServerChatHandler.writeIncomingToChat() + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/MatterLink.kt b/src/main/kotlin/matterlink/MatterLink.kt index 4dcd7da..7e82efb 100644 --- a/src/main/kotlin/matterlink/MatterLink.kt +++ b/src/main/kotlin/matterlink/MatterLink.kt @@ -1,13 +1,14 @@ package matterlink import matterlink.bridge.MessageHandler -import matterlink.bridge.ServerChatHelper import matterlink.bridge.command.BridgeCommandRegistry import matterlink.bridge.command.HelpCommand import matterlink.bridge.command.PlayerListCommand import matterlink.command.CommandMatterlink -import matterlink.eventhandlers.* +import matterlink.handlers.* +import net.minecraft.util.text.TextComponentString import net.minecraftforge.common.MinecraftForge +import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.event.* import org.apache.logging.log4j.Level @@ -60,14 +61,6 @@ object MatterLink { logger.debug("Registering server commands") event.registerServerCommand(CommandMatterlink()) MessageHandler.start() - - //maybe try registering them manually - MinecraftForge.EVENT_BUS.register(ServerChatHelper()) - MinecraftForge.EVENT_BUS.register(ChatMessageHandler()) - MinecraftForge.EVENT_BUS.register(DeathEventHandler()) - MinecraftForge.EVENT_BUS.register(CommandEventHandler()) - MinecraftForge.EVENT_BUS.register(AdvancementEventHandler()) - MinecraftForge.EVENT_BUS.register(JoinLeaveHandler()) } @Mod.EventHandler @@ -75,5 +68,9 @@ object MatterLink { MessageHandler.stop() } + //FORGE-DEPENDENT + fun writeToServerChat(msg: String) { + FMLCommonHandler.instance().minecraftServerInstance.playerList.sendMessage(TextComponentString(msg)) + } } diff --git a/src/main/kotlin/matterlink/bridge/ApiMessage.kt b/src/main/kotlin/matterlink/bridge/ApiMessage.kt index d335add..bf95d79 100644 --- a/src/main/kotlin/matterlink/bridge/ApiMessage.kt +++ b/src/main/kotlin/matterlink/bridge/ApiMessage.kt @@ -4,6 +4,9 @@ import matterlink.cfg import matterlink.antiping import com.google.gson.Gson +const val USER_ACTION: String = "user_action" +const val JOIN_LEAVE: String = "join_leave" + data class ApiMessage( val username: String = cfg!!.relay.systemUser, val text: String = "", @@ -18,6 +21,8 @@ data class ApiMessage( val id: String = "" // val Extra: Any? = null ) { + + companion object { val gson = Gson() diff --git a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt b/src/main/kotlin/matterlink/bridge/ServerChatHandler.kt similarity index 90% rename from src/main/kotlin/matterlink/bridge/ServerChatHelper.kt rename to src/main/kotlin/matterlink/bridge/ServerChatHandler.kt index 395cb00..f964254 100644 --- a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt +++ b/src/main/kotlin/matterlink/bridge/ServerChatHandler.kt @@ -8,9 +8,11 @@ import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.gameevent.TickEvent -class ServerChatHelper { - @SubscribeEvent - fun onServerUpdate(event: TickEvent.ServerTickEvent) { +object ServerChatHandler { + /** + * This method must be called every server tick with no arguments. + */ + fun writeIncomingToChat() { if (MessageHandler.rcvQueue.isNotEmpty()) MatterLink.logger.debug("incoming: " + MessageHandler.rcvQueue.toString()) val nextMessage = MessageHandler.rcvQueue.poll() @@ -29,7 +31,7 @@ class ServerChatHelper { val user = nextMessage.username val text = nextMessage.text val json = nextMessage.encode() - MatterLink.logger.debug("Threw out message with unhandled event: $event") + MatterLink.logger.debug("Threw out message with unhandled event: ${nextMessage.event}") MatterLink.logger.debug(" Message contents:") MatterLink.logger.debug(" User: $user") MatterLink.logger.debug(" Text: $text") diff --git a/src/main/kotlin/matterlink/eventhandlers/AdvancementEventHandler.kt b/src/main/kotlin/matterlink/eventhandlers/AdvancementEventHandler.kt deleted file mode 100644 index f24480e..0000000 --- a/src/main/kotlin/matterlink/eventhandlers/AdvancementEventHandler.kt +++ /dev/null @@ -1,22 +0,0 @@ -package matterlink.eventhandlers - -import matterlink.bridge.ApiMessage -import matterlink.bridge.MessageHandler -import matterlink.cfg -import matterlink.antiping -import net.minecraftforge.event.entity.player.AdvancementEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class AdvancementEventHandler { - @SubscribeEvent - fun handleAdvancements(event: AdvancementEvent) { - if (cfg!!.relay.advancements && event.advancement.display != null) { - val player = event.entityPlayer.name.antiping() - val content = event.advancement.displayText.unformattedText - MessageHandler.transmit(ApiMessage( - username = cfg!!.relay.systemUser, - text = "$player has earned the advancement $content" - )) - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/eventhandlers/ChatMessageHandler.kt b/src/main/kotlin/matterlink/eventhandlers/ChatMessageHandler.kt deleted file mode 100644 index d84d9bf..0000000 --- a/src/main/kotlin/matterlink/eventhandlers/ChatMessageHandler.kt +++ /dev/null @@ -1,18 +0,0 @@ -package matterlink.eventhandlers - -import matterlink.bridge.ApiMessage -import matterlink.bridge.MessageHandler -import net.minecraftforge.event.ServerChatEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class ChatMessageHandler { - @SubscribeEvent - fun handleServerChatEvent(event: ServerChatEvent) { - val message = event.message.trim() - if (message.isNotBlank()) - MessageHandler.transmit(ApiMessage( - username = event.username, - text = message) - ) - } -} diff --git a/src/main/kotlin/matterlink/eventhandlers/CommandEventHandler.kt b/src/main/kotlin/matterlink/eventhandlers/CommandEventHandler.kt deleted file mode 100644 index ac1b2b9..0000000 --- a/src/main/kotlin/matterlink/eventhandlers/CommandEventHandler.kt +++ /dev/null @@ -1,40 +0,0 @@ -package matterlink.eventhandlers - -import matterlink.bridge.ApiMessage -import matterlink.bridge.MessageHandler -import net.minecraft.command.server.CommandBroadcast -import net.minecraft.command.server.CommandEmote -import net.minecraft.entity.player.EntityPlayer -import net.minecraftforge.event.CommandEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class CommandEventHandler { - @SubscribeEvent - fun handleCommandEvent(event: CommandEvent) { - if (event.command is CommandEmote && event.sender is EntityPlayer) { - val args = event.parameters - - val user = event.sender.name - var message = "" - - for (word in args) { - message = message + " " + word - } - message = message.trim { it <= ' ' } - - MessageHandler.transmit(ApiMessage(username=user, text=message, event="user_action")) - } else if(event.command is CommandBroadcast) { - var message = "" - for (word in event.parameters) { - message = message + " " + word - } - message = message.trim { it <= ' ' } - val name: String = if(event.sender.name.equals("@")) "CommandBlock" else event.sender.name - - MessageHandler.transmit(ApiMessage( - username = name, - text = message - )) - } - } -} diff --git a/src/main/kotlin/matterlink/eventhandlers/DeathEventHandler.kt b/src/main/kotlin/matterlink/eventhandlers/DeathEventHandler.kt deleted file mode 100644 index 2497977..0000000 --- a/src/main/kotlin/matterlink/eventhandlers/DeathEventHandler.kt +++ /dev/null @@ -1,26 +0,0 @@ -package matterlink.eventhandlers - -import matterlink.bridge.ApiMessage -import matterlink.bridge.MessageHandler -import matterlink.cfg -import matterlink.antiping -import net.minecraft.entity.player.EntityPlayer -import net.minecraftforge.event.entity.living.LivingDeathEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class DeathEventHandler { - @SubscribeEvent - fun handleLivingDeathEvent(event: LivingDeathEvent) { - if (cfg!!.relay.deathEvents) { - val entity = event.entityLiving - if (entity is EntityPlayer) { - val message = entity.getCombatTracker().deathMessage.unformattedText - .replace(entity.name, entity.name.antiping()) - MessageHandler.transmit(ApiMessage( - username = cfg!!.relay.systemUser, - text = message - )) - } - } - } -} diff --git a/src/main/kotlin/matterlink/handlers/ChatHandler.kt b/src/main/kotlin/matterlink/handlers/ChatHandler.kt new file mode 100644 index 0000000..11bb10e --- /dev/null +++ b/src/main/kotlin/matterlink/handlers/ChatHandler.kt @@ -0,0 +1,15 @@ +package matterlink.handlers + +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler + +object ChatHandler { + fun handleChat(user: String, msg: String) { + val message = msg.trim() + if (message.isNotBlank()) + MessageHandler.transmit(ApiMessage( + username = user, + text = message + )) + } +} diff --git a/src/main/kotlin/matterlink/handlers/CommandHandler.kt b/src/main/kotlin/matterlink/handlers/CommandHandler.kt new file mode 100644 index 0000000..e188a9a --- /dev/null +++ b/src/main/kotlin/matterlink/handlers/CommandHandler.kt @@ -0,0 +1,14 @@ +package matterlink.handlers + +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler + +object CommandHandler { + fun handleCommand(sender: String, args: String, type: String) { + MessageHandler.transmit(ApiMessage( + username = sender, + text = args, + event = type + )) + } +} diff --git a/src/main/kotlin/matterlink/handlers/DeathHandler.kt b/src/main/kotlin/matterlink/handlers/DeathHandler.kt new file mode 100644 index 0000000..63c074c --- /dev/null +++ b/src/main/kotlin/matterlink/handlers/DeathHandler.kt @@ -0,0 +1,21 @@ +package matterlink.handlers + +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler +import matterlink.cfg +import matterlink.antiping +import net.minecraft.entity.player.EntityPlayer +import net.minecraftforge.event.entity.living.LivingDeathEvent +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +object DeathHandler { + fun handleDeath(player: String, deathMessage: String) { + if (cfg!!.relay.deathEvents) { + val msg = deathMessage.replace(player, player.antiping()) + MessageHandler.transmit(ApiMessage( + username = cfg!!.relay.systemUser, + text = msg + )) + } + } +} diff --git a/src/main/kotlin/matterlink/eventhandlers/JoinLeaveHandler.kt b/src/main/kotlin/matterlink/handlers/JoinLeaveHandler.kt similarity index 51% rename from src/main/kotlin/matterlink/eventhandlers/JoinLeaveHandler.kt rename to src/main/kotlin/matterlink/handlers/JoinLeaveHandler.kt index 652b59c..5dd5677 100644 --- a/src/main/kotlin/matterlink/eventhandlers/JoinLeaveHandler.kt +++ b/src/main/kotlin/matterlink/handlers/JoinLeaveHandler.kt @@ -1,33 +1,32 @@ -package matterlink.eventhandlers +package matterlink.handlers import matterlink.bridge.ApiMessage import matterlink.bridge.MessageHandler import matterlink.cfg import matterlink.antiping +import matterlink.bridge.JOIN_LEAVE import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.gameevent.PlayerEvent -class JoinLeaveHandler { - @SubscribeEvent - fun handleJoinEvent(event: PlayerEvent.PlayerLoggedInEvent) { +object JoinLeaveHandler { + fun handleJoin(player: String) { if (cfg!!.relay.joinLeave) { - val player: String = event.player.name.antiping() + val user = player.antiping() MessageHandler.transmit(ApiMessage( username = cfg!!.relay.systemUser, - text = "$player has connected to the server.", - event = "join_leave" + text = "$user has connected to the server.", + event = JOIN_LEAVE )) } } - @SubscribeEvent - fun handleLeaveEvent(event: PlayerEvent.PlayerLoggedOutEvent) { + fun handleLeave(player: String) { if (cfg!!.relay.joinLeave) { - val player = event.player.name.antiping() + val user = player.antiping() MessageHandler.transmit(ApiMessage( username = cfg!!.relay.systemUser, - text = "$player has disconnected from the server.", - event = "join_leave" + text = "$user has disconnected from the server.", + event = JOIN_LEAVE )) } } diff --git a/src/main/kotlin/matterlink/handlers/ProgressHandler.kt b/src/main/kotlin/matterlink/handlers/ProgressHandler.kt new file mode 100644 index 0000000..4c73d78 --- /dev/null +++ b/src/main/kotlin/matterlink/handlers/ProgressHandler.kt @@ -0,0 +1,18 @@ +package matterlink.handlers + +import matterlink.antiping +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler +import matterlink.cfg + +object ProgressHandler { + + fun handleProgress(name: String, text: String) { + if (!cfg!!.relay.advancements) return + val usr = name.antiping() + MessageHandler.transmit(ApiMessage( + username = cfg!!.relay.systemUser, + text = "$usr $text" + )) + } +} \ No newline at end of file