Skip to content

Commit 9b44697

Browse files
committed
code quality improvement
1 parent 5b02521 commit 9b44697

File tree

8 files changed

+135
-130
lines changed

8 files changed

+135
-130
lines changed

src/main/kotlin/tech/aliorpse/mcutils/util/Color.kt renamed to src/main/kotlin/tech/aliorpse/mcutils/model/status/Color.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tech.aliorpse.mcutils.util
1+
package tech.aliorpse.mcutils.model.status
22

33
/**
44
* Represents a color in Minecraft.
@@ -30,6 +30,30 @@ sealed interface Color {
3030
WHITE("#FFFFFF");
3131

3232
companion object {
33+
private val codeToColor = mapOf(
34+
'0' to BLACK,
35+
'1' to DARK_BLUE,
36+
'2' to DARK_GREEN,
37+
'3' to DARK_AQUA,
38+
'4' to DARK_RED,
39+
'5' to DARK_PURPLE,
40+
'6' to GOLD,
41+
'7' to GRAY,
42+
'8' to DARK_GRAY,
43+
'9' to BLUE,
44+
'a' to GREEN,
45+
'b' to AQUA,
46+
'c' to RED,
47+
'd' to LIGHT_PURPLE,
48+
'e' to YELLOW,
49+
'f' to WHITE
50+
)
51+
52+
/**
53+
* Get a [Color.Named] from Minecraft format code (e.g. 'a', 'c').
54+
*/
55+
fun fromCode(code: Char): Named? = codeToColor[code.lowercaseChar()]
56+
3357
private val NAME_MAP = entries.associateBy { it.name.lowercase() }
3458

