Skip to content

Commit

Permalink
Experimental | Update Item&Fix&Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed May 24, 2024
1 parent 412c123 commit 6ef9735
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object JsonHandler {
if (targetValue is JsonObject) {
// 判断当前值类型 (若非复合类型,则替换,此时目标值是复合类型的)
val value: JsonObject? = ownValue as? JsonObject
map[key] = JsonObject(merge((value ?: emptyJson()), targetValue, replace))
map[key] = JsonObject(merge((value ?: emptyJsonObject()), targetValue, replace))
} else map[key] = targetValue
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
@file:OptIn(ExperimentalSerializationApi::class)
@file:Suppress("NOTHING_TO_INLINE")

package cn.fd.ratziel.core.serialization

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import java.util.function.Function

val baseJson by lazy {
Expand All @@ -15,6 +17,8 @@ val baseJson by lazy {
ignoreUnknownKeys = true
// 隐式空值
explicitNulls = false
// 数据修正
coerceInputValues = true
// 美观的打印方式
prettyPrint = true
// 枚举类不区分大小写
Expand All @@ -35,7 +39,12 @@ operator fun JsonObject.get(names: Iterable<String>): JsonElement? {
/**
* 可变化
*/
inline fun Map<String, JsonElement>.asMutable(): MutableJsonObject = MutableJsonObject(this)
fun Map<String, JsonElement>.asMutable(): MutableJsonObject =
when (this) {
is MutableJsonObject -> this
is MutableMap<*, *> -> MutableJsonObject(this as MutableMap<String, JsonElement>)
else -> MutableJsonObject(this.toMutableMap())
}

/**
* 处理 [JsonObject]
Expand All @@ -57,24 +66,11 @@ fun JsonElement.handlePrimitives(action: Function<JsonPrimitive, JsonElement>):
fun JsonObject.merge(target: JsonObject, replace: Boolean = true): MutableJsonObject = JsonHandler.merge(this, target, replace)

/**
* 构造一个空的[JsonObject]
*/
inline fun emptyJson() = JsonObject(emptyMap())

/**
* 简易的[JsonObject]检查
* 构造一个空的 [JsonObject]
*/
fun String.isJsonObject(): Boolean = startsWith('{') && endsWith('}')
fun emptyJsonObject() = JsonObject(emptyMap())

/**
* [JsonPrimitive]类型的判断
* 简易的 [JsonObject] 检查
*/
fun JsonPrimitive.isInt() = !this.isString && this.intOrNull != null

fun JsonPrimitive.isLong() = !this.isString && this.longOrNull != null

fun JsonPrimitive.isBoolean() = !this.isString && this.booleanOrNull != null

fun JsonPrimitive.isDouble() = !this.isString && this.doubleOrNull != null

fun JsonPrimitive.isFloat() = !this.isString && this.floatOrNull != null
fun String.isJsonObject(): Boolean = startsWith('{') && endsWith('}')
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.fd.ratziel.core.serialization

import cn.fd.ratziel.core.serialization.MutableJsonObject.MutableJsonObjectSerializer.JsonObjectSerializer
import cn.fd.ratziel.function.util.uncheck
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
Expand All @@ -11,7 +10,7 @@ import kotlinx.serialization.json.JsonObject
import taboolib.common.io.getInstance

/**
* MutableJsonObject
* MutableJsonObject - 可变的 [JsonObject]
*
* @author TheFloodDragon
* @since 2024/5/22 22:09
Expand All @@ -23,8 +22,6 @@ open class MutableJsonObject(

constructor() : this(mutableMapOf())

constructor(content: Map<String, JsonElement>) : this(content.toMutableMap())

/**
* [MutableJsonObject] 存储的不可变 [JsonObject] 实例
* 其 [JsonObject.content] 应该与 [MutableJsonObject.content] 是同一个对象 (内存地址相同)
Expand Down Expand Up @@ -53,7 +50,7 @@ open class MutableJsonObject(

/**
* [MutableJsonObject] 的序列化器
* 使用 [JsonObjectSerializer] 进行序列化/反序列化
* 使用 [MutableJsonObjectSerializer.JsonObjectSerializer] 进行序列化/反序列化
*/
object MutableJsonObjectSerializer : KSerializer<MutableJsonObject> {

Expand All @@ -65,7 +62,7 @@ open class MutableJsonObject(

override val descriptor = JsonObjectSerializer.descriptor

override fun deserialize(decoder: Decoder) = MutableJsonObject(JsonObjectSerializer.deserialize(decoder))
override fun deserialize(decoder: Decoder) = MutableJsonObject(JsonObjectSerializer.deserialize(decoder).toMutableMap())

override fun serialize(encoder: Encoder, value: MutableJsonObject) = JsonObjectSerializer.serialize(encoder, value.asImmutable())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import cn.fd.ratziel.common.element.registry.NewElement
import cn.fd.ratziel.common.event.WorkspaceLoadEvent
import cn.fd.ratziel.core.element.Element
import cn.fd.ratziel.core.element.api.ElementHandler
import cn.fd.ratziel.core.serialization.baseJson
import cn.fd.ratziel.module.item.impl.ItemManager
import cn.fd.ratziel.module.item.impl.builder.DefaultItemGenerator
import cn.fd.ratziel.module.item.reflex.RefItemStack
import kotlinx.serialization.json.Json
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import taboolib.common.platform.event.SubscribeEvent
import java.util.concurrent.Executors

/**
* ItemElement
Expand All @@ -26,9 +25,11 @@ import taboolib.common.platform.event.SubscribeEvent
)
object ItemElement : ElementHandler {

val json by lazy {
Json(baseJson) {
}
/**
* 构建物品用到的线程池
*/
val executor by lazy {
Executors.newFixedThreadPool(8)
}

override fun handle(element: Element) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cn.fd.ratziel.module.item.api.common
package cn.fd.ratziel.module.item.api

import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.function.argument.DefaultArgumentFactory
import cn.fd.ratziel.module.item.api.Resolver

/**
* ArgumentResolver
Expand All @@ -18,7 +17,7 @@ interface ArgumentResolver<E, T> : Resolver<E, T> {
fun resolve(element: E, arguments: ArgumentFactory): T

/**
* 解析元素 (带空参数)
* 解析元素 (不带参数)
*/
override fun resolve(element: E): T = resolve(element, DefaultArgumentFactory())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package cn.fd.ratziel.module.item.api.builder

import cn.fd.ratziel.core.element.Element
import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.function.argument.DefaultArgumentFactory
import cn.fd.ratziel.module.item.api.NeoItem
import java.util.concurrent.CompletableFuture

/**
* ItemGenerator
* ItemGenerator - 物品构生成器
*
* @author TheFloodDragon
* @since 2023/10/28 12:20
*/
interface ItemGenerator {

/**
* 原始物品配置 (元素)
*/
val origin: Element

/**
* 构建物品
* @return [CompletableFuture] - [NeoItem]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cn.fd.ratziel.module.item.api.builder

import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.function.argument.DefaultArgumentFactory
import cn.fd.ratziel.module.item.api.Resolver
import cn.fd.ratziel.module.item.api.ArgumentResolver
import kotlinx.serialization.json.JsonElement

/**
Expand All @@ -11,16 +9,4 @@ import kotlinx.serialization.json.JsonElement
* @author TheFloodDragon
* @since 2024/4/14 12:01
*/
interface ItemResolver : Resolver<JsonElement, JsonElement> {

/**
* 解析元素 (带参数)
*/
fun resolve(element: JsonElement, arguments: ArgumentFactory): JsonElement

/**
* 解析元素 (带空参数)
*/
override fun resolve(element: JsonElement): JsonElement = resolve(element, DefaultArgumentFactory())

}
interface ItemResolver : ArgumentResolver<JsonElement, JsonElement>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cn.fd.ratziel.module.item.api.builder

import cn.fd.ratziel.core.serialization.MutableJsonObject
import cn.fd.ratziel.core.serialization.asMutable
import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.module.item.api.ArgumentResolver
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject

/**
* ItemSectionResolver
*
* @author TheFloodDragon
* @since 2024/5/24 20:53
*/
interface ItemSectionResolver : ItemResolver {

fun resolveWith(builder: MutableJsonObject, arguments: ArgumentFactory): JsonElement

fun resolveWith(element: JsonElement, arguments: ArgumentFactory): JsonElement

override fun resolve(element: JsonElement, arguments: ArgumentFactory): JsonElement =
if (element is JsonObject) resolveWith(element.asMutable(), arguments)
else resolveWith(element, arguments)

interface TagResolver : ArgumentResolver<Iterable<String>, String?> {

/**
* 解析器名称
*/
val name: String

/**
* 解析器别名
*/
val alias: Array<String>

}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 6ef9735

Please sign in to comment.