fold
inline fun <R> CommandSender.fold(ifIsConsole: ConsoleCommandSender.() -> R, ifIsUser: UserCommandSender.() -> R, otherwise: CommandSender.() -> R = { error("CommandSender ${this::class.qualifiedName} is not supported") }): R
Content copied to clipboard
折叠 AbstractCommandSender 的可能性.
当 this 为 ConsoleCommandSender 时执行 ifIsConsole
当 this 为 UserCommandSender 时执行 ifIsUser
否则执行 otherwise
示例
// 当一个指令执行过程出错
val exception: Exception = ...
sender.fold(
ifIsConsole = { // this: ConsoleCommandSender
sendMessage(exception.stackTraceToString()) // 展示整个 stacktrace
},
ifIsUser = { // this: UserCommandSender
sendMessage(exception.message.toString()) // 只展示 Exception.message
}
)
Content copied to clipboard
Return
ifIsConsole, ifIsUser 或 otherwise 执行结果.