Skip to content

Commit

Permalink
Experimental | Update Skull&Renames&Optimize [skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Aug 12, 2024
1 parent 973ccc4 commit 9436bc5
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
* @author TheFloodDragon
* @since 2024/7/16 11:56
*/
@SuppressWarnings("NullableProblems")
public interface ArgumentContext {

/**
* 弹出第一个指定类型的参数
*
* @throws NullPointerException 当无法找到指定类型的参数时抛出
*/
@NotNull
default <T> T pop(@NotNull Class<T> type) throws NullPointerException {
default <@NotNull T> @NotNull T pop(@NotNull Class<T> type) throws NullPointerException {
T result = popOrNull(type);
if (result == null) throw new NullPointerException("Cannot find argument: " + type.getName() + " !");
return result;
Expand All @@ -28,15 +28,13 @@ default <T> T pop(@NotNull Class<T> type) throws NullPointerException {
* 弹出第一个指定类型的参数
* 若无法找到, 则返回空
*/
@Nullable
<T> T popOrNull(@NotNull Class<T> type);
<@NotNull T> @Nullable T popOrNull(@NotNull Class<T> type);

/**
* 弹出第一个指定类型的参数
* 若无法找到, 则返回默认值
*/
@NotNull
default <T> T popOr(@NotNull Class<T> type, @NotNull T def) {
default <@NotNull T> @NotNull T popOr(@NotNull Class<T> type, @NotNull T def) {
T result = popOrNull(type);
if (result == null) return def;
return result;
Expand All @@ -45,8 +43,7 @@ default <T> T popOr(@NotNull Class<T> type, @NotNull T def) {
/**
* 弹出所有指定类型的参数
*/
@NotNull
<T> Iterable<? extends T> popAll(@NotNull Class<T> type);
<@NotNull T> @NotNull Iterable<? extends T> popAll(@NotNull Class<T> type);

/**
* 添加一个参数元素
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import cn.fd.ratziel.module.item.api.NeoItem
import cn.fd.ratziel.module.item.api.builder.ItemGenerator

/**
* ItemBuildEvent
* ItemGenerateEvent
*
* @author TheFloodDragon
* @since 2024/7/3 14:26
*/
class ItemBuildEvent {
class ItemGenerateEvent {

/**
* 物品构建之前触发
* 物品生成之前触发
*/
class Pre(
/**
Expand All @@ -32,7 +32,7 @@ class ItemBuildEvent {
) : ItemEvent(identifier)

/**
* 物品构建结束后触发
* 物品生成结束后触发
*/
class Post(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import cn.fd.ratziel.function.ArgumentContext
import kotlinx.serialization.json.JsonElement

/**
* ItemResolvedEvent
* ItemResolveEvent
*
* 构建物品中的所有解析阶段完成后触发
*
* @author TheFloodDragon
* @since 2024/7/3 15:15
*/
class ItemResolvedEvent(
class ItemResolveEvent(
/**
* 物品标识符
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cn.fd.ratziel.script.api.ScriptEnvironment
* @author TheFloodDragon
* @since 2024/7/3 15:11
*/
@Deprecated("需要重构")
interface ItemAction {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cn.fd.ratziel.module.item.api.feature
* @author TheFloodDragon
* @since 2024/7/3 18:41
*/
@Deprecated("需要重构")
interface ItemTrigger {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import cn.fd.ratziel.module.item.util.ComponentUtil
* @author TheFloodDragon
* @since 2024/6/28 15:27
*/
@Deprecated("影响物品表现")
data class ItemInfo(
/**
* 物品唯一标识符 [Identifier]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ 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.event.ItemBuildEvent
import cn.fd.ratziel.module.item.api.event.ItemResolvedEvent
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.ItemInfo
import cn.fd.ratziel.module.item.impl.RatzielItem
import cn.fd.ratziel.module.item.impl.SimpleItemData
Expand All @@ -42,12 +42,12 @@ class DefaultItemGenerator(
val identifier = IdentifierImpl()

// PreEvent
ItemBuildEvent.Pre(identifier, this, context).call()
ItemGenerateEvent.Pre(identifier, this, context).call()

// Step1: Resolve (with priorities)
val element = resolve(origin.property, context, ItemRegistry.Resolver.getResolversSorted())
// ResolvedEvent
ItemResolvedEvent(identifier, element, context).call()
ItemResolveEvent(identifier, element, context).call()

// Step2: Serialize (async)
val serializeFactory = FutureFactory<Any?>()
Expand All @@ -72,7 +72,7 @@ class DefaultItemGenerator(
// 合成最终结果
createRatzielItem(origin, sourceData, identifier).let { item ->
// PostEvent
val event = ItemBuildEvent.Post(item.identifier, this, item, context)
val event = ItemGenerateEvent.Post(item.identifier, this, item, context)
event.call()
event.item
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ 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.SimpleItemMaterial
import cn.fd.ratziel.module.item.impl.component.util.HeadUtil
import cn.fd.ratziel.module.item.impl.component.util.SkullData
import cn.fd.ratziel.module.item.impl.component.util.SkullUtil
import cn.fd.ratziel.module.item.nbt.NBTInt
import cn.fd.ratziel.module.item.nbt.read
import cn.fd.ratziel.module.item.nbt.write
import cn.fd.ratziel.module.item.nms.ItemSheet
import cn.fd.ratziel.module.item.nms.RefItemMeta
import cn.fd.ratziel.module.item.nms.RefItemStack
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames
import org.bukkit.Color
import org.bukkit.DyeColor
import org.bukkit.inventory.meta.SkullMeta
import taboolib.library.xseries.XSkull
import taboolib.platform.util.getSkullValue

/**
* ItemCharacteristic
Expand All @@ -32,11 +31,10 @@ import taboolib.platform.util.getSkullValue
@Serializable
data class ItemCharacteristic(
/**
* 头颅元数据:
* 使用字符串存储, 值的类型详见 [XSkull.ValueType]
* 头颅数据
*/
@JsonNames("skull", "skull-meta", "skullMeta", "head", "head-meta")
var headMeta: String? = null,
@JsonNames("head", "skull-meta", "skullMeta", "head", "head-meta")
var skull: SkullData? = null,
/**
* 染色皮革物品和药水的颜色
*/
Expand All @@ -51,11 +49,12 @@ data class ItemCharacteristic(
override fun transform(data: ItemData.Mutable, component: ItemCharacteristic) {
// 头颅处理 (当源数据的材料为空或者是PLAYER_HEAD时, 才处理相关)
if (data.material.isEmpty() || SimpleItemMaterial.equal(data.material, BukkitMaterial.PLAYER_HEAD)) {
component.headMeta?.let { HeadUtil.getHeadTag(it) }?.let {
val skullTag = component.skull?.let { RefItemStack(it) }?.getTag()
if (skullTag != null) {
// 设置材质
data.material = SimpleItemMaterial(BukkitMaterial.PLAYER_HEAD)
// 应用标签
data.tag.merge(it, true)
data.tag.merge(skullTag, true)
}
}
// 颜色处理
Expand All @@ -72,8 +71,8 @@ data class ItemCharacteristic(
when {
// 头颅处理 (需要对应材质为PLAYER_HEAD)
data.material.name == BukkitMaterial.PLAYER_HEAD.name -> {
val skullMeta = RefItemMeta.of(RefItemMeta.META_SKULL, data.tag).handle as? SkullMeta
impl.headMeta = skullMeta?.owner ?: skullMeta?.getSkullValue() ?: return impl
val skullMeta = RefItemMeta.of(RefItemMeta.META_SKULL, data.tag).handle
if (skullMeta.hasOwner()) impl.skull = SkullUtil.fetchSkull(skullMeta)
}
// 皮革颜色处理
SimpleItemMaterial.isLeatherArmor(data.material) ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@file:OptIn(ExperimentalSerializationApi::class)

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

import cn.fd.ratziel.module.item.impl.BukkitMaterial
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.SkullMeta
import taboolib.library.xseries.profiles.builder.XSkull
import taboolib.library.xseries.profiles.objects.Profileable
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap

typealias SkullData = @Serializable(SkullUtil.Serializer::class) ItemStack

/**
* SkullUtil - 头颅工具
*
* @author TheFloodDragon
* @since 2024/8/12 16:13
*/
object SkullUtil {

/**
* 头颅缓存
*/
private val CACHE: MutableMap<String, CompletableFuture<SkullData>> = ConcurrentHashMap()

fun fetchSkull(profileStr: String): ItemStack {
return fetchSkullFuture(profileStr).get()
}

fun fetchSkullFuture(profileStr: String): CompletableFuture<SkullData> {
return CACHE.computeIfAbsent(profileStr) { generateSkullItem(profileStr) }
}

fun generateSkullItem(profileStr: String): CompletableFuture<SkullData> {
return XSkull.createItem().profile(Profileable.detect(profileStr)).applyAsync()
}

fun fetchSkull(skullMeta: SkullMeta): SkullData {
return ItemStack(BukkitMaterial.PLAYER_HEAD).apply { itemMeta = skullMeta }
}

fun fetchProfileString(skullItem: ItemStack): String? {
return XSkull.of(skullItem).profileString
}

object Serializer : KSerializer<SkullData> {

override val descriptor = PrimitiveSerialDescriptor("item.SkullData", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): SkullData {
return fetchSkull(decoder.decodeString())
}

override fun serialize(encoder: Encoder, value: SkullData) {
val profileString = fetchProfileString(value)
if (profileString == null) encoder.encodeNull() else encoder.encodeString(profileString)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cn.fd.ratziel.module.item.impl.feature.action

import cn.fd.ratziel.core.serialization.getBy
import cn.fd.ratziel.core.serialization.toBasic
import cn.fd.ratziel.module.item.api.event.ItemResolvedEvent
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
Expand Down Expand Up @@ -45,7 +45,7 @@ object ActionParser {
}

@SubscribeEvent
fun onResolvingFinished(event: ItemResolvedEvent) {
fun onResolvingFinished(event: ItemResolveEvent) {
val element = event.result as? JsonObject ?: return
// 获取原始动作
val raw = element.getBy(nodeNames.asIterable()) ?: return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.impl.feature.action.triggers

import cn.fd.ratziel.module.item.api.feature.ItemTrigger
import cn.fd.ratziel.module.item.api.event.ItemBuildEvent
import cn.fd.ratziel.module.item.api.event.ItemGenerateEvent
import cn.fd.ratziel.module.item.impl.RatzielItem
import cn.fd.ratziel.module.item.impl.feature.action.ActionManager
import cn.fd.ratziel.module.item.nms.RefItemStack
Expand All @@ -18,7 +18,7 @@ internal object ReleaseTrigger : ItemTrigger {
override val names = arrayOf("onRelease", "release", "onFinished", "onFinish", "finish", "onEnd", "end")

@SubscribeEvent
fun onRelease(event: ItemBuildEvent.Post) {
fun onRelease(event: ItemGenerateEvent.Post) {
val neoItem = event.item as? RatzielItem ?: return
// 获取攻击时的物品
val item = RefItemStack(event.item.data).getAsBukkit()
Expand Down

0 comments on commit 9436bc5

Please sign in to comment.