diff --git a/build.gradle.kts b/build.gradle.kts index 5e6f6ad..878fb99 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.9.23" + kotlin("jvm") version "1.9.0" id("io.codearte.nexus-staging") version "0.30.0" } diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt index c573e32..39d4d9f 100644 --- a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/Test.kt @@ -1,12 +1,10 @@ package tech.carcadex.kotlinbukkitkit.commands -import tech.carcadex.kotlinbukkitkit.architecture.KotlinPlugin +import org.bukkit.plugin.Plugin import tech.carcadex.kotlinbukkitkit.commands.dsl.builder.simpleCommand -fun KotlinPlugin.start() { - simpleCommand("test", permission = "test.use") { - val arg1 = string(0) - val arg2 = integer(1) - sender.sendMessage(arg1) +fun Plugin.cmd() { + simpleCommand("test") { + player.sendMessage("test") } -} \ No newline at end of file +} diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/DSLCommandArguments.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/DSLCommandArguments.kt new file mode 100644 index 0000000..524e302 --- /dev/null +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/dsl/DSLCommandArguments.kt @@ -0,0 +1,50 @@ +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.entity.Player +import tech.carcadex.kotlinbukkitkit.commands.exceptions.ArgumentNotFoundException +import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException +import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService + +fun ExecutorContext.string(index: Int): String = argument(index) ?: throw ArgumentNotFoundException(index) +fun ExecutorContext.stringOrNull(index: Int): String? = argument(index) +fun ExecutorContext.integer(index: Int): Int = try { + argument(index)?.toInt() +} catch (e: NumberFormatException) { + MessagesService.byTag("#not-a-number")(sender) + throw TypeParseException() +} ?: throw ArgumentNotFoundException(index) +fun ExecutorContext.integerOrNull(index: Int): Int? = argument(index)?.toIntOrNull() + +fun ExecutorContext.double(index: Int): Double = try { + argument(index)?.toDouble() +} catch (e: NumberFormatException) { + MessagesService.byTag("#not-a-number")(sender) + throw TypeParseException() +} ?: throw ArgumentNotFoundException(index) +fun ExecutorContext.doubleOrNull(index: Int): Double? = argument(index)?.toDoubleOrNull() + +fun ExecutorContext.materialOrNull(index: Int): Material? = Material.matchMaterial(string(index)) +fun ExecutorContext.material(index: Int): Material = + materialOrNull(index) ?: run { + MessagesService.byTag("#no-such-material")(sender) + throw TypeParseException() + } +fun ExecutorContext.worldOrNull(index: Int): World? = Bukkit.getWorld(argument(index) ?: "") +fun ExecutorContext.world(index: Int): World = + worldOrNull(index) ?: run { + MessagesService.byTag("#no-such-world")(sender) + throw TypeParseException() + } + +fun ExecutorContext.playerOrNull(index: Int): Player? = Bukkit.getPlayer(argument(index) ?: "") +fun ExecutorContext.player(index: Int): Player = + playerOrNull(index) ?: run { + MessagesService.byTag("#no-such-player")(sender) + throw TypeParseException() + } + +fun ExecutorContext.offlinePlayer(index: Int): OfflinePlayer = Bukkit.getOfflinePlayer(argument(index) ?: "") \ 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 ed76bf0..cf9648f 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 @@ -7,48 +7,19 @@ 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.NotPlayerException import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService -class ExecutorContext(var sender: CommandSender, val args: Array) { +class ExecutorContext(val sender: CommandSender, val args: Array) { + val player: Player + get() { + if (sender !is Player) { + MessagesService.byTag("#for-player-only")(sender) + throw NotPlayerException() + } + return sender + } + fun argument(index: Int): String? = if (args.size <= index) null else args[index] - fun string(index: Int): String = argument(index) ?: throw ArgumentNotFoundException(index) - fun stringOrNull(index: Int): String? = argument(index) - fun integer(index: Int): Int = try { - argument(index)?.toInt() - } catch (e: NumberFormatException) { - MessagesService.byTag("#not-a-number")(sender) - 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(argument(index) ?: "") - fun world(index: Int): World = - worldOrNull(index) ?: run { - MessagesService.byTag("#no-such-world")(sender) - throw TypeParseException() - } - - fun playerOrNull(index: Int): Player? = Bukkit.getPlayer(argument(index) ?: "") - fun player(index: Int): World = - worldOrNull(index) ?: run { - MessagesService.byTag("#no-such-player")(sender) - throw TypeParseException() - } - - fun offlinePlayer(index: Int): OfflinePlayer = Bukkit.getOfflinePlayer(argument(index) ?: "") } \ No newline at end of file diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/exceptions/NotPlayerException.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/exceptions/NotPlayerException.kt new file mode 100644 index 0000000..28d810b --- /dev/null +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/exceptions/NotPlayerException.kt @@ -0,0 +1,6 @@ +package tech.carcadex.kotlinbukkitkit.commands.exceptions + +import java.lang.RuntimeException + +class NotPlayerException : RuntimeException() { +} \ No newline at end of file diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/manager/CommandManagerImpl.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/manager/CommandManagerImpl.kt index f6c4782..5d1c26d 100644 --- a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/manager/CommandManagerImpl.kt +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/manager/CommandManagerImpl.kt @@ -4,6 +4,7 @@ import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.command.TabCompleter import org.bukkit.plugin.Plugin +import tech.carcadex.kotlinbukkitkit.commands.exceptions.NotPlayerException import tech.carcadex.kotlinbukkitkit.commands.reflection.CommandParams import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser.paramsFromAnnotations @@ -96,6 +97,7 @@ class CommandManagerImpl(private val plugin: Plugin) : CommandManager { ) } catch (ignored: TypeParseException) { + } catch (ignored: NotPlayerException) { } catch (e: IllegalArgumentException) { MessagesService.byTag("#wrong-usage")(sender) } diff --git a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/reflection/service/SubCommandsService.kt b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/reflection/service/SubCommandsService.kt index 25d78dd..da40c61 100644 --- a/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/reflection/service/SubCommandsService.kt +++ b/commands/src/main/kotlin/tech/carcadex/kotlinbukkitkit/commands/reflection/service/SubCommandsService.kt @@ -2,6 +2,7 @@ package tech.carcadex.kotlinbukkitkit.commands.reflection.service import org.bukkit.command.CommandSender import org.bukkit.command.TabCompleter +import tech.carcadex.kotlinbukkitkit.commands.exceptions.NotPlayerException import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService import tech.carcadex.kotlinbukkitkit.commands.service.TabCompleteService import tech.carcadex.kotlinbukkitkit.commands.reflection.CommandParams @@ -77,6 +78,8 @@ object SubCommandsService { true } catch (ignored: TypeParseException) { false + } catch (ignored: NotPlayerException) { + false } catch (e: IllegalArgumentException) { MessagesService.byTag("#wrong-usage")(sender) false 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..845e5b7 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 @@ -8,6 +8,7 @@ object MessagesService { init { //REGISTER DEFAULT MESSAGES + messages["#for-player-only"] = { it.sendMessage("${ChatColor.RED}This sender is not a player") } 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") } diff --git a/gradle.properties b/gradle.properties index 0d35993..3f3c255 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version=1.0.0.3 +version=1.0.0.4-SNAPSHOT signing.gnupg.executable=C:/Program Files (x86)/GnuPG/bin/gpg.exe signing.gnupg.useLegacyGpg=false diff --git a/settings.gradle.kts b/settings.gradle.kts index 3506523..b10e388 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")