-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental | Update ItemResolver&Improve Argument
- Loading branch information
1 parent
6e20255
commit fe878bc
Showing
9 changed files
with
165 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/common/StringResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cn.fd.ratziel.module.item.api.common | ||
|
||
import cn.fd.ratziel.function.argument.ArgumentFactory | ||
import cn.fd.ratziel.function.argument.DefaultArgumentFactory | ||
import cn.fd.ratziel.module.item.api.Resolver | ||
|
||
/** | ||
* StringResolver | ||
* | ||
* @author TheFloodDragon | ||
* @since 2024/5/18 15:15 | ||
*/ | ||
interface StringResolver : Resolver<Array<String>, String?> { | ||
|
||
/** | ||
* 解析器名称 | ||
*/ | ||
val name: String | ||
|
||
/** | ||
* 解析器别名 | ||
*/ | ||
val alias: Array<String> | ||
|
||
/** | ||
* 解析元素 (带参数) | ||
*/ | ||
fun resolve(element: Array<String>, arguments: ArgumentFactory): String? | ||
|
||
/** | ||
* 解析元素 (带空参数) | ||
*/ | ||
override fun resolve(element: Array<String>): String? = resolve(element, DefaultArgumentFactory()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 97 additions & 10 deletions
107
...module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,115 @@ | ||
package cn.fd.ratziel.module.item.impl.builder | ||
|
||
import cn.fd.ratziel.core.serialization.asMutable | ||
import cn.fd.ratziel.core.serialization.handle | ||
import cn.fd.ratziel.core.serialization.MutableJsonObject | ||
import cn.fd.ratziel.core.serialization.handlePrimitives | ||
import cn.fd.ratziel.core.util.splitNonEscaped | ||
import cn.fd.ratziel.function.argument.ArgumentFactory | ||
import cn.fd.ratziel.function.argument.popOrNull | ||
import cn.fd.ratziel.module.item.api.builder.ItemResolver | ||
import cn.fd.ratziel.module.item.api.common.StringResolver | ||
import cn.fd.ratziel.module.item.impl.builder.resolver.RandomResolver | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import org.bukkit.OfflinePlayer | ||
import taboolib.common.util.VariableReader | ||
import taboolib.platform.compat.replacePlaceholder | ||
|
||
/** | ||
* DefaultItemResolver | ||
* | ||
* @author TheFloodDragon | ||
* @since 2024/4/20 10:03 | ||
*/ | ||
class DefaultItemResolver : ItemResolver { | ||
object DefaultItemResolver : ItemResolver { | ||
|
||
override fun resolve(element: JsonElement, arguments: ArgumentFactory) = access(element) { origin -> | ||
// 过滤拿到处理对象并处理 | ||
val handle = origin.filter { DefaultItemSerializer.occupiedNodes.contains(it.key) }.asMutable() | ||
// 写回修改过的部分 | ||
putAll(handle) | ||
/** | ||
* 参数分割符 | ||
*/ | ||
const val ARGUMENT_SEPRATION_SIGN = ":" | ||
|
||
/** | ||
* 变量读取器 | ||
*/ | ||
val reader = VariableReader("{", "}") | ||
|
||
/** | ||
* 字符串解析器 (解析器名称-解析器) | ||
*/ | ||
val resolvers = mutableListOf<StringResolver>( | ||
RandomResolver | ||
) | ||
|
||
/** | ||
* 解析: | ||
* 过滤 -> 字符串处理 | ||
*/ | ||
override fun resolve(element: JsonElement, arguments: ArgumentFactory): JsonElement { | ||
// 过滤该处理的 | ||
val filtered = filter(element) | ||
// 处理所有字符串 | ||
val resolved = handleStrings(filtered, arguments) | ||
// 返回结果 | ||
return resolved | ||
} | ||
|
||
fun access(target: JsonElement, action: MutableMap<String, JsonElement>.(JsonObject) -> Unit) = | ||
if (target is JsonObject) target.handle(action) else target | ||
/** | ||
* 处理所有字符串 | ||
*/ | ||
fun handleStrings(json: JsonElement, arguments: ArgumentFactory) = json.handlePrimitives { p -> | ||
p.takeIf { it.isString }?.content?.let { source -> | ||
var handle = source | ||
// 获取玩家 | ||
val player = arguments.popOrNull<OfflinePlayer>()?.value | ||
// 处理PAPI变量 | ||
if (player != null) handle = handle.replacePlaceholder(player) | ||
// 处理变量 | ||
val result = reader.readToFlatten(handle).map { | ||
// 如果是变量, 则通过字符串解析器解析 | ||
if (it.isVariable) { | ||
distribute(it.text, arguments) | ||
} else it // 不然就是他本身 | ||
} | ||
JsonPrimitive(result.joinToString("")) // 拼接结果成字符串并返回 | ||
} ?: p | ||
} | ||
|
||
/** | ||
* 分配 [StringResolver] | ||
*/ | ||
fun distribute(source: String, arguments: ArgumentFactory): String { | ||
// 分割 | ||
val split = source.splitNonEscaped(ARGUMENT_SEPRATION_SIGN) | ||
val name = split.firstOrNull() | ||
// 获取解析器 | ||
val resolver = resolvers.find { it.name == name || it.alias.contains(name) } | ||
// 解析并返回 | ||
return resolver?.resolve(split.drop(1).toTypedArray(), arguments) ?: source | ||
} | ||
|
||
/** | ||
* 判断此节点是否应该被解析 | ||
*/ | ||
fun isResolvable(node: String) = DefaultItemSerializer.occupiedNodes.contains(node) | ||
|
||
/** | ||
* 初步过滤掉无用内容 | ||
*/ | ||
fun filter(element: JsonElement) = access(element) { origin -> | ||
for (entry in origin) { | ||
// 过滤, 放入应该被处理的 | ||
if (isResolvable(entry.key)) { | ||
put(entry.key, entry.value) | ||
} | ||
} | ||
} | ||
|
||
fun access(target: JsonElement, action: MutableJsonObject.(JsonObject) -> Unit): JsonElement { | ||
if (target is JsonObject) { | ||
val map = LinkedHashMap<String, JsonElement>() | ||
action(map, target) | ||
return JsonObject(map) | ||
} else return target | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...le-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/resolver/RandomResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cn.fd.ratziel.module.item.impl.builder.resolver | ||
|
||
import cn.fd.ratziel.function.argument.ArgumentFactory | ||
import cn.fd.ratziel.module.item.api.common.StringResolver | ||
import taboolib.common.util.random | ||
|
||
/** | ||
* RandomResolver | ||
* | ||
* @author TheFloodDragon | ||
* @since 2024/5/18 16:22 | ||
*/ | ||
object RandomResolver : StringResolver { | ||
|
||
override val name = "random" | ||
|
||
override val alias = arrayOf("ran", "rd") | ||
|
||
override fun resolve(element: Array<String>, arguments: ArgumentFactory): String? { | ||
if (element.isEmpty()) return null | ||
// TODO 写完这个 | ||
return random().nextInt().toString() | ||
} | ||
|
||
} |