Skip to content

Commit

Permalink
fix discord messages cross proxy and add spyperms cross proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jul 30, 2022
1 parent d6d8e08 commit 6321dee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.mineinabyss.chatty.listeners
import com.mineinabyss.chatty.ChattyContext
import com.mineinabyss.chatty.chattyProxyChannel
import com.mineinabyss.chatty.components.ChannelType
import com.mineinabyss.chatty.components.SpyOnChannels
import com.mineinabyss.chatty.components.chattyData
import com.mineinabyss.chatty.helpers.*
import com.mineinabyss.geary.papermc.access.toGeary
import com.mineinabyss.idofront.messaging.miniMsg
import github.scarsz.discordsrv.Debug
import github.scarsz.discordsrv.DiscordSRV
Expand All @@ -26,17 +29,25 @@ class ChattyProxyListener : PluginMessageListener {
val senderName = decoded.substringBefore(ZERO_WIDTH)
val channelId = decoded.substringAfter(ZERO_WIDTH).split(ZERO_WIDTH).first()
val channelFormat = decoded.substringAfter(channelId + ZERO_WIDTH).split("$ZERO_WIDTH ").first()
val channel = getChannelFromId(channelId) ?: return
val channel = getChannelFromId(channelId)
val proxyMessage = decoded.substringAfter(channelFormat).replaceFirst("$ZERO_WIDTH ", "")
val onlinePlayers = Bukkit.getOnlinePlayers()
val canSpy = onlinePlayers.filter { it.toGeary().get<SpyOnChannels>()?.channels?.contains(player.chattyData.channelId) == true }

when (channel.channelType) {
ChannelType.GLOBAL -> Bukkit.getOnlinePlayers()
ChannelType.RADIUS -> emptyList()
ChannelType.PERMISSION -> Bukkit.getOnlinePlayers().filter { it.hasPermission(channel.permission) }
ChannelType.PRIVATE -> emptyList() //TODO Implement this when PRIVATE is more clear
}.forEach { it.sendMessage(proxyMessage.miniMsg()) }
when (channel?.channelType) {
ChannelType.GLOBAL -> onlinePlayers
ChannelType.RADIUS -> canSpy
ChannelType.PERMISSION -> onlinePlayers.filter { it.hasPermission(channel.permission) || it in canSpy }
ChannelType.PRIVATE -> onlinePlayers.filter { it.getChannelFromPlayer() == channel || it in canSpy }
else -> onlinePlayers
}.forEach {
// Sent from discord
if (channel == null)
it.sendMessage(decoded.miniMsg())
else it.sendMessage(proxyMessage.miniMsg())
}

if (!ChattyContext.isDiscordSRVLoaded || !channel.discordsrv) return
if (!ChattyContext.isDiscordSRVLoaded || channel?.discordsrv != true) return
val dsrv = DiscordSRV.getPlugin()
var discordMessage = proxyMessage.replaceFirst(channelFormat, "")
val reserializer = DiscordSRV.config().getBoolean("Experiment_MCDiscordReserializer_ToDiscord")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.mineinabyss.chatty.listeners

import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.chattyProxyChannel
import com.mineinabyss.chatty.components.chattyData
import com.mineinabyss.chatty.helpers.*
import com.mineinabyss.idofront.messaging.miniMsg
import github.scarsz.discordsrv.api.ListenerPriority
import github.scarsz.discordsrv.api.Subscribe
import github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent
import github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent
import github.scarsz.discordsrv.api.events.DeathMessagePreProcessEvent
import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent
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
Expand All @@ -18,14 +17,21 @@ import github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacemen
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage
import github.scarsz.discordsrv.objects.MessageFormat
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import org.bukkit.Bukkit


class DiscordListener {

@Subscribe(priority = ListenerPriority.HIGHEST)
fun DiscordGuildMessagePostProcessEvent.sendDiscordToProxy() {
Bukkit.getServer().sendPluginMessage(chatty, chattyProxyChannel, minecraftMessage.serialize().toByteArray())
}

@Subscribe(priority = ListenerPriority.NORMAL)
fun GameChatMessagePreProcessEvent.onChat() {
val channel = getChannelFromId(player.chattyData.channelId) ?: return
val lastUsedChannel = getChannelFromId(player.chattyData.lastChannelUsed) ?: return

if (isCancelled) return
else if (!channel.discordsrv || (channel != lastUsedChannel && !lastUsedChannel.discordsrv))
isCancelled = true
Expand Down

0 comments on commit 6321dee

Please sign in to comment.