Skip to content

Commit

Permalink
feat: message-deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 28, 2023
1 parent 21aa3af commit a0b678d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@ data class ChattyChannel(
val simpleConsoleMessages: Boolean = false,
val proxy: Boolean = false,
val discordsrv: Boolean = true,
val messageDeletion: MessageDeletion = MessageDeletion(),
val isDefaultChannel: Boolean = false,
val isStaffChannel: Boolean = false,
val format: String = "",
@SerialName("messageColor") val _messageColor: String = "white",
val channelRadius: Int = 0,
val channelAliases: List<String> = listOf(),
) {

@Serializable data class MessageDeletion(
val enabled: Boolean = false,
val position: MessageDeletionPosition = MessageDeletionPosition.PREFIX,
val format: String = "<gray>[<red>X</red>]</gray>",
val notifyStaff: Boolean = true,
) {
enum class MessageDeletionPosition {
PREFIX, SUFFIX
}
}
val key by lazy { chatty.config.channels.entries.first { it.value == this }.key }
val messageColor: TextColor
get() = TextColor.fromHexString(_messageColor) ?: NamedTextColor.NAMES.value(_messageColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class ChattyConfig(
val privateMessages: PrivateMessages = PrivateMessages(),
// Mutable so other plugins can add channels on their end
// Might be a safer way to do this but 3AM so first solution is the best solution
val channels: MutableMap<String, ChattyChannel> = mutableMapOf("global" to ChattyChannel(ChannelType.GLOBAL)),
val channels: MutableMap<String, ChattyChannel> = mutableMapOf("global" to ChattyChannel(ChannelType.GLOBAL, messageDeletion = ChattyChannel.MessageDeletion(true))),
) {

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ class ChattyMessages(
val privateMessages: PrivateMessages = PrivateMessages(),
val spying: Spying = Spying(),
val ping: Pings = Pings(),
val messageDeletion: MessageDeletion = MessageDeletion(),
val channels: Channels = Channels(),
val proxies: Proxies = Proxies(),
val joinLeave: JoinLeave = JoinLeave(),
val other: Other = Other()
) {

@Serializable
data class MessageDeletion(
val hoverText: String = "<red>Click to delete message",
val notifyStaff: String = "<gold>Message deleted by <yellow>%player_name%</yellow>.",
val format: String = "<gray>[<red>X</red>]</gray>",
val deletionSuccess: String = "<green><i>Message deleted",
val deletionFailed: String = "<red>Could not delete message...",
)

@Serializable
data class PrivateMessages(
val disabled: String = "<red>Private messages are disabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mineinabyss.chatty.helpers
import com.combimagnetron.imageloader.Avatar
import com.combimagnetron.imageloader.Image.ColorType
import com.combimagnetron.imageloader.ImageUtils
import com.mineinabyss.chatty.ChattyChannel
import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.components.ChannelData
import com.mineinabyss.chatty.components.ChannelType
Expand All @@ -12,9 +13,15 @@ import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import me.clip.placeholderapi.PlaceholderAPI
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.chat.SignedMessage
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.event.HoverEvent
import net.kyori.adventure.text.event.HoverEventSource
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import org.bukkit.Bukkit
Expand Down Expand Up @@ -191,6 +198,14 @@ fun String.toPlayer() = Bukkit.getPlayer(this)
fun Player.sendFormattedMessage(message: String) =
this.sendMessage(translatePlaceholders(this, message).parseTags(player, true))

fun Player.sendFormattedMessage(vararg message: String, optionalPlayer: Player? = null) =
this.sendMessage(
translatePlaceholders((optionalPlayer ?: this), message.joinToString(" ")).parseTags(
optionalPlayer ?: this,
true
)
)

fun formattedResult(player: Player, message: Component): Component {
val channelData = player.toGeary().get<ChannelData>()?.withChannelVerified()
val channel = channelData?.channel ?: return message
Expand All @@ -199,3 +214,29 @@ fun formattedResult(player: Player, message: Component): Component {

return parsedFormat.append(parsedMessage)
}

fun Component.hoverEventShowText(text: Component) = this.hoverEvent(HoverEventSource.unbox(HoverEvent.showText(text)))

fun formatModerationMessage(messageDeletion: ChattyChannel.MessageDeletion, message: Component, signedMessage: SignedMessage, audience: Audience, source: Player, viewers: Set<Player>): Component {
fun Component.appendDeletionHover(player: Player): Component {
return this.hoverEventShowText(chatty.messages.messageDeletion.hoverText.miniMsg())
.clickEvent(ClickEvent.callback {
val hoverString = Component.empty().hoverEventShowText(message).serialize()
if (!signedMessage.canDelete()) return@callback player.sendFormattedMessage(hoverString + chatty.messages.messageDeletion.deletionFailed)

viewers.forEach {
it.deleteMessage(signedMessage)
if (player != it && it.hasPermission(ChattyPermissions.MODERATION_PERM))
it.sendFormattedMessage(hoverString + chatty.messages.messageDeletion.notifyStaff, optionalPlayer = player)
}
player.sendFormattedMessage(hoverString + chatty.messages.messageDeletion.deletionSuccess)
}).compact()
}

return when {
!messageDeletion.enabled || audience !is Player || audience == source || !audience.hasPermission(ChattyPermissions.MODERATION_PERM) -> message
messageDeletion.position == ChattyChannel.MessageDeletion.MessageDeletionPosition.PREFIX -> messageDeletion.format.miniMsg().appendDeletionHover(audience).append(message)
messageDeletion.position == ChattyChannel.MessageDeletion.MessageDeletionPosition.SUFFIX -> message.append(messageDeletion.format.miniMsg().appendDeletionHover(audience))
else -> message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object ChattyPermissions {
const val NICKNAME = "chatty.nickname"
const val NICKNAME_OTHERS = "chatty.nickname.others"
const val BYPASS_TAG_PERM = "chatty.tags.bypass"
const val MODERATION_PERM = "chatty.moderation"
val chatFormattingPerms = mapOf(
Permission("chatty.tags.color") to StandardTags.color(),
Permission("chatty.tags.rainbow") to StandardTags.rainbow(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mineinabyss.chatty.listeners

import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.chatty.ChattyChannel
import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.chattyProxyChannel
Expand All @@ -10,9 +9,6 @@ import com.mineinabyss.geary.datatypes.family.family
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.systems.accessors.Pointer
import com.mineinabyss.geary.systems.query.GearyQuery
import com.mineinabyss.idofront.messaging.broadcastVal
import com.mineinabyss.idofront.messaging.logError
import com.mineinabyss.idofront.messaging.logInfo
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import io.papermc.paper.event.player.AsyncChatCommandDecorateEvent
Expand Down Expand Up @@ -93,24 +89,21 @@ class ChatListener : Listener {
else Bukkit.getConsoleSender().sendMessage(message())
}

val playerViewers = viewers().filterIsInstance<Player>().toSet()
if (pingedPlayer == null && viewers().isEmpty()) {
player.sendFormattedMessage(chatty.messages.channels.emptyChannelMessage)
viewers().clear()
} else if (chatty.config.chat.disableChatSigning) {
viewers().filterIsInstance<Player>().forEach { it.sendMessage(message()) }
playerViewers.forEach { receiver ->
receiver.sendMessage(formatModerationMessage(channel.messageDeletion, message(), signedMessage(), receiver, player, playerViewers))
}
viewers().clear()
isCancelled = true
} else renderer { _, _, _, _ -> return@renderer message() }
} else renderer { source, _, message, audience ->
return@renderer formatModerationMessage(channel.messageDeletion, message, signedMessage(), audience, source, playerViewers)
}
}

private fun Player.sendFormattedMessage(vararg message: String, optionalPlayer: Player? = null) =
this.sendMessage(
translatePlaceholders((optionalPlayer ?: this), message.joinToString(" ")).parseTags(
optionalPlayer ?: this,
true
)
)

private fun Component.stripMessageFormat(player: Player, channel: ChattyChannel) =
plainText.serialize(this)
.replace(plainText.serialize(translatePlaceholders(player, channel.format).parseTags(player, true)), "")
Expand Down

0 comments on commit a0b678d

Please sign in to comment.