diff --git a/modules/listener/src/main/kotlin/com/imedvedev/minecraft/notification/bot/listener/NotificationEventListener.kt b/modules/listener/src/main/kotlin/com/imedvedev/minecraft/notification/bot/listener/NotificationEventListener.kt index 31056eb..95c9a8a 100644 --- a/modules/listener/src/main/kotlin/com/imedvedev/minecraft/notification/bot/listener/NotificationEventListener.kt +++ b/modules/listener/src/main/kotlin/com/imedvedev/minecraft/notification/bot/listener/NotificationEventListener.kt @@ -1,9 +1,9 @@ package com.imedvedev.minecraft.notification.bot.listener import com.imedvedev.minecraft.notification.bot.messenger.Messenger -import fr.xephi.authme.api.v3.AuthMeApi import fr.xephi.authme.events.LoginEvent import fr.xephi.authme.events.LogoutEvent +import org.bukkit.ChatColor import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority @@ -11,27 +11,33 @@ import org.bukkit.event.Listener import org.bukkit.event.player.PlayerQuitEvent import java.util.logging.Level import java.util.logging.Logger -import kotlin.collections.LinkedHashMap +import kotlin.math.absoluteValue class NotificationEventListener(private val joinedMessage: String, private val leftMessage: String, private val onlineMessage: String, + private val chatColors: List, private val messenger: Messenger, - private val authMeApi: AuthMeApi, + private val onlinePlayers: () -> Iterable, + private val isAuthenticated: (Player) -> Boolean, private val logger: Logger, - private val quarantineScheduler: (() -> Unit) -> Unit) : Listener { - private val onlinePlayers: MutableMap = LinkedHashMap() + private val quarantineScheduler: (() -> Unit) -> Unit, + private val pluginName: String, + private val pluginVersion: String) : Listener { + private val authenticatedPlayers: MutableMap = LinkedHashMap() @EventHandler(priority = EventPriority.MONITOR) fun onLogin(event: LoginEvent) { event.player.takeIf(Player::isOnline)?.run { - if (onlinePlayers.put(name, entityId) == null) { + if (authenticatedPlayers.put(name, entityId) == null) { + sendMessage("$pluginName $pluginVersion") logger.log(Level.INFO) { "$name joined the game. " + "Server is sending notification" } sendNotification(joinedMessage) } else { logger.log(Level.INFO) { "$name joined the game when was quarantined. " + "Server is not sending notification" } + sendMessage(authenticatedPlayerNames()) } } } @@ -39,7 +45,7 @@ class NotificationEventListener(private val joinedMessage: String, @EventHandler(priority = EventPriority.MONITOR) fun onLogout(event: LogoutEvent) { event.player.takeIf(Player::isOnline)?.run { - onlinePlayers.remove(name) + authenticatedPlayers.remove(name) logger.log(Level.INFO) { "$name left the game. " + "Server is sending notification" } sendNotification(leftMessage) @@ -48,24 +54,35 @@ class NotificationEventListener(private val joinedMessage: String, @EventHandler(priority = EventPriority.MONITOR) fun onQuit(event: PlayerQuitEvent) { - event.player.takeIf(authMeApi::isAuthenticated)?.run { + event.player.takeIf(isAuthenticated)?.run { logger.log(Level.INFO) { "$name has been quarantined" } quarantineScheduler.invoke { - if (onlinePlayers.remove(name, entityId)) { + if (authenticatedPlayers.remove(name, entityId)) { logger.log(Level.INFO) { "Quarantine has been finished. $name left the game. " + - "Server is sending notification" } + "Server is sending notification" } sendNotification(leftMessage) } else { logger.log(Level.INFO) { "Quarantine has been finished. $name already joined the game again. " + - "Server is not sending notification" } + "Server is not sending notification" } } } } } - private fun Player.sendNotification(message: String) = messenger - .send("$name $message\n$onlineMessage: ${onlinePlayers.keys.map { "${it}" }}") - .forEach { job -> job.exceptionally { exception -> logger.log(Level.WARNING, exception) { - "Server can not send the notification: \"$message\"" - } } } + private fun Player.sendNotification(message: String) { + messenger.send("$name $message\n$onlineMessage: ${authenticatedPlayers.keys.map { "$it" }}") + .forEach { job -> job.exceptionally { exception -> logger.log(Level.WARNING, exception) { + "Server can not send the notification: \"$message\"" + } } } + + authenticatedPlayerNames().let { authenticatedPlayerNames -> + onlinePlayers().filter(isAuthenticated) + .forEach { it.sendMessage(authenticatedPlayerNames) } + } + } + + private fun authenticatedPlayerNames(): String = authenticatedPlayers.keys.map { name -> + name.toByteArray().reduce { first: Byte, second: Byte -> (first + second).toByte() }.toInt().absoluteValue + .let { "${chatColors[it % chatColors.size]}${ChatColor.stripColor(name)}${ChatColor.RESET}" } + }.let { "${ChatColor.GREEN}${ChatColor.stripColor(onlineMessage)}: ${ChatColor.RESET}$it" } } diff --git a/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/NotificationBotPlugin.kt b/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/NotificationBotPlugin.kt index 8f9347b..425e1bd 100644 --- a/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/NotificationBotPlugin.kt +++ b/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/NotificationBotPlugin.kt @@ -1,8 +1,27 @@ package com.imedvedev.minecraft.notification.bot -import com.imedvedev.minecraft.notification.bot.injector.get +import com.imedvedev.minecraft.notification.bot.injector.module.notificationEventListenerModule +import com.imedvedev.minecraft.notification.bot.injector.module.proxyModule +import com.imedvedev.minecraft.notification.bot.injector.module.telegramMessengerModule +import com.imedvedev.minecraft.notification.bot.messenger.Messenger +import org.bukkit.plugin.Plugin import org.bukkit.plugin.java.JavaPlugin +import org.koin.core.context.startKoin +import org.koin.dsl.module class NotificationBotPlugin : JavaPlugin() { - override fun onEnable() = server.pluginManager.registerEvents(get(this), this) + override fun onEnable() { + val koin = startKoin { modules(listOf( + module(true) { single { this@NotificationBotPlugin } }, + proxyModule, + telegramMessengerModule, + notificationEventListenerModule + )) }.koin + + config.getConfigurationSection("message")!!.getString("started").let { + koin.get().send("$it\n${description.name}: ${description.version}") + } + + server.pluginManager.registerEvents(koin.get(), this) + } } diff --git a/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/Injector.kt b/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/Injector.kt deleted file mode 100644 index 1ece588..0000000 --- a/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/Injector.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.imedvedev.minecraft.notification.bot.injector - -import com.imedvedev.minecraft.notification.bot.injector.module.notificationEventListenerModule -import com.imedvedev.minecraft.notification.bot.injector.module.proxyModule -import com.imedvedev.minecraft.notification.bot.injector.module.telegramMessengerModule -import org.bukkit.plugin.Plugin -import org.koin.core.context.startKoin -import org.koin.dsl.module - -inline fun get(plugin: Plugin): T { - val app = startKoin { modules(listOf( - module(true) { single { plugin } }, - proxyModule, - telegramMessengerModule, - notificationEventListenerModule - )) } - - val result = app.koin.get() - app.close() - return result -} diff --git a/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/module/NotificationEventListenerModule.kt b/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/module/NotificationEventListenerModule.kt index 7c55360..b1691e7 100644 --- a/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/module/NotificationEventListenerModule.kt +++ b/modules/plugin/src/main/kotlin/com/imedvedev/minecraft/notification/bot/injector/module/NotificationEventListenerModule.kt @@ -2,23 +2,30 @@ package com.imedvedev.minecraft.notification.bot.injector.module import com.imedvedev.minecraft.notification.bot.listener.NotificationEventListener import fr.xephi.authme.api.v3.AuthMeApi +import org.bukkit.ChatColor import org.bukkit.event.Listener import org.bukkit.plugin.Plugin import org.koin.dsl.module val notificationEventListenerModule = module(true) { single { - get().let { plugin -> plugin.config.getConfigurationSection("message")!!.let { config -> + get().let { plugin -> plugin.config.let { config -> + val messageConfig = config.getConfigurationSection("message")!! NotificationEventListener( - config.getString("joined")!!, - config.getString("left")!!, - config.getString("online")!!, - get(), - AuthMeApi.getInstance(), - plugin.logger, - config.getLong("leftMessageQuarantineInTicks").let Unit) -> Unit> { delay -> - { task -> plugin.server.scheduler.runTaskLater(plugin, task, delay) } - } + joinedMessage = messageConfig.getString("joined")!!, + leftMessage = messageConfig.getString("left")!!, + onlineMessage = messageConfig.getString("online")!!, + chatColors = config.getStringList("nameColors").map(ChatColor::valueOf), + messenger = get(), + onlinePlayers = plugin.server::getOnlinePlayers, + isAuthenticated = AuthMeApi.getInstance()::isAuthenticated, + logger = plugin.logger, + quarantineScheduler = messageConfig.getLong("leftMessageQuarantineInTicks") + .let Unit) -> Unit> { delay -> + { task -> plugin.server.scheduler.runTaskLater(plugin, task, delay) } + }, + pluginName = plugin.description.name, + pluginVersion = plugin.description.version ) } } } diff --git a/modules/plugin/src/main/resources/config.yml b/modules/plugin/src/main/resources/config.yml index ddb2531..f446f34 100644 --- a/modules/plugin/src/main/resources/config.yml +++ b/modules/plugin/src/main/resources/config.yml @@ -5,7 +5,21 @@ messenger.telegram: token: "" chats: [""] message: + started: "Server has been started" joined: "joined the game" left: "left the game" online: "Online players" - leftMessageQuarantineInTicks: 300 \ No newline at end of file + leftMessageQuarantineInTicks: 300 +nameColors: + - DARK_GREEN + - DARK_AQUA + - DARK_RED + - DARK_PURPLE + - GOLD + - GRAY + - BLUE + - GREEN + - AQUA + - RED + - LIGHT_PURPLE + - YELLOW \ No newline at end of file diff --git a/pom.xml b/pom.xml index c87a30a..7ec2c95 100644 --- a/pom.xml +++ b/pom.xml @@ -63,12 +63,6 @@ org.spigotmc spigot-api 1.14.4-R0.1-SNAPSHOT - - - net.md-5 - bungeecord-chat - - fr.xephi