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

Commit

Permalink
api(component): 内部仕様を改善。関数を追加 #52
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Mar 23, 2021
1 parent 5fdf872 commit 634413c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

group = "com.github.sya-ri.spigot.api"
version = "2.2.1"
version = "2.2.2"

bukkit {
name = "EasySpigotAPI"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,7 +9,7 @@ import net.md_5.bungee.api.chat.TextComponent
* @since 1.6.0
*/
class TextComponentBuilder {
private val components = mutableListOf<Component>()
private val components = mutableListOf<TextComponent>()

/**
* 末尾に文字列を挿入する。
Expand All @@ -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)

/**
* 末尾に文字列を挿入し、改行する。
Expand All @@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
*/
Expand Down

0 comments on commit 634413c

Please sign in to comment.