Skip to content

Commit

Permalink
Merge pull request #52 from MineInAbyss/develop
Browse files Browse the repository at this point in the history
Avoid async geary writes
  • Loading branch information
0ffz authored Dec 5, 2023
2 parents 16a1935 + cfd266f commit 9adc3eb
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 295 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.mineinabyss.chatty

import com.mineinabyss.chatty.components.ChannelType
import com.mineinabyss.chatty.components.SpyOnChannels
import com.mineinabyss.chatty.queries.SpyingPlayers
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextColor
import org.bukkit.Bukkit
import org.bukkit.entity.Player

@Serializable
data class ChattyChannel(
val channelType: ChannelType,
val permission: String = "",
val logToConsole: Boolean = true,
val simpleConsoleMessages: Boolean = false,
val proxy: Boolean = false,
val discordsrv: Boolean = true,
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(),
) {
val key by lazy { chatty.config.channels.entries.first { it.value == this }.key }
val messageColor: TextColor
get() = TextColor.fromHexString(_messageColor) ?: NamedTextColor.NAMES.value(_messageColor)
?: NamedTextColor.WHITE


fun getAudience(player: Player): Collection<Audience> {
val onlinePlayers by lazy { Bukkit.getOnlinePlayers() }
val audiences = mutableSetOf<Audience>()

when (channelType) {
ChannelType.GLOBAL -> audiences.addAll(onlinePlayers)
ChannelType.RADIUS -> {
if (channelRadius <= 0) audiences.addAll(onlinePlayers)
else audiences.addAll(player.world.players.filter { p ->
player.location.distanceSquared(p.location) <= (channelRadius * channelRadius)
})
}

ChannelType.PERMISSION -> audiences.addAll(onlinePlayers.filter { p -> p.hasPermission(permission) })
// Intended for Guilds etc., want to consider finding a non-permission way for this
ChannelType.PRIVATE -> audiences.add(player)
}

// Add spying players
val spies = chatty.spyingPlayers.run {
toList { query -> query.player.takeIf { query.spying.channels.contains(key) } }.filterNotNull()
}
audiences.addAll(spies)

return audiences
}
}
261 changes: 139 additions & 122 deletions chatty-paper/src/main/kotlin/com/mineinabyss/chatty/ChattyCommands.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.mineinabyss.chatty

import com.mineinabyss.chatty.components.ChannelType
import com.mineinabyss.idofront.serialization.DurationSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextColor
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes

Expand Down Expand Up @@ -72,26 +69,6 @@ data class ChattyConfig(
val sendProxyMessagesToDiscord: Boolean = true,
)

@Serializable
data class ChattyChannel(
val channelType: ChannelType,
val permission: String = "",
val logToConsole: Boolean = true,
val simpleConsoleMessages: Boolean = false,
val proxy: Boolean = false,
val discordsrv: Boolean = true,
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(),
) {
val messageColor: TextColor
get() = TextColor.fromHexString(_messageColor) ?: NamedTextColor.NAMES.value(_messageColor)
?: NamedTextColor.WHITE
}

