Check Command.owner, #216

This commit is contained in:
Karlatemp 2020-11-29 18:00:48 +08:00
parent 4803a21488
commit 595c9480a6
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 16 additions and 0 deletions

View File

@ -37,6 +37,8 @@ public abstract class AbstractCommand
init {
Command.checkCommandName(primaryName)
@Suppress("LeakingThis")
Command.checkCommandOwner(this)
secondaryNames.forEach(Command.Companion::checkCommandName)
}

View File

@ -117,5 +117,19 @@ public interface Command {
name.contains('.') -> throw IllegalArgumentException("'.' is forbidden in command name.")
}
}
/**
* 检查指令 [owner] 的合法性, 在非法时抛出 [IllegalArgumentException]
*/
@JvmStatic
@Throws(IllegalArgumentException::class)
public fun checkCommandOwner(command: Command) {
val owner = command.owner
if (owner is ConsoleCommandOwner) {
if (command.javaClass.enclosingClass != BuiltInCommands::class.java) {
throw IllegalArgumentException("ConsoleCommandOwner is not allowed")
}
}
}
}
}