Skip to content

Commit 5f5d39c

Browse files
committed
[6.1.1][dev] Update command
1 parent c5861ab commit 5f5d39c

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraComponentSuggest.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package taboolib.common.platform.command
22

33
import taboolib.common.platform.ProxyCommandSender
44
import taboolib.common.platform.command.component.CommandComponentDynamic
5+
import taboolib.common.platform.command.component.ExecuteContext
6+
import taboolib.common.platform.command.component.SuggestContext
57
import taboolib.common.platform.function.allWorlds
68
import taboolib.common.platform.function.onlinePlayers
79

@@ -10,17 +12,17 @@ import taboolib.common.platform.function.onlinePlayers
1012
*
1113
* @param suggest 补全表达式
1214
*/
13-
fun CommandComponentDynamic.suggest(suggest: () -> List<String>?): CommandComponentDynamic {
14-
return suggestion<ProxyCommandSender> { _, _ -> suggest() }
15+
fun CommandComponentDynamic.suggest(suggest: SuggestContext<ProxyCommandSender>.() -> List<String>?): CommandComponentDynamic {
16+
return suggestion<ProxyCommandSender> { sender, ctx -> suggest(SuggestContext(sender, ctx)) }
1517
}
1618

1719
/**
1820
* 创建一个不检查的参数不全
1921
*
2022
* @param suggest 补全表达式
2123
*/
22-
fun CommandComponentDynamic.suggestUncheck(suggest: () -> List<String>?): CommandComponentDynamic {
23-
return suggestion<ProxyCommandSender>(uncheck = true) { _, _ -> suggest() }
24+
fun CommandComponentDynamic.suggestUncheck(suggest: SuggestContext<ProxyCommandSender>.() -> List<String>?): CommandComponentDynamic {
25+
return suggestion<ProxyCommandSender>(uncheck = true) { sender, ctx -> suggest(SuggestContext(sender, ctx)) }
2426
}
2527

2628
/**

common-platform-api/src/main/kotlin/taboolib/common/platform/command/ExtraContextPlayer.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import taboolib.common.platform.function.onlinePlayers
1212
* @throws IllegalStateException 参数不存在,或者玩家不存在
1313
*/
1414
fun <T> CommandContext<T>.player(id: String): ProxyPlayer {
15-
return getProxyPlayer(get(id))!!
15+
return getProxyPlayer(get(id).substringBefore(' '))!!
1616
}
1717

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

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

@@ -45,6 +45,6 @@ fun <T> CommandContext<T>.players(id: String): List<ProxyPlayer> {
4545
* @return 指定位置的输入参数
4646
*/
4747
fun <T> CommandContext<T>.playersOrNull(id: String): List<ProxyPlayer>? {
48-
val text = getOrNull(id) ?: return null
48+
val text = getOrNull(id)?.substringBefore(' ') ?: return null
4949
return if (text == "*") onlinePlayers() else listOf(getProxyPlayer(text) ?: return null)
5050
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package taboolib.common.platform.command.component
2+
3+
import taboolib.common.platform.command.CommandContext
4+
5+
/**
6+
* TabooLib
7+
* taboolib.common.platform.command.component.SuggestContext
8+
*
9+
* @author 坏黑
10+
* @since 2024/3/24 15:42
11+
*/
12+
data class SuggestContext<T>(val sender: T, val ctx: CommandContext<T>)

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=taboolib
2-
version=6.1.0
2+
version=6.1.1
33
kotlin.incremental=true
44
kotlin.incremental.java=true
55
kotlin.caching.enabled=true

module/module-bukkit-util/src/main/kotlin/taboolib/platform/util/EntityUtil.kt

+20-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,27 @@ class SafeEntity<T : Entity>(private var entity: T) {
5353
*/
5454
fun get(): T {
5555
if (entity is Player && !entity.isValid) {
56-
entity = Bukkit.getPlayerExact(entity.name) as T
56+
val playerExact = Bukkit.getPlayerExact(entity.name)
57+
if (playerExact != null) {
58+
entity = playerExact as T
59+
} else {
60+
error("Player ${entity.name} is offline.")
61+
}
5762
}
5863
return entity
5964
}
65+
66+
/**
67+
* 获取实体,如果实体失效则返回 null
68+
*/
69+
fun getOrNull(): T? {
70+
return runCatching { get() }.getOrNull()
71+
}
72+
73+
/**
74+
* 是否有效
75+
*/
76+
fun isValid(): Boolean {
77+
return getOrNull() != null
78+
}
6079
}

0 commit comments

Comments
 (0)