Skip to content

Commit

Permalink
fix: chatty channel casting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 5, 2024
1 parent e497ac8 commit a3a8a84
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChattyChannel, String> {

override fun getExamples() = nativeType.examples
override fun getNativeType(): ArgumentType<String> {
return StringArgumentType.word()
}

override fun getNativeType() = StringArgumentType.greedyString()
override fun <S : Any> listSuggestions(
context: CommandContext<S>,
builder: SuggestionsBuilder
): CompletableFuture<Suggestions> {
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)
}
}


}

0 comments on commit a3a8a84

Please sign in to comment.