From a3a8a849878294cb7eaeb52c27f07da3e0469c20 Mon Sep 17 00:00:00 2001 From: Boy Date: Thu, 5 Dec 2024 22:37:23 +0100 Subject: [PATCH] fix: chatty channel casting issue --- .../commands/ChattyBrigadierCommands.kt | 10 +---- .../chatty/commands/ChattyChannelArgument.kt | 44 +++++++++++++++++-- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyBrigadierCommands.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyBrigadierCommands.kt index 1bc7a05..ed04eb8 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyBrigadierCommands.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyBrigadierCommands.kt @@ -96,15 +96,7 @@ object ChattyBrigadierCommands { } "channel" { requiresPermission("") - playerExecutes(ChattyChannelArgument().suggests { - suggest(chatty.config.channels.entries.asSequence() - .filter { it.value.channelType != ChannelType.CUSTOM } - .filter { it.value.permission.isEmpty() || context.source.sender.hasPermission(it.value.permission) } - .sortedBy { - it.key in setOf(defaultChannel().key, radiusChannel()?.key, adminChannel()?.key).filterNotNull() - }.map { it.key }.toList() - ) - }.named("channel")) { channel -> + playerExecutes(ChattyChannelArgument()) { channel -> if (channel.channelType != ChannelType.CUSTOM) swapChannel(player, channel) } } diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyChannelArgument.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyChannelArgument.kt index f5dd4f1..ac5638e 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyChannelArgument.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/commands/ChattyChannelArgument.kt @@ -2,16 +2,54 @@ package com.mineinabyss.chatty.commands import com.mineinabyss.chatty.ChattyChannel import com.mineinabyss.chatty.chatty +import com.mineinabyss.chatty.components.ChannelType +import com.mineinabyss.chatty.helpers.adminChannel +import com.mineinabyss.chatty.helpers.defaultChannel +import com.mineinabyss.chatty.helpers.radiusChannel +import com.mojang.brigadier.arguments.ArgumentType import com.mojang.brigadier.arguments.StringArgumentType +import com.mojang.brigadier.context.CommandContext +import com.mojang.brigadier.exceptions.CommandSyntaxException +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType +import com.mojang.brigadier.suggestion.Suggestions +import com.mojang.brigadier.suggestion.SuggestionsBuilder +import io.papermc.paper.command.brigadier.MessageComponentSerializer import io.papermc.paper.command.brigadier.argument.CustomArgumentType +import net.kyori.adventure.text.Component +import net.kyori.adventure.text.format.NamedTextColor +import java.util.concurrent.CompletableFuture class ChattyChannelArgument : CustomArgumentType.Converted { - override fun getExamples() = nativeType.examples + override fun getNativeType(): ArgumentType { + return StringArgumentType.word() + } - override fun getNativeType() = StringArgumentType.greedyString() + override fun listSuggestions( + context: CommandContext, + builder: SuggestionsBuilder + ): CompletableFuture { + val messageComponent = MessageComponentSerializer.message().serialize(Component.text("look at this cool green tooltip!", NamedTextColor.GREEN)) + chatty.config.channels.entries.asSequence() + .filter { it.value.channelType != ChannelType.CUSTOM } + .filter { it.value.permission.isEmpty() } + .sortedBy { + it.key in setOf(defaultChannel().key, radiusChannel()?.key, adminChannel()?.key).filterNotNull() + }.forEach { + builder.suggest(it.key, messageComponent) + } - override fun convert(nativeType: String) = chatty.config.channels[nativeType]!! + return builder.buildFuture() + } + + override fun convert(nativeType: String): ChattyChannel { + return runCatching { + chatty.config.channels[nativeType]!! + }.getOrElse { + val message = MessageComponentSerializer.message().serialize(Component.text("Invalid channel $nativeType", NamedTextColor.RED)) + throw CommandSyntaxException(SimpleCommandExceptionType(message), message) + } + } } \ No newline at end of file