Skip to content

Commit

Permalink
Merge pull request #53 from MineInAbyss/develop
Browse files Browse the repository at this point in the history
Fix DiscordSRV sync
  • Loading branch information
0ffz authored Dec 18, 2023
2 parents 0457789 + ab5186c commit 23bcb95
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
1 change: 0 additions & 1 deletion chatty-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ repositories {
maven("https://repo.mineinabyss.com/snapshots")
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.idofront.textcomponents.miniMsg
import github.scarsz.discordsrv.Debug
import github.scarsz.discordsrv.DiscordSRV
import github.scarsz.discordsrv.dependencies.jda.api.Permission
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed
import github.scarsz.discordsrv.util.DiscordUtil
import github.scarsz.discordsrv.util.MessageUtil
import github.scarsz.discordsrv.util.PlaceholderUtil
import github.scarsz.discordsrv.util.WebhookUtil
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.MessageEmbed
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.plugin.messaging.PluginMessageListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,30 @@ import com.mineinabyss.idofront.textcomponents.serialize
import github.scarsz.discordsrv.api.ListenerPriority
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.Message
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed
import github.scarsz.discordsrv.objects.MessageFormat
import net.dv8tion.jda.api.MessageBuilder
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.entities.MessageEmbed.Field
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component as ComponentDSV


class DiscordListener {

private val mm = MiniMessage.miniMessage()
private val plainText = PlainTextComponentSerializer.plainText()
private val mm = github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage.miniMessage()
private val plainText = github.scarsz.discordsrv.dependencies.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer.plainText()
private val legacy = LegacyComponentSerializer.builder().useUnusualXRepeatedCharacterHexFormat().build()

@Subscribe(priority = ListenerPriority.HIGHEST)
fun DiscordGuildMessagePostProcessEvent.sendDiscordToProxy() {
minecraftMessage = (minecraftMessage.serialize()
.substringBefore(message.contentRaw) + mm.stripTags(message.contentStripped)).miniMsg()
minecraftMessage = mm.deserialize(
mm.serialize(minecraftMessage).substringBefore(message.contentRaw) + mm.stripTags(message.contentStripped)
)
Bukkit.getServer()
.sendPluginMessage(chatty.plugin, chattyProxyChannel, minecraftMessage.serialize().toByteArray())
.sendPluginMessage(chatty.plugin, chattyProxyChannel, mm.serialize(minecraftMessage).toByteArray())
}

@Subscribe(priority = ListenerPriority.NORMAL)
Expand All @@ -53,12 +52,12 @@ class DiscordListener {
}

// Parse the DSRV Component through the Chatty normal MM instance to format <chatty> tags, then serialize/deserialize it back to DSRV Component
private fun Component.stripFormat(player: Player, channel: ChattyChannel) =
plainText.serialize(this).replace(
private fun ComponentDSV.stripFormat(player: Player, channel: ChattyChannel): ComponentDSV =
mm.deserialize(plainText.serialize(this).replace(
plainText.serialize(
translatePlaceholders(player, channel.format).parseTags(player, true).serialize().miniMsg()
mm.deserialize(translatePlaceholders(player, channel.format).parseTags(player, true).serialize())
), ""
).miniMsg()
))

@Subscribe
fun DeathMessagePreProcessEvent.onDeath() {
Expand All @@ -83,12 +82,18 @@ class DiscordListener {
private fun Message.translatePostFormat(): Message {
val message = this
val embeds = mutableListOf<MessageEmbed>()
val fields = mutableListOf<Field>()
val fields = mutableListOf<MessageEmbed.Field>()

message.embeds.forEach { embed ->
if (embed.fields.isNotEmpty())
embed.fields.forEach { field ->
fields.add(Field(field.name?.translateEmoteIDs(), field.value?.translateEmoteIDs(), field.isInline))
fields.add(
MessageEmbed.Field(
field.name?.translateEmoteIDs(),
field.value?.translateEmoteIDs(),
field.isInline
)
)
}
embeds.add(
MessageEmbed(
Expand All @@ -104,7 +109,7 @@ class DiscordListener {
}

private fun MessageFormat.translatePreFormat(): MessageFormat {
val fields = mutableListOf<Field>()
val fields = mutableListOf<MessageEmbed.Field>()
val format = this
if (content != null)
format.content = content.translateEmoteIDs()
Expand All @@ -114,14 +119,14 @@ class DiscordListener {
format.description = description.translateEmoteIDs()
if (format.fields != null)
format.fields.forEach { f ->
fields.add(Field(f.name?.translateEmoteIDs(), f.value?.translateEmoteIDs(), f.isInline))
fields.add(MessageEmbed.Field(f.name?.translateEmoteIDs(), f.value?.translateEmoteIDs(), f.isInline))
}
format.fields = fields
return format
}

private fun String.cleanUpHackyFix() =
plainText.serialize(this.miniMsg()).replace("\\<", "<").replace("<<", "<")
plainText.serialize(mm.deserialize(this)).replace("\\<", "<").replace("<<", "<")

private fun String.translateEmoteIDs(): String {
var translated = this
Expand Down
8 changes: 5 additions & 3 deletions gradle/chattyLibs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[versions]
geary = "0.27.0"
velocity = "3.2.0-SNAPSHOT"
discordsrv = "1.26.2"
discordsrv = "1.27.0-SNAPSHOT"
placeholderapi = "2.11.4"
imageloader = "1.1-SNAPSHOT"

[libraries]
geary-papermc = { module = "com.mineinabyss:geary-papermc", version.ref = "geary" }
velocity = { module = "com.velocitypowered:velocity-api", version.ref = "velocity" }
discordsrv = { module = "com.discordsrv:discordsrv", version.ref = "discordsrv" }
placeholderapi = "me.clip:placeholderapi:2.11.4"
imageloader = "com.combimagnetron:imageloader:1.1-SNAPSHOT"
placeholderapi = { module = "me.clip:placeholderapi", version.ref = "placeholderapi" }
imageloader = { module = "com.combimagnetron:imageloader", version.ref = "imageloader" }

0 comments on commit 23bcb95

Please sign in to comment.