Skip to content

Commit 2126a24

Browse files
authored
GH-5: Chat component support (#14)
1 parent 727111e commit 2126a24

File tree

9 files changed

+126
-22
lines changed

9 files changed

+126
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44
### Added
5+
- GH-5: JSON Chat Component (JSON) support in messages (@tajobe)
6+
- Uses legacy conversion for Boss and Title announcements, full component support for Chat
57
- GH-4: Sound support (@tajobe)
68
- GH-3: Title type announcement sender (@tajobe)
79
- MIT license

README.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Messages can be sent server-wide or controlled by permissions after a delay and
3434
- ...additional options depending on announcement type
3535
- `Chat` type announcement:
3636
- `messages`(ChatMessage list): Message(s) to send
37-
- `message`(string): message string
37+
- `message`([Chat Component]): message component (plain text, or json, see examples)
3838
- `sound`(SoundConfig, optional): Override announcement SoundConfig
3939
- `Boss` type announcement:
4040
- `hold`(duration*): Time for boss bar to be on screen
@@ -43,16 +43,16 @@ Messages can be sent server-wide or controlled by permissions after a delay and
4343
- `animate`(boolean): if bar should animate over hold time
4444
- `reverseAnimation`(boolean): if animation should be reversed
4545
- `messages`(BossBarMessage list):
46-
- `message`(string): message string
46+
- `message`([Chat Component]): message component (plain text, or json, see examples)
4747
- `sound`(SoundConfig, optional): Override announcement SoundConfig
4848
- ...boss bar config overrides per message eg hold, color, style, animate, etc...
4949
- `Title` type announcement:
5050
- `fadeIn`(duration*): Time it takes for title to fade in
5151
- `stay`(duration*): Time for title to stay on screen
5252
- `fadeOut`(duration*): Time it takes for title to fade out
5353
- `messages`(TitleMessage list):
54-
- `title`(string): title string
55-
- `subtitle`(string): subtitle string, appears below title slightly smaller
54+
- `title`([Chat Component]): title component (plain text, or json, see examples)
55+
- `subtitle`([Chat Component]): subtitle component (plain text, or json, see examples), appears below title slightly smaller
5656
- `sound`(SoundConfig, optional): Override announcement SoundConfig
5757
- ...title config overrides eg fadeIn, stay, fadeOut...
5858
- `config-version`: **Internal use for configuration migrations, do not edit**
@@ -120,6 +120,59 @@ announcements:
120120
config-version: 1
121121
```
122122
123+
#### Chat Components
124+
125+
Message contents are parsed as [Chat Component]s. This means that in addition to the normal `message: some message`, you can use JSON or YAML-formatted components.
126+
127+
```yaml
128+
- type: Chat
129+
repeat: 30s
130+
messages:
131+
- message: {
132+
"extra": [
133+
{
134+
"color": "gold",
135+
"text": "This is a "
136+
},
137+
{
138+
"bold": true,
139+
"color": "gold",
140+
"clickEvent": {
141+
"action": "open_url",
142+
"value": "https://www.spigotmc.org/wiki/the-chat-component-api/"
143+
},
144+
"hoverEvent": {
145+
"action": "show_text",
146+
"contents": "Chat Component API"
147+
},
148+
"text": "TextComponent"
149+
},
150+
{ "text": " announcement!" }
151+
]
152+
}
153+
```
154+
or
155+
```yaml
156+
- type: Chat
157+
repeat: 30s
158+
messages:
159+
- message:
160+
extra:
161+
- color: gold
162+
text: "This is a "
163+
- bold: true
164+
color: gold
165+
text: TextComponent
166+
clickEvent:
167+
action: open_url
168+
value: "https://www.spigotmc.org/wiki/the-chat-component-api/"
169+
hoverEvent:
170+
action: show_text
171+
"contents": "Chat Component API"
172+
- text: " announcement!"
173+
```
174+
123175
[Sound]: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html
124176
[BarColor]: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/boss/BarColor.html
125177
[BarStyle]: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/boss/BarStyle.html
178+
[Chat Component]: https://www.spigotmc.org/wiki/the-chat-component-api/

src/main/kotlin/org/simplemc/simpleannounce/SimpleAnnounce.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import com.fasterxml.jackson.databind.ObjectMapper
55
import com.fasterxml.jackson.databind.module.SimpleModule
66
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
77
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
8+
import com.fasterxml.jackson.module.kotlin.addDeserializer
89
import com.fasterxml.jackson.module.kotlin.readValue
910
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
11+
import net.md_5.bungee.api.chat.BaseComponent
1012
import org.bukkit.command.Command
1113
import org.bukkit.command.CommandSender
1214
import org.bukkit.plugin.java.JavaPlugin
15+
import org.simplemc.simpleannounce.config.BaseComponentDeserializer
1316
import org.simplemc.simpleannounce.config.DurationDeserializer
1417
import org.simplemc.simpleannounce.config.DurationSerializer
1518
import org.simplemc.simpleannounce.config.SimpleAnnounceConfig
@@ -27,7 +30,8 @@ class SimpleAnnounce : JavaPlugin() {
2730
private const val RELOAD_COMMAND = "simpleannouncereload"
2831

2932
private val durationModule = SimpleModule()
30-
.addDeserializer(Duration::class.java, DurationDeserializer())
33+
.addDeserializer(Duration::class, DurationDeserializer())
34+
.addDeserializer(BaseComponent::class, BaseComponentDeserializer())
3135
.addSerializer(DurationSerializer())
3236
}
3337

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.simplemc.simpleannounce.config
2+
3+
import com.fasterxml.jackson.core.JsonParser
4+
import com.fasterxml.jackson.databind.DeserializationContext
5+
import com.fasterxml.jackson.databind.JsonDeserializer
6+
import com.fasterxml.jackson.databind.JsonNode
7+
import net.md_5.bungee.api.chat.BaseComponent
8+
import net.md_5.bungee.chat.ComponentSerializer
9+
10+
class BaseComponentDeserializer : JsonDeserializer<BaseComponent>() {
11+
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): BaseComponent? {
12+
val json = p.readValueAsTree<JsonNode>().toString()
13+
return try {
14+
ComponentSerializer.deserialize(json)
15+
} catch (_: IllegalArgumentException) {
16+
throw ctxt.weirdKeyException(
17+
BaseComponent::class.java,
18+
json,
19+
"Couldn't parse TextComponent",
20+
)
21+
}
22+
}
23+
}

src/main/kotlin/org/simplemc/simpleannounce/config/SimpleAnnounceConfig.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonAlias
44
import com.fasterxml.jackson.annotation.JsonIgnore
55
import com.fasterxml.jackson.annotation.JsonTypeInfo
66
import com.fasterxml.jackson.annotation.JsonUnwrapped
7+
import net.md_5.bungee.api.chat.BaseComponent
78
import org.bukkit.Sound
89
import org.bukkit.boss.BarColor
910
import org.bukkit.boss.BarStyle
@@ -68,7 +69,7 @@ data class SimpleAnnounceConfig(
6869
override val excludesPermissions: List<String> = emptyList(),
6970
@field:JsonAlias("message") override val messages: List<ChatMessage>,
7071
) : AnnouncementConfig<ChatMessage>() {
71-
data class ChatMessage(val message: String, override val sound: SoundConfig? = null) : Message
72+
data class ChatMessage(val message: BaseComponent, override val sound: SoundConfig? = null) : Message
7273
}
7374

7475
data class Boss(
@@ -82,14 +83,10 @@ data class SimpleAnnounceConfig(
8283
@field:JsonUnwrapped val barConfig: BarConfig = BarConfig(),
8384
) : AnnouncementConfig<Boss.BossBarMessage>() {
8485
data class BossBarMessage(
85-
val message: String,
86+
val message: BaseComponent,
8687
override val sound: SoundConfig? = null,
8788
@field:JsonUnwrapped val barConfig: BarConfig? = null,
88-
) : Message {
89-
init {
90-
require(message.length <= 64) { "Boss Bar text must be <= 64 characters" }
91-
}
92-
}
89+
) : Message
9390

9491
data class BarConfig(
9592
val hold: Duration = 5.seconds,
@@ -118,8 +115,8 @@ data class SimpleAnnounceConfig(
118115
@field:JsonUnwrapped val titleConfig: TitleConfig = TitleConfig(),
119116
) : AnnouncementConfig<Title.TitleMessage>() {
120117
data class TitleMessage(
121-
val title: String,
122-
val subtitle: String? = null,
118+
val title: BaseComponent,
119+
val subtitle: BaseComponent? = null,
123120
override val sound: SoundConfig? = null,
124121
@field:JsonUnwrapped val titleConfig: TitleConfig? = null,
125122
) : Message

src/main/kotlin/org/simplemc/simpleannounce/sender/BossBarSender.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BossBarSender(
1515
val barConfig = message.barConfig ?: announcement.barConfig
1616

1717
// create the bar
18-
val bar = Bukkit.createBossBar(message.message, barConfig.color, barConfig.style)
18+
val bar = Bukkit.createBossBar(message.message.toLegacyText(), barConfig.color, barConfig.style)
1919
bar.progress = if (barConfig.reverseAnimation) 1.0 else 0.0
2020

2121
// show bar to players

src/main/kotlin/org/simplemc/simpleannounce/sender/ChatSender.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class ChatSender(
99
) : AnnouncementSender<Chat.ChatMessage, Chat>(plugin, announcement) {
1010
override fun run() {
1111
val message = getNextMessage()
12-
send(message) { it.sendMessage(message.message) }
12+
send(message) { it.spigot().sendMessage(message.message) }
1313
}
1414
}

src/main/kotlin/org/simplemc/simpleannounce/sender/TitleSender.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class TitleSender(
1313

1414
send(message) {
1515
it.sendTitle(
16-
message.title,
17-
message.subtitle,
16+
message.title.toLegacyText(),
17+
message.subtitle?.toLegacyText(),
1818
titleConfig.fadeInTicks,
1919
titleConfig.stayTicks,
2020
titleConfig.fadeOutTicks,

src/main/resources/config.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# Chat type:
3232

3333
# messages(ChatMessage list):
34-
# - message(string): <message string>
34+
# - message(Chat Component): <message component (string or JSON component, see https://www.spigotmc.org/wiki/the-chat-component-api/)>
3535
# sound(SoundConfig, optional): <Override announcement Sound config per message>
3636
#
3737
# Boss type:
@@ -42,7 +42,7 @@
4242
# animate(boolean): <if bar should animate over hold time>
4343
# reverseAnimation(boolean): <if animation should be reversed>
4444
# messages(BossBarMessage list):
45-
# - message(string): <message string>
45+
# - message(Chat Component): <message component (string or JSON component, see https://www.spigotmc.org/wiki/the-chat-component-api/)>
4646
# sound(SoundConfig, optional): <Override announcement Sound config per message>
4747
# <boss bar config overrides eg hold, color, style, animate, etc...see above>
4848
#
@@ -52,8 +52,8 @@
5252
# stay(duration): <Time for title to stay on screen>
5353
# fadeOut(duration): <Time it takes for title to fade out>
5454
# messages(TitleMessage list):
55-
# - title(string): <title string>
56-
# subtitle(string): <subtitle string, appears below title slightly smaller>
55+
# - title(Chat Component): <title component (string or JSON component, see https://www.spigotmc.org/wiki/the-chat-component-api/)>
56+
# subtitle(Chat Component): <subtitle component (string or JSON component, see https://www.spigotmc.org/wiki/the-chat-component-api/), appears below title slightly smaller>
5757
# sound(SoundConfig, optional): <Override announcement Sound config per message>
5858
# <title config overrides eg fadeIn, stay, fadeOut...see above>
5959
#
@@ -111,4 +111,29 @@ announcements:
111111
color: PURPLE
112112
style: SOLID
113113
animate: true
114+
- type: Chat
115+
repeat: 5m
116+
messages:
117+
- message: {
118+
"extra": [
119+
{
120+
"color": "gold",
121+
"text": "This is a "
122+
},
123+
{
124+
"bold": true,
125+
"color": "gold",
126+
"clickEvent": {
127+
"action": "open_url",
128+
"value": "https://www.spigotmc.org/wiki/the-chat-component-api/"
129+
},
130+
"hoverEvent": {
131+
"action": "show_text",
132+
"contents": "Chat Component API"
133+
},
134+
"text": "TextComponent"
135+
},
136+
{ "text": " announcement!" }
137+
]
138+
}
114139
config-version: 1

0 commit comments

Comments
 (0)