Skip to content

Commit

Permalink
fix: Onlinetime working (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE authored Apr 23, 2024
1 parent 134bd8b commit d5fe29d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/main/kotlin/net/blockventuremc/BlockVenture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.blockventuremc.database.DatabaseManager
import net.blockventuremc.modules.i18n.TranslationCache
import net.blockventuremc.modules.placeholders.registerPlaceholders
import net.blockventuremc.utils.RegisterManager.registerAll
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin

class BlockVenture: JavaPlugin() {
Expand All @@ -19,6 +20,13 @@ class BlockVenture: JavaPlugin() {
instance = this
}

override fun onLoad() {
server.spigot().spigotConfig["messages.unknown-command"] = "§c" + "Unknown Command"
server.spigot().spigotConfig["messages.server-full"] = "${"server full"} - Club Members can join at any time"
server.spigot().spigotConfig["messages.outdated-client"] = "Your client is outdated, please use the latest version of Minecraft"
server.spigot().spigotConfig["messages.outdated-server"] = "Hold on! We are not that fast. We upgrade as soon as we can"
}

override fun onEnable() {
logger.info("Loading database...")
DatabaseManager.database
Expand All @@ -38,4 +46,15 @@ class BlockVenture: JavaPlugin() {

logger.info("Hello, Minecraft!")
}

override fun onDisable() {
PlayerCache.cleanup()

for (player in Bukkit.getOnlinePlayers()) {
val pixelPlayer = PlayerCache.getOrNull(player.uniqueId) ?: continue
PlayerCache.saveToDB(pixelPlayer.copy(username = player.name))
}

logger.info("Plugin has been disabled")
}
}
24 changes: 22 additions & 2 deletions src/main/kotlin/net/blockventuremc/cache/PlayerCache.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.blockventuremc.cache

import net.blockventuremc.BlockVenture
import net.blockventuremc.database.functions.getDatabaseUserOrNull
import net.blockventuremc.database.functions.updateDatabaseUser
import net.blockventuremc.database.model.DatabaseUser
import net.blockventuremc.extensions.toDatabaseUserDB
import org.bukkit.Bukkit
Expand All @@ -11,13 +13,23 @@ import kotlin.time.Duration.Companion.seconds
object PlayerCache {
private var _cache = mapOf<UUID, DatabaseUser>()

fun get(uuid: UUID): DatabaseUser = _cache[uuid] ?: register(uuid.toDatabaseUserDB())
fun getOrNull(uuid: UUID): DatabaseUser? = _cache[uuid]

private fun register(user: DatabaseUser): DatabaseUser {
fun get(uuid: UUID): DatabaseUser = _cache[uuid] ?: addToCache(uuid.toDatabaseUserDB())

fun addToCache(user: DatabaseUser): DatabaseUser {
_cache += Pair(user.uuid, user)
return user
}

/**
* Updates the given user in the database.
*
* @param user The user to be saved to the database.
*/
fun saveToDB(user: DatabaseUser) {
updateDatabaseUser(user)
}

fun updateCached(user: DatabaseUser): DatabaseUser {
_cache -= user.uuid
Expand All @@ -29,6 +41,14 @@ object PlayerCache {
_cache -= uuid
}

private fun loadPlayer(uuid: UUID): DatabaseUser {
val player = getDatabaseUserOrNull(uuid) ?: return DatabaseUser(uuid, "Unknown")
return addToCache(player)
}

fun reloadPlayer(uuid: UUID): DatabaseUser {
return loadPlayer(uuid)
}

private var task: BukkitTask? = null
fun runOnlineTimeScheduler() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package net.blockventuremc.modules.general.commands

import net.blockventuremc.annotations.BlockCommand
import net.blockventuremc.extensions.getLogger
import net.blockventuremc.extensions.sendMessagePrefixed
import net.blockventuremc.extensions.toDatabaseUser
import net.blockventuremc.extensions.translate
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.permissions.PermissionDefault
import kotlin.time.Duration.Companion.milliseconds


@BlockCommand(
name = "onlinetime",
description = "Check your onlinetime",
permission = "blockventure.onlinetime",
permissionDefault = PermissionDefault.TRUE,
usage = "/onlinetime",
aliases = ["ot"]
)
class OnlinetimeCommand : CommandExecutor {


override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
if (sender !is Player) {
val jreRunningSince = System.currentTimeMillis() - java.lang.management.ManagementFactory.getRuntimeMXBean().startTime
val runTimeDuration = jreRunningSince.milliseconds
getLogger().info("The server is running for $runTimeDuration")
return true
}

val player = sender
val onlinetime = player.toDatabaseUser().onlineTime

player.sendMessagePrefixed(player.translate("onlinetime", mapOf("onlinetime" to onlinetime.toString()))?.message ?: "You have been online for $onlinetime")
return true
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.blockventuremc.modules.general.events

import dev.fruxz.ascend.tool.time.calendar.Calendar
import dev.fruxz.stacked.text
import net.blockventuremc.BlockVenture
import net.blockventuremc.cache.PlayerCache
import org.bukkit.Bukkit
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

class PlayerLoadSaveListener : Listener {


@EventHandler(priority = EventPriority.LOWEST)
fun onJoin(event: PlayerJoinEvent): Unit = with(event) {
player.sendActionBar(text("<green>" + "Loading userdata..."))
Bukkit.getScheduler().runTaskLater(BlockVenture.instance, Runnable {
val pixelPlayer = PlayerCache.reloadPlayer(player.uniqueId).copy(
username = player.name,
lastTimeJoined = Calendar.now()
)
PlayerCache.updateCached(
pixelPlayer
)
player.sendActionBar(text("<green>" + "Loaded userdata..."))
}, 10L)
}

@EventHandler(priority = EventPriority.HIGHEST)
fun onQuit(event: PlayerQuitEvent): Unit = with(event) {
val databaseUser = PlayerCache.get(player.uniqueId)
PlayerCache.saveToDB(
databaseUser.copy(
username = player.name
)
)
PlayerCache.remove(player.uniqueId)
}
}
3 changes: 2 additions & 1 deletion src/main/resources/translations/en-US.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"build_mode_toggled": "Build mode is now %enabled%."
"build_mode_toggled": "Build mode is now %enabled%.",
"onlinetime": "Your online time is %onlinetime%."
}

0 comments on commit d5fe29d

Please sign in to comment.