@Serializable
data class Ping(
val enabledChannels: List<String> = listOf(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mineinabyss.chatty

import com.mineinabyss.chatty.helpers.DiscordEmoteFixer
import com.mineinabyss.chatty.queries.SpyingPlayers
import com.mineinabyss.idofront.config.IdofrontConfig
import com.mineinabyss.idofront.di.DI
import org.bukkit.Bukkit
Expand All @@ -14,4 +15,5 @@ interface ChattyContext {
val emotefixer: DiscordEmoteFixer
val isPlaceholderApiLoaded: Boolean
val isDiscordSRVLoaded: Boolean
val spyingPlayers: SpyingPlayers
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.mineinabyss.chatty

import com.mineinabyss.chatty.components.ChannelData
import com.mineinabyss.chatty.components.ChattyNickname
import com.mineinabyss.chatty.components.CommandSpy
import com.mineinabyss.chatty.helpers.DiscordEmoteFixer
import com.mineinabyss.chatty.listeners.ChatListener
import com.mineinabyss.chatty.listeners.ChattyProxyListener
import com.mineinabyss.chatty.listeners.DiscordListener
import com.mineinabyss.chatty.listeners.PlayerListener
import com.mineinabyss.chatty.placeholders.PlaceholderAPIHook
import com.mineinabyss.chatty.queries.SpyingPlayers
import com.mineinabyss.geary.autoscan.autoscan
import com.mineinabyss.geary.helpers.componentId
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.idofront.config.config
import com.mineinabyss.idofront.di.DI
Expand All @@ -22,6 +27,10 @@ class ChattyPlugin : JavaPlugin() {
all()
}
}

// register components we'll use async now since they'll error otherwise
componentId<ChattyNickname>()
componentId<ChannelData>()
}

override fun onEnable() {
Expand All @@ -34,7 +43,6 @@ class ChattyPlugin : JavaPlugin() {

ChattyCommands()
listeners(ChatListener(), PlayerListener())

if (chatty.isPlaceholderApiLoaded)
PlaceholderAPIHook().register()

Expand All @@ -51,6 +59,9 @@ class ChattyPlugin : JavaPlugin() {
override val emotefixer: DiscordEmoteFixer by config("emotefixer", dataFolder.toPath(), DiscordEmoteFixer())
override val isPlaceholderApiLoaded: Boolean = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")
override val isDiscordSRVLoaded: Boolean = Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")
override val spyingPlayers: SpyingPlayers = SpyingPlayers().apply {
registerIfNotRegistered()
}
}

DI.add<ChattyContext>(chattyContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package com.mineinabyss.chatty.components

import com.mineinabyss.chatty.ChattyChannel
import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.helpers.getDefaultChat
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.idofront.serialization.UUIDSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bukkit.entity.Player
import java.util.*

@Serializable
@SerialName("chatty:chatty_data")
data class ChannelData(
val channelId: String = getDefaultChat().key,
val lastChannelUsed: String = channelId,
val lastChannelUsedId: String = channelId,
val disablePingSound: Boolean = false,
val pingSound: String? = null,
val lastMessager: @Serializable(UUIDSerializer::class) UUID? = null,
)
) {
val channel: ChattyChannel? get() = chatty.config.channels[channelId]
val lastChannelUsed: ChattyChannel? get() = chatty.config.channels[lastChannelUsedId]

fun withChannelVerified(): ChannelData {
if (channelId !in chatty.config.channels)
return copy(channelId = getDefaultChat().key)
return this
}
}

//val Player.chattyData get() = toGeary().getOrSetPersisting { ChannelData() }

val Player.chattyData get() = toGeary().getOrSetPersisting { ChannelData() }
enum class ChannelType {
GLOBAL,
RADIUS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.combimagnetron.imageloader.Avatar
import com.combimagnetron.imageloader.Image.ColorType
import com.combimagnetron.imageloader.ImageUtils
import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.components.ChannelData
import com.mineinabyss.chatty.components.ChannelType
import com.mineinabyss.chatty.components.chattyData
import com.mineinabyss.chatty.components.chattyNickname
import com.mineinabyss.chatty.placeholders.chattyPlaceholderTags
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
Expand Down Expand Up @@ -45,10 +45,9 @@ fun String.checkForPlayerPings(channelId: String): Player? {
}
}

fun Component.handlePlayerPings(player: Player, pingedPlayer: Player) {
getChannelFromId(player.chattyData.channelId) ?: return
fun Component.handlePlayerPings(player: Player, pingedPlayer: Player, pingedChannelData: ChannelData) {
val ping = chatty.config.ping
val pingSound = pingedPlayer.chattyData.pingSound ?: ping.defaultPingSound
val pingSound = pingedChannelData.pingSound ?: ping.defaultPingSound
val clickToReply =
if (ping.clickToReply) "<insert:@${
player.chattyNickname?.let { MiniMessage.miniMessage().stripTags(it) }
Expand All @@ -61,7 +60,7 @@ fun Component.handlePlayerPings(player: Player, pingedPlayer: Player) {
.build()
)

if (!pingedPlayer.chattyData.disablePingSound)
if (!pingedChannelData.disablePingSound)
pingedPlayer.playSound(pingedPlayer.location, pingSound, SoundCategory.VOICE, ping.pingVolume, ping.pingPitch)
pingedPlayer.sendMessage(pingMessage)

Expand Down Expand Up @@ -113,16 +112,12 @@ fun getDefaultChat() =
?: getGlobalChat()
?: throw IllegalStateException("No Default or Global channel found")

fun getChannelFromId(channelId: String) =
chatty.config.channels.entries.firstOrNull { it.key == channelId }?.value
// TODO change to data.channel
//fun getChannelFromId(channelId: String) =
// chatty.config.channels[channelId]

fun Player.getChannelFromPlayer() =
chatty.config.channels.entries.firstOrNull { it.key == this.chattyData.channelId }?.value

fun Player.verifyPlayerChannel() {
if (chattyData.channelId !in chatty.config.channels)
toGeary().setPersisting(chattyData.copy(channelId = getDefaultChat().key))
}
//fun Player.getChannelFromPlayer() =
// chatty.config.channels.entries.firstOrNull { it.key == this.chattyData.channelId }?.value

fun getAllChannelNames() = chatty.config.channels.keys.toList()

Expand Down Expand Up @@ -194,34 +189,14 @@ fun List<String>.toSentence() = this.joinToString(" ")

fun String.toPlayer() = Bukkit.getPlayer(this)

fun Player.swapChannelCommand(channelId: String) {
val newChannel = getChannelFromId(channelId)
when {
newChannel == null ->
sendFormattedMessage(chatty.messages.channels.noChannelWithName)

newChannel.permission.isNotBlank() && !hasPermission(newChannel.permission) ->
sendFormattedMessage(chatty.messages.channels.missingChannelPermission)

else -> {
toGeary().setPersisting(chattyData.copy(channelId = channelId, lastChannelUsed = channelId))
sendFormattedMessage(chatty.messages.channels.channelChanged)
}
}
}

fun Player.sendFormattedMessage(message: String) =
this.sendMessage(translatePlaceholders(this, message).parseTags(player, true))

fun formattedResult(player: Player, message: Component): Component {
player.verifyPlayerChannel()
val channel = player.getChannelFromPlayer() ?: return message
val channelData = player.toGeary().get<ChannelData>()?.withChannelVerified()
val channel = channelData?.channel ?: return message
val parsedFormat = translatePlaceholders(player, channel.format).parseTags(player, true)
val parsedMessage = Component.text("").color(channel.messageColor).append(message.parseTags(player, false))

return parsedFormat.append(parsedMessage)
}

fun <T> T.copyWithEdit(block: T.() -> T): T {
return block()
}
Loading

0 comments on commit 9adc3eb

Please sign in to comment.