Skip to content

Commit

Permalink
refactor: code cleanup & method renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Feb 2, 2024
1 parent 5339c2d commit cbb9635
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import com.mineinabyss.idofront.commands.extensions.actions.ensureSenderIsPlayer
import com.mineinabyss.idofront.commands.extensions.actions.playerAction
import com.mineinabyss.idofront.entities.toPlayer
import com.mineinabyss.idofront.events.call
import com.mineinabyss.idofront.messaging.broadcast
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import io.papermc.paper.event.player.AsyncChatDecorateEvent
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -53,7 +51,7 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
playerAction {
val gearyPlayer = player.toGeary()
val oldData = gearyPlayer.get<ChannelData>() ?: return@playerAction
if (soundName in getAlternativePingSounds) {
if (soundName in alternativePingSounds) {
gearyPlayer.setPersisting(oldData.copy(pingSound = soundName))
player.sendFormattedMessage(chatty.messages.ping.changedPingSound)
} else player.sendFormattedMessage(chatty.messages.ping.invalidPingSound)
Expand Down Expand Up @@ -83,7 +81,7 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
}

arguments.first().startsWith(chatty.config.nicknames.nickNameOtherPrefix) -> {
val otherPlayer = arguments.getPlayerToNick()
val otherPlayer = arguments.playerToNick()
val otherNick = nick.removePlayerToNickFromString()

when {
Expand Down Expand Up @@ -173,19 +171,19 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
("global" / "g") {
ensureSenderIsPlayer()
action {
(sender as? Player)?.shortcutCommand(getGlobalChat(), arguments)
(sender as? Player)?.shortcutCommand(globalChannel(), arguments)
}
}
("local" / "l") {
ensureSenderIsPlayer()
action {
(sender as? Player)?.shortcutCommand(getRadiusChannel(), arguments)
(sender as? Player)?.shortcutCommand(radiusChannel(), arguments)
}
}
("admin" / "a") {
ensureSenderIsPlayer()
action {
(sender as? Player)?.shortcutCommand(getAdminChannel(), arguments)
(sender as? Player)?.shortcutCommand(adminChannel(), arguments)
}
}
("message" / "msg")(desc = "Private message another player") {
Expand Down Expand Up @@ -241,7 +239,7 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {

3 -> when {
args[0] == "ping" && args[1] == "sound" ->
getAlternativePingSounds.filter { s -> s.startsWith(args[2], true) }
alternativePingSounds.filter { s -> s.startsWith(args[2], true) }

args[0] == "nickname" -> args[1].drop(1).takeIf { args[1].startsWith(otherPrefix) && it.isNotEmpty() }?.toPlayer()?.let { player ->
listOf(player.chattyNickname ?: player.name).filter { s -> s.startsWith(args[2], true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.mineinabyss.chatty.components

import com.mineinabyss.chatty.ChattyChannel
import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.helpers.getDefaultChat
import com.mineinabyss.chatty.helpers.defaultChannel
import com.mineinabyss.idofront.serialization.UUIDSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -11,7 +11,7 @@ import java.util.*
@Serializable
@SerialName("chatty:chatty_data")
data class ChannelData(
val channelId: String = getDefaultChat().key,
val channelId: String = defaultChannel().key,
val lastChannelUsedId: String = channelId,
val disablePingSound: Boolean = false,
val pingSound: String? = null,
Expand All @@ -22,7 +22,7 @@ data class ChannelData(

fun withChannelVerified(): ChannelData {
if (channelId !in chatty.config.channels)
return copy(channelId = getDefaultChat().key)
return copy(channelId = defaultChannel().key)
return this
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
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.ChattyConfig
import com.mineinabyss.chatty.ChattyConfig.Chat.FilterFormat
import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.components.ChannelData
import com.mineinabyss.chatty.ChattyConfig.Chat.*
import com.mineinabyss.chatty.ChattyMessages
import com.mineinabyss.chatty.components.ChannelType
import com.mineinabyss.chatty.components.chattyNickname
import com.mineinabyss.chatty.placeholders.chattyPlaceholderTags
import com.mineinabyss.chatty.tags.ChattyTags
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.idofront.messaging.warn
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.TextComponent
import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.event.HoverEvent
Expand All @@ -31,28 +23,23 @@ import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.Sound
import org.bukkit.SoundCategory
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.profile.PlayerTextures.SkinModel
import java.util.regex.Pattern

val getAlternativePingSounds: List<String> =
val alternativePingSounds: List<String> =
chatty.config.ping.let { ping -> if ("*" in ping.alternativePingSounds || "all" in ping.alternativePingSounds)
Sound.entries.map { it.key.toString() }.toList() else ping.alternativePingSounds }

val getPingEnabledChannels: List<String> =
chatty.config.ping.let { ping -> if ("*" in ping.enabledChannels || "all" in ping.enabledChannels) getAllChannelNames() else ping.enabledChannels }
val pingEnabledChannels: List<String> =
chatty.config.ping.let { ping -> if ("*" in ping.enabledChannels || "all" in ping.enabledChannels) channelNames() else ping.enabledChannels }

fun String.checkForPlayerPings(channelId: String): Player? {
val ping = chatty.config.ping
if (channelId !in getPingEnabledChannels || ping.pingPrefix.isEmpty() || ping.pingPrefix !in this) return null
if (channelId !in pingEnabledChannels || ping.pingPrefix.isEmpty() || ping.pingPrefix !in this) return null
val pingedName = this.substringAfter(ping.pingPrefix).substringBefore(" ")
return Bukkit.getOnlinePlayers().firstOrNull { it.name == pingedName }
}
Expand Down Expand Up @@ -87,33 +74,28 @@ fun Component.parseTags(player: Player? = null, ignorePermissions: Boolean = fal

fun Component.removeTrailingSpaces() = this.replaceText(TextReplacementConfig.builder().match(" +\$").replacement("").build())

fun getGlobalChat() =
fun globalChannel() =
chatty.config.channels.entries.firstOrNull { it.value.channelType == ChannelType.GLOBAL }

fun getRadiusChannel() =
fun radiusChannel() =
chatty.config.channels.entries.firstOrNull { it.value.channelType == ChannelType.RADIUS }

fun getAdminChannel() =
fun adminChannel() =
chatty.config.channels.entries.firstOrNull { it.value.isStaffChannel }

fun getDefaultChat() =
fun defaultChannel() =
chatty.config.channels.entries.firstOrNull { it.value.isDefaultChannel }
?: getGlobalChat()
?: globalChannel()
?: throw IllegalStateException("No Default or Global channel found")

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

fun translatePlaceholders(player: Player?, message: String) = if (chatty.isPlaceholderApiLoaded) PlaceholderAPI.setPlaceholders(player, message) else message

fun String.fixSerializedTags(): String = this.replaceAll("\\\\(?!u)(?!\")(?!:)", "")

fun String.replaceAll(regex: String, replacement: String): String = Pattern.compile(regex).matcher(this).replaceAll(replacement)

fun String.fixLegacy(): Component {
return if ("§" in this) legacy.deserialize(this)
else this.fixSerializedTags().miniMsg()
}

fun List<String>.toSentence() = this.joinToString(" ")

fun String.toPlayer() = Bukkit.getPlayer(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mineinabyss.chatty.helpers

import com.mineinabyss.chatty.chatty
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.Bukkit
import org.bukkit.entity.Player
fun String.stripTags() = MiniMessage.miniMessage().stripTags(this)
fun String.verifyNickLength(): Boolean {
Expand All @@ -12,7 +11,7 @@ fun String.verifyNickLength(): Boolean {
}
}

fun List<String>.getPlayerToNick(): Player? =
fun List<String>.playerToNick(): Player? =
this.first().replace(chatty.config.nicknames.nickNameOtherPrefix.toString(), "").toPlayer()

fun String.removePlayerToNickFromString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.entity.Player

val legacy = LegacyComponentSerializer.builder().useUnusualXRepeatedCharacterHexFormat().build()
fun GenericChattyDecorateEvent(player: Player, msg: Component) =
AsyncChatDecorateEvent(true, player, msg, msg)
fun GenericChattyChatEvent(player: Player, msg: Component) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.bukkit.profile.PlayerTextures
val playerHeadMapCache = mutableMapOf<OfflinePlayer, Component>()
fun OfflinePlayer.translatePlayerHeadComponent(): Component {
if (this !in playerHeadMapCache || playerHeadMapCache[this]!!.font() != Key.key(chatty.config.playerHeadFont)) {
playerHeadMapCache[this] = runCatching { getPlayerHeadTexture(ascent = -5) }.getOrDefault(Component.empty())
playerHeadMapCache[this] = runCatching { playerHeadTexture(ascent = -5) }.getOrDefault(Component.empty())
}
return playerHeadMapCache[this] ?: Component.empty()
}
Expand All @@ -26,12 +26,12 @@ fun Player.refreshSkinInCaches() {
}
fun OfflinePlayer.translateFullPlayerSkinComponent(): Component {
if (this !in playerBodyMapCache || playerBodyMapCache[this]!!.font() != Key.key(chatty.config.playerHeadFont)) {
playerBodyMapCache[this] = runCatching { getFullPlayerBodyTexture(ascent = -5) }.getOrDefault(Component.empty())
playerBodyMapCache[this] = runCatching { fullPlayerBodyTexture(ascent = -5) }.getOrDefault(Component.empty())
}
return playerBodyMapCache[this] ?: Component.empty()
}

fun OfflinePlayer.getPlayerHeadTexture(
fun OfflinePlayer.playerHeadTexture(
scale: Int = 1,
ascent: Int = 0,
colorType: Image.ColorType = Image.ColorType.MINIMESSAGE,
Expand All @@ -41,7 +41,7 @@ fun OfflinePlayer.getPlayerHeadTexture(
return "<font:$font>${ImageUtils.generateStringFromImage(image, colorType, ascent)}</font>".miniMsg()
}

fun OfflinePlayer.getFullPlayerBodyTexture(
fun OfflinePlayer.fullPlayerBodyTexture(
scale: Int = 1,
ascent: Int = 0,
colorType: Image.ColorType = Image.ColorType.MINIMESSAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.components.ChannelData
import com.mineinabyss.chatty.components.SpyOnChannels
import com.mineinabyss.chatty.components.chattyNickname
import com.mineinabyss.chatty.helpers.getAllChannelNames
import com.mineinabyss.chatty.helpers.channelNames
import com.mineinabyss.chatty.helpers.translateFullPlayerSkinComponent
import com.mineinabyss.chatty.helpers.translatePlayerHeadComponent
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
Expand All @@ -22,7 +22,7 @@ fun chattyPlaceholders(player: Player?, string: String? = null): Map<String, Str
val shift = string?.substringAfter("shift_", "0")?.toIntOrNull() ?: 0

return mapOf(
"all_channels" to getAllChannelNames().joinToString(", "),
"all_channels" to channelNames().joinToString(", "),
"player_available_channels" to chatty.config.channels.values.filter {
player?.hasPermission(it.permission) ?: false
}.joinToString(", ") { it.key },
Expand Down

0 comments on commit cbb9635

Please sign in to comment.