3559
/**

src/main/kotlin/tech/aliorpse/mcutils/model/status/ColorAdapter.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.squareup.moshi.JsonAdapter
44
import com.squareup.moshi.JsonDataException
55
import com.squareup.moshi.JsonReader
66
import com.squareup.moshi.JsonWriter
7-
import tech.aliorpse.mcutils.util.Color
87

98

109
class ColorAdapter : JsonAdapter<Color>() {

src/main/kotlin/tech/aliorpse/mcutils/model/status/DescriptionAdapter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package tech.aliorpse.mcutils.model.status
33
import com.squareup.moshi.JsonAdapter
44
import com.squareup.moshi.JsonReader
55
import com.squareup.moshi.JsonWriter
6-
import tech.aliorpse.mcutils.util.MOTDParser.objectToSectionFormat
7-
import tech.aliorpse.mcutils.util.MOTDParser.sectionFormatToObject
6+
import tech.aliorpse.mcutils.util.MOTDParser.objToSection
7+
import tech.aliorpse.mcutils.util.MOTDParser.sectionToObj
88

99
class DescriptionAdapter(
1010
private val motdAdapter: JsonAdapter<MOTDTextComponent>
@@ -13,11 +13,11 @@ class DescriptionAdapter(
1313
return when (reader.peek()) {
1414
JsonReader.Token.STRING -> {
1515
val text = reader.nextString()
16-
Description(text, sectionFormatToObject(text))
16+
Description(text, sectionToObj(text))
1717
}
1818
JsonReader.Token.BEGIN_OBJECT -> {
1919
val motdComponent = motdAdapter.fromJson(reader) ?: MOTDTextComponent("")
20-
Description(objectToSectionFormat(motdComponent), motdComponent)
20+
Description(objToSection(motdComponent), motdComponent)
2121
}
2222
else -> {
2323
reader.skipValue()

src/main/kotlin/tech/aliorpse/mcutils/model/status/MOTDTextComponentAdapter.kt

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,14 @@ package tech.aliorpse.mcutils.model.status
33
import com.squareup.moshi.JsonAdapter
44
import com.squareup.moshi.JsonReader
55
import com.squareup.moshi.JsonWriter
6-
import tech.aliorpse.mcutils.util.Color
76

87
class MOTDTextComponentAdapter(
98
private val colorAdapter: JsonAdapter<Color>
109
) : JsonAdapter<MOTDTextComponent>() {
1110
override fun fromJson(reader: JsonReader): MOTDTextComponent? {
1211
return when (reader.peek()) {
1312
JsonReader.Token.STRING -> MOTDTextComponent(text = reader.nextString())
14-
JsonReader.Token.BEGIN_OBJECT -> {
15-
var text = ""
16-
var color: Color? = Color.Named.WHITE
17-
var bold = false
18-
var italic = false
19-
var underlined = false
20-
var strikethrough = false
21-
var obfuscated = false
22-
var extra: List<MOTDTextComponent>? = emptyList()
23-
24-
reader.beginObject()
25-
while (reader.hasNext()) {
26-
when (reader.nextName()) {
27-
"text" -> text = reader.nextString()
28-
"color" -> color = colorAdapter.fromJson(reader) ?: Color.Named.WHITE
29-
"bold" -> bold = reader.nextBoolean()
30-
"italic" -> italic = reader.nextBoolean()
31-
"underlined" -> underlined = reader.nextBoolean()
32-
"strikethrough" -> strikethrough = reader.nextBoolean()
33-
"obfuscated" -> obfuscated = reader.nextBoolean()
34-
"extra" -> {
35-
val list = mutableListOf<MOTDTextComponent>()
36-
reader.beginArray()
37-
while (reader.hasNext()) {
38-
val comp = fromJson(reader)
39-
if (comp != null) list.add(comp)
40-
}
41-
reader.endArray()
42-
extra = list
43-
}
44-
else -> reader.skipValue()
45-
}
46-
}
47-
reader.endObject()
48-
49-
MOTDTextComponent(
50-
text = text,
51-
color = color,
52-
bold = bold,
53-
italic = italic,
54-
underlined = underlined,
55-
strikethrough = strikethrough,
56-
obfuscated = obfuscated,
57-
extra = extra
58-
)
59-
}
13+
JsonReader.Token.BEGIN_OBJECT -> readComponentObject(reader)
6014
else -> {
6115
reader.skipValue()
6216
MOTDTextComponent("")
@@ -86,4 +40,52 @@ class MOTDTextComponentAdapter(
8640
}
8741
writer.endObject()
8842
}
43+
44+
private fun readComponentObject(reader: JsonReader): MOTDTextComponent {
45+
var text = ""
46+
var color: Color? = Color.Named.WHITE
47+
var bold = false
48+
var italic = false
49+
var underlined = false
50+
var strikethrough = false
51+
var obfuscated = false
52+
var extra: List<MOTDTextComponent> = emptyList()
53+
54+
reader.beginObject()
55+
while (reader.hasNext()) {
56+
when (reader.nextName()) {
57+
"text" -> text = reader.nextString()
58+
"color" -> color = colorAdapter.fromJson(reader) ?: Color.Named.WHITE
59+
"bold" -> bold = reader.nextBoolean()
60+
"italic" -> italic = reader.nextBoolean()
61+
"underlined" -> underlined = reader.nextBoolean()
62+
"strikethrough" -> strikethrough = reader.nextBoolean()
63+
"obfuscated" -> obfuscated = reader.nextBoolean()
64+
"extra" -> extra = readExtraArray(reader)
65+
else -> reader.skipValue()
66+
}
67+
}
68+
reader.endObject()
69+
70+
return MOTDTextComponent(
71+
text = text,
72+
color = color,
73+
bold = bold,
74+
italic = italic,
75+
underlined = underlined,
76+
strikethrough = strikethrough,
77+
obfuscated = obfuscated,
78+
extra = extra
79+
)
80+
}
81+
82+
private fun readExtraArray(reader: JsonReader): List<MOTDTextComponent> {
83+
val list = mutableListOf<MOTDTextComponent>()
84+
reader.beginArray()
85+
while (reader.hasNext()) {
86+
fromJson(reader)?.let { list.add(it) }
87+
}
88+
reader.endArray()
89+
return list
90+
}
8991
}

src/main/kotlin/tech/aliorpse/mcutils/model/status/ServerStatus.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tech.aliorpse.mcutils.model.status
22

33
import com.squareup.moshi.JsonClass
4-
import tech.aliorpse.mcutils.util.Color
54

65
/**
76
* Represents the status of a server.

src/main/kotlin/tech/aliorpse/mcutils/module/status/BedrockPing.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import tech.aliorpse.mcutils.model.status.Description
88
import tech.aliorpse.mcutils.model.status.GameMode
99
import tech.aliorpse.mcutils.model.status.Players
1010
import tech.aliorpse.mcutils.model.status.Version
11-
import tech.aliorpse.mcutils.util.MOTDParser.sectionFormatToObject
11+
import tech.aliorpse.mcutils.util.MOTDParser.sectionToObj
1212
import java.io.IOException
1313
import java.net.DatagramPacket
1414
import java.net.DatagramSocket
@@ -98,7 +98,7 @@ object BedrockPing {
9898
val gameMode = runCatching { GameMode.valueOf(parts[8].uppercase()) }.getOrDefault(GameMode.UNKNOWN)
9999

100100
return@withContext BedrockServerStatus(
101-
description = Description(parts[1], sectionFormatToObject(parts[1])),
101+
description = Description(parts[1], sectionToObj(parts[1])),
102102
players = Players(online = online, max = max, sample = emptyList()),
103103
version = Version(name = parts[3], protocol = protocol),
104104
ping = end - start,

src/main/kotlin/tech/aliorpse/mcutils/module/status/JavaPing.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import tech.aliorpse.mcutils.model.status.MOTDTextComponent
1313
import tech.aliorpse.mcutils.model.status.MOTDTextComponentAdapter
1414
import tech.aliorpse.mcutils.model.status.Players
1515
import tech.aliorpse.mcutils.model.status.Version
16-
import tech.aliorpse.mcutils.util.Color
16+
import tech.aliorpse.mcutils.model.status.Color
1717
import java.io.ByteArrayOutputStream
1818
import java.io.DataInputStream
1919
import java.io.DataOutputStream
@@ -35,7 +35,7 @@ import javax.naming.directory.InitialDirContext
3535
* Internal constants and helper methods handle Minecraft's communication protocol, including
3636
* packet construction and parsing.
3737
*/
38-
@Suppress("MagicNumber")
38+
@Suppress("MagicNumber", "TooManyFunctions")
3939
object JavaPing {
4040
private const val HANDSHAKE_PACKET_ID = 0x00
4141
private const val STATUS_REQUEST_PACKET_ID = 0x00
Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tech.aliorpse.mcutils.util
22

3+
import tech.aliorpse.mcutils.model.status.Color
34
import tech.aliorpse.mcutils.model.status.MOTDTextComponent
5+
import tech.aliorpse.mcutils.model.status.colorToOriginalCode
46

57
/**
68
* Utility functions for MOTD analyzing.
@@ -13,7 +15,7 @@ object MOTDParser {
1315
*
1416
* @param component The TextComponent needed to be converted.
1517
*/
16-
fun objectToSectionFormat(component: MOTDTextComponent): String {
18+
fun objToSection(component: MOTDTextComponent): String {
1719
val sb = StringBuilder()
1820

1921
// Process different kinds of color
@@ -51,7 +53,7 @@ object MOTDParser {
5153

5254
// rescue
5355
for (extra in component.extra.orEmpty()) {
54-
sb.append(objectToSectionFormat(extra))
56+
sb.append(objToSection(extra))
5557
}
5658

5759
return sb.toString()
@@ -60,91 +62,70 @@ object MOTDParser {
6062
/**
6163
* § format to [MOTDTextComponent].
6264
*/
63-
@Suppress("AssignedValueIsNeverRead")
64-
fun sectionFormatToObject(text: String): MOTDTextComponent {
65+
fun sectionToObj(text: String): MOTDTextComponent {
6566
val components = mutableListOf<MOTDTextComponent>()
66-
67+
val style = StyleState()
6768
var currentText = StringBuilder()
6869

69-
var currentColor: Color? = Color.Named.WHITE
70-
var bold = false
71-
var italic = false
72-
var underlined = false
73-
var strikethrough = false
74-
var obfuscated = false
75-
76-
fun flushComponent() {
70+
fun flush() {
7771
if (currentText.isEmpty()) return
78-
components.add(
79-
MOTDTextComponent(
80-
text = currentText.toString(),
81-
color = currentColor,
82-
bold = bold,
83-
italic = italic,
84-
underlined = underlined,
85-
strikethrough = strikethrough,
86-
obfuscated = obfuscated,
87-
extra = emptyList()
88-
)
89-
)
72+
components.add(style.toComponent(currentText.toString()))
9073
currentText = StringBuilder()
9174
}
9275

9376
var i = 0
9477
while (i < text.length) {
9578
val c = text[i]
9679
if (c == '§' && i + 1 < text.length) {
97-
flushComponent()
98-
99-
val code = text[i + 1].lowercaseChar()
100-
when (code) {
101-
in '0'..'9' -> currentColor = Color.Named.entries.toTypedArray()[code - '0']
102-
'a' -> currentColor = Color.Named.GREEN
103-
'b' -> currentColor = Color.Named.AQUA
104-
'c' -> currentColor = Color.Named.RED
105-
'd' -> currentColor = Color.Named.LIGHT_PURPLE
106-
'e' -> currentColor = Color.Named.YELLOW
107-
'f' -> currentColor = Color.Named.WHITE
108-
'1' -> currentColor = Color.Named.DARK_BLUE
109-
'2' -> currentColor = Color.Named.DARK_GREEN
110-
'3' -> currentColor = Color.Named.DARK_AQUA
111-
'4' -> currentColor = Color.Named.DARK_RED
112-
'5' -> currentColor = Color.Named.DARK_PURPLE
113-
'6' -> currentColor = Color.Named.GOLD
114-
'7' -> currentColor = Color.Named.GRAY
115-
'8' -> currentColor = Color.Named.DARK_GRAY
116-
'9' -> currentColor = Color.Named.BLUE
117-
'0' -> currentColor = Color.Named.BLACK
118-
119-
'l' -> bold = true
120-
'o' -> italic = true
121-
'n' -> underlined = true
122-
'm' -> strikethrough = true
123-
'k' -> obfuscated = true
124-
125-
'r' -> {
126-
currentColor = Color.Named.WHITE
127-
bold = false
128-
italic = false
129-
underlined = false
130-
strikethrough = false
131-
obfuscated = false
132-
}
133-
}
80+
flush()
81+
style.updateWithCode(text[i + 1])
13482
i += 2
135-
continue
83+
} else {
84+
currentText.append(c)
85+
i++
13686
}
137-
138-
currentText.append(c)
139-
i++
14087
}
14188

142-
flushComponent()
89+
flush()
90+
return MOTDTextComponent(text = "", color = null, extra = components)
91+
}
92+
}
14393

144-
return MOTDTextComponent(
145-
text = "",
146-
color = null,
147-
extra = components
148-
)
94+
private class StyleState {
95+
var color: Color? = Color.Named.WHITE
96+
var bold = false
97+
var italic = false
98+
var underlined = false
99+
var strikethrough = false
100+
var obfuscated = false
101+
102+
fun toComponent(text: String) = MOTDTextComponent(
103+
text = text,
104+
color = color,
105+
bold = bold,
106+
italic = italic,
107+
underlined = underlined,
108+
strikethrough = strikethrough,
109+
obfuscated = obfuscated,
110+
extra = emptyList()
111+
)
112+
113+
fun updateWithCode(code: Char) {
114+
when (code.lowercaseChar()) {
115+
in '0'..'9', in 'a'..'f' -> color = Color.Named.fromCode(code)
116+
'l' -> bold = true
117+
'o' -> italic = true
118+
'n' -> underlined = true
119+
'm' -> strikethrough = true
120+
'k' -> obfuscated = true
121+
'r' -> {
122+
color = Color.Named.WHITE
123+
bold = false
124+
italic = false
125+
underlined = false
126+
strikethrough = false
127+
obfuscated = false
128+
}
129+
}
149130
}
150131
}

0 commit comments

Comments
 (0)