Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
9 270
src/main/kotlin/com/kodari/compressedkilltoken/CompressedCommand.kt
package com.kodari.compressedkilltoken
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
import java.time.Duration
class CompressedCommand(
private val plugin: CompressedKilltokenPlugin,
private val tokenService: KilltokenService
) : CommandExecutor, TabCompleter {
override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
if (args.size !in 2..3 || !args[0].equals("killtoken", ignoreCase = true)) {
sender.sendMessage(Component.text("Usage: /$label killtoken [infinite]", NamedTextColor.RED))
return true
}
val infinite = args.size == 3 && args[2].equals("infinite", ignoreCase = true)
if (args.size == 3 && !infinite) {
sender.sendMessage(Component.text("Usage: /$label killtoken [infinite]", NamedTextColor.RED))
return true
}
if (infinite && !sender.isOp) {
sender.sendMessage(Component.text("Only operators can use the infinite option.", NamedTextColor.RED))
return true
}
val player = sender as? Player
if (player == null) {
sender.sendMessage(Component.text("Only players can receive a Compressed Killtoken.", NamedTextColor.RED))
return true
}
if (!infinite) {
val remaining = tokenService.cooldownRemaining(player.uniqueId)
if (remaining != null) {
player.sendMessage(
Component.text(
"You can claim another Compressed Killtoken in ${formatDuration(remaining)}.",
NamedTextColor.YELLOW
)
)
return true
}
}
tokenService.giveToken(player, infinite)
val message = if (infinite) {
"You received a Compressed Killtoken without a cooldown."
} else {
"You received a Compressed Killtoken. You can claim another one in 3 days."
}
player.sendMessage(Component.text(message, NamedTextColor.GREEN))
return true
}
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>
): List<String> = when (args.size) {
1 -> listOf("killtoken").filter { it.startsWith(args[0], ignoreCase = true) }
2 -> if (args[0].equals("killtoken", ignoreCase = true) && sender.isOp) {
listOf("infinite").filter { it.startsWith(args[1], ignoreCase = true) }
} else {
emptyList()
}
else -> emptyList()
}
private fun formatDuration(milliseconds: Long): String {
val duration = Duration.ofMillis(milliseconds)
val days = duration.toDays()
val hours = duration.minusDays(days).toHours()
val minutes = duration.minusDays(days).minusHours(hours).toMinutes()
return when {
days > 0 -> "$days day${if (days == 1L) "" else "s"} ${hours}h"
hours > 0 -> "$hours hour${if (hours == 1L) "" else "s"} ${minutes}m"
else -> "$minutes minute${if (minutes == 1L) "" else "s"}"
}
}
}