Skip to content

Commit

Permalink
[6.1.1][dev] Update command
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Apr 21, 2024
1 parent c5861ab commit 5f5d39c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package taboolib.common.platform.command

import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.component.CommandComponentDynamic
import taboolib.common.platform.command.component.ExecuteContext
import taboolib.common.platform.command.component.SuggestContext
import taboolib.common.platform.function.allWorlds
import taboolib.common.platform.function.onlinePlayers

Expand All @@ -10,17 +12,17 @@ import taboolib.common.platform.function.onlinePlayers
*
* @param suggest 补全表达式
*/
fun CommandComponentDynamic.suggest(suggest: () -> List<String>?): CommandComponentDynamic {
return suggestion<ProxyCommandSender> { _, _ -> suggest() }
fun CommandComponentDynamic.suggest(suggest: SuggestContext<ProxyCommandSender>.() -> List<String>?): CommandComponentDynamic {
return suggestion<ProxyCommandSender> { sender, ctx -> suggest(SuggestContext(sender, ctx)) }
}

/**
* 创建一个不检查的参数不全
*
* @param suggest 补全表达式
*/
fun CommandComponentDynamic.suggestUncheck(suggest: () -> List<String>?): CommandComponentDynamic {
return suggestion<ProxyCommandSender>(uncheck = true) { _, _ -> suggest() }
fun CommandComponentDynamic.suggestUncheck(suggest: SuggestContext<ProxyCommandSender>.() -> List<String>?): CommandComponentDynamic {
return suggestion<ProxyCommandSender>(uncheck = true) { sender, ctx -> suggest(SuggestContext(sender, ctx)) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import taboolib.common.platform.function.onlinePlayers
* @throws IllegalStateException 参数不存在,或者玩家不存在
*/
fun <T> CommandContext<T>.player(id: String): ProxyPlayer {
return getProxyPlayer(get(id))!!
return getProxyPlayer(get(id).substringBefore(' '))!!
}

/**
Expand All @@ -22,7 +22,7 @@ fun <T> CommandContext<T>.player(id: String): ProxyPlayer {
* @return 指定位置的输入参数
*/
fun <T> CommandContext<T>.playerOrNull(id: String): ProxyPlayer? {
return getProxyPlayer(getOrNull(id) ?: return null)
return getProxyPlayer(getOrNull(id)?.substringBefore(' ') ?: return null)
}

/**
Expand All @@ -34,7 +34,7 @@ fun <T> CommandContext<T>.playerOrNull(id: String): ProxyPlayer? {
* @throws IllegalStateException 参数不存在,或者玩家不存在
*/
fun <T> CommandContext<T>.players(id: String): List<ProxyPlayer> {
val text = get(id)
val text = get(id).substringBefore(' ')
return if (text == "*") onlinePlayers() else listOf(getProxyPlayer(text)!!)
}

Expand All @@ -45,6 +45,6 @@ fun <T> CommandContext<T>.players(id: String): List<ProxyPlayer> {
* @return 指定位置的输入参数
*/
fun <T> CommandContext<T>.playersOrNull(id: String): List<ProxyPlayer>? {
val text = getOrNull(id) ?: return null
val text = getOrNull(id)?.substringBefore(' ') ?: return null
return if (text == "*") onlinePlayers() else listOf(getProxyPlayer(text) ?: return null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package taboolib.common.platform.command.component

import taboolib.common.platform.command.CommandContext

/**
* TabooLib
* taboolib.common.platform.command.component.SuggestContext
*
* @author 坏黑
* @since 2024/3/24 15:42
*/
data class SuggestContext<T>(val sender: T, val ctx: CommandContext<T>)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=taboolib
version=6.1.0
version=6.1.1
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,27 @@ class SafeEntity<T : Entity>(private var entity: T) {
*/
fun get(): T {
if (entity is Player && !entity.isValid) {
entity = Bukkit.getPlayerExact(entity.name) as T
val playerExact = Bukkit.getPlayerExact(entity.name)
if (playerExact != null) {
entity = playerExact as T
} else {
error("Player ${entity.name} is offline.")
}
}
return entity
}

/**
* 获取实体,如果实体失效则返回 null
*/
fun getOrNull(): T? {
return runCatching { get() }.getOrNull()
}

/**
* 是否有效
*/
fun isValid(): Boolean {
return getOrNull() != null
}
}

0 comments on commit 5f5d39c

Please sign in to comment.