diff --git a/README.md b/README.md index 8ff68b4..fb430cc 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ KotlinBukkitKit is an API for Bukkit/SpigotAPI using features Kotlin. DevSrSouza ```kotlin -implementation("tech.carcadex:kotlinbukkitkit-architecture:1.0.0.0") -implementation("tech.carcadex:kotlinbukkitkit-extensions:1.0.0.0") -implementation("tech.carcadex:kotlinbukkitkit-serialization:1.0.0.0") -implementation("tech.carcadex:kotlinbukkitkit-genref:1.0.0.0") -implementation("tech.carcadex:kotlinbukkitkit-messages:1.0.0.0") -implementation("tech.carcadex:kotlinbukkitkit-commands:1.0.0.0") +implementation("tech.carcadex:kotlinbukkitkit-architecture:1.0.0.1") +implementation("tech.carcadex:kotlinbukkitkit-extensions:1.0.0.1") +implementation("tech.carcadex:kotlinbukkitkit-serialization:1.0.0.1") +implementation("tech.carcadex:kotlinbukkitkit-genref:1.0.0.1") +implementation("tech.carcadex:kotlinbukkitkit-messages:1.0.0.1") +implementation("tech.carcadex:kotlinbukkitkit-commands:1.0.0.1") ``` diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt new file mode 100644 index 0000000..ca3854a --- /dev/null +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt @@ -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 + } +} \ No newline at end of file diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/ExecutorContext.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/ExecutorContext.kt index 997e968..756e31f 100644 --- a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/ExecutorContext.kt +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/ExecutorContext.kt @@ -1,6 +1,11 @@ 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.entity.Player import tech.carcadex.kotlinbukkitkit.commands.exceptions.ArgumentNotFoundException import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService @@ -16,4 +21,34 @@ class ExecutorContext(var sender: CommandSender, val args: Array) { throw TypeParseException() } ?: throw ArgumentNotFoundException(index) 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)) } \ No newline at end of file diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/service/MessagesService.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/service/MessagesService.kt index 6793217..209f159 100644 --- a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/service/MessagesService.kt +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/service/MessagesService.kt @@ -11,6 +11,7 @@ object MessagesService { 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-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["#not-a-number"] = { it.sendMessage("${ChatColor.RED}It is not a number") } messages["#wrong-usage"] = { it.sendMessage("${ChatColor.RED}Wrong usage of command") } diff --git a/extensions/src/main/kotlin/tech/carcadex/kotlinbukkitkit/extensions/ExCommandSender.kt b/extensions/src/main/kotlin/tech/carcadex/kotlinbukkitkit/extensions/ExCommandSender.kt new file mode 100644 index 0000000..4e75622 --- /dev/null +++ b/extensions/src/main/kotlin/tech/carcadex/kotlinbukkitkit/extensions/ExCommandSender.kt @@ -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)) \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 685ff43..96a784b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,5 +1,5 @@ -rootProject.name = "kotlinbukkitkit" +rootProject.name = "KotlinBukkitKit" include("architecture") include("messages") include("extensions")