1.0.0.4 - sender as player, relocate command args

This commit is contained in:
redtea committed 2024-03-28 20:54:38 +03:00
1 parent 9655d10025
commit 67075ba74d
10 files changed
+81 -50

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
kotlin("jvm") version "1.9.23" kotlin("jvm") version "1.9.0"
id("io.codearte.nexus-staging") version "0.30.0" id("io.codearte.nexus-staging") version "0.30.0"
} }
@@ -1,12 +1,10 @@
package tech.carcadex.kotlinbukkitkit.commands package tech.carcadex.kotlinbukkitkit.commands
import tech.carcadex.kotlinbukkitkit.architecture.KotlinPlugin import org.bukkit.plugin.Plugin
import tech.carcadex.kotlinbukkitkit.commands.dsl.builder.simpleCommand import tech.carcadex.kotlinbukkitkit.commands.dsl.builder.simpleCommand
fun KotlinPlugin.start() { fun Plugin.cmd() {
simpleCommand("test", permission = "test.use") { simpleCommand("test") {
val arg1 = string(0) player.sendMessage("test")
val arg2 = integer(1)
sender.sendMessage(arg1)
} }
} }
@@ -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) ?: "")
@@ -7,48 +7,19 @@ import org.bukkit.World
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.entity.Player 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.NotPlayerException
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
class ExecutorContext(var sender: CommandSender, val args: Array<String>) { class ExecutorContext(val sender: CommandSender, val args: Array<String>) {
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 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) ?: "")
} }
@@ -0,0 +1,6 @@
package tech.carcadex.kotlinbukkitkit.commands.exceptions
import java.lang.RuntimeException
class NotPlayerException : RuntimeException() {
}
@@ -4,6 +4,7 @@ import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter import org.bukkit.command.TabCompleter
import org.bukkit.plugin.Plugin 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.CommandParams
import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser
import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser.paramsFromAnnotations import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser.paramsFromAnnotations
@@ -96,6 +97,7 @@ class CommandManagerImpl(private val plugin: Plugin) : CommandManager {
) )
} catch (ignored: TypeParseException) { } catch (ignored: TypeParseException) {
} catch (ignored: NotPlayerException) {
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
MessagesService.byTag("#wrong-usage")(sender) MessagesService.byTag("#wrong-usage")(sender)
} }
@@ -2,6 +2,7 @@ package tech.carcadex.kotlinbukkitkit.commands.reflection.service
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter 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.MessagesService
import tech.carcadex.kotlinbukkitkit.commands.service.TabCompleteService import tech.carcadex.kotlinbukkitkit.commands.service.TabCompleteService
import tech.carcadex.kotlinbukkitkit.commands.reflection.CommandParams import tech.carcadex.kotlinbukkitkit.commands.reflection.CommandParams
@@ -77,6 +78,8 @@ object SubCommandsService {
true true
} catch (ignored: TypeParseException) { } catch (ignored: TypeParseException) {
false false
} catch (ignored: NotPlayerException) {
false
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
MessagesService.byTag("#wrong-usage")(sender) MessagesService.byTag("#wrong-usage")(sender)
false false
@@ -8,6 +8,7 @@ object MessagesService {
init { init {
//REGISTER DEFAULT MESSAGES //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-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") }
+1 -1
View File
@@ -1,4 +1,4 @@
version=1.0.0.3 version=1.0.0.4-SNAPSHOT
signing.gnupg.executable=***REMOVED*** signing.gnupg.executable=***REMOVED***
signing.gnupg.useLegacyGpg=false signing.gnupg.useLegacyGpg=false
+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")