diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/ItemElement.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/ItemElement.kt index 1d058626..4db3b280 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/ItemElement.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/ItemElement.kt @@ -53,7 +53,7 @@ object ItemElement : ElementHandler { println(test.getData()) // 注册 - ItemManager.registry[element.name] = DefaultItemGenerator + ItemManager.registry[element.name] = element } @SubscribeEvent diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/ItemComponent.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/ItemComponent.kt deleted file mode 100644 index d502b262..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/ItemComponent.kt +++ /dev/null @@ -1,32 +0,0 @@ -package cn.fd.ratziel.module.item.api - -import cn.fd.ratziel.module.item.impl.TheItemData -import cn.fd.ratziel.module.item.nbt.NBTCompound - -/** - * ItemComponent - 物品组件 - * - * @author TheFloodDragon - * @since 2024/4/20 9:14 - */ -@Deprecated("") -interface ItemComponent : Transformable { - - /** - * 获取数据的节点分配器 - */ - fun getNode(): ItemNode - - /** - * 正向转化 - 输出型转化 (可对[source]进行任意操作) - * @param source 源标签 - */ - fun transform(source: ItemData) - - /** - * 正向转化 - 输出型转化 - * 重写并调用 [transform] 方法, 默认使用空的 [NBTCompound] 作为源数据 - */ - override fun transform(): ItemData = TheItemData().also { transform(it) } - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/Transformable.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/Transformable.kt deleted file mode 100644 index 1ca6c1d9..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/Transformable.kt +++ /dev/null @@ -1,22 +0,0 @@ -package cn.fd.ratziel.module.item.api - -/** - * Transformable - 可转换的 - * - * @author TheFloodDragon - * @since 2024/4/20 09:11 - */ -@Deprecated("") -interface Transformable { - - /** - * 正向转化 - 输出型转换 - */ - fun transform(): T - - /** - * 反向转化 - 应用型转换 (禁止改写[target]) - */ - fun detransform(target: T) - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemDisplay.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemDisplay.kt deleted file mode 100644 index b51bd172..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemDisplay.kt +++ /dev/null @@ -1,45 +0,0 @@ -package cn.fd.ratziel.module.item.api.part - -import cn.fd.ratziel.common.message.MessageComponent -import cn.fd.ratziel.core.serialization.EnhancedList -import cn.fd.ratziel.module.item.api.ItemComponent - -/** - * ItemDisplay - * - * @author TheFloodDragon - * @since 2024/3/16 10:56 - */ -interface ItemDisplay : ItemComponent { - - /** - * 物品名称 - */ - var name: MessageComponent? - - /** - * 物品本地化名称 - */ - var localizedName: MessageComponent? - - /** - * 物品描述 - */ - var lore: EnhancedList? - - /** - * 设置名称 - */ - fun setName(name: String) - - /** - * 设置描述 - */ - fun setLore(lore: Iterable) - - /** - * 设置物品本地化名称 - */ - fun setLocalizedName(localizedName: String) - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemDurability.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemDurability.kt deleted file mode 100644 index e388c5f1..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemDurability.kt +++ /dev/null @@ -1,28 +0,0 @@ -package cn.fd.ratziel.module.item.api.part - -import cn.fd.ratziel.module.item.api.ItemComponent - -/** - * ItemDurability - * - * @author TheFloodDragon - * @since 2024/4/13 16:02 - */ -interface ItemDurability : ItemComponent { - - /** - * 物品的最大耐久 - */ - var maxDurability: Int? - - /** - * 物品的修复消耗 - */ - var repairCost: Int? - - /** - * 物品是否无法破坏 - */ - var unbreakable: Boolean? - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemMetadata.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemMetadata.kt deleted file mode 100644 index 78e3fce4..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemMetadata.kt +++ /dev/null @@ -1,34 +0,0 @@ -package cn.fd.ratziel.module.item.api.part - -import cn.fd.ratziel.module.item.api.ItemComponent -import cn.fd.ratziel.module.item.api.ItemMaterial - -/** - * ItemMetadata - * - * @author TheFloodDragon - * @since 2024/4/4 20:06 - */ -interface ItemMetadata : ItemComponent { - - /** - * 物品材料 - */ - var material: ItemMaterial - - /** - * 物品显示部分 - */ - var display: ItemDisplay - - /** - * 物品耐久部分 - */ - var durability: ItemDurability - - /** - * 物品杂项部分 - */ - var sundry: ItemSundry - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemSundry.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemSundry.kt deleted file mode 100644 index 6d507bfe..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/api/part/ItemSundry.kt +++ /dev/null @@ -1,32 +0,0 @@ -package cn.fd.ratziel.module.item.api.part - -import cn.fd.ratziel.module.item.api.ItemComponent -import org.bukkit.attribute.Attribute -import org.bukkit.attribute.AttributeModifier - -/** - * ItemSundry - 物品杂项 - * - * @author TheFloodDragon - * @since 2024/5/3 20:59 - */ -interface ItemSundry : ItemComponent { - - /** - * 物品自定义模型数据 (1.14+) - */ - var customModelData: Int? - - /** - * 物品隐藏标签 (1.20.5- 但仍能使用) - */ - var hideFlags: MutableSet? - - /** - * 物品属性修饰符 - */ - var bukkitAttributes: MutableMap>? - -} - -typealias HideFlag = org.bukkit.inventory.ItemFlag \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/command/ItemCommand.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/command/ItemCommand.kt index 2a78f541..c484e097 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/command/ItemCommand.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/command/ItemCommand.kt @@ -3,6 +3,7 @@ package cn.fd.ratziel.module.item.command import cn.fd.ratziel.function.argument.DefaultArgumentFactory import cn.fd.ratziel.function.argument.PlayerArgument import cn.fd.ratziel.module.item.impl.ItemManager +import cn.fd.ratziel.module.item.impl.builder.DefaultItemGenerator import cn.fd.ratziel.module.item.nms.RefItemStack import org.bukkit.entity.Player import taboolib.common.platform.ProxyPlayer @@ -43,12 +44,13 @@ object ItemCommand { val amount = ctx.intOrNull("amount") ?: 1 val players = ctx.players("player") players.forEach { player -> - val generator = ItemManager.registry[id] ?: TODO("ERROR") + val element = ItemManager.registry[id] ?: TODO("ERROR") val args = DefaultArgumentFactory().apply { add(PlayerArgument(player)) } - generator.build(args).thenAccept { + DefaultItemGenerator.build(element,args).thenAccept { val item = RefItemStack(it.data).getAsBukkit().apply { setAmount(amount) } + println(item) submit { player.cast().giveItem(item) } diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemManager.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemManager.kt index 61c5ef8a..eb511e61 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemManager.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/ItemManager.kt @@ -1,6 +1,6 @@ package cn.fd.ratziel.module.item.impl -import cn.fd.ratziel.module.item.api.builder.ItemGenerator +import cn.fd.ratziel.core.element.Element import java.util.concurrent.ConcurrentHashMap /** @@ -13,8 +13,7 @@ object ItemManager { /** * 物品注册表 - * 物品标识符:物品生成器 */ - val registry: MutableMap = ConcurrentHashMap() + val registry: MutableMap = ConcurrentHashMap() } \ No newline at end of file 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 2c637e10..10953196 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 @@ -14,7 +14,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.impl.RatzielItem import cn.fd.ratziel.module.item.impl.TheItemData -import cn.fd.ratziel.module.item.impl.component.VItemDisplay +import cn.fd.ratziel.module.item.impl.component.ItemDisplay import cn.fd.ratziel.module.item.util.toApexDataUncheck import taboolib.common.platform.function.severe import java.util.concurrent.CompletableFuture @@ -46,7 +46,7 @@ object DefaultItemGenerator : ItemGenerator { * 物品转换器 */ val transformers = HashMap, Priority>>().apply { - put(VItemDisplay::class.java, VItemDisplay.priority()) + put(ItemDisplay::class.java, ItemDisplay.priority()) } // /** diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemSerializer.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemSerializer.kt index 122fb99d..695e530d 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemSerializer.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/DefaultItemSerializer.kt @@ -4,12 +4,8 @@ import cn.fd.ratziel.core.serialization.* import cn.fd.ratziel.core.serialization.serializers.UUIDSerializer import cn.fd.ratziel.module.item.ItemElement import cn.fd.ratziel.module.item.api.ItemMaterial -import cn.fd.ratziel.module.item.api.part.HideFlag import cn.fd.ratziel.module.item.impl.ItemKSerializer -import cn.fd.ratziel.module.item.impl.component.VItemDisplay -import cn.fd.ratziel.module.item.impl.component.VItemDurability -import cn.fd.ratziel.module.item.impl.component.VItemMeta -import cn.fd.ratziel.module.item.impl.component.VItemSundry +import cn.fd.ratziel.module.item.impl.component.* import cn.fd.ratziel.module.item.impl.component.serializers.* import cn.fd.ratziel.module.item.nbt.NBTData import cn.fd.ratziel.module.item.nbt.NBTSerializer @@ -29,7 +25,7 @@ import java.util.concurrent.CompletableFuture * @author TheFloodDragon * @since 2024/4/4 19:58 */ -object DefaultItemSerializer : ItemKSerializer { +object DefaultItemSerializer : ItemKSerializer { val json = Json(baseJson) { serializersModule += SerializersModule { @@ -50,45 +46,45 @@ object DefaultItemSerializer : ItemKSerializer { * 使用到的序列化器列表 */ val serializers = arrayOf( - VItemMeta.serializer(), - VItemDisplay.serializer(), - VItemDurability.serializer(), - VItemSundry.serializer(), + ItemMeta.serializer(), + ItemDisplay.serializer(), + ItemDurability.serializer(), + ItemSundry.serializer(), ) - override val descriptor = VItemMeta.serializer().descriptor + override val descriptor = ItemMeta.serializer().descriptor /** * 反序列化 (检查结构化解析) */ - override fun deserialize(element: JsonElement): VItemMeta { + override fun deserialize(element: JsonElement): ItemMeta { // 结构化解析 - if (isStructured(element)) return json.decodeFromJsonElement(VItemMeta.serializer(), element) + if (isStructured(element)) return json.decodeFromJsonElement(ItemMeta.serializer(), element) // 异步方法 fun asyncDecode(deserializer: DeserializationStrategy, from: JsonElement = element) = CompletableFuture.supplyAsync({ json.decodeFromJsonElement(deserializer, from) }, ItemElement.executor).exceptionally { it.printStackTrace();null } // 一般解析 - val display = asyncDecode(VItemDisplay.serializer()) - val durability = asyncDecode(VItemDurability.serializer()) - val sundry = asyncDecode(VItemSundry.serializer()) + val display = asyncDecode(ItemDisplay.serializer()) + val durability = asyncDecode(ItemDurability.serializer()) + val sundry = asyncDecode(ItemSundry.serializer()) val material = (element as? JsonObject)?.get(NODES_MATERIAL) ?.let { asyncDecode(ItemMaterialSerializer, it) } ?: CompletableFuture.completedFuture(ItemMaterial.EMPTY) - return VItemMeta(material.get(), display.get(), durability.get(), sundry.get()) + return ItemMeta(material.get(), display.get(), durability.get(), sundry.get()) } /** * 序列化 (强制开启结构化解析) */ - override fun serialize(component: VItemMeta) = forceStructured(json.encodeToJsonElement(VItemMeta.serializer(), component)) + override fun serialize(component: ItemMeta) = forceStructured(json.encodeToJsonElement(ItemMeta.serializer(), component)) /** * 占据的节点 */ val occupiedNodes = serializers.flatMap { it.descriptor.elementAlias } - val NODES_MATERIAL = VItemMeta.serializer().descriptor.getElementNames(VItemMeta::material.name) + val NODES_MATERIAL = ItemMeta.serializer().descriptor.getElementNames(ItemMeta::material.name) /** * 结构化解析 diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemDisplay.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemDisplay.kt similarity index 68% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemDisplay.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemDisplay.kt index 48dd21a2..ffb8645d 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemDisplay.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemDisplay.kt @@ -9,11 +9,9 @@ import cn.fd.ratziel.core.util.putNonNull import cn.fd.ratziel.module.item.api.ItemData import cn.fd.ratziel.module.item.api.ItemTransformer import cn.fd.ratziel.module.item.impl.OccupyNode -import cn.fd.ratziel.module.item.api.part.ItemDisplay import cn.fd.ratziel.module.item.impl.TheItemData import cn.fd.ratziel.module.item.nbt.NBTList import cn.fd.ratziel.module.item.nbt.NBTString -import cn.fd.ratziel.module.item.nbt.addAll import cn.fd.ratziel.module.item.nms.ItemSheet import cn.fd.ratziel.module.item.util.castThen import kotlinx.serialization.ExperimentalSerializationApi @@ -23,56 +21,33 @@ import net.kyori.adventure.text.Component import taboolib.module.nms.MinecraftVersion /** - * VItemDisplay + * ItemDisplay * * @author TheFloodDragon * @since 2024/3/16 11:27 */ @Serializable -data class VItemDisplay( +data class ItemDisplay( @JsonNames("name", "display-name", "displayName") - override var name: MessageComponent? = null, + var name: MessageComponent? = null, @JsonNames("loc-name", "locName", "local-name", "localName") - override var localizedName: MessageComponent? = null, + var localizedName: MessageComponent? = null, @JsonNames("lores") - override var lore: EnhancedList? = null, -) : ItemDisplay { + var lore: EnhancedList? = null, +) { - override fun setName(name: String) { + fun setName(name: String) { this.name = Message.buildMessage(name) } - override fun setLore(lore: Iterable) { + fun setLore(lore: Iterable) { this.lore = lore.map { Message.buildMessage(it) } } - override fun setLocalizedName(localizedName: String) { + fun setLocalizedName(localizedName: String) { this.localizedName = Message.buildMessage(localizedName) } - override fun getNode() = node - - override fun transform(source: ItemData) { - source.tag.addAll( - ItemSheet.DISPLAY_NAME to componentToData(this.name), - ItemSheet.DISPLAY_LORE to this.lore?.mapNotNull { componentToData(it) }?.let { NBTList(it) }, - ItemSheet.DISPLAY_LOCAL_NAME to componentToData(this.localizedName) - ) - } - - override fun detransform(target: ItemData) { - // Universal - target.tag[ItemSheet.DISPLAY_NAME].castThen { - this.setName(it.content) - } - target.tag[ItemSheet.DISPLAY_LORE].castThen { - this.setLore(it.content.mapNotNull { line -> (line as? NBTString)?.content }) - } - target.tag[ItemSheet.DISPLAY_LOCAL_NAME].castThen { - this.setLocalizedName(it.content) - } - } - companion object : ItemTransformer { override val node = @@ -98,7 +73,7 @@ data class VItemDisplay( tag.putNonNull(ItemSheet.DISPLAY_LOCAL_NAME, componentToData(component.localizedName)) } - override fun detransform(data: ItemData): ItemDisplay = VItemDisplay().apply { + override fun detransform(data: ItemData): ItemDisplay = ItemDisplay().apply { data.castThen(ItemSheet.DISPLAY_NAME) { this.setName(it.content) } data.castThen(ItemSheet.DISPLAY_LORE) { this.setLore(it.content.mapNotNull { line -> (line as? NBTString)?.content }) diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemDurability.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemDurability.kt similarity index 60% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemDurability.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemDurability.kt index 7bfda4e9..31712b32 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemDurability.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemDurability.kt @@ -6,12 +6,10 @@ import cn.fd.ratziel.core.util.putNonNull import cn.fd.ratziel.module.item.api.ItemData import cn.fd.ratziel.module.item.api.ItemTransformer import cn.fd.ratziel.module.item.impl.OccupyNode -import cn.fd.ratziel.module.item.api.part.ItemDurability import cn.fd.ratziel.module.item.impl.TheItemData import cn.fd.ratziel.module.item.nbt.NBTByte import cn.fd.ratziel.module.item.nbt.NBTCompound import cn.fd.ratziel.module.item.nbt.NBTInt -import cn.fd.ratziel.module.item.nbt.addAll import cn.fd.ratziel.module.item.nms.ItemSheet import cn.fd.ratziel.module.item.util.castThen import kotlinx.serialization.ExperimentalSerializationApi @@ -20,53 +18,26 @@ import kotlinx.serialization.json.JsonNames import taboolib.module.nms.MinecraftVersion /** - * VItemDurability + * ItemDurability * * @author TheFloodDragon * @since 2024/4/13 16:05 */ @Serializable -data class VItemDurability( +data class ItemDurability( @JsonNames("maxDamage", "max-damage", "max-durability", "durability") - override var maxDurability: Int? = null, + var maxDurability: Int? = null, @JsonNames("repair-cost") - override var repairCost: Int? = null, + var repairCost: Int? = null, @JsonNames("isUnbreakable") - override var unbreakable: Boolean? = null -) : ItemDurability { - - override fun getNode() = OccupyNode.APEX_NODE - - override fun transform(source: ItemData) { - source.tag.addAll(ItemSheet.REPAIR_COST to this.repairCost?.let { NBTInt(it) }, ItemSheet.MAX_DAMAGE to this.maxDurability?.let { NBTInt(it) }) - // 无法破坏部分的特殊处理 - if (MinecraftVersion.majorLegacy >= 12005) { - if (unbreakable == true && source.tag[ItemSheet.UNBREAKABLE] == null) { - source.tag.put(ItemSheet.UNBREAKABLE, NBTCompound()) - } else { - source.tag.remove(ItemSheet.UNBREAKABLE) - } - } else source.tag[ItemSheet.UNBREAKABLE] = unbreakable?.let { NBTByte(it) } - } - - override fun detransform(target: ItemData) { - target.tag[ItemSheet.REPAIR_COST].castThen { - this.repairCost = it.content - } - target.tag[ItemSheet.MAX_DAMAGE].castThen { - this.maxDurability = it.content - } - // 无法破坏部分的特殊处理 - val unsure = target.tag[ItemSheet.UNBREAKABLE] - if (MinecraftVersion.majorLegacy >= 12005) this.unbreakable = unsure != null - else if (unsure != null) this.unbreakable = NBTByte.adapt((unsure as NBTByte).content) - } + var unbreakable: Boolean? = null +) { companion object : ItemTransformer { override val node = OccupyNode.APEX_NODE - override fun detransform(data: ItemData): ItemDurability = VItemDurability().apply { + override fun detransform(data: ItemData): ItemDurability = ItemDurability().apply { data.castThen(ItemSheet.REPAIR_COST) { this.repairCost = it.content } data.castThen(ItemSheet.MAX_DAMAGE) { this.maxDurability = it.content } // 无法破坏部分的特殊处理 diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemMeta.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemMeta.kt new file mode 100644 index 00000000..605ceaff --- /dev/null +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemMeta.kt @@ -0,0 +1,53 @@ +@file:OptIn(ExperimentalSerializationApi::class) + +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.impl.OccupyNode +import cn.fd.ratziel.module.item.impl.TheItemData +import cn.fd.ratziel.module.item.util.toApexComponent +import cn.fd.ratziel.module.item.util.toApexData +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonNames + +/** + * VItemMeta + * + * @author TheFloodDragon + * @since 2024/4/4 20:10 + */ +@Serializable +data class ItemMeta( + @JsonNames("mat", "mats", "materials") + var material: ItemMaterial = ItemMaterial.EMPTY, + var display: ItemDisplay = ItemDisplay(), + var durability: ItemDurability = ItemDurability(), + var sundry: ItemSundry = ItemSundry() +) { + + companion object : ItemTransformer { + + override val node = OccupyNode.APEX_NODE + + override fun detransform(data: ItemData) = ItemMeta( + material = data.material, + display = ItemDisplay.toApexComponent(data), + durability = ItemDurability.toApexComponent(data), + sundry = ItemSundry.toApexComponent(data), + ) + + override fun transform(component: ItemMeta): ItemData { + val data = TheItemData() + data.material = component.material + TheItemData.mergeShallow(data, ItemDisplay.toApexData(component.display)) + TheItemData.mergeShallow(data, ItemDurability.toApexData(component.durability)) + TheItemData.mergeShallow(data, ItemSundry.toApexData(component.sundry)) + return data + } + + } + +} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemSundry.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemSundry.kt similarity index 65% rename from project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemSundry.kt rename to project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemSundry.kt index 8d80a9a3..0aa1711d 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemSundry.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/ItemSundry.kt @@ -6,8 +6,6 @@ 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.impl.OccupyNode -import cn.fd.ratziel.module.item.api.part.HideFlag -import cn.fd.ratziel.module.item.api.part.ItemSundry import cn.fd.ratziel.module.item.impl.TheItemData import cn.fd.ratziel.module.item.nbt.NBTInt import cn.fd.ratziel.module.item.nms.ItemSheet @@ -22,6 +20,8 @@ import org.bukkit.attribute.AttributeModifier import org.bukkit.inventory.EquipmentSlot import taboolib.module.nms.MinecraftVersion +typealias HideFlag = org.bukkit.inventory.ItemFlag + /** * VItemSundry * @@ -29,14 +29,14 @@ import taboolib.module.nms.MinecraftVersion * @since 2024/5/3 21:06 */ @Serializable -data class VItemSundry( +data class ItemSundry( @JsonNames("custom-model-data") - override var customModelData: Int? = null, + var customModelData: Int? = null, @JsonNames("hideflag", "hideflags", "hideFlag") - override var hideFlags: MutableSet<@Contextual HideFlag>? = null, + var hideFlags: MutableSet<@Contextual HideFlag>? = null, @JsonNames("attribute-modifiers", "attributeModifiers", "bukkit-attributes") - override var bukkitAttributes: MutableMap<@Contextual Attribute, MutableList<@Contextual AttributeModifier>>? = null -) : ItemSundry { + var bukkitAttributes: MutableMap<@Contextual Attribute, MutableList<@Contextual AttributeModifier>>? = null +) { /** * 添加物品隐藏标签 @@ -66,44 +66,6 @@ data class VItemSundry( fun removeAttributeModifiers(slot: EquipmentSlot) = bukkitAttributes?.forEach { (key, value) -> value.forEach { if (it.slot == slot) bukkitAttributes?.get(key)?.remove(it) } } - override fun getNode() = OccupyNode.APEX_NODE - - override fun transform(source: ItemData) { - val itemMeta = RefItemMeta() - // HideFlags - itemMeta.handle.addItemFlags(*fetchHideFlags().toTypedArray()) - // BukkitAttributes - fetchBukkitAttributes().forEach { (key, value) -> - value.forEach { itemMeta.handle.addAttributeModifier(key, it) } - } - // Merge - itemMeta.applyToTag(source.tag) - // CustomModelData (1.14+) - if (MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_14)) { - source.tag[ItemSheet.CUSTOM_MODEL_DATA] = customModelData?.let { NBTInt(it) } - } - } - - override fun detransform(target: ItemData) { - val itemMeta = RefItemMeta(target.tag) - // HideFlags - val hideFlags = itemMeta.handle.itemFlags - if (hideFlags.isNotEmpty()) { - addHideFlags(*hideFlags.toTypedArray()) - } - // BukkitAttributes - val bukkitAttributes = itemMeta.handle.attributeModifiers - if (bukkitAttributes != null && !bukkitAttributes.isEmpty) { - bukkitAttributes.forEach { key, value -> addAttributeModifiers(key, value) } - } - // CustomModelData (1.14+) - if (MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_14)) { - target.tag[ItemSheet.CUSTOM_MODEL_DATA].castThen { - customModelData = it.content - } - } - } - /** * 空->默认值处理 */ @@ -112,11 +74,11 @@ data class VItemSundry( private fun fetchBukkitAttributes() = bukkitAttributes ?: HashMap>().also { bukkitAttributes = it } - companion object : ItemTransformer { + companion object : ItemTransformer { override val node = OccupyNode.APEX_NODE - override fun transform(component: VItemSundry): ItemData = TheItemData().apply { + override fun transform(component: ItemSundry): ItemData = TheItemData().apply { val itemMeta = RefItemMeta() // HideFlags itemMeta.handle.addItemFlags(*component.fetchHideFlags().toTypedArray()) @@ -132,7 +94,7 @@ data class VItemSundry( } } - override fun detransform(data: ItemData): VItemSundry = VItemSundry().apply { + override fun detransform(data: ItemData): ItemSundry = ItemSundry().apply { val itemMeta = RefItemMeta(data.tag) // HideFlags val hideFlags = itemMeta.handle.itemFlags @@ -151,6 +113,7 @@ data class VItemSundry( } } } + } } \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemMeta.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemMeta.kt deleted file mode 100644 index 0ca1267e..00000000 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/VItemMeta.kt +++ /dev/null @@ -1,73 +0,0 @@ -@file:OptIn(ExperimentalSerializationApi::class) - -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.part.ItemDisplay -import cn.fd.ratziel.module.item.api.part.ItemDurability -import cn.fd.ratziel.module.item.api.part.ItemMetadata -import cn.fd.ratziel.module.item.api.part.ItemSundry -import cn.fd.ratziel.module.item.impl.OccupyNode -import cn.fd.ratziel.module.item.impl.TheItemData -import cn.fd.ratziel.module.item.util.toApexComponent -import cn.fd.ratziel.module.item.util.toApexData -import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.Serializable -import kotlinx.serialization.json.JsonNames - -/** - * VItemMeta - * - * @author TheFloodDragon - * @since 2024/4/4 20:10 - */ -@Serializable -data class VItemMeta( - @JsonNames("mat") - override var material: ItemMaterial = ItemMaterial.EMPTY, - override var display: ItemDisplay = VItemDisplay(), - override var durability: ItemDurability = VItemDurability(), - override var sundry: ItemSundry = VItemSundry() -) : ItemMetadata { - - override fun getNode() = OccupyNode.APEX_NODE - - override fun transform(source: ItemData) { -// source.material = this.material -// this.display.transfer(source) -// this.durability.transfer(source) -// this.sundry.transfer(source) - } - - override fun detransform(target: ItemData) { -// this.material = target.material -// this.display.accept(target) -// this.durability.accept(target) -// this.sundry.accept(target) - } - - companion object : ItemTransformer { - - override val node = OccupyNode.APEX_NODE - - override fun detransform(data: ItemData) = VItemMeta( - material = data.material, - display = VItemDisplay.toApexComponent(data), - durability = VItemDurability.toApexComponent(data), - sundry = VItemSundry.toApexComponent(data), - ) - - override fun transform(component: VItemMeta): ItemData { - val data = TheItemData() - data.material = component.material - TheItemData.mergeShallow(data, VItemDisplay.toApexData(component.display)) - TheItemData.mergeShallow(data, VItemDurability.toApexData(component.durability)) -// TheItemData.mergeShallow(data,VItemSundry.toApexData(component.sundry)) - return data - } - - } - -} \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/serializers/HideFlagSerializer.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/serializers/HideFlagSerializer.kt index e9a42446..a0347e00 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/serializers/HideFlagSerializer.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/component/serializers/HideFlagSerializer.kt @@ -1,6 +1,6 @@ package cn.fd.ratziel.module.item.impl.component.serializers -import cn.fd.ratziel.module.item.api.part.HideFlag +import cn.fd.ratziel.module.item.impl.component.HideFlag import cn.fd.ratziel.module.item.util.MetaMatcher import kotlinx.serialization.KSerializer import kotlinx.serialization.descriptors.PrimitiveKind