From 5339c2d082d1d14ecdb056791f34892fdda96541 Mon Sep 17 00:00:00 2001 From: Boy Date: Fri, 2 Feb 2024 19:22:40 +0100 Subject: [PATCH] feat: add nickname tabcompletion & player-suggestion --- .../com/mineinabyss/chatty/ChattyCommands.kt | 37 ++++++++++++------- .../chatty/helpers/NicknameHelpers.kt | 5 +-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/ChattyCommands.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/ChattyCommands.kt index 5d359da..aa3f04f 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/ChattyCommands.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/ChattyCommands.kt @@ -109,7 +109,7 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter { } } - else -> { + else -> { if (!bypassFormatPerm && !nick.verifyNickLength()) { sender.sendFormattedMessage(nickMessage.tooLong) } else { @@ -229,19 +229,23 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter { 2 -> when (args[0]) { "ping" -> listOf("toggle", "sound").filter { s -> s.startsWith(args[1]) } - "spy" -> - chatty.config.channels.entries.filter { s -> - s.key.startsWith(args[1], true) && s.value.channelType != ChannelType.GLOBAL - }.map { it.key } + "spy" -> chatty.config.channels.entries.filter { s -> + s.key.startsWith(args[1], true) && s.value.channelType != ChannelType.GLOBAL + }.map { it.key } + "nickname" -> (onlinePlayers.takeIf { args[1].startsWith(otherPrefix) }?.map { otherPrefix + it } + ?: (sender as? Player)?.let { player -> listOf(player.chattyNickname ?: player.name) } + ?: emptyList()).filter { s -> s.startsWith(args[1], true) } else -> emptyList() } 3 -> when { - args[1] == "sound" -> getAlternativePingSounds.filter { s -> s.startsWith(args[2], true) } - args[1].startsWith(otherPrefix) -> onlinePlayers.filter { s -> - s.replace(otherPrefix.toString(), "").startsWith(args[2], true) - } + args[0] == "ping" && args[1] == "sound" -> + getAlternativePingSounds.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) } + } ?: emptyList() else -> emptyList() } @@ -249,11 +253,13 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter { else -> emptyList() } } + "message", "msg" -> when (args.size) { 0, 1 -> onlinePlayers.filter { s -> s.startsWith(args[0], true) }.take(25) else -> emptyList() } + else -> emptyList() } } @@ -332,7 +338,6 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter { } - companion object { private fun CommandSender.sendFormattedMessage(message: String, optionalPlayer: Player? = null) = (optionalPlayer ?: this as? Player)?.let { player -> @@ -340,11 +345,15 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter { } private fun Player.sendFormattedPrivateMessage(messageFormat: String, message: String, receiver: Player) = - this.sendMessage(Component.textOfChildren( - translatePlaceholders(receiver, messageFormat).miniMsg(receiver.buildTagResolver(true)), - message.miniMsg(receiver.buildTagResolver(true))) + this.sendMessage( + Component.textOfChildren( + translatePlaceholders(receiver, messageFormat).miniMsg(receiver.buildTagResolver(true)), + message.miniMsg(receiver.buildTagResolver(true)) + ) ) - private fun CommandSender.sendConsoleMessage(message: String) = this.sendMessage(message.miniMsg().parseTags(null, true)) + + private fun CommandSender.sendConsoleMessage(message: String) = + this.sendMessage(message.miniMsg().parseTags(null, true)) private fun List.removeFirstArgumentOfStringList(): String = this.filter { it != this.first() }.toSentence() diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/NicknameHelpers.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/NicknameHelpers.kt index ed277ba..ed3f89b 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/NicknameHelpers.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/NicknameHelpers.kt @@ -12,9 +12,8 @@ fun String.verifyNickLength(): Boolean { } } -fun List.getPlayerToNick(): Player? = Bukkit.getPlayer( - this.first().replace(chatty.config.nicknames.nickNameOtherPrefix.toString(), "") -) +fun List.getPlayerToNick(): Player? = + this.first().replace(chatty.config.nicknames.nickNameOtherPrefix.toString(), "").toPlayer() fun String.removePlayerToNickFromString(): String = this.split(" ").filter { it != this.split(" ").first() }.toSentence()