Skip to content

Commit

Permalink
Experimental | Fix & Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Aug 13, 2024
1 parent d346e49 commit ad73a28
Show file tree
Hide file tree
Showing 34 changed files with 300 additions and 116 deletions.
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ subprojects {
// Isolated Mode
enableIsolatedClassloader = true
// Common Modules
install(taboolibModules)
taboolibModules.forEach { install(it) }
}

description {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public IsolatedClassLoader(URL[] urls, ClassLoader parent) {

// 储存数据
excludedClasses.add("taboolib.common.TabooLib");
excludedClasses.add("taboolib.common.TabooLib$ClassFinder");
excludedClasses.add("taboolib.common.TabooLib$1");
excludedClasses.add("taboolib.common.TabooLib$2");
excludedClasses.add("taboolib.common.ClassAppender");
excludedClasses.add("taboolib.common.ClassAppender$Callback");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.fd.ratziel.core.serialization

import cn.fd.ratziel.function.util.uncheck
import cn.fd.ratziel.function.uncheck
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.encoding.Decoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cn.fd.ratziel.function
import cn.fd.ratziel.function.argument.Argument
import cn.fd.ratziel.function.argument.ArgumentSupplier
import cn.fd.ratziel.function.argument.exception.ArgumentException
import cn.fd.ratziel.function.util.uncheck
import cn.fd.ratziel.function.uncheck
import java.util.function.Function

fun <T : Any> ArgumentSupplier.supplyOrNull(type: Class<T>): Argument<T>? =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.fd.ratziel.function

import cn.fd.ratziel.function.util.uncheck
import java.util.concurrent.CopyOnWriteArraySet

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.fd.ratziel.function.internal

import java.util.concurrent.Future

/**
* CompletableFutureLazyImpl
*
* @author TheFloodDragon
* @since 2024/8/13 21:07
*/
internal class FutureLazyImpl<out T>(private val future: Future<T>) : Lazy<T> {

override fun isInitialized() = future.isDone

override val value: T get() = if (isInitialized()) future.get() else throw UninitializedPropertyAccessException("Lazy value not initialized yet!")

}
21 changes: 21 additions & 0 deletions project/module-core/src/main/kotlin/cn/fd/ratziel/function/lazy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.fd.ratziel.function

import cn.fd.ratziel.function.internal.FutureLazyImpl
import java.util.concurrent.Future

/**
* 将 [Future] 转为 [Lazy]
*/
fun <T> Future<T>.asLazy(): Lazy<T> = FutureLazyImpl(this)

/**
* 获取 [Lazy] 的值
* 未初始化时返回为空
*/
fun <T> Lazy<T>.getOrNull(): T? = if (isInitialized()) value else null

/**
* 获取 [Lazy] 的值
* 未初始化时返回为默认值
*/
fun <T> Lazy<T>.getOr(default: T): T = if (isInitialized()) value else default
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")

package cn.fd.ratziel.function.util
package cn.fd.ratziel.function

/**
* 简便方法 - 用于表示类型已受过检查 (忽略编译器警告)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cn.fd.ratziel.module.item

import cn.fd.ratziel.core.Priority
import cn.fd.ratziel.core.util.sortPriority
import cn.fd.ratziel.function.util.uncheck
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.function.uncheck
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.api.builder.ItemResolver
import cn.fd.ratziel.module.item.api.builder.ItemSerializer
import cn.fd.ratziel.module.item.api.registry.ComponentRegistry
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.fd.ratziel.module.item.api.builder

import cn.fd.ratziel.function.ArgumentContext

/**
* ItemStream - 物品生成过程流
*
* @author TheFloodDragon
* @since 2024/8/13 22:11
*/
interface ItemStream {

/**
* 上下文参数
*/
val context: ArgumentContext

// TODO Finish this

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package cn.fd.ratziel.module.item.api
package cn.fd.ratziel.module.item.api.builder

import cn.fd.ratziel.module.item.api.ItemData

/**
* ItemTransformer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.api.registry

import cn.fd.ratziel.core.Priority
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.builder.ItemTransformer

/**
* ComponentRegistry - 物品组件注册表
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ open class SimpleItemMaterial(private val ref: BukkitMaterial) : ItemMaterial {

override fun hashCode() = name.hashCode()

override fun equals(other: Any?) = equal(this, other)
override fun equals(other: Any?) = equals(this, other)

companion object {

fun equal(material: ItemMaterial, other: Any?) = material === other
fun equals(material: ItemMaterial, other: Any?) = material === other
|| (other as? ItemMaterial)?.name == material.name
|| (other as? BukkitMaterial)?.name == material.name
|| (other as? XMaterial)?.name == material.name
Expand Down Expand Up @@ -98,23 +98,6 @@ open class SimpleItemMaterial(private val ref: BukkitMaterial) : ItemMaterial {
ReflexClass.of(BukkitMaterial::class.java, false).structure.getField("id")
}

/**
* 类型判断
*/

fun isPotion(material: ItemMaterial) = material.name.contains("POTION", true)

private val leatherArmors by lazy {
arrayOf(
BukkitMaterial.LEATHER_HELMET.name,
BukkitMaterial.LEATHER_CHESTPLATE.name,
BukkitMaterial.LEATHER_LEGGINGS.name,
BukkitMaterial.LEATHER_BOOTS.name
)
}

fun isLeatherArmor(material: ItemMaterial) = leatherArmors.contains(material.name.uppercase())

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cn.fd.ratziel.core.serialization.getBy
import cn.fd.ratziel.core.serialization.toBasic
import cn.fd.ratziel.module.item.api.event.ItemResolveEvent
import cn.fd.ratziel.script.ScriptBlockBuilder
import cn.fd.ratziel.script.ScriptManager
import kotlinx.serialization.json.JsonObject
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.severe
Expand Down Expand Up @@ -37,7 +36,7 @@ object ActionParser {
continue
}
// 构建脚本块
val block = ScriptBlockBuilder.build(raw.value.toBasic(), ScriptManager.defaultLanguage.executor)
val block = ScriptBlockBuilder.build(raw.value.toBasic())
// 创建脚本动作, 放入表中
map[type] = ScriptedAction(block)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum class Triggers(
BREAK("onBreak", "break"),

// 耐久损伤
DAMAGED("onDamaged","damaged", "onDamage", "damage"),
DAMAGED("onDamaged", "damaged", "onDamage", "damage"),

// 交互
INTERACT_LEFT_CLICK("onLeft", "left", "onLeftClick", "leftClick"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import cn.fd.ratziel.core.element.Element
import cn.fd.ratziel.core.util.FutureFactory
import cn.fd.ratziel.core.util.sortPriority
import cn.fd.ratziel.function.ArgumentContext
import cn.fd.ratziel.function.util.uncheck
import cn.fd.ratziel.function.uncheck
import cn.fd.ratziel.module.item.ItemElement
import cn.fd.ratziel.module.item.ItemRegistry
import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.NeoItem
import cn.fd.ratziel.module.item.api.builder.ItemGenerator
import cn.fd.ratziel.module.item.api.builder.ItemResolver
import cn.fd.ratziel.module.item.api.builder.ItemSerializer
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.api.event.ItemGenerateEvent
import cn.fd.ratziel.module.item.api.event.ItemResolveEvent
import cn.fd.ratziel.module.item.impl.RatzielItem
Expand All @@ -38,7 +38,7 @@ class DefaultItemGenerator(

fun build(sourceData: ItemData.Mutable, context: ArgumentContext): CompletableFuture<NeoItem> {
// 生成物品唯一标识符
val identifier = IdentifierImpl()
val identifier = IdentifierImpl(origin.name)

// PreEvent
ItemGenerateEvent.Pre(identifier, this, context).call()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object SectionItemResolver : ItemResolver {
// 获取名称
val name = split.firstOrNull() ?: return null
// 获取解析器
val resolver = resolvers.find { it.names.contains(name) }
val resolver = matchResolver(name)
// 解析并返回
return resolver?.resolve(split.drop(1), context)
}
Expand All @@ -72,4 +72,22 @@ object SectionItemResolver : ItemResolver {
*/
const val TAG_ARG_SEPARATION = ":"

/**
* 检查字符串内是否包含标签
*/
fun checkHasTag(str: String): Boolean {
for (part in reader.readToFlatten(str)) {
if (part.isVariable &&
matchResolver(part.text.split(TAG_ARG_SEPARATION).first()) != null
) return true
}
return false
}

/**
* 匹配 [SectionTagResolver]
* @return 匹配不到时返回空
*/
fun matchResolver(name: String): SectionTagResolver? = resolvers.find { it.names.contains(name) }

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@file:OptIn(ExperimentalSerializationApi::class)
@file:Suppress("DEPRECATION")

package cn.fd.ratziel.module.item.impl.component

import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.ItemMaterial
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.impl.BukkitMaterial
import cn.fd.ratziel.module.item.impl.SimpleItemMaterial
import cn.fd.ratziel.module.item.impl.component.util.SkullData
Expand All @@ -20,6 +20,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames
import org.bukkit.Color
import org.bukkit.DyeColor
import java.util.concurrent.CompletableFuture

/**
* ItemCharacteristic
Expand All @@ -32,7 +33,7 @@ data class ItemCharacteristic(
/**
* 头颅数据
*/
@JsonNames("head", "skull-meta", "skullMeta", "head", "head-meta")
@JsonNames("head", "skull-meta", "skullMeta", "headMeta", "head-meta")
var skull: SkullData? = null,
/**
* 染色皮革物品和药水的颜色
Expand All @@ -45,8 +46,8 @@ data class ItemCharacteristic(

override fun transform(data: ItemData.Mutable, component: ItemCharacteristic) {
// 头颅处理 (当源数据的材料为空或者是PLAYER_HEAD时, 才处理相关)
if (data.material.isEmpty() || SimpleItemMaterial.equal(data.material, BukkitMaterial.PLAYER_HEAD)) {
val skullTag = component.skull?.let { RefItemStack(it) }?.getTag()
if (data.material.isEmpty() || SimpleItemMaterial.equals(data.material, BukkitMaterial.PLAYER_HEAD)) {
val skullTag = component.skull?.get()?.let { RefItemStack(it) }?.getTag()
if (skullTag != null) {
// 设置材质
data.material = SimpleItemMaterial(BukkitMaterial.PLAYER_HEAD)
Expand All @@ -56,8 +57,8 @@ data class ItemCharacteristic(
}
// 颜色处理
val node = when {
SimpleItemMaterial.isLeatherArmor(data.material) -> ItemSheet.DYED_COLOR
SimpleItemMaterial.isPotion(data.material) -> ItemSheet.POTION_COLOR
isLeatherArmor(data.material) -> ItemSheet.DYED_COLOR
isPotion(data.material) -> ItemSheet.POTION_COLOR
else -> return
}
data.write(node, component.color?.let { parseColor(it) }?.let { NBTInt(it) })
Expand All @@ -69,13 +70,13 @@ data class ItemCharacteristic(
// 头颅处理 (需要对应材质为PLAYER_HEAD)
data.material.name == BukkitMaterial.PLAYER_HEAD.name -> {
val skullMeta = RefItemMeta.of(RefItemMeta.META_SKULL, data.tag).handle
if (skullMeta.hasOwner()) impl.skull = SkullUtil.fetchSkull(skullMeta)
if (skullMeta.hasOwner()) impl.skull = CompletableFuture.completedFuture(SkullUtil.fetchSkull(skullMeta))
}
// 皮革颜色处理
SimpleItemMaterial.isLeatherArmor(data.material) ->
isLeatherArmor(data.material) ->
data.read<NBTInt>(ItemSheet.DYED_COLOR) { impl.color = it.content.toString() }
// 药水颜色处理
SimpleItemMaterial.isPotion(data.material) ->
isPotion(data.material) ->
data.read<NBTInt>(ItemSheet.POTION_COLOR) { impl.color = it.content.toString() }
}
// 返回
Expand All @@ -100,6 +101,19 @@ data class ItemCharacteristic(
}
}

fun isPotion(material: ItemMaterial) = material.name.contains("POTION", true)

private val leatherArmors by lazy {
arrayOf(
BukkitMaterial.LEATHER_HELMET.name,
BukkitMaterial.LEATHER_CHESTPLATE.name,
BukkitMaterial.LEATHER_LEGGINGS.name,
BukkitMaterial.LEATHER_BOOTS.name
)
}

fun isLeatherArmor(material: ItemMaterial) = leatherArmors.contains(material.name.uppercase())

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cn.fd.ratziel.common.message.Message
import cn.fd.ratziel.common.message.MessageComponent
import cn.fd.ratziel.core.serialization.EnhancedList
import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.nbt.NBTList
import cn.fd.ratziel.module.item.nbt.NBTString
import cn.fd.ratziel.module.item.nms.ItemSheet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package cn.fd.ratziel.module.item.impl.component

import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.nbt.NBTByte
import cn.fd.ratziel.module.item.nbt.NBTCompound
import cn.fd.ratziel.module.item.nbt.NBTInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package cn.fd.ratziel.module.item.impl.component

import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.ItemMaterial
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.nbt.NBTCompound
import kotlinx.serialization.Contextual
import kotlinx.serialization.ExperimentalSerializationApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package cn.fd.ratziel.module.item.impl.component

import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.ItemTransformer
import cn.fd.ratziel.module.item.api.builder.ItemTransformer
import cn.fd.ratziel.module.item.nbt.NBTInt
import cn.fd.ratziel.module.item.nms.ItemSheet
import cn.fd.ratziel.module.item.nms.RefItemMeta
Expand Down
Loading

0 comments on commit ad73a28

Please sign in to comment.