Wrap forge event handlers

This commit is contained in:
DaMachinator 2018-02-09 13:06:53 -05:00
parent 42b5ba9186
commit cac5c59f75
13 changed files with 197 additions and 132 deletions

View File

@ -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()
}
}
}

View File

@ -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))
}
}

View File

@ -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()

View File

@ -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")

View File

@ -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"
))
}
}
}

View File

@ -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)
)
}
}

View File

@ -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
))
}
}
}

View File

@ -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
))
}
}
}
}

View File

@ -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
))
}
}

View File

@ -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
))
}
}

View File

@ -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
))
}
}
}

View File

@ -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
))
}
}

View File

@ -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"
))
}
}