-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnouncementSender.kt
More file actions
56 lines (48 loc) · 2.46 KB
/
Copy pathAnnouncementSender.kt
File metadata and controls
56 lines (48 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.simplemc.simpleannounce.sender
import io.github.oshai.kotlinlogging.KotlinLogging
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.simplemc.simpleannounce.config.SimpleAnnounceConfig.AnnouncementConfig
import org.simplemc.simpleannounce.inTicks
import java.util.concurrent.atomic.AtomicInteger
import kotlin.random.Random
private val logger = KotlinLogging.logger("SimpleAnnounce AnnouncementSender")
abstract class AnnouncementSender<MessageType : AnnouncementConfig.Message, ConfigType : AnnouncementConfig<MessageType>>(
internal val plugin: Plugin,
internal val announcement: ConfigType,
) : Runnable {
private val currentIndex = AtomicInteger()
private val scheduler = Bukkit.getScheduler()
init {
check(announcement.messages.isNotEmpty()) { "Cannot create an announcement with no messages!" }
scheduleTask()
logger.trace {
"${this::class.java.simpleName} created with ${announcement.messages.size} messages (first message '${announcement.messages.firstOrNull()}'}!"
}
}
private fun scheduleTask() = announcement.repeat?.inTicks?.let { repeatTicks ->
scheduler.runTaskTimer(plugin, this, announcement.delay.inTicks, repeatTicks)
} ?: scheduler.runTaskLater(plugin, this, announcement.delay.inTicks)
internal fun shouldSendTo(player: Player): Boolean {
val hasAllRequiredPermissions = announcement.includesPermissions.none { !player.hasPermission(it) }
val hasNoExcludedPermissions = announcement.excludesPermissions.none { player.hasPermission(it) }
return hasAllRequiredPermissions && hasNoExcludedPermissions
}
internal fun send(message: MessageType, messageAction: (Player) -> Unit) {
val sound = message.sound ?: announcement.sound
Bukkit.getOnlinePlayers().filterNotNull().filter(this::shouldSendTo).forEach {
messageAction(it)
sound?.let { sound -> it.playSound(it.location, sound.sound, sound.volume, sound.pitch) }
}
}
internal fun getNextMessage(): MessageType = when {
announcement.messages.size == 1 -> announcement.messages[0]
announcement.random -> announcement.messages[Random.nextInt(announcement.messages.size)]
else -> {
val nextIndex = currentIndex.getAndIncrement()
if (nextIndex == announcement.messages.lastIndex) currentIndex.set(0)
announcement.messages[nextIndex]
}
}
}