Skip to content

Commit

Permalink
Experimental | Remove item part of interfaces
Browse files Browse the repository at this point in the history
Shit
  • Loading branch information
TheFloodDragon committed Jun 24, 2024
1 parent 4fbd600 commit c6f011b
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object ItemElement : ElementHandler {
println(test.getData())

// 注册
ItemManager.registry[element.name] = DefaultItemGenerator
ItemManager.registry[element.name] = element
}

@SubscribeEvent
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Player>().giveItem(item)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -13,8 +13,7 @@ object ItemManager {

/**
* 物品注册表
* 物品标识符:物品生成器
*/
val registry: MutableMap<String, ItemGenerator> = ConcurrentHashMap()
val registry: MutableMap<String, Element> = ConcurrentHashMap()

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,7 +46,7 @@ object DefaultItemGenerator : ItemGenerator {
* 物品转换器
*/
val transformers = HashMap<Class<*>, Priority<ItemTransformer<*>>>().apply {
put(VItemDisplay::class.java, VItemDisplay.priority())
put(ItemDisplay::class.java, ItemDisplay.priority())
}

// /**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +25,7 @@ import java.util.concurrent.CompletableFuture
* @author TheFloodDragon
* @since 2024/4/4 19:58
*/
object DefaultItemSerializer : ItemKSerializer<VItemMeta> {
object DefaultItemSerializer : ItemKSerializer<ItemMeta> {

val json = Json(baseJson) {
serializersModule += SerializersModule {
Expand All @@ -50,45 +46,45 @@ object DefaultItemSerializer : ItemKSerializer<VItemMeta> {
* 使用到的序列化器列表
*/
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 <T> asyncDecode(deserializer: DeserializationStrategy<T>, 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)

/**
* 结构化解析
Expand Down
Loading

0 comments on commit c6f011b

Please sign in to comment.