@@ -4,8 +4,10 @@ import com.fasterxml.jackson.annotation.JsonAlias
44import com.fasterxml.jackson.annotation.JsonIgnore
55import com.fasterxml.jackson.annotation.JsonTypeInfo
66import com.fasterxml.jackson.annotation.JsonUnwrapped
7+ import org.bukkit.Sound
78import org.bukkit.boss.BarColor
89import org.bukkit.boss.BarStyle
10+ import org.simplemc.simpleannounce.config.SimpleAnnounceConfig.AnnouncementConfig.Chat.ChatMessage
911import org.simplemc.simpleannounce.inTicks
1012import kotlin.time.Duration
1113import kotlin.time.Duration.Companion.milliseconds
@@ -16,8 +18,8 @@ data class SimpleAnnounceConfig(
1618 val announcements : List <AnnouncementConfig <* >>,
1719) {
1820 companion object {
19- private fun Duration?.checkNullZeroOrPositive (name : String ) {
20- check (this == null || this == Duration .Companion .ZERO || this .isPositive()) {
21+ private fun Duration?.requireNullZeroOrPositive (name : String ) {
22+ require (this == null || this == Duration .Companion .ZERO || this .isPositive()) {
2123 " When set, $name must be >= 0s"
2224 }
2325 }
@@ -27,53 +29,63 @@ data class SimpleAnnounceConfig(
2729 val autoReloadTicks = autoReload?.inTicks
2830
2931 init {
30- check (autoReload == null || autoReload == Duration .Companion .ZERO || autoReload.inWholeMinutes >= 1 ) {
32+ require (autoReload == null || autoReload == Duration .Companion .ZERO || autoReload.inWholeMinutes >= 1 ) {
3133 " When set, Auto Reload Duration must be > 1 minute"
3234 }
3335 }
3436
3537 @JsonTypeInfo(use = JsonTypeInfo .Id .SIMPLE_NAME , include = JsonTypeInfo .As .PROPERTY , property = " type" )
36- sealed class AnnouncementConfig <T > {
38+ sealed class AnnouncementConfig <T : AnnouncementConfig . Message > {
3739 abstract val random: Boolean
3840 abstract val delay: Duration
3941 abstract val repeat: Duration ?
42+ abstract val sound: SoundConfig ?
4043 abstract val includesPermissions: List <String >
4144 abstract val excludesPermissions: List <String >
4245 abstract val messages: List <T >
4346
47+ interface Message {
48+ val sound: SoundConfig ?
49+ }
50+
4451 @JsonIgnore
4552 val delayTicks = delay.inTicks.toInt()
4653
4754 @JsonIgnore
4855 val repeatTicks = repeat?.inTicks?.toInt()
4956
5057 init {
51- delay.checkNullZeroOrPositive (" delay" )
52- repeat.checkNullZeroOrPositive (" repeat" )
58+ delay.requireNullZeroOrPositive (" delay" )
59+ repeat.requireNullZeroOrPositive (" repeat" )
5360 }
5461
5562 data class Chat (
5663 override val random : Boolean = false ,
5764 override val delay : Duration = Duration .Companion .ZERO ,
5865 override val repeat : Duration ? = null ,
66+ override val sound : SoundConfig ? = null ,
5967 override val includesPermissions : List <String > = emptyList(),
6068 override val excludesPermissions : List <String > = emptyList(),
61- @field:JsonAlias("message") override val messages : List <String >,
62- ) : AnnouncementConfig<String>()
69+ @field:JsonAlias("message") override val messages : List <ChatMessage >,
70+ ) : AnnouncementConfig<ChatMessage>() {
71+ data class ChatMessage (val message : String , override val sound : SoundConfig ? = null ) : Message
72+ }
6373
6474 data class Boss (
6575 override val random : Boolean = false ,
6676 override val delay : Duration = Duration .Companion .ZERO ,
6777 override val repeat : Duration ? = null ,
78+ override val sound : SoundConfig ? = null ,
6879 override val includesPermissions : List <String > = emptyList(),
6980 override val excludesPermissions : List <String > = emptyList(),
7081 @field:JsonAlias("message") override val messages : List <BossBarMessage >,
7182 @field:JsonUnwrapped val barConfig : BarConfig = BarConfig (),
7283 ) : AnnouncementConfig<Boss.BossBarMessage>() {
7384 data class BossBarMessage (
7485 val message : String ,
86+ override val sound : SoundConfig ? = null ,
7587 @field:JsonUnwrapped val barConfig : BarConfig ? = null ,
76- ) {
88+ ) : Message {
7789 init {
7890 require(message.length <= 64 ) { " Boss Bar text must be <= 64 characters" }
7991 }
@@ -90,7 +102,7 @@ data class SimpleAnnounceConfig(
90102 val holdTicks = hold.inTicks
91103
92104 init {
93- hold.checkNullZeroOrPositive (" hold" )
105+ hold.requireNullZeroOrPositive (" hold" )
94106 }
95107 }
96108 }
@@ -99,6 +111,7 @@ data class SimpleAnnounceConfig(
99111 override val random : Boolean = false ,
100112 override val delay : Duration = Duration .Companion .ZERO ,
101113 override val repeat : Duration ? = null ,
114+ override val sound : SoundConfig ? = null ,
102115 override val includesPermissions : List <String > = emptyList(),
103116 override val excludesPermissions : List <String > = emptyList(),
104117 @field:JsonAlias("message") override val messages : List <TitleMessage >,
@@ -107,8 +120,9 @@ data class SimpleAnnounceConfig(
107120 data class TitleMessage (
108121 val title : String ,
109122 val subtitle : String? = null ,
123+ override val sound : SoundConfig ? = null ,
110124 @field:JsonUnwrapped val titleConfig : TitleConfig ? = null ,
111- )
125+ ) : Message
112126
113127 data class TitleConfig (
114128 val fadeIn : Duration = 500 .milliseconds,
@@ -125,11 +139,18 @@ data class SimpleAnnounceConfig(
125139 val fadeOutTicks = fadeOut.inTicks.toInt()
126140
127141 init {
128- fadeIn.checkNullZeroOrPositive (" fadeIn" )
129- stay.checkNullZeroOrPositive (" stay" )
130- fadeOut.checkNullZeroOrPositive (" fadeOut" )
142+ fadeIn.requireNullZeroOrPositive (" fadeIn" )
143+ stay.requireNullZeroOrPositive (" stay" )
144+ fadeOut.requireNullZeroOrPositive (" fadeOut" )
131145 }
132146 }
133147 }
148+
149+ data class SoundConfig (val sound : Sound , val volume : Float = 1F , val pitch : Float = 1F ) {
150+ init {
151+ require(volume >= 0 && volume <= 1 ) { " Sound volume must be between 0 and 1" }
152+ require(pitch >= 0.5 && pitch <= 2 ) { " Sound pitch must be between 0.5 and 2" }
153+ }
154+ }
134155 }
135156}
0 commit comments