Skip to content

Commit 2a185ba

Browse files
committed
Support 1.21.6+, json as compound instead of string
1 parent e20b56e commit 2a185ba

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

bukkit/src/main/kotlin/io/github/rothes/protocolstringreplacer/replacer/containers/ItemStackContainer.kt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
114114
}
115115
if (display.hasTag(LORE_KEY)) {
116116
if (LORE_JSON) {
117-
addJsonList(display.getStringList(LORE_KEY))
117+
if (COMPOUND_JSON)
118+
addJsonList(display.getCompoundList(LORE_KEY))
119+
else
120+
addJsonList(display.getStringList(LORE_KEY))
118121
} else {
119122
addTextList(display.getStringList(LORE_KEY))
120123
}
@@ -150,13 +153,13 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
150153
if (compound.hasTag("pages")) {
151154
if (type == Material.WRITTEN_BOOK) {
152155
if (NEW_NBT && compound.getListType("pages") == NBTType.NBTTagCompound) {
153-
addJsonList(compound.getCompoundList("pages"))
156+
addJsonListBook(compound.getCompoundList("pages"))
154157
} else {
155158
addJsonList(compound.getStringList("pages"))
156159
}
157160
} else {
158161
if (NEW_NBT && compound.getListType("pages") == NBTType.NBTTagCompound) {
159-
addTextList(compound.getCompoundList("pages"))
162+
addTextListBook(compound.getCompoundList("pages"))
160163
} else {
161164
addTextList(compound.getStringList("pages"))
162165
}
@@ -181,7 +184,7 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
181184
}
182185
}
183186

184-
private fun addJsonList(list: ReadWriteNBTCompoundList) {
187+
private fun addJsonListBook(list: ReadWriteNBTCompoundList) {
185188
val size = list.size()
186189
for (line in 0 until size) {
187190
val compound = list[line]
@@ -190,7 +193,7 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
190193
}
191194
}
192195

193-
private fun addTextList(list: ReadWriteNBTCompoundList) {
196+
private fun addTextListBook(list: ReadWriteNBTCompoundList) {
194197
val size = list.size()
195198
for (line in 0 until size) {
196199
val compound = list[line]
@@ -212,6 +215,24 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
212215
}
213216
}
214217

218+
private fun addJsonList(list: ReadWriteNBTCompoundList) {
219+
val size = list.size()
220+
for (line in 0 until size) {
221+
val compound = list[line]
222+
val string = compound.toString()
223+
children.add(object : ChatJsonContainer(string, root, true) {
224+
override fun getResult(): String {
225+
val result = super.getResult()
226+
if (string != result) {
227+
compound.clearNBT()
228+
compound.mergeCompound(NBT.parseNBT(result))
229+
}
230+
return result
231+
}
232+
})
233+
}
234+
}
235+
215236
private fun addTextList(list: ReadWriteNBTList<String>) {
216237
val size = list.size()
217238
for (line in 0 until size) {
@@ -268,13 +289,20 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
268289
private val key: String,
269290
root: Container<*>,
270291
createComponents: Boolean,
271-
private val original: String = compound.getString(key)
292+
private val jsonCompound: ReadWriteNBT? = if (COMPOUND_JSON) compound.getCompound(key) else null,
293+
private val original: String = if (COMPOUND_JSON) jsonCompound!!.toString() else compound.getString(key)
272294
): ChatJsonContainer(original, root, createComponents) {
273295

274296
override fun getResult(): String {
275297
val result = super.getResult()
276298
if (result != original) {
277-
compound.setString(key, result)
299+
if (COMPOUND_JSON) {
300+
jsonCompound!!
301+
jsonCompound.clearNBT()
302+
jsonCompound.mergeCompound(NBT.parseNBT(result))
303+
} else {
304+
compound.setString(key, result)
305+
}
278306
}
279307
return result
280308
}
@@ -301,6 +329,7 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
301329
private val NAME_JSON = plugin.serverMajorVersion >= 13
302330
private val LORE_JSON = plugin.serverMajorVersion >= 14
303331
private val NEW_NBT = plugin.serverMajorVersion == 20.toByte() && plugin.serverMinorVersion >= 5 || plugin.serverMajorVersion > 20
332+
private val COMPOUND_JSON = plugin.serverMajorVersion == 21.toByte() && plugin.serverMinorVersion >= 6 || plugin.serverMajorVersion > 21
304333
private val TAG_PATH = if (NEW_NBT) arrayOf("components") else arrayOf("tag")
305334
private val DISPLAY_PATH = if (NEW_NBT) arrayOf() else arrayOf("display")
306335
private val NAME_KEY = if (NEW_NBT) "minecraft:custom_name" else "Name"

0 commit comments

Comments
 (0)