Skip to content

Commit

Permalink
Offline mode compatibility.
Browse files Browse the repository at this point in the history
Search players by their ign inside the database.
  • Loading branch information
NickAcPT committed May 10, 2021
1 parent 2b0980a commit 164d76d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import io.github.openminigameserver.nickarcade.core.data.sender.misc.ArcadeConso
import io.github.openminigameserver.nickarcade.core.data.sender.player.ArcadePlayer
import io.github.openminigameserver.nickarcade.core.data.sender.player.ArcadePlayerData
import io.github.openminigameserver.nickarcade.core.database
import io.github.openminigameserver.nickarcade.core.div
import io.github.openminigameserver.nickarcade.core.events.data.PlayerDataReloadEvent
import io.github.openminigameserver.nickarcade.core.hypixelPlayerInfoHelper
import io.github.openminigameserver.nickarcade.core.logger
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.datetime.Clock
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.litote.kmongo.eq
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.time.measureTimedValue
Expand Down Expand Up @@ -58,11 +61,12 @@ object PlayerDataManager {

suspend fun getPlayerData(uniqueId: UUID, name: String): ArcadePlayer {
return getPlayerDataMutexForUUID(uniqueId).withLock {
if (loadedPlayerMap[uniqueId] != null) {
loadedPlayerMap[uniqueId]!!
} else {
if (loadedPlayerMap[uniqueId] != null) {
loadedPlayerMap[uniqueId]!!
} else {
logger.info("Unable to find cached player data for $name [$uniqueId]. Fetching from MongoDb or Hypixel.")
val playerData = loadPlayerDataFromMongoDb(uniqueId) ?: createPlayerDataFromHypixel(uniqueId, name)
val playerData =
loadPlayerDataFromMongoDb(uniqueId, name) ?: createPlayerDataFromHypixel(uniqueId, name)
playerData.also {
loadedPlayerMap[uniqueId] = it
}
Expand All @@ -71,20 +75,27 @@ object PlayerDataManager {
}

private val refreshTime = 30.minutes
private suspend fun loadPlayerDataFromMongoDb(uniqueId: UUID) = playerDataCollection.findOneById(uniqueId)?.also {
if ((Clock.System.now() - it.lastProfileUpdate) >= refreshTime) {
val user = "${it.hypixelData?.displayName ?: "Unknown name"} [${it.uuid}]"
println("Updating user $user due to profile being too old.")
val (value, duration) = measureTimedValue {
fetchHypixelPlayerData(it.uuid, it.hypixelData?.displayName!!)
}
it.hypixelData = value
it.updateHypixelData(false)
it.lastProfileUpdate = Clock.System.now()
savePlayerData(it)
println("Updated user $user successfully (Took ${duration}).")
private suspend fun loadPlayerDataFromMongoDb(uniqueId: UUID, name: String): ArcadePlayer? {
if (!Bukkit.getOnlineMode()) {
return playerDataCollection.findOne(ArcadePlayerData::rawHypixelData / HypixelPlayer::displayName eq name)
?.let { ArcadePlayer(it) }
}
}?.let { ArcadePlayer(it) }

return playerDataCollection.findOneById(uniqueId)?.also {
if ((Clock.System.now() - it.lastProfileUpdate) >= refreshTime) {
val user = "${it.hypixelData?.displayName ?: "Unknown name"} [${it.uuid}]"
println("Updating user $user due to profile being too old.")
val (value, duration) = measureTimedValue {
fetchHypixelPlayerData(it.uuid, it.hypixelData?.displayName!!)
}
it.hypixelData = value
it.updateHypixelData(false)
it.lastProfileUpdate = Clock.System.now()
savePlayerData(it)
println("Updated user $user successfully (Took ${duration}).")
}
}?.let { ArcadePlayer(it) }
}

val playerDataCollection by lazy {
database.getCollection<ArcadePlayerData>("players")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import io.github.openminigameserver.nickarcade.core.commandAnnotationParser
import io.github.openminigameserver.nickarcade.core.io.config.ArcadeConfigurationFile
import io.github.openminigameserver.nickarcade.core.io.config.impl.MainConfigurationFile
import io.github.openminigameserver.nickarcade.core.io.database.DatabaseService
import io.github.openminigameserver.nickarcade.core.manager.PlayerDataManager
import io.github.openminigameserver.nickarcade.plugin.events.PlayerEvents
import io.github.openminigameserver.nickarcade.plugin.extensions.ComponentExtensions
import io.github.openminigameserver.nickarcade.plugin.extensions.event
import io.github.openminigameserver.nickarcade.plugin.helper.commands.NickArcadeCommandHelper
import org.bukkit.Bukkit
import org.bukkit.event.EventPriority
import org.bukkit.event.player.AsyncPlayerPreLoginEvent
import org.bukkit.plugin.java.JavaPlugin
import java.util.*

Expand All @@ -34,6 +39,13 @@ class NickArcadePlugin : JavaPlugin() {
IoC += NickArcadeCommandHelper().apply { init() }

commandAnnotationParser.parse(ComponentExtensions)
if (!Bukkit.getOnlineMode()) {
event<AsyncPlayerPreLoginEvent>(forceBlocking = true, eventPriority = EventPriority.LOWEST) {
PlayerDataManager.getPlayerData(uniqueId, name).also {
this.playerProfile.id = it.uuid
}
}
}
}

private fun prepareIoCValues() {
Expand Down

0 comments on commit 164d76d

Please sign in to comment.