Skip to content

Commit

Permalink
Change transform to replace list directly instead of removing and adding
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Nov 30, 2023
1 parent 7d86f5d commit 2a30137
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ class EmojyNMSHandler : IEmojyNMSHandler {

override fun writeNbt(compound: Tag?): FriendlyByteBuf {
return super.writeNbt(compound?.apply { this as CompoundTag
transform(this, Function { string: String ->
return@Function runCatching {
transform(this) { string: String ->
runCatching {
val element = JsonParser.parseString(string)
if (element.isJsonObject) element.asJsonObject.formatString(false)
else string
}.getOrNull() ?: string
})
}
})
}

Expand All @@ -183,12 +183,12 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

private fun transform(list: ListTag, transformer: Function<String, String>) {
for (base in list) when (base) {
val listCopy = list.toList()
for (base in listCopy) when (base) {
is CompoundTag -> transform(base, transformer)
is ListTag -> transform(base, transformer)
is StringTag -> list.indexOf(base).let { index ->
list.add(index, StringTag.valueOf(transformer.apply(base.asString)))
list.removeAt(index + 1)
list[index] = StringTag.valueOf(transformer.apply(base.asString))
}
}
}
Expand Down

0 comments on commit 2a30137

Please sign in to comment.