Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 12, 2023
1 parent 88f727e commit 635341f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.entity.Player

fun Component.transform(player: Player?, insert: Boolean, isUtf: Boolean) = player?.let { replaceEmoteIds(it, insert) } ?: transformEmoteIds(insert, isUtf)
fun Component.transform(player: Player?, insert: Boolean, isUtf: Boolean = true) = player?.let { replaceEmoteIds(it, insert) } ?: transformEmoteIds(insert, isUtf)

private val spaceRegex: Regex = "(?<!\\\\):space_(-?\\d+):".toRegex()
private val escapedSpaceRegex: Regex = "\\\\(:space_(-?\\d+):)".toRegex()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mineinabyss.emojy.nms
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.mineinabyss.emojy.transform
import io.papermc.paper.text.PaperComponents
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import org.bukkit.entity.Player

Expand Down Expand Up @@ -31,13 +30,13 @@ object EmojyNMSHandlers {
}

private val gson = GsonComponentSerializer.gson()
fun JsonObject.formatString(player: Player? = null): String {
fun JsonObject.formatString() : String {
return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) {
gson.serialize(gson.deserializeFromTree(this).transform(player, true, true))
gson.serialize(gson.deserializeFromTree(this).transform(null, true))
} else this.toString()
}

fun transformer(player: Player? = null) = { string: String ->
fun transformer() = { string: String ->
runCatching {
val element = JsonParser.parseString(string)
if (element.isJsonObject) element.asJsonObject.formatString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

private fun JsonObject.returnFormattedString(insert: Boolean = true): String {
return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) {
gson.serialize(gson.deserialize(this.toString()).transform(null, insert, true))
gson.serialize(gson.deserialize(this.toString()).transform(null, insert))
} else this.toString()
}

Expand All @@ -235,7 +235,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, false, true).serialize()
return super.readUtf(maxLength).miniMsg().transform(player, false).serialize()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

private fun JsonObject.returnFormattedString(): String {
return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) {
gson.serialize(gson.deserialize(this.toString()).transform(null, true, true))
gson.serialize(gson.deserialize(this.toString()).transform(null, true))
} else this.toString()
}

Expand All @@ -239,7 +239,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, false, true).serialize()
return super.readUtf(maxLength).miniMsg().transform(player, false).serialize()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

private fun JsonObject.returnFormattedString(): String {
return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) {
gson.serialize(gson.deserialize(this.toString()).transform(player, true, true))
gson.serialize(gson.deserialize(this.toString()).transform(player, true))
} else this.toString()
}

Expand All @@ -249,7 +249,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, false, true).serialize()
return super.readUtf(maxLength).miniMsg().transform(player, false).serialize()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.mineinabyss.emojy.nms.v1_20_R1

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.mineinabyss.emojy.emojy
import com.mineinabyss.emojy.nms.EmojyNMSHandlers
Expand All @@ -16,7 +15,6 @@ import io.netty.buffer.ByteBuf
import io.netty.channel.*
import io.netty.handler.codec.ByteToMessageDecoder
import io.netty.handler.codec.MessageToByteEncoder
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
import net.minecraft.nbt.StringTag
Expand Down Expand Up @@ -184,7 +182,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, false, true).serialize()
return super.readUtf(maxLength).miniMsg().transform(player, false).serialize()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

override fun readComponent(): net.minecraft.network.chat.Component {
return PaperAdventure.asVanilla(PaperAdventure.asAdventure(super.readComponent()).transform(player, true, true))
return PaperAdventure.asVanilla(PaperAdventure.asAdventure(super.readComponent()).transform(player, true, false))
}

override fun writeUtf(string: String, maxLength: Int): FriendlyByteBuf {
Expand All @@ -159,7 +159,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, true, true).serialize()
return super.readUtf(maxLength).miniMsg().transform(player, true).serialize()
}

override fun writeNbt(compound: Tag?): FriendlyByteBuf {
Expand All @@ -170,7 +170,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

override fun readNbt(): CompoundTag? {
return super.readNbt()?.apply {
transform(this, EmojyNMSHandlers.transformer(player))
transform(this, EmojyNMSHandlers.transformer())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.mineinabyss.emojy.nms.v1_20_R2

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.mineinabyss.emojy.emojy
import com.mineinabyss.emojy.nms.EmojyNMSHandlers
Expand All @@ -15,8 +14,8 @@ import io.netty.buffer.ByteBuf
import io.netty.channel.*
import io.netty.handler.codec.ByteToMessageDecoder
import io.netty.handler.codec.MessageToByteEncoder
import io.papermc.paper.adventure.PaperAdventure
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
import net.minecraft.nbt.StringTag
Expand Down Expand Up @@ -143,7 +142,11 @@ class EmojyNMSHandler : IEmojyNMSHandler {

private class CustomDataSerializer(val player: Player?, bytebuf: ByteBuf) : FriendlyByteBuf(bytebuf) {
override fun writeComponent(component: Component): FriendlyByteBuf {
return super.writeComponent(component.transform(null, true, true))
return super.writeComponent(component.transform(null, true, false))
}

override fun readComponent(): net.minecraft.network.chat.Component {
return PaperAdventure.asVanilla(PaperAdventure.asAdventure(super.readComponent()).transform(player, true, false))
}

override fun writeUtf(string: String, maxLength: Int): FriendlyByteBuf {
Expand All @@ -155,16 +158,22 @@ class EmojyNMSHandler : IEmojyNMSHandler {
return super.writeUtf(string, maxLength)
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, true).serialize()
}

override fun writeNbt(compound: Tag?): FriendlyByteBuf {
return super.writeNbt(compound?.apply {
when (this) {
is CompoundTag -> transform(this, EmojyNMSHandlers.transformer())
is ListTag -> transform(this, EmojyNMSHandlers.transformer())
is StringTag -> transform(this, EmojyNMSHandlers.transformer())
}
transform(this as CompoundTag, EmojyNMSHandlers.transformer())
})
}

override fun readNbt(): CompoundTag? {
return super.readNbt()?.apply {
transform(this, EmojyNMSHandlers.transformer())
}
}

private fun transform(compound: CompoundTag, transformer: Function<String, String>) {
for (key in compound.allKeys) when (val base = compound.get(key)) {
is CompoundTag -> transform(base, transformer)
Expand All @@ -188,10 +197,6 @@ class EmojyNMSHandler : IEmojyNMSHandler {
transformer.apply(string.asString)
}

override fun readUtf(maxLength: Int): String {
return super.readUtf(maxLength).miniMsg().transform(player, true, true).serialize()
}

}

private class CustomPacketEncoder(val player: Player? = null) : MessageToByteEncoder<Packet<*>>() {
Expand Down

0 comments on commit 635341f

Please sign in to comment.