diff --git a/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraComponentSuggest.kt b/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraComponentSuggest.kt index f116a6ff1..581f913a1 100644 --- a/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraComponentSuggest.kt +++ b/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraComponentSuggest.kt @@ -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 @@ -10,8 +12,8 @@ import taboolib.common.platform.function.onlinePlayers * * @param suggest 补全表达式 */ -fun CommandComponentDynamic.suggest(suggest: () -> List?): CommandComponentDynamic { - return suggestion { _, _ -> suggest() } +fun CommandComponentDynamic.suggest(suggest: SuggestContext.() -> List?): CommandComponentDynamic { + return suggestion { sender, ctx -> suggest(SuggestContext(sender, ctx)) } } /** @@ -19,8 +21,8 @@ fun CommandComponentDynamic.suggest(suggest: () -> List?): CommandCompon * * @param suggest 补全表达式 */ -fun CommandComponentDynamic.suggestUncheck(suggest: () -> List?): CommandComponentDynamic { - return suggestion(uncheck = true) { _, _ -> suggest() } +fun CommandComponentDynamic.suggestUncheck(suggest: SuggestContext.() -> List?): CommandComponentDynamic { + return suggestion(uncheck = true) { sender, ctx -> suggest(SuggestContext(sender, ctx)) } } /** diff --git a/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraContextPlayer.kt b/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraContextPlayer.kt index 1c4cef95f..617baaa87 100644 --- a/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraContextPlayer.kt +++ b/common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraContextPlayer.kt @@ -12,7 +12,7 @@ import taboolib.common.platform.function.onlinePlayers * @throws IllegalStateException 参数不存在,或者玩家不存在 */ fun CommandContext.player(id: String): ProxyPlayer { - return getProxyPlayer(get(id))!! + return getProxyPlayer(get(id).substringBefore(' '))!! } /** @@ -22,7 +22,7 @@ fun CommandContext.player(id: String): ProxyPlayer { * @return 指定位置的输入参数 */ fun CommandContext.playerOrNull(id: String): ProxyPlayer? { - return getProxyPlayer(getOrNull(id) ?: return null) + return getProxyPlayer(getOrNull(id)?.substringBefore(' ') ?: return null) } /** @@ -34,7 +34,7 @@ fun CommandContext.playerOrNull(id: String): ProxyPlayer? { * @throws IllegalStateException 参数不存在,或者玩家不存在 */ fun CommandContext.players(id: String): List { - val text = get(id) + val text = get(id).substringBefore(' ') return if (text == "*") onlinePlayers() else listOf(getProxyPlayer(text)!!) } @@ -45,6 +45,6 @@ fun CommandContext.players(id: String): List { * @return 指定位置的输入参数 */ fun CommandContext.playersOrNull(id: String): List? { - val text = getOrNull(id) ?: return null + val text = getOrNull(id)?.substringBefore(' ') ?: return null return if (text == "*") onlinePlayers() else listOf(getProxyPlayer(text) ?: return null) } \ No newline at end of file diff --git a/common-platform-api/src/main/kotlin/taboolib/common/platform/command/component/SuggestContext.kt b/common-platform-api/src/main/kotlin/taboolib/common/platform/command/component/SuggestContext.kt new file mode 100644 index 000000000..fcb56b400 --- /dev/null +++ b/common-platform-api/src/main/kotlin/taboolib/common/platform/command/component/SuggestContext.kt @@ -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(val sender: T, val ctx: CommandContext) \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 56d710505..bd3ad0c6d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/module/module-bukkit-util/src/main/kotlin/taboolib/platform/util/EntityUtil.kt b/module/module-bukkit-util/src/main/kotlin/taboolib/platform/util/EntityUtil.kt index b4bb592dc..bb474c1ef 100644 --- a/module/module-bukkit-util/src/main/kotlin/taboolib/platform/util/EntityUtil.kt +++ b/module/module-bukkit-util/src/main/kotlin/taboolib/platform/util/EntityUtil.kt @@ -53,8 +53,27 @@ class SafeEntity(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 + } } \ No newline at end of file