Skip to content

Commit

Permalink
Various fixes, bump to 1.1:
Browse files Browse the repository at this point in the history
* No more JSON injection
* Threads used for config loading and webhook sending
* More useful error messages for users
* Corrected some issues in the readme.
* Version bump to 1.1
  • Loading branch information
jrddunbr committed Dec 29, 2021
1 parent b07d50d commit 79254d6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

This is Chat Age. It is a Minecraft Mod for 1.18.1

You put a Discord webhook into the config file and it mirrors your Minecraft
You put a Discord webhook into the config file, and it mirrors your Minecraft
chat messages into your Discord chat channel. Pretty simple and effective.

This mod depends on Kotlin for Forge to be installed.

## Config File

See `config/chat_age.yaml` for configuration options. A blank config will be
See `config/chat_age.json` for configuration options. A blank config will be
generated on first start of the server for you.

```json
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apply from: 'https://raw.githubusercontent.com/thedarkcolour/KotlinForForge/site
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.0'
version = '1.1'
group = 'org.ageseries.chatage' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'chatage'

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/ageseries/chatage/ChatAge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ import net.minecraftforge.network.NetworkConstants.IGNORESERVERONLY
import thedarkcolour.kotlinforforge.forge.DIST
import thedarkcolour.kotlinforforge.forge.LOADING_CONTEXT
import java.util.function.Supplier
import kotlin.concurrent.thread

@Mod(ChatAge.MODID)
object ChatAge {
const val MODID: String = "chatage"

private fun chatEvent(event: ServerChatEvent) {
LOGGER.info(event.player)
LOGGER.info(event.username)
LOGGER.info(event.message)
DiscordWebhook.discordWebhook(event.message, event.username)
}

private fun playerLogsIn(event: PlayerEvent.PlayerLoggedInEvent) {
LOGGER.info("Player ${event.player.name.contents} joined")
DiscordWebhook.discordWebhook("Player ${event.player.name.contents} joined")
}

private fun playerLogsOut(event: PlayerEvent.PlayerLoggedOutEvent) {
LOGGER.info("Player ${event.player.name.contents} left")
DiscordWebhook.discordWebhook("Player ${event.player.name.contents} left")
}

Expand All @@ -37,7 +33,7 @@ object ChatAge {
EVENT_BUS.addListener {event: PlayerEvent.PlayerLoggedInEvent -> playerLogsIn(event)}
EVENT_BUS.addListener {event: PlayerEvent.PlayerLoggedOutEvent -> playerLogsOut(event)}
EVENT_BUS.register(this)
ChatAgeConfig.loadConfig()
thread (name = "CA Config Loader Thread"){ ChatAgeConfig.loadConfig() }

val extension = Supplier<IExtensionPoint.DisplayTest>(
fun(): IExtensionPoint.DisplayTest = IExtensionPoint.DisplayTest(
Expand Down
35 changes: 22 additions & 13 deletions src/main/java/org/ageseries/chatage/ChatAgeConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@ object ChatAgeConfig {
var config: ChatAgeData = ChatAgeData()

fun loadConfig(configFile: File = CONFIG_FILE) {
if(configFile.isFile) {
LOGGER.info("Reading config from ${configFile.absoluteFile}")
config = Json.decodeFromString(ChatAgeData.serializer(), configFile.readLines().joinToString("\n"))
//config = Yaml.default.decodeFromStream(ChatAgeData.serializer(), configFile.inputStream())
} else {
config = ChatAgeData()
saveConfig()
try {
if (configFile.isFile) {
LOGGER.info("[Chat Age] Reading config from ${configFile.absoluteFile}")
config = Json.decodeFromString(ChatAgeData.serializer(), configFile.readLines().joinToString("\n"))
//config = Yaml.default.decodeFromStream(ChatAgeData.serializer(), configFile.inputStream())
} else {
config = ChatAgeData()
saveConfig()
}
} catch (e: Exception) {
LOGGER.error("Chat Age had an issue with loading the configuration file, please check the file for errors.")
LOGGER.error("Check that 1) You have valid JSON 2) the config directives are spelled correctly (see documentation)")
}
}

private fun saveConfig(configFile: File = CONFIG_FILE) {
LOGGER.info("Writing config to ${configFile.absoluteFile}")
val configText = Json.encodeToString(ChatAgeData.serializer(), config)
//val configText = Yaml.default.encodeToString(ChatAgeData.serializer(), config)
if (!configFile.exists()) {
configFile.createNewFile()
try {
LOGGER.info("[Chat Age] Writing config to ${configFile.absoluteFile}")
val configText = Json.encodeToString(ChatAgeData.serializer(), config)
//val configText = Yaml.default.encodeToString(ChatAgeData.serializer(), config)
if (!configFile.exists()) {
configFile.createNewFile()
}
configFile.writeText(configText)
} catch (e: Exception) {
LOGGER.error("Chat Age was unable to write back the config file, please check filesystem permissions: $e")
}
configFile.writeText(configText)
}
}

Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/ageseries/chatage/DiscordWebhook.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ageseries.chatage

import java.io.File
import kotlin.concurrent.thread

object DiscordWebhook {

Expand Down Expand Up @@ -48,21 +49,23 @@ object DiscordWebhook {
}
*/

fun discordWebhook(message: String, username: String? = ChatAgeConfig.config.botName) {
val json = """
fun discordWebhook(message: String, username: String = ChatAgeConfig.config.botName) {
thread (name = "CA Discord Webhook Thread") {
val json = """
{
"username": "$username",
"content": "$message"
"username": "${username.replace("\"", "")}",
"content": "${message.replace("\"", "")}"
}
""".trimIndent()

// Currently, not used because I need to come up with a logo to use and a CDN to provide it from.
// 'avatar_url': '${avatar_url}',
// Currently, not used because I need to come up with a logo to use and a CDN to provide it from.
// 'avatar_url': '${avatar_url}',

if (ChatAgeConfig.config.webhookUrl.isNotBlank())
httpsPost(ChatAgeConfig.config.webhookUrl, json)
else {
LOGGER.info("Error! Webhook URL must be defined in the config!")
if (ChatAgeConfig.config.webhookUrl.isNotBlank())
httpsPost(ChatAgeConfig.config.webhookUrl, json)
else {
LOGGER.error("[Chat Age] Webhook URL must be defined in the config.")
}
}
}
}
Expand Down

0 comments on commit 79254d6

Please sign in to comment.