Skip to content

Commit

Permalink
Minor changes (#32)
Browse files Browse the repository at this point in the history
* merge config and message reloads into one command

* fix issue with serializing legacy

* add option to log messages in console per channel
  • Loading branch information
Boy0000 authored Sep 14, 2022
1 parent e785bb7 commit 074e097
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,16 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
}
}
("reload" / "rl") {
"config" {
action {
ChattyConfig.reload()
ChattyConfig.load()
(sender as? Player)?.sendFormattedMessage(chattyMessages.other.configReloaded)
?: sender.sendConsoleMessage(chattyMessages.other.configReloaded)
}
}
"messages" {
action {
ChattyMessages.reload()
ChattyMessages.load()
(sender as? Player)?.sendFormattedMessage(chattyMessages.other.messagesReloaded)
?: sender.sendConsoleMessage(chattyMessages.other.messagesReloaded)
}
action {
ChattyConfig.reload()
ChattyConfig.load()
(sender as? Player)?.sendFormattedMessage(chattyMessages.other.configReloaded)
?: sender.sendConsoleMessage(chattyMessages.other.configReloaded)

ChattyMessages.reload()
ChattyMessages.load()
(sender as? Player)?.sendFormattedMessage(chattyMessages.other.messagesReloaded)
?: sender.sendConsoleMessage(chattyMessages.other.messagesReloaded)
}

}
Expand Down Expand Up @@ -266,7 +261,6 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
1 -> listOf("message", "ping", "reload", "channels", "nickname", "spy", "commandspy").filter { s -> s.startsWith(args[0]) }
2 -> when (args[0]) {
"ping" -> listOf("toggle", "sound").filter { s -> s.startsWith(args[1]) }
"reload", "rl" -> listOf("config", "messages").filter { s -> s.startsWith(args[1]) }
"message", "msg" -> onlinePlayers.filter { s -> s.startsWith(args[1], true) }
"spy" ->
chattyConfig.channels.keys.toList().filter { s -> s.startsWith(args[1], true) && getChannelFromId(s)?.channelType != ChannelType.GLOBAL }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ object ChattyConfig : IdofrontConfig<ChattyConfig.Data>(chatty, Data.serializer(
@Serializable
data class Proxy(
val enableProxySwitchMessages: Boolean = true,
val discordSrvChannelID: String = "Global"
val discordSrvChannelID: String = "Global",
val sendProxyMessagesToDiscord: Boolean = false,
)

@Serializable
data class ChattyChannel(
val channelType: ChannelType,
val permission: String = "",
val logToConsole: Boolean = true,
val proxy: Boolean = false,
val discordsrv: Boolean = true,
val isDefaultChannel: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ private fun avatarBuilder(player: Player, scale: Int = 1, ascent: Int = 0, color
return Avatar.builder().isSlim(player.playerProfile.textures.skinModel == SkinModel.SLIM).playerName(player.name).ascent(ascent).colorType(colorType).scale(scale).build()
}

fun Component.fixLegacy(): Component =
this.serialize().replace("\\<", "<").replace("\\>", ">").miniMsg()
fun Component.fixLegacy(): Component {
val string = this.serialize().replace("\\<", "<").replace("\\>", ">")
return if ("§" in serialize())
legacy.deserialize(string)
else string.miniMsg()
}

fun Component.serialize() = mm.serialize(this)

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.mineinabyss.chatty.ChattyConfig
import com.mineinabyss.chatty.ChattyMessages
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.entity.Player

Expand All @@ -12,6 +13,7 @@ val chattyMessages = ChattyMessages.data
val emoteFixer = DiscordEmoteFixer.data
val mm = MiniMessage.miniMessage()
val plainText = PlainTextComponentSerializer.plainText()
val legacy = LegacyComponentSerializer.builder().useUnusualXRepeatedCharacterHexFormat().build()

fun Player.checkPermission(perm: String): Boolean {
return if (perm.isEmpty()) true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class ChatListener : Listener {
player.sendPluginMessage(chatty, chattyProxyChannel, proxyMessage)
}

if (channel.logToConsole)
Bukkit.getConsoleSender().sendMessage(message())

if (pingedPlayer == null && viewers().isEmpty()) {
player.sendFormattedMessage(chattyMessages.channels.emptyChannelMessage)
viewers().clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,26 @@ class ChattyProxyListener : PluginMessageListener {
}

// If the channel is not found, it is discord
//TODO Find out why this gets triggered repeatedly and therefore message is sent a lot of times
// Seems to tie into how many players are on other servers under proxy?
if (channel == null) {
onlinePlayers.forEach {
//it.sendMessage(decoded.miniMsg())
}
} else 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 }
}.forEach { it.sendMessage(proxyMessage.miniMsg()) }
}
else {
if (channel.logToConsole)
Bukkit.getConsoleSender().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 }
}.forEach { it.sendMessage(proxyMessage.miniMsg()) }
}


if (!ChattyContext.isDiscordSRVLoaded || channel?.discordsrv != true) return
if (!chattyConfig.proxy.sendProxyMessagesToDiscord ||
channel?.discordsrv != true || !ChattyContext.isDiscordSRVLoaded) return

val dsrv = DiscordSRV.getPlugin()
var discordMessage = proxyMessage.replaceFirst(channelFormat, "")
Expand Down Expand Up @@ -86,18 +92,18 @@ class ChattyProxyListener : PluginMessageListener {
)
}
}
}

