Fix commands, new arguments
This commit is contained in:
redtea committed 2024-03-25 22:45:46 +03:00
1 parent bb68292af0
commit 4c06690f1a
6 files changed
+73 -7

No files matched your search

+6 -6
View File
@@ -3,12 +3,12 @@ KotlinBukkitKit is an API for Bukkit/SpigotAPI using features Kotlin. DevSrSouza
```kotlin ```kotlin
implementation("tech.carcadex:kotlinbukkitkit-architecture:1.0.0.0") implementation("tech.carcadex:kotlinbukkitkit-architecture:1.0.0.1")
implementation("tech.carcadex:kotlinbukkitkit-extensions:1.0.0.0") implementation("tech.carcadex:kotlinbukkitkit-extensions:1.0.0.1")
implementation("tech.carcadex:kotlinbukkitkit-serialization:1.0.0.0") implementation("tech.carcadex:kotlinbukkitkit-serialization:1.0.0.1")
implementation("tech.carcadex:kotlinbukkitkit-genref:1.0.0.0") implementation("tech.carcadex:kotlinbukkitkit-genref:1.0.0.1")
implementation("tech.carcadex:kotlinbukkitkit-messages:1.0.0.0") implementation("tech.carcadex:kotlinbukkitkit-messages:1.0.0.1")
implementation("tech.carcadex:kotlinbukkitkit-commands:1.0.0.0") implementation("tech.carcadex:kotlinbukkitkit-commands:1.0.0.1")
``` ```
@@ -0,0 +1,21 @@
package tech.carcadex.kotlinbukkitkit.commands
import tech.carcadex.kotlinbukkitkit.architecture.KotlinPlugin
import tech.carcadex.kotlinbukkitkit.commands.dsl.builder.simpleCommand
fun KotlinPlugin.onEnable() {
simpleCommand(
name = "test",
permission = "test.use",
tabComplete = listOf("subcmd1", "subcmd2")
) {
sender.sendMessage("test")
val arg1 = argument(0) //getting 1 argument of command as nullable string
//also you can use stringOrNull
val arg2 = string(1) //getting 1 argument of command as string
val arg3 = integer(2) //getting 2 argument of command as integer
//also you can use integerOrNull
val arg4 = double(3) //getting 3 argument of command as integer
//also you can use doubleOrNull
}
}
@@ -1,6 +1,11 @@
package tech.carcadex.kotlinbukkitkit.commands.dsl package tech.carcadex.kotlinbukkitkit.commands.dsl
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.OfflinePlayer
import org.bukkit.World
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import tech.carcadex.kotlinbukkitkit.commands.exceptions.ArgumentNotFoundException import tech.carcadex.kotlinbukkitkit.commands.exceptions.ArgumentNotFoundException
import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException
import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService
@@ -16,4 +21,34 @@ class ExecutorContext(var sender: CommandSender, val args: Array<String>) {
throw TypeParseException() throw TypeParseException()
} ?: throw ArgumentNotFoundException(index) } ?: throw ArgumentNotFoundException(index)
fun integerOrNull(index: Int): Int? = argument(index)?.toIntOrNull() fun integerOrNull(index: Int): Int? = argument(index)?.toIntOrNull()
fun double(index: Int): Double = try {
argument(index)?.toDouble()
} catch (e: NumberFormatException) {
MessagesService.byTag("#not-a-number")(sender)
throw TypeParseException()
} ?: throw ArgumentNotFoundException(index)
fun doubleOrNull(index: Int): Double? = argument(index)?.toDoubleOrNull()
fun materialOrNull(index: Int): Material? = Material.matchMaterial(string(index))
fun material(index: Int): Material =
materialOrNull(index) ?: run {
MessagesService.byTag("#no-such-material")(sender)
throw TypeParseException()
}
fun worldOrNull(index: Int): World? = Bukkit.getWorld(string(index))
fun world(index: Int): World =
worldOrNull(index) ?: run {
MessagesService.byTag("#no-such-world")(sender)
throw TypeParseException()
}
fun playerOrNull(index: Int): Player? = Bukkit.getPlayer(string(index))
fun player(index: Int): World =
worldOrNull(index) ?: run {
MessagesService.byTag("#no-such-player")(sender)
throw TypeParseException()
}
fun offlinePlayer(index: Int): OfflinePlayer = Bukkit.getOfflinePlayer(string(index))
} }
@@ -11,6 +11,7 @@ object MessagesService {
messages["#no-such-player"] = { it.sendMessage("${ChatColor.RED}No such player was found") } messages["#no-such-player"] = { it.sendMessage("${ChatColor.RED}No such player was found") }
messages["#no-such-world"] = { it.sendMessage("${ChatColor.RED}No such world was found") } messages["#no-such-world"] = { it.sendMessage("${ChatColor.RED}No such world was found") }
messages["#no-such-material"] = { it.sendMessage("${ChatColor.RED}No such material was found") } messages["#no-such-material"] = { it.sendMessage("${ChatColor.RED}No such material was found") }
messages["#no-such-world"] = { it.sendMessage("${ChatColor.RED}No such world was found") }
messages["#no-such-enum"] = { it.sendMessage("${ChatColor.RED}No such value for this enum was found") } messages["#no-such-enum"] = { it.sendMessage("${ChatColor.RED}No such value for this enum was found") }
messages["#not-a-number"] = { it.sendMessage("${ChatColor.RED}It is not a number") } messages["#not-a-number"] = { it.sendMessage("${ChatColor.RED}It is not a number") }
messages["#wrong-usage"] = { it.sendMessage("${ChatColor.RED}Wrong usage of command") } messages["#wrong-usage"] = { it.sendMessage("${ChatColor.RED}Wrong usage of command") }
@@ -0,0 +1,9 @@
package tech.carcadex.kotlinbukkitkit.extensions
import org.bukkit.command.CommandSender
public fun CommandSender.send(message: String) {
sendMessage(message)
}
public fun CommandSender.hasNotPerm(permission: String) = (!hasPermission(permission))
+1 -1
View File
@@ -1,5 +1,5 @@
rootProject.name = "kotlinbukkitkit" rootProject.name = "KotlinBukkitKit"
include("architecture") include("architecture")
include("messages") include("messages")
include("extensions") include("extensions")