1.0.1.0
Commands: one plugin - one command manager, sender as player Kotlin 1.9.20 Genref update
This commit is contained in:
1 parent
6648337a34
commit
bace476970
32 files changed
+264
-116
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
id("java")
|
||||
kotlin("jvm")
|
||||
@@ -22,6 +24,7 @@ dependencies {
|
||||
compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
|
||||
compileOnly(project(":extensions"))
|
||||
implementation(kotlin("reflect"))
|
||||
implementation(kotlin("stdlib-jdk8"))
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
@@ -120,4 +123,12 @@ tasks.shadowJar {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
val compileKotlin: KotlinCompile by tasks
|
||||
compileKotlin.kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
val compileTestKotlin: KotlinCompile by tasks
|
||||
compileTestKotlin.kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
@@ -21,7 +21,7 @@ interface Command {
|
||||
* @author itzRedTea
|
||||
* @since 1.0.0
|
||||
*/
|
||||
fun execute(invoker: ExecutorContext)
|
||||
fun execute(context: ExecutorContext)
|
||||
/**
|
||||
* Invokes when command must be executed
|
||||
* @param sender who executes this command
|
||||
@@ -34,10 +34,7 @@ interface Command {
|
||||
fun tabComplete(sender: CommandSender, args: Array<String>): List<String>
|
||||
|
||||
|
||||
fun hasNotPermission(sender: CommandSender): Boolean =
|
||||
context.permission != "" && (!sender.hasPermission(context.permission)).also {
|
||||
if(it) MessagesService.byTag("#no-perm")(sender)
|
||||
}
|
||||
|
||||
fun hasNotPermission(sender: CommandSender): Boolean = context.permission != ""
|
||||
&& (!sender.hasPermission(context.permission))
|
||||
}
|
||||
|
||||
+6
-12
@@ -6,45 +6,39 @@ 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
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
|
||||
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 CommandExecuteException("#not-a-number")
|
||||
} ?: 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 CommandExecuteException("#not-a-number")
|
||||
} ?: 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()
|
||||
throw CommandExecuteException("#no-such-material")
|
||||
}
|
||||
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()
|
||||
throw CommandExecuteException("#no-such-world")
|
||||
}
|
||||
|
||||
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()
|
||||
throw CommandExecuteException("#no-such-player")
|
||||
}
|
||||
|
||||
fun ExecutorContext.offlinePlayer(index: Int): OfflinePlayer = Bukkit.getOfflinePlayer(argument(index) ?: "")
|
||||
@@ -1,21 +1,13 @@
|
||||
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.NotPlayerException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService
|
||||
|
||||
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
|
||||
|
||||
+29
-19
@@ -1,17 +1,24 @@
|
||||
package tech.carcadex.kotlinbukkitkit.commands.dsl.builder
|
||||
|
||||
import org.bukkit.command.CommandSender
|
||||
import org.bukkit.plugin.Plugin
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.Command
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.CommandContext
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.ExecutorContext
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.leaf.CommandLeaf
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.node.CommandNodeImpl
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.LeafCommandHasNotExecutorException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.manager.CommandManagerInternal
|
||||
import tech.carcadex.kotlinbukkitkit.commands.manager.commandManager
|
||||
import tech.carcadex.kotlinbukkitkit.commands.manager.commandManagerInternal
|
||||
import tech.carcadex.kotlinbukkitkit.commands.register.CommandRegister
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.TabCompleteService
|
||||
|
||||
class CommandBuilder(
|
||||
val context: CommandContext,
|
||||
var tabComplete: Collection<String> = emptyList(),
|
||||
val plugin: Plugin,
|
||||
var tabComplete: List<(CommandSender) -> List<String>> = emptyList(),
|
||||
val subs: MutableMap<String, Command> = mutableMapOf(),
|
||||
var executor: (ExecutorContext.() -> Unit)? = null
|
||||
) {
|
||||
@@ -22,7 +29,7 @@ class CommandBuilder(
|
||||
}
|
||||
|
||||
fun CommandBuilder.subCommand(name: String, permission: String, tabComplete: Collection<String>, invoke: CommandBuilder.() -> Unit) {
|
||||
subs[name.lowercase()] = createCommand(name, permission, tabComplete, invoke)
|
||||
subs[name.lowercase()] = plugin.createCommand(name, permission, tabComplete, invoke)
|
||||
}
|
||||
|
||||
fun CommandBuilder.executor(executor: ExecutorContext.() -> Unit) {
|
||||
@@ -30,38 +37,41 @@ fun CommandBuilder.executor(executor: ExecutorContext.() -> Unit) {
|
||||
}
|
||||
|
||||
|
||||
private fun createCommand(name: String, permission: String, tabComplete: Collection<String>, invoke: CommandBuilder.() -> Unit): Command {
|
||||
val builder = CommandBuilder(CommandContext(name))
|
||||
private fun Plugin.createCommand(name: String, permission: String, tabComplete: Collection<String>, invoke: CommandBuilder.() -> Unit): Command {
|
||||
val builder = CommandBuilder(CommandContext(name), this)
|
||||
builder.permission = permission
|
||||
builder.tabComplete = tabComplete
|
||||
builder.tabComplete = commandManagerInternal().tabCompleteService.parseTags(tabComplete.toList())
|
||||
builder.invoke()
|
||||
return createCommand(builder)
|
||||
}
|
||||
private fun createCommand(builder: CommandBuilder): Command {
|
||||
return if (builder.subs.isEmpty()) {
|
||||
if (builder.executor == null) throw LeafCommandHasNotExecutorException(builder.context.name)
|
||||
CommandLeaf(builder.context, builder.executor!!, builder.tabComplete.toList())
|
||||
CommandLeaf(builder.context, builder.executor!!, (builder.tabComplete.toList()))
|
||||
} else {
|
||||
CommandNodeImpl(builder.context, builder.subs)
|
||||
CommandNodeImpl(builder.context, builder.subs,
|
||||
if(builder.executor != null) CommandLeaf(builder.context, builder.executor!!, builder.tabComplete) else null)
|
||||
}
|
||||
}
|
||||
|
||||
fun Plugin.simpleCommand(name: String, permission: String, tabComplete: Collection<String>, executor: ExecutorContext.() -> Unit) {
|
||||
val command = createCommand(CommandBuilder(CommandContext(name),
|
||||
executor = executor).apply {
|
||||
this.permission = permission
|
||||
this.tabComplete = tabComplete
|
||||
})
|
||||
CommandRegister.register(this, { sender, _, _, args -> command.execute(ExecutorContext(sender, args)); true },
|
||||
command.context.aliases, command.context.description, command.context.usage, { sender, _, _, args ->
|
||||
command.tabComplete(sender, args)
|
||||
}
|
||||
)
|
||||
registerCommand(createCommand(CommandBuilder(CommandContext(name),
|
||||
executor = executor, plugin = this).apply {
|
||||
this.permission = permission
|
||||
this.tabComplete = commandManagerInternal().tabCompleteService.parseTags(tabComplete.toList())
|
||||
}))
|
||||
}
|
||||
|
||||
fun Plugin.command(name: String, permission: String, tabComplete: Collection<String>, invoke: CommandBuilder.() -> Unit) {
|
||||
val command = createCommand(name, permission, tabComplete, invoke)
|
||||
CommandRegister.register(this, { sender, _, _, args -> command.execute(ExecutorContext(sender, args)); true },
|
||||
registerCommand(createCommand(name, permission, tabComplete, invoke))
|
||||
}
|
||||
|
||||
private fun Plugin.registerCommand(command: Command) {
|
||||
CommandRegister.register(this, { sender, _, _, args -> try {
|
||||
command.execute(ExecutorContext(sender, args))
|
||||
} catch (e: CommandExecuteException) {
|
||||
commandManager().readMessage(e.messageTag)(sender)
|
||||
}; true },
|
||||
command.context.aliases, command.context.description, command.context.usage, { sender, _, _, args ->
|
||||
command.tabComplete(sender, args)
|
||||
}
|
||||
|
||||
+5
-6
@@ -5,21 +5,20 @@ import tech.carcadex.kotlinbukkitkit.commands.dsl.Command
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.CommandContext
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.ExecutorContext
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.ArgumentNotFoundException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.TabCompleteService
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
|
||||
class CommandLeaf(
|
||||
override val context: CommandContext,
|
||||
private val executor: (ExecutorContext.() -> Unit),
|
||||
completeTags: List<String> = emptyList()
|
||||
private val completer: List<(CommandSender) -> List<String>>
|
||||
) : Command {
|
||||
private val completer = TabCompleteService.parseTags(completeTags)
|
||||
override fun execute(context: ExecutorContext) {
|
||||
if(hasNotPermission(context.sender)) return
|
||||
if(hasNotPermission(context.sender))
|
||||
throw CommandExecuteException("#no-perm")
|
||||
try {
|
||||
executor(context)
|
||||
} catch (e: ArgumentNotFoundException) {
|
||||
MessagesService.byTag("#wrong-usage")(context.sender)
|
||||
throw CommandExecuteException("#wrong-usage")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -5,13 +5,15 @@ import tech.carcadex.kotlinbukkitkit.commands.dsl.Command
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.CommandContext
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.CommandNode
|
||||
import tech.carcadex.kotlinbukkitkit.commands.dsl.ExecutorContext
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService
|
||||
|
||||
class CommandNodeImpl(
|
||||
override val context: CommandContext,
|
||||
private val subs: MutableMap<String, Command> = mutableMapOf()
|
||||
private val subs: MutableMap<String, Command> = mutableMapOf(),
|
||||
private val default: Command? = null
|
||||
) : CommandNode {
|
||||
private var default: Command? = null
|
||||
|
||||
override fun execute(context: ExecutorContext) {
|
||||
if(hasNotPermission(context.sender)) return
|
||||
if(context.args.isNotEmpty() && context.args[0].lowercase() in subs) subs[context.args[0].lowercase()]?.execute(
|
||||
@@ -19,7 +21,7 @@ class CommandNodeImpl(
|
||||
else {
|
||||
if(default != null) {
|
||||
default!!.execute(ExecutorContext(context.sender, context.args))
|
||||
} else MessagesService.byTag("#wrong-usage")(context.sender)
|
||||
} else throw CommandExecuteException("#wrong-usage")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package tech.carcadex.kotlinbukkitkit.commands.exceptions
|
||||
|
||||
open class CommandExecuteException(val messageTag: String) : RuntimeException() {
|
||||
}
|
||||
+1
-3
@@ -1,6 +1,4 @@
|
||||
package tech.carcadex.kotlinbukkitkit.commands.exceptions
|
||||
|
||||
import java.lang.RuntimeException
|
||||
|
||||
class NotPlayerException : RuntimeException() {
|
||||
class NotPlayerException() : CommandExecuteException(messageTag = "#for-player-only") {
|
||||
}
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
package tech.carcadex.kotlinbukkitkit.commands.exceptions
|
||||
|
||||
class TypeParseException() : RuntimeException() {
|
||||
}
|
||||
+9
-2
@@ -10,11 +10,18 @@ interface CommandManager {
|
||||
fun register(func: KFunction<Unit>)
|
||||
fun tabComplete(tag: String, func: (CommandSender) -> List<String>)
|
||||
fun message(tag: String, func: (CommandSender) -> Unit)
|
||||
fun readMessage(tag: String): (CommandSender) -> Unit
|
||||
|
||||
companion object {
|
||||
private val registeredManagers = mutableMapOf<Plugin, CommandManager>()
|
||||
@JvmStatic
|
||||
fun get(plugin: Plugin): CommandManager = CommandManagerImpl(plugin)
|
||||
fun get(plugin: Plugin): CommandManager =
|
||||
registeredManagers[plugin] ?: run {
|
||||
registeredManagers[plugin] = CommandManagerInternal(plugin)
|
||||
get(plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Plugin.commandManager(): CommandManager = CommandManagerImpl(this)
|
||||
fun Plugin.commandManager(): CommandManager = CommandManager.get(this)
|
||||
internal fun Plugin.commandManagerInternal(): CommandManagerInternal = CommandManager.get(this) as CommandManagerInternal
|
||||
+22
-21
@@ -4,26 +4,26 @@ 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
|
||||
import tech.carcadex.kotlinbukkitkit.commands.reflection.annotations.Command
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.register.CommandRegister
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService
|
||||
import tech.carcadex.kotlinbukkitkit.commands.reflection.service.SubCommandsService
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.TabCompleteService
|
||||
import kotlin.reflect.KFunction
|
||||
import kotlin.reflect.full.createType
|
||||
import kotlin.reflect.full.findAnnotations
|
||||
import kotlin.reflect.full.hasAnnotation
|
||||
import kotlin.reflect.full.isSubtypeOf
|
||||
import kotlin.reflect.jvm.javaType
|
||||
|
||||
|
||||
|
||||
class CommandManagerImpl(private val plugin: Plugin) : CommandManager {
|
||||
|
||||
class CommandManagerInternal(private val plugin: Plugin) : CommandManager {
|
||||
val messagesService = MessagesService()
|
||||
val tabCompleteService = TabCompleteService()
|
||||
private val subCommandsService = SubCommandsService(messagesService, tabCompleteService)
|
||||
private fun register(
|
||||
commandExecutor: CommandExecutor, commandParams: CommandParams, tabCompleter: TabCompleter =
|
||||
TabCompleter { _, _, _, _ -> mutableListOf() }
|
||||
@@ -39,10 +39,10 @@ class CommandManagerImpl(private val plugin: Plugin) : CommandManager {
|
||||
}
|
||||
|
||||
override fun register(commandClass: Any) {
|
||||
if(commandClass::class.objectInstance == null) {
|
||||
throw IllegalArgumentException("commandClass is not a object!")
|
||||
if((commandClass::class.findAnnotations(Command::class).isEmpty())) {
|
||||
throw IllegalArgumentException("commandClass has not contains Command annotation!")
|
||||
}
|
||||
val (name, subs) = SubCommandsService.registerRootCommand(commandClass)
|
||||
val (name, subs) = subCommandsService.registerRootCommand(commandClass)
|
||||
val subsNames = mutableListOf<String>()
|
||||
subs.forEach {
|
||||
subsNames.addAll(it.context.aliases)
|
||||
@@ -50,10 +50,10 @@ class CommandManagerImpl(private val plugin: Plugin) : CommandManager {
|
||||
val context = paramsFromAnnotations(commandClass::class)
|
||||
register(CommandExecutor { sender, _, _, args ->
|
||||
if (context.perm != null && !sender.hasPermission(context.perm)) {
|
||||
MessagesService.byTag("#no-perm")(sender)
|
||||
messagesService.byTag("#no-perm")(sender)
|
||||
return@CommandExecutor true
|
||||
}
|
||||
SubCommandsService.handleCommand(name, sender, args);
|
||||
subCommandsService.handleCommand(name, sender, args);
|
||||
true
|
||||
},
|
||||
context,
|
||||
@@ -87,19 +87,19 @@ class CommandManagerImpl(private val plugin: Plugin) : CommandManager {
|
||||
|
||||
register(CommandExecutor { sender, _, _, argsFromExecutor ->
|
||||
if (cmd.perm != null && !sender.hasPermission(cmd.perm)) {
|
||||
MessagesService.byTag("#no-perm")(sender)
|
||||
messagesService.byTag("#no-perm")(sender)
|
||||
return@CommandExecutor true
|
||||
}
|
||||
try {
|
||||
func.callBy(
|
||||
|
||||
Parser.paramMap(cmd, argsFromExecutor, argsSize, sender, argsParsers, func)
|
||||
|
||||
)
|
||||
} catch (ignored: TypeParseException) {
|
||||
} catch (ignored: NotPlayerException) {
|
||||
} catch (e: CommandExecuteException) {
|
||||
messagesService.byTag(e.messageTag)(sender)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
MessagesService.byTag("#wrong-usage")(sender)
|
||||
messagesService.byTag("#wrong-usage")(sender)
|
||||
} catch (e: Throwable) {
|
||||
messagesService.byTag("#unknown-error")(sender)
|
||||
}
|
||||
|
||||
return@CommandExecutor true
|
||||
@@ -107,20 +107,21 @@ class CommandManagerImpl(private val plugin: Plugin) : CommandManager {
|
||||
},
|
||||
cmd,
|
||||
cmd.completeTags.let {
|
||||
TabCompleteService.completerReflection(it.toMutableList(),
|
||||
tabCompleteService.completerReflection(it.toMutableList(),
|
||||
func.parameters.map { it.type.javaType as Class<*> })
|
||||
}
|
||||
?: TabCompleter { _, _, _, _ -> mutableListOf() }
|
||||
)
|
||||
} else throw IllegalArgumentException("It must be a command!")
|
||||
}
|
||||
|
||||
override fun tabComplete(tag: String, func: (CommandSender) -> List<String>) {
|
||||
TabCompleteService.register(tag, func)
|
||||
tabCompleteService.register(tag, func)
|
||||
}
|
||||
|
||||
|
||||
override fun message(tag: String, func: (CommandSender) -> Unit) {
|
||||
MessagesService.register(tag, func)
|
||||
messagesService.register(tag, func)
|
||||
}
|
||||
|
||||
override fun readMessage(tag: String): (CommandSender) -> Unit = messagesService.byTag(tag)
|
||||
}
|
||||
+5
-9
@@ -1,7 +1,7 @@
|
||||
package tech.carcadex.kotlinbukkitkit.commands.reflection
|
||||
|
||||
import org.bukkit.command.CommandSender
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.UnsupportedTypeException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.reflection.annotations.*
|
||||
import tech.carcadex.kotlinbukkitkit.commands.service.MessagesService
|
||||
@@ -59,19 +59,17 @@ object Parser {
|
||||
try {
|
||||
TypeService.byType(argType.type)(context)
|
||||
} catch (e: Throwable) {
|
||||
if (context.errorMessage != null) MessagesService.byTag(context.errorMessage!!)(sender)
|
||||
throw TypeParseException()
|
||||
throw CommandExecuteException(context.errorMessage ?: "#empty")
|
||||
}
|
||||
}
|
||||
} else if (!clazz.isEnum) throw UnsupportedTypeException("Type ${argType.type.javaType.typeName} is not supported")
|
||||
else {
|
||||
enums[argIndex] = { it, sender ->
|
||||
enums[argIndex] = { it, _ ->
|
||||
var res: Enum<*>? = null;
|
||||
for (e in clazz.enumConstants) {
|
||||
if ((e as Enum<*>).name.equals(it, ignoreCase = true)) res = e;
|
||||
}
|
||||
if (res == null) MessagesService.byTag("#no-such-enum")(sender)
|
||||
res ?: throw TypeParseException()
|
||||
res ?: throw CommandExecuteException("#no-such-enum")
|
||||
}
|
||||
argsParsers.add { _, _ -> {} }
|
||||
}
|
||||
@@ -102,8 +100,6 @@ object Parser {
|
||||
instanceAtStart: Boolean = false,
|
||||
instance: Any = Any()
|
||||
): MutableMap<KParameter, Any?> {
|
||||
val tempArgs = if (cmd.isLastOptional && argsFromExecutor.size < argsSize) argsFromExecutor.copyOf(argsSize)
|
||||
else argsFromExecutor
|
||||
|
||||
val finalArgs = mutableListOf<Any?>()
|
||||
if(instanceAtStart) finalArgs.add(instance)
|
||||
@@ -113,7 +109,7 @@ object Parser {
|
||||
)
|
||||
)
|
||||
finalArgs.add(sender)
|
||||
finalArgs.addAll(tempArgs.mapIndexed { i, it ->
|
||||
finalArgs.addAll(argsFromExecutor.mapIndexed { i, it ->
|
||||
if (it == null) return@mapIndexed null
|
||||
argsParsers[i + 1](it, sender)
|
||||
})
|
||||
|
||||
+7
-7
@@ -9,12 +9,12 @@ import tech.carcadex.kotlinbukkitkit.commands.reflection.CommandParams
|
||||
import tech.carcadex.kotlinbukkitkit.commands.reflection.Parser
|
||||
import tech.carcadex.kotlinbukkitkit.commands.reflection.annotations.Command
|
||||
import tech.carcadex.kotlinbukkitkit.commands.reflection.annotations.Default
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.TypeParseException
|
||||
import tech.carcadex.kotlinbukkitkit.commands.exceptions.CommandExecuteException
|
||||
import kotlin.reflect.KFunction
|
||||
import kotlin.reflect.full.*
|
||||
import kotlin.reflect.jvm.javaType
|
||||
|
||||
object SubCommandsService {
|
||||
class SubCommandsService(private val messagesService: MessagesService, private val tabCompleteService: TabCompleteService) {
|
||||
data class SubCommand(val name: String, val context: CommandParams,
|
||||
val executor: (sender: CommandSender, args: Array<String>) -> Boolean, val tabCompleter: TabCompleter)
|
||||
|
||||
@@ -44,7 +44,7 @@ object SubCommandsService {
|
||||
if(args.isNotEmpty()) for(sub in subs[rootCommand.lowercase()]!!) {
|
||||
if(sub.name == args[0]) {
|
||||
if (sub.context.perm != null && sub.context.perm != "" && !sender.hasPermission(sub.context.perm)) {
|
||||
MessagesService.byTag("#no-perm")(sender)
|
||||
messagesService.byTag("#no-perm")(sender)
|
||||
return
|
||||
}
|
||||
sub.executor(sender, args.copyOfRange(1, args.size))
|
||||
@@ -70,18 +70,18 @@ object SubCommandsService {
|
||||
}
|
||||
val executor: (sender: CommandSender, args: Array<String>) -> Boolean = { sender, argsFromExecutor ->
|
||||
if (context.perm != null && !sender.hasPermission(context.perm)) {
|
||||
MessagesService.byTag("#no-perm")(sender)
|
||||
messagesService.byTag("#no-perm")(sender)
|
||||
true
|
||||
} else {
|
||||
try {
|
||||
func.callBy(Parser.paramMap(context, argsFromExecutor, argsSize, sender, argsParsers, func, true, rootCommand))
|
||||
true
|
||||
} catch (ignored: TypeParseException) {
|
||||
} catch (ignored: CommandExecuteException) {
|
||||
false
|
||||
} catch (ignored: NotPlayerException) {
|
||||
false
|
||||
} catch (e: IllegalArgumentException) {
|
||||
MessagesService.byTag("#wrong-usage")(sender)
|
||||
messagesService.byTag("#wrong-usage")(sender)
|
||||
false
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ object SubCommandsService {
|
||||
}
|
||||
|
||||
return SubCommand(name, context, executor, context.completeTags.let {
|
||||
TabCompleteService.completerReflection(it.toMutableList(),
|
||||
tabCompleteService.completerReflection(it.toMutableList(),
|
||||
func.parameters.map { it.type.javaType as Class<*> })
|
||||
}
|
||||
?: TabCompleter { _, _, _, _ -> mutableListOf() })
|
||||
|
||||
+3
-1
@@ -3,7 +3,7 @@ package tech.carcadex.kotlinbukkitkit.commands.service
|
||||
import org.bukkit.ChatColor
|
||||
import org.bukkit.command.CommandSender
|
||||
|
||||
object MessagesService {
|
||||
class MessagesService {
|
||||
private val messages = mutableMapOf<String, (CommandSender) -> Unit>()
|
||||
|
||||
init {
|
||||
@@ -15,7 +15,9 @@ object MessagesService {
|
||||
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") }
|
||||
messages["#unknown-error"] = { it.sendMessage("${ChatColor.RED}Something went wrong") }
|
||||
messages["#no-perm"] = { it.sendMessage("${ChatColor.RED}You have not permissions for this command!") }
|
||||
messages["#empty"] = { }
|
||||
}
|
||||
|
||||
fun register(tag: String, func: (CommandSender) -> Unit) {
|
||||
|
||||
+1
-2
@@ -3,9 +3,8 @@ package tech.carcadex.kotlinbukkitkit.commands.service
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.command.CommandSender
|
||||
import org.bukkit.command.TabCompleter
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
object TabCompleteService {
|
||||
class TabCompleteService {
|
||||
private val completes = mutableMapOf<String, (CommandSender) -> List<String>>()
|
||||
init {
|
||||
//REGISTER DEFAULT COMPLETES
|
||||
|
||||
Reference in new issue
Block a user