Skip to content

Commit

Permalink
Replace kotlin coroutines with default minecraft server scheduler (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
imedvedko authored Oct 14, 2019
1 parent 8c77537 commit cf6f658
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ 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 kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
Expand All @@ -18,22 +15,25 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.logging.Level
import java.util.logging.Logger


class NotificationEventListener(private val joinedMessage: String,
private val leftMessage: String,
private val onlineMessage: String,
private val messenger: Messenger,
private val onlinePlayers: () -> Iterable<Player>,
private val authMeApi: AuthMeApi,
private val logger: Logger,
private val quarantineDelayMillis: Long) : Listener {
private val quarantineScheduler: (() -> Unit) -> Unit) : Listener {
private val quarantine: MutableMap<String, UUID> = ConcurrentHashMap()

@EventHandler(priority = EventPriority.MONITOR)
fun onJoin(event: PlayerJoinEvent) {
event.player.takeIf(AuthMeApi.getInstance()::isAuthenticated)?.joinNotification()
event.player.takeIf(authMeApi::isAuthenticated)?.joinNotification()
}

@EventHandler(priority = EventPriority.MONITOR)
fun onQuit(event: PlayerQuitEvent) {
event.player.takeIf(AuthMeApi.getInstance()::isAuthenticated)?.leftNotification()
event.player.takeIf(authMeApi::isAuthenticated)?.leftNotification()
}

@EventHandler(priority = EventPriority.MONITOR)
Expand All @@ -48,24 +48,23 @@ class NotificationEventListener(private val joinedMessage: String,

private fun Player.joinNotification() {
if (quarantine.remove(name) == null) {
notification(joinedMessage)
notification(joinedMessage, onlinePlayers().filter(authMeApi::isAuthenticated).map(Player::getName))
}
}

private fun Player.leftNotification() {
val quarantineId = UUID.randomUUID()
quarantine[name] = quarantineId
GlobalScope.launch {
delay(quarantineDelayMillis)
if (quarantine.remove(name, quarantineId)) {
notification(leftMessage)
onlinePlayers().filter { this != it }.filter(authMeApi::isAuthenticated).map(Player::getName).let {
quarantineScheduler.invoke {
if (quarantine.remove(name, quarantineId)) {
notification(leftMessage, it)
}
}
}
}

private fun Player.notification(message: String) = onlinePlayers()
.filter(AuthMeApi.getInstance()::isAuthenticated).map { "<i>${it.name}</i>" }
.let { messenger.send("<b>$name $message</b>\nOnline players: $it") }.forEach { job ->
job.invokeOnCompletion { e -> e?.let { logger.log(Level.WARNING, "Can not send message", it) } }
}
private fun Player.notification(message: String, onlinePlayers: Iterable<String>) = messenger
.send("<b>$name $message</b>\n$onlineMessage: ${onlinePlayers.map { "<i>$it</i>" }}")
.forEach { job -> job.exceptionally { e -> logger.log(Level.WARNING, "Can not send the message", e) } }
}
6 changes: 0 additions & 6 deletions modules/messenger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,4 @@

<artifactId>messenger</artifactId>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.imedvedev.minecraft.notification.bot.messenger

import kotlinx.coroutines.Job
import java.util.concurrent.CompletableFuture

interface Messenger {
fun send(message: String): List<Job>
fun send(message: String): List<CompletableFuture<Unit>>
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
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.event.Listener
import org.bukkit.plugin.Plugin
import org.koin.dsl.module
import java.time.Duration

val notificationEventListenerModule = module(true) {
single<Listener> {
get<Plugin>().let { plugin -> plugin.config.getConfigurationSection("message")!!.let { config ->
NotificationEventListener(
config.getString("joined")!!,
config.getString("left")!!,
config.getString("online")!!,
get(),
plugin.server::getOnlinePlayers,
AuthMeApi.getInstance(),
plugin.logger,
Duration.parse(config.getString("quarantine")).toMillis()
config.getLong("leftMessageQuarantineInTicks").let { delay ->
{ task: () -> Unit -> plugin.server.scheduler.runTaskLater(plugin, task, delay).let { Unit } }
}
)
} }
}
Expand Down
3 changes: 2 additions & 1 deletion modules/plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ messenger.telegram:
message:
joined: "joined the game"
left: "left the game"
quarantine: PT15S
online: "Online players"
leftMessageQuarantineInTicks: 300
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import com.pengrad.telegrambot.TelegramBot
import com.pengrad.telegrambot.model.request.ParseMode
import com.pengrad.telegrambot.request.SendMessage
import com.pengrad.telegrambot.response.SendResponse
import kotlinx.coroutines.Job
import java.io.IOException
import java.util.concurrent.CompletableFuture

class TelegramMessenger(private val bot: TelegramBot, private val chats: Iterable<String>) : Messenger {

override fun send(message: String): List<Job> = chats.map { chat ->
Job().apply {
override fun send(message: String): List<CompletableFuture<Unit>> = chats.map { chat ->
CompletableFuture<Unit>().apply {
object : Callback<SendMessage, SendResponse> {
override fun onResponse(request: SendMessage, response: SendResponse) {
if (response.isOk) {
complete()
complete(Unit)
} else {
completeExceptionally(TelegramMessengerException(response.errorCode(), response.description()))
}
Expand Down
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.koin</groupId>
<artifactId>koin-core</artifactId>
Expand Down

0 comments on commit cf6f658

Please sign in to comment.