Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Commit 634413c

Browse files
committed
api(component): 内部仕様を改善。関数を追加 #52
1 parent 5fdf872 commit 634413c

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

api/build.gradle.kts

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

1313
group = "com.github.sya-ri.spigot.api"
14-
version = "2.2.1"
14+
version = "2.2.2"
1515

1616
bukkit {
1717
name = "EasySpigotAPI"
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.syari.spigot.api.component
22

3-
import com.github.syari.spigot.api.string.toColor
43
import net.md_5.bungee.api.chat.ClickEvent
54
import net.md_5.bungee.api.chat.HoverEvent
65
import net.md_5.bungee.api.chat.TextComponent
@@ -10,7 +9,7 @@ import net.md_5.bungee.api.chat.TextComponent
109
* @since 1.6.0
1110
*/
1211
class TextComponentBuilder {
13-
private val components = mutableListOf<Component>()
12+
private val components = mutableListOf<TextComponent>()
1413

1514
/**
1615
* 末尾に文字列を挿入する。
@@ -23,17 +22,22 @@ class TextComponentBuilder {
2322
text: String,
2423
hover: HoverEvent? = null,
2524
click: ClickEvent? = null
26-
) = apply {
27-
components.add(Component(text, hover, click))
25+
) = append(textComponent(text, hover, click))
26+
27+
/**
28+
* 末尾に [TextComponent] を挿入する。
29+
* @param component
30+
* @since 2.2.2
31+
*/
32+
fun append(component: TextComponent) = apply {
33+
components.add(component)
2834
}
2935

3036
/**
3137
* 末尾に改行を挿入する。
3238
* @since 1.6.0
3339
*/
34-
fun appendLine() = apply {
35-
components.add(Component.NewLine)
36-
}
40+
fun appendLine() = append(NewLine)
3741

3842
/**
3943
* 末尾に文字列を挿入し、改行する。
@@ -48,22 +52,22 @@ class TextComponentBuilder {
4852
click: ClickEvent? = null
4953
) = append(text, hover, click).appendLine()
5054

55+
/**
56+
* 末尾に [TextComponent] を挿入し、改行する。
57+
* @param component
58+
* @since 2.2.2
59+
*/
60+
fun appendLine(component: TextComponent) = append(component).appendLine()
61+
5162
/**
5263
* [TextComponent] に変換する。
5364
* @since 1.6.0
5465
*/
5566
fun build() = TextComponent().apply {
56-
components.forEach {
57-
TextComponent(it.text.toColor()).apply {
58-
hoverEvent = it.hover
59-
clickEvent = it.click
60-
}.let(::addExtra)
61-
}
67+
components.forEach(::addExtra)
6268
}
6369

64-
private class Component(val text: String, val hover: HoverEvent?, val click: ClickEvent?) {
65-
companion object {
66-
val NewLine = Component("\n", null, null)
67-
}
70+
companion object {
71+
val NewLine = TextComponent(System.lineSeparator())
6872
}
6973
}

api/src/main/kotlin/com/github/syari/spigot/api/component/extension.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.github.syari.spigot.api.string.toColor
66
import net.md_5.bungee.api.chat.ClickEvent
77
import net.md_5.bungee.api.chat.HoverEvent
88
import net.md_5.bungee.api.chat.ItemTag
9+
import net.md_5.bungee.api.chat.TextComponent
910
import net.md_5.bungee.api.chat.hover.content.Item
1011
import net.md_5.bungee.api.chat.hover.content.Text
1112
import org.bukkit.inventory.ItemStack
@@ -16,6 +17,21 @@ import org.bukkit.inventory.ItemStack
1617
*/
1718
inline fun buildTextComponent(action: TextComponentBuilder.() -> Unit) = TextComponentBuilder().apply(action).build()
1819

20+
/**
21+
* @param text 文字列
22+
* @param hover [HoverEvent] default: null
23+
* @param click [ClickEvent] default: null
24+
* @since 2.2.2
25+
*/
26+
fun textComponent(
27+
text: String,
28+
hover: HoverEvent? = null,
29+
click: ClickEvent? = null,
30+
) = TextComponent(text.toColor()).apply {
31+
hoverEvent = hover
32+
clickEvent = click
33+
}
34+
1935
/**
2036
* [ClickEvent.Action.RUN_COMMAND] の [ClickEvent]
2137
* @since 1.6.0

sample/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ class TextComponentBuilder {
7777
*/
7878
inline fun buildTextComponent(action: TextComponentBuilder.() -> Unit): TextComponent
7979

80+
81+
/**
82+
* @param text 文字列
83+
* @param hover [HoverEvent] default: null
84+
* @param click [ClickEvent] default: null
85+
*/
86+
fun textComponent(
87+
text: String,
88+
hover: HoverEvent? = null,
89+
click: ClickEvent? = null,
90+
): TextComponent
91+
8092
/**
8193
* [ClickEvent.Action.RUN_COMMAND] の [ClickEvent]
8294
*/

0 commit comments

Comments
 (0)