Fix broadcast message check (#134)

- Now it keeps track of Bukkit broadcast messages instead of checking the stack trace
This commit is contained in:
Norbi Peti 2022-07-07 01:13:23 +02:00
parent a4f1463314
commit 0610ee434b
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 14 additions and 10 deletions

View file

@ -22,17 +22,17 @@ libraryDependencies ++= Seq(
"org.spigotmc." % "spigot" % "1.14.4-R0.1-SNAPSHOT" % Provided, "org.spigotmc." % "spigot" % "1.14.4-R0.1-SNAPSHOT" % Provided,
"com.destroystokyo.paper" % "paper" % "1.16.3-R0.1-SNAPSHOT" % Provided, "com.destroystokyo.paper" % "paper" % "1.16.3-R0.1-SNAPSHOT" % Provided,
"com.discord4j" % "discord4j-core" % "3.2.1", "com.discord4j" % "discord4j-core" % "3.2.2",
"org.slf4j" % "slf4j-jdk14" % "1.7.32", "org.slf4j" % "slf4j-jdk14" % "1.7.36",
"com.vdurmont" % "emoji-java" % "5.1.1", "com.vdurmont" % "emoji-java" % "5.1.1",
"org.mockito" % "mockito-core" % "4.2.0", "org.mockito" % "mockito-core" % "4.6.1",
"io.projectreactor" % "reactor-scala-extensions_2.13" % "0.8.0", "io.projectreactor" % "reactor-scala-extensions_2.13" % "0.8.0",
// https://mvnrepository.com/artifact/org.immutables/value // https://mvnrepository.com/artifact/org.immutables/value
"org.immutables" % "value" % "2.8.8" % "provided", "org.immutables" % "value" % "2.9.0" % "provided",
"com.github.TBMCPlugins.ChromaCore" % "Chroma-Core" % "v1.0.0" % Provided, "com.github.TBMCPlugins.ChromaCore" % "Chroma-Core" % "v1.0.0" % Provided,
"net.ess3" % "EssentialsX" % "2.17.1" % Provided, "net.ess3" % "EssentialsX" % "2.17.1" % Provided,
"net.luckperms" % "api" % "5.3" % Provided, "net.luckperms" % "api" % "5.4" % Provided,
) )
assembly / assemblyJarName := "Chroma-Discord.jar" assembly / assemblyJarName := "Chroma-Discord.jar"

View file

@ -31,7 +31,7 @@ import java.util.concurrent.atomic.AtomicInteger
import java.util.logging.Level import java.util.logging.Level
import java.util.stream.Collectors import java.util.stream.Collectors
import javax.annotation.Nullable import javax.annotation.Nullable
import scala.collection.concurrent import scala.collection.{concurrent, mutable}
import scala.collection.convert.ImplicitConversions.`map AsJavaMap` import scala.collection.convert.ImplicitConversions.`map AsJavaMap`
import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer
import scala.jdk.CollectionConverters.{CollectionHasAsScala, SeqHasAsJava} import scala.jdk.CollectionConverters.{CollectionHasAsScala, SeqHasAsJava}
@ -50,6 +50,8 @@ object MCChatUtils {
private var module: MinecraftChatModule = null private var module: MinecraftChatModule = null
private val staticExcludedPlugins: concurrent.Map[Class[_ <: Event], util.HashSet[String]] = concurrent.TrieMap() private val staticExcludedPlugins: concurrent.Map[Class[_ <: Event], util.HashSet[String]] = concurrent.TrieMap()
val broadcastedMessages: mutable.Map[String, Long] = mutable.Map()
def updatePlayerList(): Unit = { def updatePlayerList(): Unit = {
val mod = getModule val mod = getModule
if (mod == null || !mod.showPlayerListOnDC.get) return () if (mod == null || !mod.showPlayerListOnDC.get) return ()

View file

@ -109,7 +109,7 @@ class MCListener(val module: MinecraftChatModule) extends Listener {
MCChatUtils.forAllowedMCChat(MCChatUtils.send(event.getMessage), event).subscribe() MCChatUtils.forAllowedMCChat(MCChatUtils.send(event.getMessage), event).subscribe()
@EventHandler def onBroadcastMessage(event: BroadcastMessageEvent): Unit = { @EventHandler def onBroadcastMessage(event: BroadcastMessageEvent): Unit = {
module.broadcastedMessages += ((event.getMessage, System.nanoTime())) MCChatUtils.broadcastedMessages += ((event.getMessage, System.nanoTime()))
MCChatUtils.forCustomAndAllMCChat(MCChatUtils.send(event.getMessage), ChannelconBroadcast.BROADCAST, hookmsg = false).subscribe() MCChatUtils.forCustomAndAllMCChat(MCChatUtils.send(event.getMessage), ChannelconBroadcast.BROADCAST, hookmsg = false).subscribe()
} }

View file

@ -102,8 +102,6 @@ class MinecraftChatModule extends Component[DiscordPlugin] {
final private val mcChatCommand = new MCChatCommand(this) final private val mcChatCommand = new MCChatCommand(this)
final private val channelconCommand = new ChannelconCommand(this) final private val channelconCommand = new ChannelconCommand(this)
val broadcastedMessages: mutable.Map[String, Long] = mutable.Map()
override protected def enable(): Unit = { override protected def enable(): Unit = {
if (DPUtils.disableIfConfigErrorRes(this, chatChannel, chatChannelMono)) return () if (DPUtils.disableIfConfigErrorRes(this, chatChannel, chatChannelMono)) return ()
listener = new MCChatListener(this) listener = new MCChatListener(this)

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.mcchat.sender package buttondevteam.discordplugin.mcchat.sender
import buttondevteam.discordplugin.mcchat.MCChatUtils
import buttondevteam.discordplugin.{DPUtils, DiscordPlugin} import buttondevteam.discordplugin.{DPUtils, DiscordPlugin}
import buttondevteam.lib.TBMCCoreAPI import buttondevteam.lib.TBMCCoreAPI
import buttondevteam.lib.player.ChromaGamerBase import buttondevteam.lib.player.ChromaGamerBase
@ -40,8 +41,11 @@ abstract class DiscordSenderBase protected(var user: User, var channel: MessageC
} }
override def sendMessage(message: String): Unit = try { override def sendMessage(message: String): Unit = try {
val broadcast = new Exception().getStackTrace()(2).getMethodName.contains("broadcast") val broadcast = MCChatUtils.broadcastedMessages.contains(message);
if (broadcast) { //We're catching broadcasts using the Bukkit event if (broadcast) { //We're catching broadcasts using the Bukkit event
if (MCChatUtils.broadcastedMessages.size >= 4) { // We really don't need to store messages for long
MCChatUtils.broadcastedMessages.filterInPlace((_, time) => time > System.nanoTime() - 1000 * 1000 * 1000)
}
return () return ()
} }
val sendmsg = DPUtils.sanitizeString(message) val sendmsg = DPUtils.sanitizeString(message)