From 6321dee788da276bbfecfb793563cc2cbfece98e Mon Sep 17 00:00:00 2001 From: Boy Date: Sat, 30 Jul 2022 16:23:08 +0200 Subject: [PATCH] fix discord messages cross proxy and add spyperms cross proxy --- .../chatty/listeners/ChattyProxyListener.kt | 27 +++++++++++++------ .../chatty/listeners/DiscordListener.kt | 14 +++++++--- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/ChattyProxyListener.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/ChattyProxyListener.kt index 59796c6..f078788 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/ChattyProxyListener.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/ChattyProxyListener.kt @@ -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 @@ -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()?.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") diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/DiscordListener.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/DiscordListener.kt index 3553766..d16fac1 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/DiscordListener.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/DiscordListener.kt @@ -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 @@ -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