diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemBuildEvent.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemBuildEvent.kt similarity index 100% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemBuildEvent.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemBuildEvent.kt diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemEvent.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemEvent.kt similarity index 100% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemEvent.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemEvent.kt diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemResolvedEvent.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemResolvedEvent.kt similarity index 92% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemResolvedEvent.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemResolvedEvent.kt index 8fe7759b..3f100831 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/event/ItemResolvedEvent.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/event/ItemResolvedEvent.kt @@ -1,4 +1,4 @@ -package cn.fd.ratziel.module.item.event +package cn.fd.ratziel.module.item.api.event import cn.fd.ratziel.core.Identifier import cn.fd.ratziel.function.ArgumentContext diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/exception/NonSpecialItemException.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/exception/NonSpecialItemException.kt deleted file mode 100644 index 019a4c68..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/exception/NonSpecialItemException.kt +++ /dev/null @@ -1,18 +0,0 @@ -package cn.fd.ratziel.module.item.exception - -/** - * NonSpecialItemException - * 在校验物品标签中的信息时, 校验失败(不是特殊物品-RatzielItem)时抛出 - * - * @author TheFloodDragon - * @since 2024/1/28 14:13 - */ -class NonSpecialItemException() : IllegalStateException() { - - override var message = "It isn't a Special Item!" - - constructor(data: String) : this() { - message = "It isn't a Special Item! Unsupported data '$data'!" - } - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/exception/UnknownMaterialException.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/exception/UnknownMaterialException.kt deleted file mode 100644 index ad255b9a..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/exception/UnknownMaterialException.kt +++ /dev/null @@ -1,22 +0,0 @@ -package cn.fd.ratziel.module.item.exception - -/** - * UnknownMaterialException - * 在获取 [org.bukkit.Material] 时, 无法找到对应名称的材料时抛出 - * - * @author TheFloodDragon - * @since 2024/1/29 13:33 - */ -class UnknownMaterialException() : IllegalArgumentException() { - - override var message = "Unknown Material!" - - constructor(name: String) : this() { - message = "Unknown Material Name: '$name' !" - } - - constructor(id: Int) : this() { - message = "Unknown Material Id: '$id' !" - } - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemInfo.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemInfo.kt index 50c6dc91..c4a0b164 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemInfo.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemInfo.kt @@ -20,9 +20,9 @@ data class ItemInfo( */ val id: Identifier, /** - * 物品元素名称 + * 物品类型 (物品元素名称) */ - val name: String, + val type: String, /** * 物品元素内容的哈希值 */ @@ -44,7 +44,7 @@ data class ItemInfo( /** * [RatzielItem] 物品元素名称 */ - val RATZIEL_ELEMENT_NODE = OccupyNode("Element", RATZIEL_NODE) + val RATZIEL_ELEMENT_NODE = OccupyNode("Type", RATZIEL_NODE) /** * [RatzielItem] 物品元素内容哈希值 @@ -59,7 +59,7 @@ data class ItemInfo( val data = ComponentUtil.findByNode(tag, RATZIEL_NODE) // 写入数据 data[RATZIEL_IDENTIFIER_NODE.name] = NBTString(info.id.toString()) - data[RATZIEL_ELEMENT_NODE.name] = NBTString(info.name) + data[RATZIEL_ELEMENT_NODE.name] = NBTString(info.type) data[RATZIEL_HASH_NODE.name] = NBTInt(info.hash) } @@ -76,7 +76,7 @@ data class ItemInfo( // 合成结果 return ItemInfo( id = IdentifierImpl(id.content), - name = name.content, + type = name.content, hash = hash.content, ) } diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/RatzielItem.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/RatzielItem.kt index 06bad15b..510be3d3 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/RatzielItem.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/RatzielItem.kt @@ -15,7 +15,7 @@ import org.bukkit.inventory.ItemStack */ open class RatzielItem : NeoItem { - constructor(info: ItemInfo) : this(info, ItemDataImpl()) + constructor(info: ItemInfo) : this(info, SimpleItemData()) constructor(info: ItemInfo, data: ItemData) { // 写入数据 diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemDataImpl.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/SimpleItemData.kt similarity index 94% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemDataImpl.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/SimpleItemData.kt index c08e3dfc..d8a40d0b 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemDataImpl.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/SimpleItemData.kt @@ -5,12 +5,12 @@ import cn.fd.ratziel.module.item.api.ItemMaterial import cn.fd.ratziel.module.item.nbt.NBTCompound /** - * ItemDataImpl + * SimpleItemData * * @author TheFloodDragon * @since 2024/5/5 13:33 */ -data class ItemDataImpl( +data class SimpleItemData( /** * 物品材料 */ @@ -27,7 +27,7 @@ data class ItemDataImpl( companion object { - val EMPTY = ItemDataImpl() + val EMPTY = SimpleItemData() /** * 将 [target] 合并到 [source] 中 diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemMaterialImpl.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/SimpleItemMaterial.kt similarity index 63% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemMaterialImpl.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/SimpleItemMaterial.kt index 3220687a..3419659f 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemMaterialImpl.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/SimpleItemMaterial.kt @@ -1,80 +1,74 @@ -@file:Suppress("DEPRECATION") - package cn.fd.ratziel.module.item.impl import cn.fd.ratziel.module.item.api.ItemMaterial -import cn.fd.ratziel.module.item.exception.UnknownMaterialException import taboolib.library.reflex.ReflexClass import taboolib.library.xseries.XMaterial /** - * ItemMaterialImpl + * SimpleItemMaterial * * 由于不可抗力的影响(我不会), 仅支持 [BukkitMaterial], 即仅支持原版物品 * * @author TheFloodDragon * @since 2024/4/5 13:26 */ -data class ItemMaterialImpl(override val name: String) : ItemMaterial { +open class SimpleItemMaterial(private val ref: BukkitMaterial) : ItemMaterial { - constructor(mat: XMaterial) : this(mat.name) + constructor(name: String) : this(getBukkitMaterial(name) ?: BukkitMaterial.AIR) - constructor(mat: BukkitMaterial) : this(mat.name) + constructor(id: Int) : this(getBukkitMaterial(id) ?: BukkitMaterial.AIR) - constructor(mat: ItemMaterial) : this(mat.name) + constructor(mat: XMaterial) : this(mat.name) - constructor(id: Int) : this(getBukkitMaterial(id) ?: BukkitMaterial.AIR) + constructor(mat: ItemMaterial) : this(mat.name) /** * 材料标识符 (低版本) */ - override val id: Int get() = getIdUnsafe(insBukkit) + override val id: Int get() = getIdUnsafe(ref) /** - * 材料的默认最大堆叠数量 + * 材料名称 */ - override val maxStackSize: Int get() = insBukkit.maxStackSize + override val name: String get() = ref.name /** - * 材料的默认最大耐久度 + * 材料的默认最大堆叠数量 */ - override val maxDurability: Int get() = insBukkit.maxDurability.toInt() + override val maxStackSize: Int get() = ref.maxStackSize /** - * 材料是否为空气材料 + * 材料的默认最大耐久度 */ - fun isAir() = insBukkit.isAir || this.isEmpty() + override val maxDurability: Int get() = ref.maxDurability.toInt() /** - * 获取 [BukkitMaterial] 形式 (若获取不到则抛出异常) + * 材料是否为空气材料 */ - fun getAsBukkit(): BukkitMaterial = insBukkit + open fun isAir() = ref.isAir || this.isEmpty() /** - * 获取 [XMaterial] 形式 (若获取不到则抛出异常) + * 获取 [BukkitMaterial] 形式 */ - fun getAsXSeries(): XMaterial = insXSeries + open fun getAsBukkit(): BukkitMaterial = ref /** - * [BukkitMaterial] 形式 + * 获取 [XMaterial] 形式 */ - private val insBukkit: BukkitMaterial by lazy { getBukkitMaterial(name) ?: throw UnknownMaterialException(name) } + open fun getAsXSeries(): XMaterial = XMaterial.matchXMaterial(ref) - /** - * [XMaterial] 形式 - */ - private val insXSeries: XMaterial by lazy { XMaterial.matchXMaterial(insBukkit) } + override fun toString() = "SimpleItemMaterial(name=$name,id=$id)" - override fun hashCode() = id.hashCode() + override fun hashCode() = name.hashCode() override fun equals(other: Any?) = equal(this, other) companion object { fun equal(material: ItemMaterial, other: Any?) = material === other - || (other as? ItemMaterial)?.id == material.id - || (other as? BukkitMaterial)?.let { getIdUnsafe(it) } == material.id - || (other as? XMaterial)?.id == material.id + || (other as? ItemMaterial)?.name == material.name + || (other as? BukkitMaterial)?.name == material.name + || (other as? XMaterial)?.name == material.name /** * 获取 [BukkitMaterial] 形式的物品材料 @@ -88,7 +82,7 @@ data class ItemMaterialImpl(override val name: String) : ItemMaterial { */ val materialsMap by lazy { HashMap().apply { - BukkitMaterial.entries.forEach { put(it.name, ItemMaterialImpl(it)) } + BukkitMaterial.entries.forEach { put(it.name, SimpleItemMaterial(it)) } } } diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemGenerator.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemGenerator.kt index 2eac4c36..0f92fc7c 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemGenerator.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemGenerator.kt @@ -16,7 +16,7 @@ import cn.fd.ratziel.module.item.api.builder.ItemResolver import cn.fd.ratziel.module.item.api.builder.ItemSerializer import cn.fd.ratziel.module.item.event.ItemBuildEvent import cn.fd.ratziel.module.item.event.ItemResolvedEvent -import cn.fd.ratziel.module.item.impl.ItemDataImpl +import cn.fd.ratziel.module.item.impl.SimpleItemData import cn.fd.ratziel.module.item.impl.ItemInfo import cn.fd.ratziel.module.item.impl.RatzielItem import cn.fd.ratziel.module.item.util.toApexDataUncheck @@ -67,7 +67,7 @@ class DefaultItemGenerator( return transformFactory.thenApply { results -> // 优先级排列 (优先级低的在前面) for (data in results.mapNotNull { it }.sortPriority().reversed()) { - ItemDataImpl.merge(sourceData, data, true) // 合并数据 + SimpleItemData.merge(sourceData, data, true) // 合并数据 } // 合成最终结果 createRatzielItem(origin, sourceData, identifier).let { item -> @@ -79,7 +79,7 @@ class DefaultItemGenerator( } } - override fun build(context: ArgumentContext) = build(ItemDataImpl(), context) + override fun build(context: ArgumentContext) = build(SimpleItemData(), context) private fun resolve(element: JsonElement, context: ArgumentContext, resolvers: List): JsonElement { var result = element @@ -116,7 +116,7 @@ class DefaultItemGenerator( } // 转换成以顶级节点为根节点的数据 try { - Priority(prt.priority, transformer.toApexDataUncheck(component, ItemDataImpl())) // 封装成优先级对象后传递给合并阶段 + Priority(prt.priority, transformer.toApexDataUncheck(component, SimpleItemData())) // 封装成优先级对象后传递给合并阶段 } catch (ex: Exception) { severe("Failed to transform component by \"$transformer\"! Target component: $component") ex.printStackTrace(); null diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemCharacteristic.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemCharacteristic.kt index dbddc446..7a41cc0b 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemCharacteristic.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemCharacteristic.kt @@ -7,7 +7,7 @@ import cn.fd.ratziel.module.item.api.ItemData import cn.fd.ratziel.module.item.api.ItemNode import cn.fd.ratziel.module.item.api.ItemTransformer import cn.fd.ratziel.module.item.impl.BukkitMaterial -import cn.fd.ratziel.module.item.impl.ItemMaterialImpl +import cn.fd.ratziel.module.item.impl.SimpleItemMaterial import cn.fd.ratziel.module.item.impl.component.util.HeadUtil import cn.fd.ratziel.module.item.nbt.NBTInt import cn.fd.ratziel.module.item.nbt.read @@ -50,18 +50,18 @@ data class ItemCharacteristic( override fun transform(data: ItemData, component: ItemCharacteristic) { // 头颅处理 (当源数据的材料为空或者是PLAYER_HEAD时, 才处理相关) - if (data.material.isEmpty() || ItemMaterialImpl.equal(data.material, BukkitMaterial.PLAYER_HEAD)) { + if (data.material.isEmpty() || SimpleItemMaterial.equal(data.material, BukkitMaterial.PLAYER_HEAD)) { component.headMeta?.let { HeadUtil.getHeadTag(it) }?.let { // 设置材质 - data.material = ItemMaterialImpl(BukkitMaterial.PLAYER_HEAD) + data.material = SimpleItemMaterial(BukkitMaterial.PLAYER_HEAD) // 应用标签 data.tag.merge(it, true) } } // 颜色处理 val node = when { - ItemMaterialImpl.isLeatherArmor(data.material) -> ItemSheet.DYED_COLOR - ItemMaterialImpl.isPotion(data.material) -> ItemSheet.POTION_COLOR + SimpleItemMaterial.isLeatherArmor(data.material) -> ItemSheet.DYED_COLOR + SimpleItemMaterial.isPotion(data.material) -> ItemSheet.POTION_COLOR else -> return } data.tag.write(node, component.color?.let { parseColor(it) }?.let { NBTInt(it) }) @@ -76,10 +76,10 @@ data class ItemCharacteristic( impl.headMeta = skullMeta?.owner ?: skullMeta?.getSkullValue() ?: return impl } // 皮革颜色处理 - ItemMaterialImpl.isLeatherArmor(data.material) -> + SimpleItemMaterial.isLeatherArmor(data.material) -> data.tag.read(ItemSheet.DYED_COLOR) { impl.color = it.content.toString() } // 药水颜色处理 - ItemMaterialImpl.isPotion(data.material) -> + SimpleItemMaterial.isPotion(data.material) -> data.tag.read(ItemSheet.POTION_COLOR) { impl.color = it.content.toString() } } // 返回 diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemStack.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemStack.kt index 7fb5fc25..b67bbb87 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemStack.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemStack.kt @@ -4,8 +4,8 @@ import cn.fd.ratziel.core.exception.UnsupportedTypeException import cn.fd.ratziel.module.item.api.ItemData import cn.fd.ratziel.module.item.api.ItemMaterial import cn.fd.ratziel.module.item.impl.BukkitMaterial -import cn.fd.ratziel.module.item.impl.ItemDataImpl -import cn.fd.ratziel.module.item.impl.ItemMaterialImpl +import cn.fd.ratziel.module.item.impl.SimpleItemData +import cn.fd.ratziel.module.item.impl.SimpleItemMaterial import cn.fd.ratziel.module.item.nbt.NBTCompound import taboolib.library.reflex.Reflex.Companion.invokeConstructor import taboolib.library.reflex.Reflex.Companion.invokeMethod @@ -77,8 +77,8 @@ class RefItemStack(raw: Any) { * 获取物品数据 */ fun getData(): ItemData? { - return ItemDataImpl( - material = ItemMaterialImpl(getMaterial()), + return SimpleItemData( + material = SimpleItemMaterial(getMaterial()), tag = getTag() ?: return null, amount = getAmount() ) @@ -100,7 +100,7 @@ class RefItemStack(raw: Any) { if (MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_13)) { InternalUtil.obcGetMaterialMethod.invoke(handle)!! as BukkitMaterial } else { - ItemMaterialImpl.getBukkitMaterial(InternalUtil.obcGetMaterialMethodLegacy.invoke(handle)!! as Int)!! + SimpleItemMaterial.getBukkitMaterial(InternalUtil.obcGetMaterialMethodLegacy.invoke(handle)!! as Int)!! } /** @@ -110,12 +110,12 @@ class RefItemStack(raw: Any) { if (MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_13)) { InternalUtil.obcSetMaterialMethod.invoke(handle, material) } else { - InternalUtil.obcSetMaterialMethodLegacy.invoke(handle, ItemMaterialImpl.getIdUnsafe(material)) + InternalUtil.obcSetMaterialMethodLegacy.invoke(handle, SimpleItemMaterial.getIdUnsafe(material)) } } fun setMaterial(material: ItemMaterial) { - ItemMaterialImpl.getBukkitMaterial(material.name)?.let { setMaterial(it) } + SimpleItemMaterial.getBukkitMaterial(material.name)?.let { setMaterial(it) } } /** diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/ComponentUtil.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/ComponentUtil.kt index 487aeea6..eaa1c5ff 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/ComponentUtil.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/ComponentUtil.kt @@ -3,7 +3,7 @@ package cn.fd.ratziel.module.item.util import cn.fd.ratziel.module.item.api.ItemData import cn.fd.ratziel.module.item.api.ItemNode import cn.fd.ratziel.module.item.api.ItemTransformer -import cn.fd.ratziel.module.item.impl.ItemDataImpl +import cn.fd.ratziel.module.item.impl.SimpleItemData import cn.fd.ratziel.module.item.nbt.NBTCompound import cn.fd.ratziel.module.item.nbt.NBTData @@ -22,7 +22,7 @@ object ComponentUtil { transformer.transform(data, component) // 转换底层数据 val newTag = NBTCompound() // 创建新NBT setByNode(newTag, transformer.node, data.tag) // 设置新NBT - return ItemDataImpl(data.material, newTag, data.amount) + return SimpleItemData(data.material, newTag, data.amount) } /** @@ -30,7 +30,7 @@ object ComponentUtil { */ fun toComponent(data: ItemData, transformer: ItemTransformer): T { val find = findByNode(data.tag, transformer.node) - return transformer.detransform(ItemDataImpl(data.material, find, data.amount)) + return transformer.detransform(SimpleItemData(data.material, find, data.amount)) } fun findByNode(source: NBTCompound, tailNode: ItemNode) = findByNode(source, fold(tailNode)) diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/MetaMatcher.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/MetaMatcher.kt index f09d3c4c..05f30eda 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/MetaMatcher.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/MetaMatcher.kt @@ -4,7 +4,7 @@ package cn.fd.ratziel.module.item.util import cn.fd.ratziel.module.item.api.ItemMaterial import cn.fd.ratziel.module.item.impl.BukkitMaterial -import cn.fd.ratziel.module.item.impl.ItemMaterialImpl +import cn.fd.ratziel.module.item.impl.SimpleItemMaterial import cn.fd.ratziel.module.item.impl.component.HideFlag import org.bukkit.attribute.AttributeModifier import taboolib.common.util.Strings @@ -90,8 +90,8 @@ object MetaMatcher { val exactName: String? = BukkitMaterial.getMaterial(name)?.name // BukkitMaterial Match ?: XMaterial.matchXMaterial(name).getOrNull()?.name // XMaterial Match - return if (exactName != null) ItemMaterialImpl(exactName) - else ItemMaterialImpl.materialsMap.maxBy { Strings.similarDegree(it.key, source) }.value // Similar + return if (exactName != null) SimpleItemMaterial(exactName) + else SimpleItemMaterial.materialsMap.maxBy { Strings.similarDegree(it.key, source) }.value // Similar } private fun clean(source: String): String = source.uppercase().replace(" ", "_").replace('-', '_') diff --git a/project/module-script/src/main/java/cn/fd/ratziel/script/api/StorableScript.java b/project/module-script/src/main/java/cn/fd/ratziel/script/api/StorableScript.java index d44fd12c..354e6887 100644 --- a/project/module-script/src/main/java/cn/fd/ratziel/script/api/StorableScript.java +++ b/project/module-script/src/main/java/cn/fd/ratziel/script/api/StorableScript.java @@ -27,9 +27,8 @@ public interface StorableScript { * * @param executor 脚本执行器 * @return 脚本是否可编译 - * @throws ScriptException 编译过程中出现异常时抛出 */ - default boolean compile(@NotNull ScriptExecutor executor) throws ScriptException { + default boolean compile(@NotNull ScriptExecutor executor) { return false; } diff --git a/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScript.java b/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScript.java index be8850de..88c7af1d 100644 --- a/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScript.java +++ b/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScript.java @@ -7,6 +7,7 @@ import javax.script.Compilable; import javax.script.CompiledScript; import javax.script.ScriptException; +import java.util.concurrent.CompletableFuture; /** * SimpleScript @@ -23,35 +24,47 @@ public SimpleScript(@NotNull String content, @NotNull ScriptExecutor executor) { private final String content; private final ScriptExecutor executor; - private CompiledScript compiledScript = null; + private CompletableFuture future = null; @Override public @Nullable Object evaluate(@NotNull ScriptEnvironment environment) throws ScriptException { - // 已存在编译过的脚本, 或者脚本可被编译(已通过compile方法编译) - if (compiledScript != null || compile(getExecutor())) { + CompiledScript compiled = getCompiled(); + // 已存在编译过的脚本 + if (compiled != null) { // 直接用编译后的脚本评估 - return compiledScript.eval(environment.getBindings()); + return compiled.eval(environment.getBindings()); } else { + // 尝试编译脚本 (compile是异步执行的, 不然也不敢放在这) + compile(getExecutor()); // 通过执行器评估脚本 return getExecutor().evaluate(this, environment); } } @Override - public boolean compile(@NotNull ScriptExecutor executor) throws ScriptException { + public boolean compile(@NotNull ScriptExecutor executor) { if (executor instanceof Compilable) { // 编译脚本 - this.compiledScript = ((Compilable) executor).compile(getContent()); + future = CompletableFuture.supplyAsync(() -> { + try { + return ((Compilable) executor).compile(getContent()); + } catch (Exception e) { + //noinspection CallToPrintStackTrace + e.printStackTrace(); + } + return null; + }); return true; } else return false; } @Override public @Nullable CompiledScript getCompiled() { - return compiledScript; + if (future.isDone()) + return future.getNow(null); + else return null; } - @Override public @NotNull ScriptExecutor getExecutor() { return executor; diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptBlockBuilder.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptBlockBuilder.kt index 8a1a49fc..166749e5 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptBlockBuilder.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptBlockBuilder.kt @@ -58,7 +58,7 @@ object ScriptBlockBuilder { constructor(content: String, executor: ScriptExecutor) : this(SimpleScript(content, executor), executor) init { - // 预编译 + // 预编译 (SimpleScript#compile()为异步执行, 一般情况下这里也是异步的) if (script is StorableScript) script.compile(executor) }