Skip to content

Commit abde415

Browse files
committed
[publish] 6.0.12 fix nbt
1 parent 6dbccd4 commit abde415

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

common/src/main/kotlin/taboolib/common/Test.kt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import taboolib.common.platform.function.info
99
* @author 坏黑
1010
* @since 2023/8/4 02:33
1111
*/
12+
@Isolated
1213
abstract class Test {
1314

1415
/**

module/module-nms-util/src/main/kotlin/taboolib/module/nms/ItemTag.kt

+17-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class ItemTag : ItemTagData, MutableMap<String, ItemTagData> {
4747
return GsonBuilder().setPrettyPrinting().create().toJson(serializeTag(this))
4848
}
4949

50+
override fun asCompound(): ItemTag {
51+
return this
52+
}
53+
5054
override fun toJsonSimplified(): String {
5155
return toJsonSimplified(0)
5256
}
@@ -96,7 +100,7 @@ class ItemTag : ItemTagData, MutableMap<String, ItemTagData> {
96100
*/
97101
fun removeDeep(key: String): ItemTagData? {
98102
return if (key.contains('.')) {
99-
getDeepWith(key) { it.remove(key.substringAfterLast('.')) }
103+
getDeepWith(key, false) { it.remove(key.substringAfterLast('.')) }
100104
} else {
101105
remove(key)
102106
}
@@ -119,7 +123,7 @@ class ItemTag : ItemTagData, MutableMap<String, ItemTagData> {
119123
*/
120124
fun putDeep(key: String, value: Any): ItemTagData? {
121125
return if (key.contains('.')) {
122-
getDeepWith(key) { it.put(key.substringAfterLast('.'), toNBT(value)) }
126+
getDeepWith(key, true) { it.put(key.substringAfterLast('.'), toNBT(value)) }
123127
} else {
124128
put(key, value)
125129
}
@@ -138,7 +142,7 @@ class ItemTag : ItemTagData, MutableMap<String, ItemTagData> {
138142
*/
139143
fun getDeep(key: String): ItemTagData? {
140144
return if (key.contains('.')) {
141-
getDeepWith(key) { it[key.substringAfterLast('.')] }
145+
getDeepWith(key, false) { it[key.substringAfterLast('.')] }
142146
} else {
143147
get(key)
144148
}
@@ -147,14 +151,22 @@ class ItemTag : ItemTagData, MutableMap<String, ItemTagData> {
147151
/**
148152
* 针对 getDeep, putDeep, removeDeep 的重复代码做出的优化
149153
*/
150-
fun getDeepWith(key: String, action: (ItemTag) -> ItemTagData?): ItemTagData? {
154+
fun getDeepWith(key: String, create: Boolean, action: (ItemTag) -> ItemTagData?): ItemTagData? {
151155
val keys = key.split('.').dropLast(1)
152156
if (keys.isEmpty()) {
153157
return null
154158
}
155159
var find: ItemTag = this
156160
for (element in keys) {
157-
val next = find[element] ?: return null
161+
var next = find[element]
162+
if (next == null) {
163+
if (create) {
164+
next = ItemTag()
165+
find[element] = next
166+
} else {
167+
return null
168+
}
169+
}
158170
if (next.type == ItemTagType.COMPOUND) {
159171
find = next.asCompound()
160172
} else {

module/module-nms-util/src/main/kotlin/taboolib/module/nms/ItemTagData.kt

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ open class ItemTagData(val type: ItemTagType, protected var data: Any) {
157157
is Byte -> ItemTagData(obj)
158158
is ByteArray -> ItemTagData(obj)
159159
is IntArray -> ItemTagData(obj)
160+
is List<*> -> translateList(ItemTagList(), obj)
160161
is Map<*, *> -> ItemTag(obj.map { (k, v) -> k.toString() to toNBT(v) }.toMap())
161162
is ConfigurationSection -> translateSection(ItemTag(), obj)
162163
else -> error("Unsupported nbt: $obj (${obj.javaClass})")

module/module-nms-util/src/main/kotlin/taboolib/module/nms/ItemTagList.kt

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class ItemTagList : ItemTagData, MutableList<ItemTagData> {
2323
this.value += list
2424
}
2525

26+
override fun asList(): ItemTagList {
27+
return this
28+
}
29+
2630
override fun toJsonSimplified(): String {
2731
return toJsonSimplified(0)
2832
}

module/module-nms-util/src/main/kotlin/taboolib/module/nms/ItemTagType.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.google.common.base.Enums
88
* @author 坏黑
99
* @since 2019-05-24 17:46
1010
*/
11-
enum class ItemTagType(@JvmField val id: Int) {
11+
enum class ItemTagType(@JvmField val id: Byte) {
1212

1313
END(0),
1414
BYTE(1),

module/module-nms-util/src/main/kotlin/taboolib/module/nms/NMSItemTag.kt

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package taboolib.module.nms
33
import org.bukkit.Material
44
import org.bukkit.inventory.ItemStack
55
import org.tabooproject.reflex.UnsafeAccess
6+
import taboolib.common.platform.function.info
67
import java.lang.invoke.MethodHandle
78

89
/**
@@ -59,6 +60,7 @@ class NMSItemTagImpl : NMSItemTag() {
5960

6061
val nbtTagCompoundGetter = unreflectGetter<net.minecraft.server.v1_12_R1.NBTTagCompound>(if (MinecraftVersion.isUniversal) "x" else "map")
6162
val nbtTagListGetter = unreflectGetter<net.minecraft.server.v1_12_R1.NBTTagList>(if (MinecraftVersion.isUniversal) "c" else "list")
63+
val nbtTagListTypeSetter = unreflectSetter<net.minecraft.server.v1_12_R1.NBTTagList>(if (MinecraftVersion.isUniversal) "w" else "type")
6264
val nbtTagByteGetter = unreflectGetter<net.minecraft.server.v1_12_R1.NBTTagByte>(if (MinecraftVersion.isUniversal) "x" else "data")
6365
val nbtTagShortGetter = unreflectGetter<net.minecraft.server.v1_12_R1.NBTTagShort>(if (MinecraftVersion.isUniversal) "c" else "data")
6466
val nbtTagIntGetter = unreflectGetter<net.minecraft.server.v1_12_R1.NBTTagInt>(if (MinecraftVersion.isUniversal) "c" else "data")
@@ -106,7 +108,12 @@ class NMSItemTagImpl : NMSItemTag() {
106108
// 反射获取字段:
107109
// private final List<NBTBase> list;
108110
val list = nbtTagListGetter.get<MutableList<Any>>(nmsList)
109-
itemTagData.asList().forEach { list += itemTagToNMSCopy(it) }
111+
val dataList = itemTagData.asList()
112+
if (dataList.isNotEmpty()) {
113+
dataList.forEach { list += itemTagToNMSCopy(it) }
114+
// 修改 NBTTagList 的类型,不改他妈这条 List 作废,天坑。。。
115+
nbtTagListTypeSetter.set(nmsList, dataList.first().type.id)
116+
}
110117
}
111118
}
112119

@@ -117,6 +124,7 @@ class NMSItemTagImpl : NMSItemTag() {
117124
// private final Map<String, NBTBase> map
118125
val map = nbtTagCompoundGetter.get<MutableMap<String, Any>>(nmsCompound)
119126
itemTagData.asCompound().entries.forEach { (key, value) -> map[key] = itemTagToNMSCopy(value) }
127+
info("nmsCompound $nmsCompound")
120128
}
121129
}
122130

@@ -142,12 +150,12 @@ class NMSItemTagImpl : NMSItemTag() {
142150

143151
// 列表类型特殊处理
144152
is net.minecraft.server.v1_12_R1.NBTTagList -> {
145-
ItemTagData(ItemTagType.LIST, ItemTagList(nbtTagListGetter.get<List<Any>>(nbtTag).map { itemTagToBukkitCopy(it) }))
153+
ItemTagList(nbtTagListGetter.get<List<Any>>(nbtTag).map { itemTagToBukkitCopy(it) })
146154
}
147155

148156
// 复合类型特殊处理
149157
is net.minecraft.server.v1_12_R1.NBTTagCompound -> {
150-
ItemTagData(ItemTagType.COMPOUND, ItemTag().apply { nbtTagCompoundGetter.get<Map<String, Any>>(nbtTag).forEach { put(it.key, itemTagToBukkitCopy(it.value)) } })
158+
ItemTag().apply { nbtTagCompoundGetter.get<Map<String, Any>>(nbtTag).forEach { put(it.key, itemTagToBukkitCopy(it.value)) } }
151159
}
152160

153161
// 不支持的类型
@@ -159,9 +167,17 @@ class NMSItemTagImpl : NMSItemTag() {
159167
return UnsafeAccess.lookup.unreflectGetter(T::class.java.getDeclaredField(name).apply { isAccessible = true })
160168
}
161169

170+
private inline fun <reified T> unreflectSetter(name: String): MethodHandle {
171+
return UnsafeAccess.lookup.unreflectSetter(T::class.java.getDeclaredField(name).apply { isAccessible = true })
172+
}
173+
162174
private fun <T> MethodHandle.get(src: Any): T {
163175
return bindTo(src).invoke() as T
164176
}
177+
178+
private fun <T> MethodHandle.set(src: Any, value: T) {
179+
bindTo(src).invoke(value)
180+
}
165181
}
166182

167183
/**

0 commit comments

Comments
 (0)