Skip to content

Commit

Permalink
Experimental | Optimize & Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Jul 21, 2024
1 parent 2ae8990 commit ac12da6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cn.fd.ratziel.module.item.event
package cn.fd.ratziel.module.item.api.event

import cn.fd.ratziel.core.Identifier
import cn.fd.ratziel.function.ArgumentContext
import cn.fd.ratziel.module.item.api.NeoItem
import cn.fd.ratziel.module.item.api.builder.ItemGenerator
import cn.fd.ratziel.module.item.api.event.ItemEvent

/**
* ItemBuildEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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.ItemResolvedEvent
import cn.fd.ratziel.module.item.event.ItemBuildEvent
import cn.fd.ratziel.module.item.api.event.ItemBuildEvent
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 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.event.ItemBuildEvent
import cn.fd.ratziel.module.item.api.event.ItemBuildEvent
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 Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.fd.ratziel.module.item.nms

import cn.fd.ratziel.function.util.uncheck
import cn.fd.ratziel.module.item.nbt.NMSUtil
import com.mojang.serialization.Codec
import com.mojang.serialization.DynamicOps
import net.minecraft.core.IRegistryCustom
Expand All @@ -10,12 +9,10 @@ import net.minecraft.core.component.DataComponentPatch
import net.minecraft.nbt.DynamicOpsNBT
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.resources.RegistryOps
import net.minecraft.server.MinecraftServer
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.v1_20_R4.CraftServer
import taboolib.library.reflex.ReflexClass
import taboolib.module.nms.MinecraftVersion
import taboolib.module.nms.minecraftServerObject
import taboolib.module.nms.nmsClass
import taboolib.module.nms.nmsProxy

Expand Down Expand Up @@ -61,24 +58,10 @@ abstract class NMS12005 {
/**
* [net.minecraft.core.component.DataComponentPatch]
*/
val DATA_COMPONENT_PATCH_CLASS by lazy {
val dataComponentPatchClass by lazy {
nmsClass("DataComponentPatch")
}

/**
* [net.minecraft.world.item.component.CustomData]
*/
val customDataClass by lazy {
nmsClass("CustomData")
}

/**
* private CustomData(NBTTagCompound var0)
*/
val customDataConstructor by lazy {
ReflexClass.of(customDataClass).structure.getConstructorByType(NMSUtil.NtCompound.nmsClass)
}

}

}
Expand Down Expand Up @@ -106,7 +89,7 @@ class NMS12005Impl : NMS12005() {

override fun saveMap(dcm: Any): Any? = save(DataComponentMap.CODEC, dcm as DataComponentMap)

val access: IRegistryCustom.Dimension by lazy { minecraftServerObject as MinecraftServer.registryAccess() }
val access: IRegistryCustom.Dimension by lazy { (Bukkit.getServer() as CraftServer).server.registryAccess() }

val method by lazy {
val ref = ReflexClass.of(net.minecraft.core.HolderLookup.a::class.java, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class RefItemMeta<T : ItemMeta>(raw: T) {
var handle: T = raw as? T ?: throw UnsupportedTypeException(raw)

/**
* [MetaType]
* [CraftMetaType]
*/
val metaType by lazy {
MetaType.registry.find { it.clazz == handle::class.java } ?: MetaType(handle::class.java)
registry.find { it.craftClass.isAssignableFrom(handle::class.java) } ?: CraftMetaType(handle::class.java)
}

/**
Expand All @@ -43,105 +43,100 @@ class RefItemMeta<T : ItemMeta>(raw: T) {
/**
* inventory.CraftMetaItem
*/
val META_ITEM: MetaType<ItemMeta> = MetaType("inventory.CraftMetaItem")
val META_SKULL: MetaType<SkullMeta> = MetaType("inventory.CraftMetaSkull")
val META_ITEM: CraftMetaType<ItemMeta> = CraftMetaType("inventory.CraftMetaItem")
val META_SKULL: CraftMetaType<SkullMeta> = CraftMetaType("inventory.CraftMetaSkull")

/**
* Registry for [CraftMetaType]
*/
val registry: MutableSet<CraftMetaType<*>> = CopyOnWriteArraySet()

/**
* CraftMetaItem#constructor(NBTTagCompound)
* CraftMetaItem(DataComponentPatch tag)
* @return CraftMetaItem
*/
fun <T : ItemMeta> new(type: MetaType<T>, tag: NBTCompound): T = type.new(tag)
fun <T : ItemMeta> new(type: CraftMetaType<T>, tag: NBTCompound): T = type.new(tag)

@JvmStatic
fun <T : ItemMeta> of(type: MetaType<T>) = of(type, NBTCompound())
fun <T : ItemMeta> of(type: CraftMetaType<T>) = of(type, NBTCompound())

@JvmStatic
fun <T : ItemMeta> of(type: MetaType<T>, tag: NBTCompound) = RefItemMeta(new(type, tag))
fun <T : ItemMeta> of(type: CraftMetaType<T>, tag: NBTCompound) = RefItemMeta(new(type, tag))

}

class MetaType<T : ItemMeta>(
val clazz: Class<in T>,
class CraftMetaType<T : ItemMeta>(
val craftClass: Class<T>,
) {

init {
registry.add(this)
}

companion object {
val registry: MutableSet<MetaType<*>> = CopyOnWriteArraySet()
}

internal constructor(obcName: String) : this(uncheck<Class<in T>>(obcClass(obcName)))
internal constructor(obcName: String) : this(uncheck<Class<T>>(obcClass(obcName)))

/**
* CraftMetaItem#constructor(NBTTagCompound)
* CraftMetaItem(DataComponentPatch tag)
* @return CraftMetaItem
*/
private val craftMetaConstructor by lazy {
ReflexClass.of(clazz, false).structure.getConstructorByType(
if (MinecraftVersion.majorLegacy >= 12005) NMS12005.DATA_COMPONENT_PATCH_CLASS else NMSUtil.NtCompound.nmsClass
ReflexClass.of(craftClass, false).structure.getConstructorByType(
if (MinecraftVersion.majorLegacy >= 12005) NMS12005.dataComponentPatchClass else NMSUtil.NtCompound.nmsClass
)
}

private val applyToItemMethod by lazy {
ReflexClass.of(clazz, false).structure.getMethodByType(
ReflexClass.of(craftClass, false).structure.getMethodByType(
"applyToItem",
if (MinecraftVersion.majorLegacy >= 12005) InternalUtil.applicatorClass else NMSUtil.NtCompound.nmsClass
if (MinecraftVersion.majorLegacy >= 12005) applicatorClass else NMSUtil.NtCompound.nmsClass
)
}

internal fun new(tag: NBTCompound): T {
val handled =
if (MinecraftVersion.majorLegacy >= 12005)
NMS12005.INSTANCE.parsePatch(tag.getData())!!
else tag
val handled = if (MinecraftVersion.majorLegacy >= 12005) NMS12005.INSTANCE.parsePatch(tag.getData())!! else tag
return uncheck(craftMetaConstructor.instance(handled)!!)
}

internal fun applyToItem(meta: ItemMeta, tag: NBTCompound) {
if (MinecraftVersion.majorLegacy >= 12005) {
/*
if (MinecraftVersion.majorLegacy >= 12005) {/*
1.20.5+ 巨tm坑:
if (this.customTag != null) {
itemTag.put(CUSTOM_DATA, CustomData.a(this.customTag));
}
*/
val applicator = InternalUtil.applicatorConstructor.instance()!! // new Applicator
val applicator = applicatorConstructor.instance()!! // new Applicator
applyToItemMethod.invoke(meta, applicator) // Apply to the applicator
val dcp = InternalUtil.applicatorToDcp(applicator) // Applicator to DataComponentPatch
val dcp = applicatorBuildMethod.invoke(applicator)!! // Applicator to DataComponentPatch
val newTag = NMS12005.INSTANCE.savePatch(dcp) // DataComponentPatch save to NBT
if (newTag != null) tag.mergeShallow(NBTCompound(newTag), true)
} else {
applyToItemMethod.invoke(meta, tag.getData())
}
}

}

internal object InternalUtil {
companion object {

/**
* 处理1.20.5的CraftMetaItem.Applicator
*
* Applicator(){}
* <T> Applicator put(ItemMetaKeyType<T> key, T value)
* DataComponentPatch build()
*/
val applicatorClass by lazy {
obcClass("inventory.CraftMetaItem\$Applicator")
}
/**
* 处理1.20.5的CraftMetaItem.Applicator
*
* Applicator(){}
* <T> Applicator put(ItemMetaKeyType<T> key, T value)
* DataComponentPatch build()
*/
val applicatorClass by lazy {
obcClass("inventory.CraftMetaItem\$Applicator")
}

fun applicatorToDcp(applicator: Any): Any = applicatorBuildMethod.invoke(applicator)!!
val applicatorConstructor by lazy {
ReflexClass.of(applicatorClass, false).getConstructor()
}

val applicatorConstructor by lazy {
ReflexClass.of(applicatorClass, false).getConstructor()
}
val applicatorBuildMethod by lazy {
ReflexClass.of(applicatorClass, false).getMethod("build")
}

val applicatorBuildMethod by lazy {
ReflexClass.of(applicatorClass, false).getMethod("build")
}

}
Expand Down

0 comments on commit ac12da6

Please sign in to comment.