Skip to content

Commit

Permalink
Add death messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-korsa committed Apr 8, 2021
1 parent e6c31ee commit 394ee77
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.dominikkorsa.discordintegration'
version '1.5'
version '1.6'

repositories {
mavenCentral()
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/dominikkorsa/discordintegration/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ class Client(private val plugin: DiscordIntegration) {
suspend fun disconnect() {
gateway?.logout()?.awaitFirstOrNull()
}

suspend fun sendDeathInfo(deathMessage: String) {
sendMessage { it.setContent(deathMessage) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ class ConfigManager(private val plugin: DiscordIntegration) {
val minecraftTooltipMessage get() = getString("messages.minecraft.tooltip")
val discordJoinMessage get() = getString("messages.discord.join")
val discordQuitMessage get() = getString("messages.discord.quit")
val discordDeathMessage get() = getString("messages.discord.death")
val discordDeathFallbackMessage get() = getString("messages.discord.death-fallback")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dominikkorsa.discordintegration

import com.dominikkorsa.discordintegration.listener.ChatListener
import com.dominikkorsa.discordintegration.listener.DeathListener
import com.dominikkorsa.discordintegration.listener.PlayerCountListener
import com.github.shynixn.mccoroutine.launchAsync
import com.github.shynixn.mccoroutine.registerSuspendingEvents
import discord4j.core.`object`.entity.Message
Expand All @@ -10,11 +13,9 @@ import kotlinx.coroutines.runBlocking
import net.md_5.bungee.api.chat.ComponentBuilder
import net.md_5.bungee.api.chat.HoverEvent
import net.md_5.bungee.api.chat.TextComponent
import net.md_5.bungee.api.chat.hover.content.Content
import org.bukkit.Bukkit
import org.bukkit.event.Listener
import org.bukkit.plugin.java.JavaPlugin
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class DiscordIntegration: JavaPlugin() {
lateinit var client: Client
Expand All @@ -27,14 +28,9 @@ class DiscordIntegration: JavaPlugin() {
client = Client(this)
this.launchAsync {
client.main()
server.pluginManager.registerSuspendingEvents(
PlayerCountListener(this@DiscordIntegration),
this@DiscordIntegration
)
server.pluginManager.registerSuspendingEvents(
ChatListener(this@DiscordIntegration),
this@DiscordIntegration
)
registerSuspendingEvents(PlayerCountListener(this@DiscordIntegration))
registerSuspendingEvents(ChatListener(this@DiscordIntegration))
registerSuspendingEvents(DeathListener(this@DiscordIntegration))
this@DiscordIntegration.launchAsync {
client.initListeners()
}
Expand Down Expand Up @@ -86,4 +82,8 @@ class DiscordIntegration: JavaPlugin() {
)
server.spigot().broadcast(messageComponent)
}

private fun registerSuspendingEvents(listener: Listener) {
server.pluginManager.registerSuspendingEvents(listener, this)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dominikkorsa.discordintegration
package com.dominikkorsa.discordintegration.listener

import com.dominikkorsa.discordintegration.AvatarService
import com.dominikkorsa.discordintegration.AvatarService.*
import com.dominikkorsa.discordintegration.DiscordIntegration
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.AsyncPlayerChatEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dominikkorsa.discordintegration.listener

import com.dominikkorsa.discordintegration.DiscordIntegration
import kotlinx.coroutines.delay
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.PlayerDeathEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent

class DeathListener(private val plugin: DiscordIntegration) : Listener {
@EventHandler
suspend fun onDeath(event: PlayerDeathEvent) {
var deathMessage = event.deathMessage?.let {
plugin.configManager.discordDeathMessage
.replace("%death-message%", it)
} ?: plugin.configManager.discordDeathFallbackMessage
deathMessage = deathMessage
.replace("%player%", event.entity.displayName)
plugin.client.sendDeathInfo(deathMessage)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dominikkorsa.discordintegration
package com.dominikkorsa.discordintegration.listener

import com.dominikkorsa.discordintegration.DiscordIntegration
import kotlinx.coroutines.delay
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ messages:
discord:
join: '**%player% joined the server**'
quit: '**%player% left the server**'
death: '**%death-message%**'
death-fallback: '**%player% died**'
minecraft:
message: '§2%nickname% §7in #%channel-name%§7 » §r%content%'
tooltip: |
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: DiscordIntegration
version: 1.5
version: 1.6
author: Dominik Korsa
main: com.dominikkorsa.discordintegration.DiscordIntegration
api-version: 1.16

0 comments on commit 394ee77

Please sign in to comment.