Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit d4abe86

Browse files
committed
fix some bugs
1 parent 81426f9 commit d4abe86

File tree

7 files changed

+82
-11
lines changed

7 files changed

+82
-11
lines changed

build.gradle.kts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
}
1212

1313
group = "io.github.dockyardmc"
14-
version = "2.1"
14+
version = "2.2"
1515

1616
repositories {
1717
mavenCentral()
@@ -34,6 +34,10 @@ tasks.withType<Test> {
3434
})
3535
}
3636

37+
tasks.withType<PublishToMavenRepository> {
38+
dependsOn("test")
39+
}
40+
3741
tasks {
3842
compileKotlin {
3943
kotlinOptions.jvmTarget = "21"

src/main/kotlin/io/github/dockyardmc/scroll/Component.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ open class Component(
6464
}
6565
}
6666

67-
fun resetFormatting(includingFont: Boolean = true) {
67+
fun resetFormatting(includingFont: Boolean) {
6868
this.color = null
6969
this.strikethrough = null
7070
this.underlined = null
7171
this.font = null
7272
this.italic = null
7373
this.bold = null
74+
7475
if(includingFont) {
7576
this.font = null
7677
this.hoverEvent = null
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.dockyardmc.scroll
2+
3+
fun main() {
4+
println("Args: ${ScrollUtil.getArguments("<hover:show_text:'<red><bold><i>bucket o' fish'>")}")
5+
}

src/main/kotlin/io/github/dockyardmc/scroll/ScrollUtil.kt

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,49 @@ object ScrollUtil {
8888
return value
8989
}
9090

91+
fun getCharacterAfterNew(string: String, currentIndex: Int): Char? {
92+
val index = currentIndex + 1
93+
val value = if (index >= string.toMutableList().size) null else string[index]
94+
return value
95+
}
96+
9197
fun getArguments(input: String): List<String> {
92-
val pattern = Pattern.compile("<(.*?):(.*)>")
93-
val matcher = pattern.matcher(input)
98+
val split = mutableListOf<String>()
9499

95-
return if (matcher.matches()) {
96-
matcher.group(2).split(":").map { it.removePrefix("'").removeSuffix("'") }
97-
} else {
98-
emptyList()
100+
var current = ""
101+
var isInsideQuotes = false
102+
103+
val validQuotesEndChars = listOf<Char>('>', ':')
104+
105+
input.forEachIndexed { index, char ->
106+
val nextChar = getCharacterAfterNew(input, index)
107+
108+
if(char == '\'') {
109+
if(!isInsideQuotes) {
110+
isInsideQuotes = true
111+
}
112+
else if(validQuotesEndChars.contains(nextChar)) {
113+
isInsideQuotes = false
114+
} else {
115+
current += char
116+
}
117+
} else if(char == ':') {
118+
if(!isInsideQuotes) {
119+
split.add(current)
120+
current = ""
121+
} else {
122+
current += char
123+
}
124+
} else {
125+
current += char
126+
}
99127
}
100-
}
101128

129+
if(current.endsWith(">")) {
130+
split.add((if(!isInsideQuotes) current else current.removePrefix("<")).removeSuffix(">"))
131+
current = ""
132+
}
133+
134+
return split.subList(1, split.size)
135+
}
102136
}

src/main/kotlin/io/github/dockyardmc/scroll/providers/default/NamedColorProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class NamedColorProvider :
1010
ClosingNamedFormatProvider("red", ScrollUtil.colorTags.keys.toList().map { it.replaceMultiple(listOf("<", ">"), "") }) {
1111

1212
override fun formatNormal(context: FormatProviderContext, component: Component) {
13-
component.resetFormatting()
13+
component.resetFormatting(false)
1414
val color = ScrollUtil.colorTags[context.token] ?: throw IllegalStateException("Invalid legacy text color: ${context.token}")
1515
component.color = color
1616
}

src/main/kotlin/io/github/dockyardmc/scroll/providers/default/ResetProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import io.github.dockyardmc.scroll.providers.NamedFormatProvider
77
class ResetProvider: NamedFormatProvider("reset", listOf("r")) {
88

99
override fun format(context: FormatProviderContext, component: Component) {
10-
component.resetFormatting()
10+
component.resetFormatting(true)
1111
}
1212
}

src/test/kotlin/StringSerializationTests.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io.github.dockyardmc.scroll.*
22
import io.github.dockyardmc.scroll.extensions.scrollSanitized
33
import io.github.dockyardmc.scroll.extensions.toComponent
4+
import kotlin.math.exp
45
import kotlin.test.Test
56
import kotlin.test.assertEquals
67

@@ -16,6 +17,16 @@ class StringSerializationTests {
1617
assertEquals(expected.toJson(), Scroll.parse(input).toJson())
1718
}
1819

20+
@Test
21+
fun testFontResettingColor() {
22+
val input = "<#ff54aa><font:ranyth>testing testing <r>testing2"
23+
val expected = Component.compound(mutableListOf(
24+
Component(text = "testing testing ", color = "#ff54aa", font = "ranyth"),
25+
Component(text = "testing2")
26+
))
27+
assertEquals(expected.toJson(), input.toComponent().toJson())
28+
}
29+
1930
@Test
2031
fun testCustomColorSerialization() {
2132
val input = "<#ff54aa>This text should be cute pink color :3"
@@ -138,6 +149,22 @@ class StringSerializationTests {
138149
assertEquals(expected.toJson(), Scroll.parse(input).toJson())
139150
}
140151

152+
@Test
153+
fun testClickWithLink() {
154+
val input = "<click:open_url:'https://lukynka.cloud'><aqua><underline>https://lukynka.cloud"
155+
156+
val expected = Component.compound(mutableListOf(
157+
Component(
158+
text = "https://lukynka.cloud",
159+
clickEvent = ClickEvent(ClickAction.OPEN_URL, "https://lukynka.cloud"),
160+
color = "#55FFFF",
161+
underlined = true
162+
),
163+
))
164+
165+
assertEquals(expected.toJson(), input.toComponent().toJson())
166+
}
167+
141168
// @Test
142169
// fun testGay() {
143170
// val input = "<rainbow>gayyyy"

0 commit comments

Comments
 (0)