Skip to content

Commit

Permalink
[A.0.3.0.5] Experimental | Final Update (Stop for a month)
Browse files Browse the repository at this point in the history
Update Item&Fix
  • Loading branch information
TheFloodDragon committed May 25, 2024
1 parent bd7b27c commit 51159fb
Show file tree
Hide file tree
Showing 23 changed files with 238 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import cn.fd.ratziel.core.element.Element
import cn.fd.ratziel.core.element.api.ElementHandler
import cn.fd.ratziel.module.item.impl.ItemManager
import cn.fd.ratziel.module.item.impl.builder.DefaultItemGenerator
import cn.fd.ratziel.module.item.reflex.RefItemStack
import cn.fd.ratziel.module.item.nms.RefItemStack
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
Expand All @@ -32,6 +34,11 @@ object ItemElement : ElementHandler {
Executors.newFixedThreadPool(8)
}

/**
* 协程作用域
*/
val scope = CoroutineScope(Dispatchers.Default)

override fun handle(element: Element) {

val generator = DefaultItemGenerator(element)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.api.common

import cn.fd.ratziel.module.item.api.ItemNode
import cn.fd.ratziel.module.item.reflex.ItemSheet
import cn.fd.ratziel.module.item.nms.ItemSheet

/**
* OccupyNode - [ItemNode] 实现
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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.reflex.RefItemStack
import cn.fd.ratziel.module.item.nms.RefItemStack
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.command.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,42 @@ data class ItemMaterialImpl(override val name: String) : ItemMaterial {
/**
* 材料标识符 (低版本)
*/
override val id: Int get() = getIdUnsafe(bukkitMaterial)
override val id: Int get() = getIdUnsafe(insBukkit)

/**
* 材料的默认最大堆叠数量
*/
override val maxStackSize: Int get() = bukkitMaterial.maxStackSize
override val maxStackSize: Int get() = insBukkit.maxStackSize

/**
* 材料的默认最大耐久度
*/
override val maxDurability: Int get() = bukkitMaterial.maxDurability.toInt()
override val maxDurability: Int get() = insBukkit.maxDurability.toInt()

/**
* 材料是否为空气材料
*/
fun isAir() = bukkitMaterial.isAir || ItemMaterial.isEmpty(this)
fun isAir() = insBukkit.isAir || ItemMaterial.isEmpty(this)

/**
* [BukkitMaterial] 形式 (若获取不到则抛出异常)
* 获取 [BukkitMaterial] 形式 (若获取不到则抛出异常)
*/
val bukkitMaterial: BukkitMaterial by lazy { getBukkitMaterial(name) ?: throw UnknownMaterialException(name) }
fun getAsBukkit(): BukkitMaterial = insBukkit

/**
* [XMaterial] 形式 (若获取不到则抛出异常)
* 获取 [XMaterial] 形式 (若获取不到则抛出异常)
*/
val xseriesMaterial: XMaterial by lazy { XMaterial.matchXMaterial(bukkitMaterial) }
fun getAsXSeries(): XMaterial = insXSeries

/**
* [BukkitMaterial] 形式
*/
private val insBukkit: BukkitMaterial by lazy { getBukkitMaterial(name) ?: throw UnknownMaterialException(name) }

/**
* [XMaterial] 形式
*/
private val insXSeries: XMaterial by lazy { XMaterial.matchXMaterial(insBukkit) }

companion object {

Expand All @@ -72,7 +82,7 @@ data class ItemMaterialImpl(override val name: String) : ItemMaterial {
}

/**
* 通过反射获取 [id], 因为 [BukkitMaterial.getId] 不会获取老版物品的 [id]
* 通过反射获取 [id], 因为 [BukkitMaterial] 不会获取老版物品的 [id]
*/
fun getIdUnsafe(material: BukkitMaterial) = bukkitIdField.get(material) as Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import cn.fd.ratziel.module.item.api.part.ItemDisplay
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.reflex.ItemSheet
import cn.fd.ratziel.module.item.nms.ItemSheet
import cn.fd.ratziel.module.item.util.castThen
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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.reflex.ItemSheet
import cn.fd.ratziel.module.item.nms.ItemSheet
import cn.fd.ratziel.module.item.util.castThen
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import cn.fd.ratziel.module.item.api.common.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.nbt.NBTInt
import cn.fd.ratziel.module.item.reflex.ItemSheet
import cn.fd.ratziel.module.item.reflex.RefItemMeta
import cn.fd.ratziel.module.item.nms.ItemSheet
import cn.fd.ratziel.module.item.nms.RefItemMeta
import cn.fd.ratziel.module.item.util.castThen
import kotlinx.serialization.Contextual
import kotlinx.serialization.ExperimentalSerializationApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.fd.ratziel.module.item.impl.part.serializers

import cn.fd.ratziel.module.item.util.MetaMather
import cn.fd.ratziel.module.item.util.MetaMatcher
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand All @@ -20,6 +20,6 @@ object AttributeOperationSerializer : KSerializer<AttributeModifier.Operation> {

override fun serialize(encoder: Encoder, value: AttributeModifier.Operation) = encoder.encodeString(value.name)

override fun deserialize(decoder: Decoder) = MetaMather.matchAttributeOperation(decoder.decodeString())
override fun deserialize(decoder: Decoder) = MetaMatcher.matchAttributeOperation(decoder.decodeString())

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.fd.ratziel.module.item.impl.part.serializers

import cn.fd.ratziel.module.item.util.MetaMather
import cn.fd.ratziel.module.item.util.MetaMatcher
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand All @@ -20,6 +20,6 @@ object AttributeSerializer : KSerializer<Attribute> {

override fun serialize(encoder: Encoder, value: Attribute) = encoder.encodeString(value.key.key)

override fun deserialize(decoder: Decoder) = MetaMather.matchAttribute(decoder.decodeString()).toBukkit()
override fun deserialize(decoder: Decoder) = MetaMatcher.matchAttribute(decoder.decodeString()).toBukkit()

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.fd.ratziel.module.item.impl.part.serializers

import cn.fd.ratziel.module.item.util.MetaMather
import cn.fd.ratziel.module.item.util.MetaMatcher
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand All @@ -20,6 +20,6 @@ object EnchantmentSerializer : KSerializer<Enchantment> {

override fun serialize(encoder: Encoder, value: Enchantment) = encoder.encodeString(value.key.key)

override fun deserialize(decoder: Decoder) = MetaMather.matchEnchantment(decoder.decodeString())
override fun deserialize(decoder: Decoder) = MetaMatcher.matchEnchantment(decoder.decodeString())

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.fd.ratziel.module.item.impl.part.serializers

import cn.fd.ratziel.module.item.util.MetaMather
import cn.fd.ratziel.module.item.util.MetaMatcher
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand All @@ -20,6 +20,6 @@ object EquipmentSlotSerializer : KSerializer<EquipmentSlot> {

override fun serialize(encoder: Encoder, value: EquipmentSlot) = encoder.encodeString(value.name)

override fun deserialize(decoder: Decoder): EquipmentSlot = MetaMather.matchEquipment(decoder.decodeString()).bukkit
override fun deserialize(decoder: Decoder): EquipmentSlot = MetaMatcher.matchEquipment(decoder.decodeString()).bukkit

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.impl.part.serializers

import cn.fd.ratziel.module.item.api.part.HideFlag
import cn.fd.ratziel.module.item.util.MetaMather
import cn.fd.ratziel.module.item.util.MetaMatcher
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand All @@ -18,7 +18,7 @@ object HideFlagSerializer : KSerializer<HideFlag> {

override val descriptor = PrimitiveSerialDescriptor("bukkit.HideFlag", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): HideFlag = MetaMather.matchHideFlag(decoder.decodeString())
override fun deserialize(decoder: Decoder): HideFlag = MetaMatcher.matchHideFlag(decoder.decodeString())

override fun serialize(encoder: Encoder, value: HideFlag) = encoder.encodeString(value.name)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.impl.part.serializers

import cn.fd.ratziel.module.item.api.ItemMaterial
import cn.fd.ratziel.module.item.util.MetaMather
import cn.fd.ratziel.module.item.util.MetaMatcher
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand All @@ -18,7 +18,7 @@ object ItemMaterialSerializer : KSerializer<ItemMaterial> {

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

override fun deserialize(decoder: Decoder): ItemMaterial = MetaMather.matchMaterial(decoder.decodeString())
override fun deserialize(decoder: Decoder): ItemMaterial = MetaMatcher.matchMaterial(decoder.decodeString())

override fun serialize(encoder: Encoder, value: ItemMaterial) = encoder.encodeString(value.name)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cn.fd.ratziel.module.item.nms

import kotlinx.serialization.json.*
import taboolib.common.io.runningResources
import taboolib.module.nms.MinecraftVersion

/**
* ItemSheet - 物品NBT标签映射表
*
* @author TheFloodDragon
* @since 2023/11/4 11:41
*/
object ItemSheet {

// Universal
val CUSTOM_DATA by mapping("CUSTOM_DATA")
val DISPLAY_NAME by mapping("DISPLAY_NAME")
val DISPLAY_LORE by mapping("DISPLAY_LORE")
val ENCHANTMENTS by mapping("ENCHANTMENTS")
val ATTRIBUTE_MODIFIERS by mapping("ATTRIBUTE_MODIFIERS")
val CUSTOM_MODEL_DATA by mapping("CUSTOM_MODEL_DATA")
val REPAIR_COST by mapping("REPAIR_COST")
val DAMAGE by mapping("DAMAGE")
val UNBREAKABLE by mapping("UNBREAKABLE")
val DISPLAY_LOCAL_NAME by mapping("DISPLAY_LOCAL_NAME") // Called "ITEM_NAME" on 1.20.5+

// Custom Features And 1.20.5+
val MAX_DAMAGE by mapping("MAX_DAMAGE")
val MAX_STACK_SIZE by mapping("MAX_STACK_SIZE")

// Only 1.20.5+
val FOOD by mapping("FOOD")
val RARITY by mapping("RARITY")
val FIRE_RESISTANT by mapping("FIRE_RESISTANT")
val ENCHANTMENT_GLINT_OVERRIDE by mapping("ENCHANTMENT_GLINT_OVERRIDE")
val HIDE_TOOLTIP by mapping("HIDE_TOOLTIP")

// Only 1.20.5-
val DISPLAY by mapping("DISPLAY")

// 1.20.5- But be retained by CraftBukkit
val HIDE_FLAG by mapping("HIDE_FLAG")

fun mapping(name: String): Lazy<String> = lazy { Mapper.map(name) }

/**
* Mapper
*
* @author TheFloodDragon
* @since 2024/4/30 20:25
*/
object Mapper {

var path = "nbt-mappings.json"

var mappingData: JsonElement = initData()

/**
* 从[mappingData]中获取对应映射值
* 注意: 所有操作均为危险操作 (不考虑数据缺失)
*/
fun map(name: String): String {
val data = mappingData.jsonObject[name]!!.jsonObject
// 使用固定值
return matchVersion(data)
}

fun matchVersion(json: JsonElement): String = when (json) {
is JsonObject ->
json.toSortedMap(Comparator.comparingInt { it.toInt() }).let { sortedMap ->
sortedMap.entries.findLast { MinecraftVersion.majorLegacy >= it.key.toInt() }
}?.let { it.value as? JsonPrimitive }?.content

is JsonNull -> null
is JsonPrimitive -> json.content
else -> null
} ?: throw IllegalStateException("Unable to match version!")

fun initData(): JsonElement {
val bytes = runningResources[path] ?: throw IllegalStateException("Not found: $path")
return Json.parseToJsonElement(bytes.toString(Charsets.UTF_8))
}

}

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

import cn.fd.ratziel.module.item.nbt.NMSUtil
import net.minecraft.core.component.DataComponentPatch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.fd.ratziel.module.item.reflex
package cn.fd.ratziel.module.item.nms

import cn.fd.ratziel.module.item.nbt.NBTCompound
import net.minecraft.core.component.DataComponentPatch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.fd.ratziel.module.item.reflex
package cn.fd.ratziel.module.item.nms

import cn.fd.ratziel.core.exception.UnsupportedTypeException
import cn.fd.ratziel.module.item.nbt.NBTCompound
Expand Down Expand Up @@ -50,23 +50,6 @@ class RefItemMeta(raw: Any) {
}
}

/**
* CraftMetaItem.ItemMetaKey
* 其他:
* 1.20.4-: static final ItemMetaKey
* 1.20.5+: static final ItemMetaKeyType<?>
* static final class ItemMetaKeyType<T> extends ItemMetaKey
*/
class RefItemMetaKey(val fieldName: String, val targetClass: Class<*> = obcClass) {

val obcData by lazy { targetClass.getProperty<Any>(fieldName, isStatic = true)!! }

val nmsName by lazy { obcData.getProperty<String>("NBT")!! }

val bukkitName by lazy { obcData.getProperty<String>("BUKKIT")!! }

}

companion object {

/**
Expand Down
Loading

0 comments on commit 51159fb

Please sign in to comment.