Skip to content

Commit

Permalink
Discordsrv (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 authored Jul 20, 2022
1 parent d6049f5 commit 48ff420
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 22 deletions.
3 changes: 3 additions & 0 deletions chatty-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ plugins {

repositories {
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
maven("https://nexus.scarsz.me/content/groups/public/") // DiscordSRV
maven("https://m2.dv8tion.net/releases") // DiscordSRV
}

dependencies {
Expand All @@ -25,6 +27,7 @@ dependencies {

/// Third-party plugins
compileOnly(chattyLibs.placeholderapi)
compileOnly(chattyLibs.discordsrv)

// Shaded
implementation(libs.idofront.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
action {
val player = sender as? Player ?: return@action
player.playerData.disablePingSound = !player.playerData.disablePingSound
player.sendFormattedMessage(messages.ping.toggledPingSound)
player.sendFormattedMessage(chattyMessages.ping.toggledPingSound)
}
}
"sound"(desc = "Change your pingsound") {
Expand All @@ -30,17 +30,17 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
val player = sender as? Player ?: return@action
if (soundName in getAlternativePingSounds) {
player.playerData.pingSound = soundName
player.sendFormattedMessage(messages.ping.changedPingSound)
player.sendFormattedMessage(chattyMessages.ping.changedPingSound)
} else {
player.sendFormattedMessage(messages.ping.invalidPingSound)
player.sendFormattedMessage(chattyMessages.ping.invalidPingSound)
}
}
}
}
"channels"(desc = "List all channels") {
action {
(sender as? Player)?.sendFormattedMessage(messages.channels.availableChannels)
?: sender.sendMessage(messages.channels.availableChannels)
(sender as? Player)?.sendFormattedMessage(chattyMessages.channels.availableChannels)
?: sender.sendMessage(chattyMessages.channels.availableChannels)
}
}
"nickname" {
Expand All @@ -50,24 +50,24 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
val player = sender as? Player ?: return@action
if (nickname.isEmpty()) player.displayName(player.name.miniMsg())
else player.displayName(arguments.joinToString().replace(", ", " ").miniMsg())
player.sendFormattedMessage(messages.other.nickNameChanged)
player.sendFormattedMessage(chattyMessages.other.nickNameChanged)
}
}
"reload" {
"config" {
action {
ChattyConfig.reload()
ChattyConfig.load()
(sender as? Player)?.sendFormattedMessage(messages.other.configReloaded)
?: sender.sendMessage(messages.other.configReloaded.miniMsg())
(sender as? Player)?.sendFormattedMessage(chattyMessages.other.configReloaded)
?: sender.sendMessage(chattyMessages.other.configReloaded.miniMsg())
}
}
"messages" {
action {
ChattyMessages.reload()
ChattyMessages.load()
(sender as? Player)?.sendFormattedMessage(messages.other.messagesReloaded)
?: sender.sendMessage(messages.other.messagesReloaded.miniMsg())
(sender as? Player)?.sendFormattedMessage(chattyMessages.other.messagesReloaded)
?: sender.sendMessage(chattyMessages.other.messagesReloaded.miniMsg())
}
}

Expand Down Expand Up @@ -118,11 +118,11 @@ private fun Player.swapChannelCommand(channelId: String) {
val newChannel = getChannelFromId(channelId)

if (newChannel == null) {
sendFormattedMessage(messages.channels.noChannelWithName)
sendFormattedMessage(chattyMessages.channels.noChannelWithName)
} else if (!hasPermission(newChannel.permission)) {
sendFormattedMessage(messages.channels.missingChannelPermission)
sendFormattedMessage(chattyMessages.channels.missingChannelPermission)
} else {
playerData.channelId = channelId
sendFormattedMessage(messages.channels.channelChanged)
sendFormattedMessage(chattyMessages.channels.channelChanged)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ object ChattyConfig : IdofrontConfig<ChattyConfig.Data>(chattyPlugin, Data.seria
@Serializable
data class Join(
val enabled: Boolean = true,
val sendAcrossProxy: Boolean = true,
val firstJoin: FirstJoin,
)

@Serializable
data class FirstJoin(
val enabled: Boolean = true,
val sendAcrossProxy: Boolean = true,
)

@Serializable
data class Leave(
val enabled: Boolean,
val sendAcrossProxy: Boolean = true,
)

@Serializable
Expand All @@ -42,6 +45,7 @@ object ChattyConfig : IdofrontConfig<ChattyConfig.Data>(chattyPlugin, Data.seria
val channelType: ChannelType,
val permission: String = "",
val proxy: Boolean = false,
val discordsrv: Boolean = true,
val isDefaultChannel: Boolean = false,
val format: Format,
val channelRadius: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.mineinabyss.chatty

import com.mineinabyss.chatty.listeners.ChatListener
import com.mineinabyss.chatty.listeners.ChattyProxyListener
import com.mineinabyss.chatty.listeners.DiscordListener
import com.mineinabyss.chatty.listeners.PlayerListener
import com.mineinabyss.chatty.placeholderapi.PlaceholderHook
import com.mineinabyss.geary.addon.autoscan
import com.mineinabyss.geary.papermc.dsl.gearyAddon
import com.mineinabyss.idofront.platforms.IdofrontPlatforms
import com.mineinabyss.idofront.plugin.getService
import com.mineinabyss.idofront.plugin.registerEvents
import github.scarsz.discordsrv.DiscordSRV
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import kotlin.io.path.Path
Expand All @@ -27,6 +29,7 @@ class ChattyPlugin : JavaPlugin() {
}

override fun onEnable() {
saveDefaultEmoteFixer()
saveDefaultMessages()
saveDefaultConfig()
reloadConfig()
Expand All @@ -46,16 +49,29 @@ class ChattyPlugin : JavaPlugin() {
server.messenger.registerIncomingPluginChannel(this, chattyProxyChannel, ChattyProxyListener())
server.messenger.registerOutgoingPluginChannel(this, chattyProxyChannel)

DiscordSRV.api.subscribe(DiscordListener())

gearyAddon {
autoscan("com.mineinabyss") {
all()
}
}
}

override fun onDisable() {
DiscordSRV.api.unsubscribe(DiscordListener())
}
}

private fun saveDefaultMessages() {
if (!Path(chattyPlugin.dataFolder.path + "/messages.yml").toFile().exists()) {
chattyPlugin.saveResource("messages.yml", false)
}
}


private fun saveDefaultEmoteFixer() {
if (!Path(chattyPlugin.dataFolder.path + "/emotefixer.yml").toFile().exists()) {
chattyPlugin.saveResource("emotefixer.yml", false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mineinabyss.chatty.helpers

import com.mineinabyss.chatty.chattyPlugin
import com.mineinabyss.idofront.config.IdofrontConfig
import kotlinx.serialization.Serializable
import kotlin.io.path.div

object DiscordEmoteFixer : IdofrontConfig<DiscordEmoteFixer.Emotes>(
chattyPlugin,
Emotes.serializer(),
file = (chattyPlugin.dataFolder.toPath() / "emotefixer.yml")
) {

@Serializable
data class Emotes(val emotes: Map<String, String>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ package com.mineinabyss.chatty.helpers

import com.mineinabyss.chatty.ChattyConfig
import com.mineinabyss.chatty.ChattyMessages
import github.scarsz.discordsrv.DiscordSRV
import io.github.bananapuncher714.bondrewd.likes.his.emotes.BondrewdLikesHisEmotes
import me.clip.placeholderapi.PlaceholderAPIPlugin


val chattyConfig = ChattyConfig.data
val messages = ChattyMessages.data
val chattyMessages = ChattyMessages.data
val emoteFixer = DiscordEmoteFixer.data

val bondrewd = BondrewdLikesHisEmotes.getPlugin(BondrewdLikesHisEmotes::class.java)
val discordSrv = DiscordSRV.getPlugin()
val papi = PlaceholderAPIPlugin.getPlugin(PlaceholderAPIPlugin::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ChatListener : Listener {

if (pingedPlayer == null && audiences.isEmpty()) {
isCancelled = true
player.sendFormattedMessage(messages.channels.emptyChannelMessage)
player.sendFormattedMessage(chattyMessages.channels.emptyChannelMessage)
} else audiences.forEach { audience ->
RendererExtension().render(player, displayName, message(), audience)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.mineinabyss.chatty.listeners

import com.mineinabyss.chatty.components.playerData
import com.mineinabyss.chatty.helpers.emoteFixer
import com.mineinabyss.chatty.helpers.getChannelFromId
import com.mineinabyss.idofront.messaging.broadcast
import github.scarsz.discordsrv.api.Subscribe
import github.scarsz.discordsrv.api.events.*
import github.scarsz.discordsrv.dependencies.jda.api.MessageBuilder
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage


class DiscordListener {

@Subscribe
fun GameChatMessagePreProcessEvent.onChat() {
val channel = getChannelFromId(player.playerData.channelId) ?: return
if (!channel.discordsrv) isCancelled = true
messageComponent = messageComponent.translateEmoteIDsToComponent()
}

@Subscribe
fun VentureChatMessagePreProcessEvent.onProxyChat() {
val channelId = messageComponent.deserialize().substringBefore(" ")
val channel = getChannelFromId(channelId) ?: return
if (!channel.discordsrv) isCancelled = true
else messageComponent =
messageComponent.removeAttachedChannel(channelId).translateEmoteIDsToComponent()
}

@Subscribe
fun DeathMessagePreProcessEvent.onDeath() {
val channel = getChannelFromId(player.playerData.channelId) ?: return
if (!channel.discordsrv) isCancelled = true
deathMessage = deathMessage.translateEmoteIDs()
}

@Subscribe
fun AchievementMessagePreProcessEvent.onAchievement() {
achievementName = achievementName.translateEmoteIDs()
val format = messageFormat
if (format.content != null)
format.content = format.content.translateEmoteIDs()
if (format.description != null)
format.description = format.description.translateEmoteIDs()
if (format.title != null)
format.title = format.title.translateEmoteIDs()
if (format.fields != null) {
val fields: MutableList<MessageEmbed.Field> = ArrayList()
format.fields.forEach { f ->
fields.add(MessageEmbed.Field(f.name?.translateEmoteIDs(), f.value?.translateEmoteIDs(), f.isInline))
}
format.fields = fields
}
messageFormat = format
}

@Subscribe
fun AchievementMessagePostProcessEvent.onAchievement() {
val message = discordMessage
val embeds = mutableListOf<MessageEmbed>()

message.embeds.forEach { embed ->
val fields = mutableListOf<MessageEmbed.Field>()
if (embed.fields.isNotEmpty())
embed.fields.forEach { field ->
fields.add(
MessageEmbed.Field(
field.name?.translateEmoteIDs(),
field.value?.translateEmoteIDs(),
field.isInline
)
)
}
embeds.add(
MessageEmbed(
embed.url, embed.title?.translateEmoteIDs(), embed.description?.translateEmoteIDs(),
embed.type, embed.timestamp, embed.colorRaw, embed.thumbnail, embed.siteProvider,
embed.author, embed.videoInfo, embed.footer, embed.image, fields
)
)
}
val builder = MessageBuilder(message)
if (embeds.isNotEmpty()) builder.setEmbeds(embeds)
discordMessage = builder.build()
}
}

private fun Component.removeAttachedChannel(id: String) =
this.replaceText(TextReplacementConfig.builder().match(id).replacement("").build())

private fun Component.cleanUpHackyFix() =
this.replaceText(TextReplacementConfig.builder().match("<<").replacement("<").build())


private fun String.cleanUpHackyFix() = this.replace("<<", "<")

private fun String.translateEmoteIDs(): String {
var translated = this
emoteFixer.emotes.entries.forEach { (emoteId, replacement) ->
val id = ":$emoteId:"
if (id in this) {
translated = translated.replace(id, replacement)
}
}
return translated.cleanUpHackyFix()
}

private fun String.translateEmoteIDsToComponent(): Component {
var translated = this
emoteFixer.emotes.entries.forEach { (emoteId, replacement) ->
val id = ":$emoteId:"
if (id in this) {
translated = translated.replace(id, "<$replacement")
}
}
return translated.miniMessage().cleanUpHackyFix()
}

private fun Component.translateEmoteIDsToComponent(): Component {
var translated = this
emoteFixer.emotes.entries.forEach { (emoteId, replacement) ->
val id = ":$emoteId:"
if (id in translated.deserialize()) {
translated = translated.replaceText(
TextReplacementConfig.builder().match(id)
.replacement("<$replacement".miniMessage()).build()
)
}
}
return translated.cleanUpHackyFix()
}

private fun Component.deserialize(): String {
return MiniMessage.builder().build()
.serialize(this)
}

private fun Component.stripTags(): String {
broadcast(this.deserialize())
return MiniMessage.builder().build().stripTokens(this.deserialize())
}

private fun String.miniMessage(): Component {
return MiniMessage.builder().build()
.deserialize(this)
}
Loading

0 comments on commit 48ff420

Please sign in to comment.