Skip to content

Commit

Permalink
refactor: cleanup commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 9, 2024
1 parent fb28fbd commit f2f4ea6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ object ChattyBrigadierCommands {
}
}
"sound" {
playerExecutes {
player.error("Missing sound-argument...")
}
val soundName by StringArgumentType.word().suggests {
suggest(
chatty.config.ping.alternativePingSounds
Expand All @@ -94,6 +97,9 @@ object ChattyBrigadierCommands {
}
}
"channel" {
playerExecutes {
player.error("Missing channel-argument...")
}
val channel by ChattyChannelArgument().suggests {
suggest(chatty.config.channels.entries.asSequence()
.filter { it.value.channelType != ChannelType.CUSTOM }
Expand All @@ -120,6 +126,9 @@ object ChattyBrigadierCommands {
}
}
"spy" {
playerExecutes {
player.error("Missing channel-argument...")
}
val channel by ChattyChannelArgument().suggests {
suggest(chatty.config.channels.entries
.filter {
Expand Down Expand Up @@ -156,59 +165,55 @@ object ChattyBrigadierCommands {
}
}
"nickname" {
val nickMessage = chatty.messages.nicknames
playerExecutes {
player.chattyNickname = null
sender.sendFormattedMessage(nickMessage.selfEmpty)
}

val nickname by StringArgumentType.greedyString().suggests {
(context.source.executor as? Player)?.chattyNickname?.let {
suggestFiltering(it)
}
}
playerExecutes {
val nickname = nickname()
val nickMessage = chatty.messages.nicknames
val bypassFormatPerm = sender.hasPermission(ChattyPermissions.BYPASS_TAG_PERM)

fun String.nicknameTooLong(): Boolean {
return when (chatty.config.nicknames.countTagsInLength) {
true -> this.length > chatty.config.nicknames.maxLength
false -> this.stripTags().length >= chatty.config.nicknames.maxLength
}
}

fun handleNickname(player: Player, applyTo: Player, nick: String) {
when {
!sender.hasPermission(ChattyPermissions.NICKNAME) ->
sender.sendFormattedMessage(nickMessage.selfDenied)

player.uniqueId != (sender as? Player)?.uniqueId -> {
when {
!sender.hasPermission(ChattyPermissions.NICKNAME_OTHERS) ->
sender.sendFormattedMessage(nickMessage.otherDenied, player)

nickname.isNullOrEmpty() -> {
player.chattyNickname = null
player.sendFormattedMessage(nickMessage.selfEmpty)
sender.sendFormattedMessage(nickMessage.otherEmpty, player)
}

!bypassFormatPerm && !nickname.verifyNickLength() ->
sender.sendFormattedMessage(nickMessage.tooLong)

nickname.isNotEmpty() -> {
player.chattyNickname = nickname
sender.sendFormattedMessage(nickMessage.otherSuccess, player)
}
}
!player.hasPermission(ChattyPermissions.NICKNAME) ->
player.sendFormattedMessage(nickMessage.selfDenied)

player.uniqueId != applyTo.uniqueId && !player.hasPermission(ChattyPermissions.NICKNAME_OTHERS) ->
player.sendFormattedMessage(nickMessage.otherDenied, applyTo)

player.uniqueId != applyTo.uniqueId && nick.isEmpty() -> {
applyTo.chattyNickname = null
applyTo.sendFormattedMessage(nickMessage.selfEmpty)
player.sendFormattedMessage(nickMessage.otherEmpty, applyTo)
}

else -> {
when {
nickname.isNullOrEmpty() -> {
player.chattyNickname = null
sender.sendFormattedMessage(nickMessage.selfEmpty)
}

!bypassFormatPerm && !nickname.verifyNickLength() ->
sender.sendFormattedMessage(nickMessage.tooLong)

else -> {
(sender as? Player)?.chattyNickname = nickname
sender.sendFormattedMessage(nickMessage.selfSuccess)
}
}
!player.hasPermission(ChattyPermissions.BYPASS_TAG_PERM) && nick.nicknameTooLong() ->
player.sendFormattedMessage(nickMessage.tooLong)

nick.isNotEmpty() -> {
applyTo.chattyNickname = nick
if (player.uniqueId != applyTo.uniqueId) player.sendFormattedMessage(nickMessage.otherSuccess, player)
else player.sendFormattedMessage(nickMessage.selfSuccess)
}
}
}

playerExecutes {
val nickname = nickname() ?: return@playerExecutes
val applyTo = nickname.substringBefore(" ").toPlayer()?.takeIf { it.uniqueId != player.uniqueId } ?: player
handleNickname(player, applyTo, nickname.removePrefix(applyTo.name.takeIf { applyTo.uniqueId != player.uniqueId } ?: ""))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import com.mineinabyss.chatty.chatty
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.entity.Player
fun String.stripTags() = MiniMessage.miniMessage().stripTags(this)
fun String.verifyNickLength(): Boolean {
return when (chatty.config.nicknames.countTagsInLength) {
true -> this.length <= chatty.config.nicknames.maxLength
false -> this.stripTags().length <= chatty.config.nicknames.maxLength
}
}


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

0 comments on commit f2f4ea6

Please sign in to comment.