@@ -2,7 +2,9 @@ package io.github.rothes.protocolstringreplacer.replacer.containers
22
33import de.tr7zw.changeme.nbtapi.NBT
44import de.tr7zw.changeme.nbtapi.NBTContainer
5+ import de.tr7zw.changeme.nbtapi.NBTType
56import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT
7+ import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList
68import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTList
79import io.github.rothes.protocolstringreplacer.ProtocolStringReplacer
810import io.github.rothes.protocolstringreplacer.plugin
@@ -105,21 +107,9 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
105107 if (display != null ) {
106108 if (display.hasTag(NAME_KEY )) {
107109 if (NAME_JSON ) {
108- children.add(object : ChatJsonContainer (display.getString(NAME_KEY ), root, true ) {
109- override fun getResult (): String {
110- val result = super .getResult()
111- display.setString(NAME_KEY , result)
112- return result
113- }
114- })
110+ children.add(CompoundJsonContainer (display, NAME_KEY , root, true ))
115111 } else {
116- children.add(object : SimpleTextContainer (display.getString(NAME_KEY ), root) {
117- override fun getResult (): String {
118- val result = super .getResult()
119- display.setString(NAME_KEY , result)
120- return result
121- }
122- })
112+ children.add(CompoundTextContainer (display, NAME_KEY , root))
123113 }
124114 }
125115 if (display.hasTag(LORE_KEY )) {
@@ -133,29 +123,43 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
133123
134124 val type = content.type
135125 if (type == WRITABLE_BOOK || type == Material .WRITTEN_BOOK ) {
136- if (nbt.hasTag(" author" )) {
137- children.add(object : SimpleTextContainer (nbt.getString(" author" ), root) {
138- override fun getResult (): String {
139- val result = super .getResult()
140- nbt.setString(" author" , result)
141- return result
142- }
143- })
126+ val compound = if (NEW_NBT ) {
127+ nbt.getCompound(" components" )?.getCompound(" minecraft:${type.name.lowercase()} _content" )
128+ } else {
129+ nbt.getCompound(" tag" )
130+ } ? : return
131+
132+ if (compound.hasTag(" author" )) {
133+ if (NEW_NBT && compound.getType(" author" ) == NBTType .NBTTagCompound ) {
134+ val author = compound.getCompound(" author" )!!
135+ addTextTag(author, " raw" )
136+ addTextTag(author, " filtered" )
137+ } else {
138+ children.add(CompoundTextContainer (compound, " author" , root))
139+ }
144140 }
145- if (nbt .hasTag(" title" )) {
146- children.add( object : SimpleTextContainer (nbt.getString (" title" ), root ) {
147- override fun getResult (): String {
148- val result = super .getResult( )
149- nbt.setString( " title " , result )
150- return result
151- }
152- })
141+ if (compound .hasTag(" title" )) {
142+ if ( NEW_NBT && compound.getType (" title" ) == NBTType . NBTTagCompound ) {
143+ val author = compound.getCompound( " title " ) !!
144+ addTextTag(author, " raw " )
145+ addTextTag(author, " filtered " )
146+ } else {
147+ children.add( CompoundTextContainer (compound, " title " , root))
148+ }
153149 }
154- if (nbt .hasTag(" pages" )) {
150+ if (compound .hasTag(" pages" )) {
155151 if (type == Material .WRITTEN_BOOK ) {
156- addJsonList(nbt.getStringList(" pages" ))
152+ if (NEW_NBT && compound.getListType(" pages" ) == NBTType .NBTTagCompound ) {
153+ addJsonList(compound.getCompoundList(" pages" ))
154+ } else {
155+ addJsonList(compound.getStringList(" pages" ))
156+ }
157157 } else {
158- addTextList(nbt.getStringList(" pages" ))
158+ if (NEW_NBT && compound.getListType(" pages" ) == NBTType .NBTTagCompound ) {
159+ addTextList(compound.getCompoundList(" pages" ))
160+ } else {
161+ addTextList(compound.getStringList(" pages" ))
162+ }
159163 }
160164 }
161165 }
@@ -165,6 +169,36 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
165169 super .createDefaultChildren()
166170 }
167171
172+ private fun addTextTag (compound : ReadWriteNBT , tag : String ) {
173+ if (compound.hasTag(tag)) {
174+ children.add(CompoundTextContainer (compound, tag, root))
175+ }
176+ }
177+
178+ private fun addJsonTag (compound : ReadWriteNBT , tag : String ) {
179+ if (compound.hasTag(tag)) {
180+ children.add(CompoundJsonContainer (compound, tag, root, true ))
181+ }
182+ }
183+
184+ private fun addJsonList (list : ReadWriteNBTCompoundList ) {
185+ val size = list.size()
186+ for (line in 0 until size) {
187+ val compound = list[line]
188+ addJsonTag(compound, " raw" )
189+ addJsonTag(compound, " filtered" )
190+ }
191+ }
192+
193+ private fun addTextList (list : ReadWriteNBTCompoundList ) {
194+ val size = list.size()
195+ for (line in 0 until size) {
196+ val compound = list[line]
197+ addTextTag(compound, " raw" )
198+ addTextTag(compound, " filtered" )
199+ }
200+ }
201+
168202 private fun addJsonList (list : ReadWriteNBTList <String >) {
169203 val size = list.size()
170204 for (line in 0 until size) {
@@ -235,6 +269,39 @@ class ItemStackContainer @JvmOverloads constructor(itemStack: ItemStack, useCach
235269 return nbt
236270 }
237271
272+ class CompoundJsonContainer (
273+ private val compound : ReadWriteNBT ,
274+ private val key : String ,
275+ root : Container <* >,
276+ createComponents : Boolean ,
277+ private val original : String = compound.getString(key)
278+ ): ChatJsonContainer(original, root, createComponents) {
279+
280+ override fun getResult (): String {
281+ val result = super .getResult()
282+ if (result != original) {
283+ compound.setString(key, result)
284+ }
285+ return result
286+ }
287+ }
288+
289+ class CompoundTextContainer (
290+ private val compound : ReadWriteNBT ,
291+ private val key : String ,
292+ root : Container <* >,
293+ private val original : String = compound.getString(key)
294+ ): SimpleTextContainer(original, root) {
295+
296+ override fun getResult (): String {
297+ val result = super .getResult()
298+ if (result != original) {
299+ compound.setString(key, result)
300+ }
301+ return result
302+ }
303+ }
304+
238305 companion object {
239306
240307 private val NAME_JSON = plugin.serverMajorVersion >= 13
0 commit comments