Skip to content

Commit

Permalink
Fix wrong detection online players (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
imedvedko authored Oct 17, 2019
1 parent 3be9419 commit 9e68bcd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,64 @@ import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.logging.Level
import java.util.logging.Logger
import kotlin.collections.LinkedHashMap

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 quarantineScheduler: (() -> Unit) -> Unit) : Listener {
private val quarantine: MutableMap<String, UUID> = ConcurrentHashMap()

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

@EventHandler(priority = EventPriority.MONITOR)
fun onQuit(event: PlayerQuitEvent) {
event.player.takeIf(authMeApi::isAuthenticated)?.leftNotification()
}
private val onlinePlayers: MutableMap<String, Int> = LinkedHashMap()

@EventHandler(priority = EventPriority.MONITOR)
fun onLogin(event: LoginEvent) {
event.player.takeIf(Player::isOnline)?.joinNotification()
event.player.takeIf(Player::isOnline)?.run {
if (onlinePlayers.put(name, entityId) == null) {
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" }
}
}
}

@EventHandler(priority = EventPriority.MONITOR)
fun onLogout(event: LogoutEvent) {
event.player.takeIf(Player::isOnline)?.leftNotification()
}

private fun Player.joinNotification() {
if (quarantine.remove(name) == null) {
notification(joinedMessage, onlinePlayers().filter(authMeApi::isAuthenticated).map(Player::getName))
event.player.takeIf(Player::isOnline)?.run {
onlinePlayers.remove(name)
logger.log(Level.INFO) { "$name left the game. " +
"Server is sending notification" }
sendNotification(leftMessage)
}
}

private fun Player.leftNotification() {
val quarantineId = UUID.randomUUID()
quarantine[name] = quarantineId
onlinePlayers().filter { this != it }.filter(authMeApi::isAuthenticated).let { onlinePlayers ->
@EventHandler(priority = EventPriority.MONITOR)
fun onQuit(event: PlayerQuitEvent) {
event.player.takeIf(authMeApi::isAuthenticated)?.run {
logger.log(Level.INFO) { "$name has been quarantined" }
quarantineScheduler.invoke {
if (quarantine.remove(name, quarantineId)) {
onlinePlayers.plus(onlinePlayers().filter(authMeApi::isAuthenticated))
.map(Player::getName)
.distinct()
.let { onlinePlayers -> notification(leftMessage, onlinePlayers) }
if (onlinePlayers.remove(name, entityId)) {
logger.log(Level.INFO) { "Quarantine has been finished. $name left the game. " +
"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" }
}
}
}
}

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) } }
private fun Player.sendNotification(message: String) = messenger
.send("<b>$name $message</b>\n$onlineMessage: ${onlinePlayers.keys.map { "<i>${it}</i>" }}")
.forEach { job -> job.exceptionally { exception -> logger.log(Level.WARNING, exception) {
"Server can not send the notification: \"$message\""
} } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ val notificationEventListenerModule = module(true) {
config.getString("left")!!,
config.getString("online")!!,
get(),
plugin.server::getOnlinePlayers,
AuthMeApi.getInstance(),
plugin.logger,
config.getLong("leftMessageQuarantineInTicks").let { delay ->
{ task: () -> Unit -> plugin.server.scheduler.runTaskLater(plugin, task, delay).let { Unit } }
config.getLong("leftMessageQuarantineInTicks").let<Long, (() -> Unit) -> Unit> { delay ->
{ task -> plugin.server.scheduler.runTaskLater(plugin, task, delay) }
}
)
} }
Expand Down

0 comments on commit 9e68bcd

Please sign in to comment.