Skip to content

Commit

Permalink
Experimental | Optimize & Update NMS
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Jun 28, 2024
1 parent 5f994fc commit 544fe50
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 143 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ fun <T : Any, R, V : Any> Argument<V>.ascertain(

inline fun <reified T : Any> ArgumentSupplier.supplyOrNull(): Argument<T>? = supplyOrNull(T::class.java)

inline fun <reified T : Any> ArgumentFactory.pop() = pop(T::class.java)

inline fun <reified T : Any> ArgumentFactory.popAll() = popAll(T::class.java)

inline fun <reified T : Any> ArgumentFactory.popOr(default: T): Argument<T> = popOr(T::class.java, default)

inline fun <reified T : Any> ArgumentFactory.popOrNull(): Argument<T>? = popOrNull(T::class.java)

inline fun <reified T> Argument<*>.instanceof(): Boolean = this.instanceof(T::class.java)

inline fun <reified T : Any, R, V : Any> Argument<V>.ascertain(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ object ItemElement : ElementHandler {

println(item.data)

val ri = RefItemStack(item.data)
println(ri)
val ri = RefItemStack(item.data)
println(ri.getTag())
val bi = ri.getAsBukkit()
println(bi)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cn.fd.ratziel.module.item.command

import cn.fd.ratziel.module.item.nbt.*
import cn.fd.ratziel.module.item.nms.NMSItem
import cn.fd.ratziel.module.item.nms.RefItemStack
import cn.fd.ratziel.module.item.util.getItemBySlot
import cn.fd.ratziel.module.item.util.handleItemTag
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyCommandSender
Expand Down Expand Up @@ -39,13 +42,14 @@ object NBTCommand {
val view = subCommand {
slot {
execute<ProxyPlayer> { player, _, arg ->
player.cast<Player>().inventory.handleItemTag(arg) { tag ->
if (tag.isEmpty()) player.sendLang("NBTAction-EmptyTag")
else {
player.cast<Player>().inventory.getItemBySlot(arg)
?.let { RefItemStack(it).getAsNms() }
?.let { NMSItem.INSTANCE.getItemTagWithDefault(it) }
?.takeIf { it.isNotEmpty() }
?.also { tag ->
// 构建消息组件并发送
nbtAsComponent(player, tag, 0, arg).sendTo(player)
}
} ?: player.sendLang("NBTAction-EmptyTag")
} ?: player.sendLang("NBTAction-EmptyTag")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ object PapiResolver : SectionStringResolver {
override fun resolve(element: String, arguments: ContextArgument): String {
// 获取玩家
val player = arguments.popOrNull<OfflinePlayer>() ?: return element // 没玩家处理啥?
println(player)
// 处理Papi变量
return element.replacePlaceholder(player)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ data class ItemSundry(
/**
* 添加物品隐藏标签
*/
fun addHideFlags(vararg flags: HideFlag) = fetchHideFlags().addAll(flags)
fun addHideFlags(vararg flags: HideFlag) = (hideFlags ?: HashSet<HideFlag>().also { hideFlags = it }).addAll(flags)

/**
* 删除物品隐藏标签
*/
fun removeHideFlags(vararg flags: HideFlag) = fetchHideFlags().removeAll(flags.toSet())
fun removeHideFlags(vararg flags: HideFlag) = hideFlags?.removeAll(flags.toSet())

/**
* 获取属性修饰符
*/
fun getAttributeModifier(attribute: Attribute) = fetchBukkitAttributes().computeIfAbsent(attribute) { mutableListOf() }
fun getAttributeModifier(attribute: Attribute) = bukkitAttributes?.get(attribute)

/**
* 添加属性修饰符
*/
fun addAttributeModifiers(attribute: Attribute, vararg modifiers: AttributeModifier) = getAttributeModifier(attribute).addAll(modifiers)
fun addAttributeModifiers(attribute: Attribute, vararg modifiers: AttributeModifier) =
(bukkitAttributes ?: HashMap<Attribute, MutableList<AttributeModifier>>().also { bukkitAttributes = it })
.computeIfAbsent(attribute) { mutableListOf() }.addAll(modifiers)

/**
* 删除属性修饰符
Expand All @@ -66,24 +68,17 @@ data class ItemSundry(
fun removeAttributeModifiers(slot: EquipmentSlot) =
bukkitAttributes?.forEach { (key, value) -> value.forEach { if (it.slot == slot) bukkitAttributes?.get(key)?.remove(it) } }

/**
* 空->默认值处理
*/

private fun fetchHideFlags() = hideFlags ?: HashSet<HideFlag>().also { hideFlags = it }

private fun fetchBukkitAttributes() = bukkitAttributes ?: HashMap<Attribute, MutableList<AttributeModifier>>().also { bukkitAttributes = it }

companion object : ItemTransformer<ItemSundry> {

override val node = ItemNode.ROOT

override fun transform(component: ItemSundry): ItemData = ItemDataImpl().apply {
val itemMeta = RefItemMeta()
// HideFlags
itemMeta.handle.addItemFlags(*component.fetchHideFlags().toTypedArray())
val flags = component.hideFlags?.toTypedArray()
if (flags != null) itemMeta.handle.addItemFlags(*flags)
// BukkitAttributes
component.fetchBukkitAttributes().forEach { (key, value) ->
component.bukkitAttributes?.forEach { (key, value) ->
value.forEach { itemMeta.handle.addAttributeModifier(key, it) }
}
// Merge
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
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.component.DataComponentMap
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.nbt.DynamicOpsNBT
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.resources.RegistryOps
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.nmsClass
import taboolib.module.nms.nmsProxy
import kotlin.jvm.optionals.getOrNull

/**
* NMS12005
Expand All @@ -23,12 +29,22 @@ abstract class NMS12005 {
/**
* [NBTTagCompound] to [DataComponentPatch]
*/
abstract fun parse(nbt: Any): Any?
abstract fun parsePatch(nbt: Any): Any?

/**
* [DataComponentPatch] to [NBTTagCompound]
*/
abstract fun save(dcp: Any): Any?
abstract fun savePatch(dcp: Any): Any?

/**
* [NBTTagCompound] to [DataComponentMap]
*/
abstract fun parseMap(nbt: Any): Any?

/**
* [DataComponentMap] to [NBTTagCompound]
*/
abstract fun saveMap(dcm: Any): Any?

companion object {

Expand Down Expand Up @@ -60,12 +76,37 @@ abstract class NMS12005 {
@Suppress("unused")
class NMS12005Impl : NMS12005() {

override fun parse(nbt: Any): Any? =
DataComponentPatch.CODEC.parse(DynamicOpsNBT.INSTANCE, nbt as NBTTagCompound)
.resultOrPartial { error("Failed to parse: $it") }.getOrNull()
fun <T> parse(codec: Codec<T>, nbt: NBTTagCompound): T? {
val result = codec.parse(createSerializationContext(DynamicOpsNBT.INSTANCE), nbt)
val opt = result.resultOrPartial { error("Failed to parse: $it") }
return if (opt.isPresent) opt.get() else null
}

fun <T> save(codec: Codec<T>, obj: T): NBTTagCompound? {
val result = codec.encodeStart(createSerializationContext(DynamicOpsNBT.INSTANCE), obj)
val opt = result.resultOrPartial { error("Failed to save: $it") }
return if (opt.isPresent) opt.get() as? NBTTagCompound else null
}

override fun parsePatch(nbt: Any): Any? = parse(DataComponentPatch.CODEC, nbt as NBTTagCompound)

override fun savePatch(dcp: Any): Any? = save(DataComponentPatch.CODEC, dcp as DataComponentPatch)

override fun parseMap(nbt: Any): Any? = parse(DataComponentMap.CODEC, nbt as NBTTagCompound)

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

val access by lazy { (Bukkit.getServer() as CraftServer).server.registryAccess() }

val method by lazy {
val ref = ReflexClass.of(access::class.java, false)
try {
ref.getMethod("a")
} catch (_: NoSuchFieldException) {
ref.getMethod("createSerializationContext")
}
}

override fun save(dcp: Any): Any? =
DataComponentPatch.CODEC.encodeStart(DynamicOpsNBT.INSTANCE, dcp as DataComponentPatch)
.resultOrPartial { error("Failed to save: $it") }.getOrNull()
fun <V> createSerializationContext(dynamicOps: DynamicOps<V>): RegistryOps<V> = uncheck(method.invoke(access, dynamicOps)!!)

}
Loading

0 comments on commit 544fe50

Please sign in to comment.