From 634413cc82390ec25a0accd8ef03bac117e98f0b Mon Sep 17 00:00:00 2001 From: sya-ri Date: Wed, 24 Mar 2021 00:00:30 +0900 Subject: [PATCH] =?UTF-8?q?api(component):=20=E5=86=85=E9=83=A8=E4=BB=95?= =?UTF-8?q?=E6=A7=98=E3=82=92=E6=94=B9=E5=96=84=E3=80=82=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/build.gradle.kts | 2 +- .../api/component/TextComponentBuilder.kt | 38 ++++++++++--------- .../syari/spigot/api/component/extension.kt | 16 ++++++++ sample/README.md | 12 ++++++ 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index ee893ff3..c319d259 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -11,7 +11,7 @@ repositories { } group = "com.github.sya-ri.spigot.api" -version = "2.2.1" +version = "2.2.2" bukkit { name = "EasySpigotAPI" diff --git a/api/src/main/kotlin/com/github/syari/spigot/api/component/TextComponentBuilder.kt b/api/src/main/kotlin/com/github/syari/spigot/api/component/TextComponentBuilder.kt index e5036de1..f181a309 100644 --- a/api/src/main/kotlin/com/github/syari/spigot/api/component/TextComponentBuilder.kt +++ b/api/src/main/kotlin/com/github/syari/spigot/api/component/TextComponentBuilder.kt @@ -1,6 +1,5 @@ package com.github.syari.spigot.api.component -import com.github.syari.spigot.api.string.toColor import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.HoverEvent import net.md_5.bungee.api.chat.TextComponent @@ -10,7 +9,7 @@ import net.md_5.bungee.api.chat.TextComponent * @since 1.6.0 */ class TextComponentBuilder { - private val components = mutableListOf() + private val components = mutableListOf() /** * 末尾に文字列を挿入する。 @@ -23,17 +22,22 @@ class TextComponentBuilder { text: String, hover: HoverEvent? = null, click: ClickEvent? = null - ) = apply { - components.add(Component(text, hover, click)) + ) = append(textComponent(text, hover, click)) + + /** + * 末尾に [TextComponent] を挿入する。 + * @param component + * @since 2.2.2 + */ + fun append(component: TextComponent) = apply { + components.add(component) } /** * 末尾に改行を挿入する。 * @since 1.6.0 */ - fun appendLine() = apply { - components.add(Component.NewLine) - } + fun appendLine() = append(NewLine) /** * 末尾に文字列を挿入し、改行する。 @@ -48,22 +52,22 @@ class TextComponentBuilder { click: ClickEvent? = null ) = append(text, hover, click).appendLine() + /** + * 末尾に [TextComponent] を挿入し、改行する。 + * @param component + * @since 2.2.2 + */ + fun appendLine(component: TextComponent) = append(component).appendLine() + /** * [TextComponent] に変換する。 * @since 1.6.0 */ fun build() = TextComponent().apply { - components.forEach { - TextComponent(it.text.toColor()).apply { - hoverEvent = it.hover - clickEvent = it.click - }.let(::addExtra) - } + components.forEach(::addExtra) } - private class Component(val text: String, val hover: HoverEvent?, val click: ClickEvent?) { - companion object { - val NewLine = Component("\n", null, null) - } + companion object { + val NewLine = TextComponent(System.lineSeparator()) } } diff --git a/api/src/main/kotlin/com/github/syari/spigot/api/component/extension.kt b/api/src/main/kotlin/com/github/syari/spigot/api/component/extension.kt index 4f2abc30..15422279 100644 --- a/api/src/main/kotlin/com/github/syari/spigot/api/component/extension.kt +++ b/api/src/main/kotlin/com/github/syari/spigot/api/component/extension.kt @@ -6,6 +6,7 @@ import com.github.syari.spigot.api.string.toColor import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.HoverEvent import net.md_5.bungee.api.chat.ItemTag +import net.md_5.bungee.api.chat.TextComponent import net.md_5.bungee.api.chat.hover.content.Item import net.md_5.bungee.api.chat.hover.content.Text import org.bukkit.inventory.ItemStack @@ -16,6 +17,21 @@ import org.bukkit.inventory.ItemStack */ inline fun buildTextComponent(action: TextComponentBuilder.() -> Unit) = TextComponentBuilder().apply(action).build() +/** + * @param text 文字列 + * @param hover [HoverEvent] default: null + * @param click [ClickEvent] default: null + * @since 2.2.2 + */ +fun textComponent( + text: String, + hover: HoverEvent? = null, + click: ClickEvent? = null, +) = TextComponent(text.toColor()).apply { + hoverEvent = hover + clickEvent = click +} + /** * [ClickEvent.Action.RUN_COMMAND] の [ClickEvent] * @since 1.6.0 diff --git a/sample/README.md b/sample/README.md index 78dfed0e..ec43eb3c 100644 --- a/sample/README.md +++ b/sample/README.md @@ -77,6 +77,18 @@ class TextComponentBuilder { */ inline fun buildTextComponent(action: TextComponentBuilder.() -> Unit): TextComponent + +/** + * @param text 文字列 + * @param hover [HoverEvent] default: null + * @param click [ClickEvent] default: null + */ +fun textComponent( + text: String, + hover: HoverEvent? = null, + click: ClickEvent? = null, +): TextComponent + /** * [ClickEvent.Action.RUN_COMMAND] の [ClickEvent] */