private val translateMentions = DiscordSRV.config().getBoolean("DiscordChatChannelTranslateMentions")
private fun String.translateEmoteIDsToComponent(): String {
var translated = this
emoteFixer.emotes.entries.forEach { (emoteId, replacement) ->
val id = ":$emoteId:"
if (id in translated)
translated = translated.replace(id, "<$replacement")
private val translateMentions = DiscordSRV.config().getBoolean("DiscordChatChannelTranslateMentions")
private fun String.translateEmoteIDsToComponent(): String {
var translated = this
emoteFixer.emotes.entries.forEach { (emoteId, replacement) ->
val id = ":$emoteId:"
if (id in translated)
translated = translated.replace(id, "<$replacement")
}
return translated.cleanUpHackyFix()
}
return translated.cleanUpHackyFix()
}

private fun String.cleanUpHackyFix() =
this.replace("<<", "<")
private fun String.cleanUpHackyFix() =
this.replace("<<", "<")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed.Field
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import github.scarsz.discordsrv.objects.MessageFormat
import me.clip.placeholderapi.PlaceholderAPI
Expand All @@ -30,6 +31,7 @@ class DiscordListener {

private val mm = MiniMessage.builder().build()
private val plainText = PlainTextComponentSerializer.plainText()
private val legacy = LegacyComponentSerializer.builder().useUnusualXRepeatedCharacterHexFormat().build()

@Subscribe(priority = ListenerPriority.HIGHEST)
fun DiscordGuildMessagePostProcessEvent.sendDiscordToProxy() {
Expand All @@ -41,7 +43,6 @@ class DiscordListener {
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 Expand Up @@ -81,8 +82,12 @@ class DiscordListener {
return PlaceholderAPI.setPlaceholders(player, msg).miniMessage().fixLegacy()
}

private fun Component.fixLegacy(): Component =
mm.deserialize(this.serialize().replace("\\<", "<").replace("\\>", ">"))
private fun Component.fixLegacy(): Component {
val string = this.serialize().replace("\\<", "<").replace("\\>", ">")
return if ("§" in serialize())
legacy.deserialize(string)
else mm.deserialize(string)
}

private fun Message.translatePostFormat(): Message {
val message = this
Expand Down
87 changes: 59 additions & 28 deletions chatty-paper/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
# Please note: This is not the most performance friendly feature so use the placeholder sparingly.
playerHeadFont: "minecraft:chatty_heads"

# Keep in mind this needs to also be enabled in your [server.properties] file
# It will warn all your players when they first join
# that the server can read all their messages before they are sent.
# Chatty does not do anything with it outside the [ChatPreview.kt] file.
enableChatPreviews: true

privateMessages:
enabled: true
proxy: true # Not currently supported
Expand All @@ -19,54 +13,71 @@ privateMessages:
messageSendSound: "" # Empty for no sound
messageReceiveSound: "" # Empty for no sound

# [enableChatSigning]: Sends messages with valid signatures to support 1.19.1+ chat reporting. Disabling this will disable reporting
# [allowedTags]: # Available formats are:
# [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
# [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
# https://docs.adventure.kyori.net/minimessage/format.html
chat:
enableChatSigning: true # Enable chat signing and reporting
enableChatSigning: true
bypassFormatPermission: "chatty.chat.bypassformat"
commandSpyFormat: "<gold>%chatty_player_displayname%</gold><yellow>: "
allowedTags: # Available formats are:
- BOLD # [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
- STRIKETHROUGH # [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
- UNDERLINE # https://docs.adventure.kyori.net/minimessage/format.html
allowedTags:
- BOLD
- STRIKETHROUGH
- UNDERLINE
- ITALIC
- TEXTCOLOR
- HEXCOLOR
- RAINBOW
- GRADIENT

# [useDisplayNameforAuthor]: Whether books should be signed using displayname or username of a player
# [allowedTags]: # Available formats are:
# [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
# [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
# https://docs.adventure.kyori.net/minimessage/format.html
book:
useDisplayNameForAuthor: false
bypassFormatPermission: "chatty.chat.bypassformat"
allowedTags: # Available formats are:
- BOLD # [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
- STRIKETHROUGH # [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
- UNDERLINE # https://docs.adventure.kyori.net/minimessage/format.html
allowedTags:
- BOLD
- STRIKETHROUGH
- UNDERLINE
- ITALIC
- OBFUSCATED
- TEXTCOLOR
- HEXCOLOR
- RAINBOW
- GRADIENT

# [allowedTags]: # Available formats are:
# [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
# [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
# https://docs.adventure.kyori.net/minimessage/format.html
sign:
bypassFormatPermission: "chatty.chat.bypassformat"
allowedTags: # Available formats are:
- BOLD # [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
- STRIKETHROUGH # [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
- UNDERLINE # https://docs.adventure.kyori.net/minimessage/format.html
allowedTags:
- BOLD
- STRIKETHROUGH
- UNDERLINE
- ITALIC
- OBFUSCATED
- TEXTCOLOR
- HEXCOLOR
- RAINBOW
- GRADIENT

# [maxLength]: The max length a nickname can be, with or without tags, depending on [countTagsInLength]
# [countTagsInLength]: If true, tags will be taken into account in [maxLength].
# [nickNameOtherPrefix]: Prefix to specify you want to nickname another player: [/chatty nickname @player nickname]
nicknames:
permission: "chatty.nickname"
nickOtherPermission: "chatty.nickname.other"
bypassFormatPermission: "chatty.nickname.bypassformat" #Permission to bypass all format checks
maxLength: 32
countTagsInLength: false # Should [maxLength] take tags into account?
nickNameOtherPrefix: '@' # Prefix to specify you wanna nickname another player: [/chatty nickname @player nickname]
countTagsInLength: false
nickNameOtherPrefix: '@'
allowedTags: # Available formats are:
- BOLD # [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
- STRIKETHROUGH # [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
Expand All @@ -86,29 +97,46 @@ join:
leave:
enabled: false

# [enableProxySwitchMessages]: If true, will send a message to all servers when a player switched servers
# [discordSrvChannelID]: The channel ID to send the message to as set in DiscordSRV's config.yml
# [sendProxyMessagesToDiscord]: Enable if you have DiscordSRV on 1 server, Disable if you have it on more than 1 server.
proxy:
enableProxySwitchMessages: false
discordSrvChannelID: "Global"
sendProxyMessagesToDiscord: false

# [enabledChannels]: List of all channelIds to enable pings for ("all" or "*" for all channels)
# [clickToReply]: Whether shift+clicking a ping should reply for a player
# [pingReceiveFormat]: Default format the ping-message is marked with for pinged
# [pingSendFormat]: Default format the ping-message is marked with for sender
# [defaultPingSound]: The default ping sound to play for a player when they are pinged
# [alternativePingSounds]: List of sounds a player can choose between for their pings ("all" for all sounds in minecarft)
ping:
enabledChannels: # channelIds to enable pings for
- "all" # "all" or "*" for all channels
enabledChannels:
- "all"
pingVolume: 1.0
pingPitch: 1.0
pingPrefix: '@'
clickToReply: true
pingReceiveFormat: "<gold><b>" # Default format the ping-message is marked with for pinged
pingSendFormat: "<i>" # Default format the ping-message is marked with for sender
pingReceiveFormat: "<gold><b>"
pingSendFormat: "<i>"
defaultPingSound: "block.amethyst_block.place"
alternativePingSounds: # "all" to let people choose any sound
alternativePingSounds:
- "all"


# [channelType]: The type this channel is, GLOBAL, LOCAL, PRIVATE (Currently does nothing), PERMISSION
# [isDefaultChannel]: Specify if this is the channel players should be put in by default
# [proxy]: Specify if messages sent in this channel should be sent across a proxy network (BungeeCord / Velocity)
# [logToConsole]: Specify if messages in this channel should be sent to the console
# [discordsrv]: Specify if messages sent in this channel should also be sent to Discord via DiscordSRV
# [format]: The format to use for this channel, this is what is added before the message itself
# [channelAliases]: List of aliases for this channel, these can be used to switch to this channel
channels:
global:
channelType: GLOBAL # GLOBAL, LOCAL, PRIVATE (Currently does nothing), PERMISSION
channelType: GLOBAL
isDefaultChannel: true
proxy: true
logToConsole: true
discordsrv: true
format: ":survival: %luckperms_prefix% %chatty_player_displayname%: "
channelAliases:
Expand All @@ -119,6 +147,7 @@ channels:
permission: "chatty.channel.playerhead"
isDefaultChannel: false
proxy: true
logToConsole: true
discordsrv: true
format: "%chatty_player_head% %luckperms_prefix %chatty_player_displayname%: "
channelAliases:
Expand All @@ -128,6 +157,7 @@ channels:
channelType: RADIUS
isDefaultChannel: false
discordsrv: false
logToConsole: true
format: ":survival:%chatty_shift_-4%%chatty_shift_-4%:local: <yellow>%chatty_player_displayname%:<yellow> "
channelRadius: 500
channelAliases:
Expand All @@ -141,6 +171,7 @@ channels:
isStaffChannel: true
discordsrv: false
proxy: true
logToConsole: false
format: ":survival:%chatty_shift_-4%%chatty_shift_-4%:admin:%chatty_player_displayname%:<red> "
channelAliases:
- "a"

0 comments on commit 074e097

Please sign in to comment.