9
0
mirror of https://github.com/Auxilor/EcoSkills.git synced 2026-01-01 21:36:34 +00:00

Player data saves on player leave

This commit is contained in:
Auxilor
2021-10-31 12:30:31 +00:00
parent 3564cdddd3
commit bf8f0f0ed5
2 changed files with 30 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
package com.willfp.ecoskills.data
import com.willfp.ecoskills.data.storage.PlayerProfile
import com.willfp.ecoskills.effects.Effect
import com.willfp.ecoskills.effects.Effects
import com.willfp.ecoskills.getSkillLevel
@@ -10,6 +11,7 @@ import org.bukkit.attribute.Attribute
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
class DataListener : Listener {
@EventHandler
@@ -53,4 +55,9 @@ class DataListener : Listener {
stat.updateStatLevel(event.player)
}
}
@EventHandler
fun onLeave(event: PlayerQuitEvent) {
PlayerProfile.savePlayer(event.player.uniqueId)
}
}

View File

@@ -45,19 +45,33 @@ class PlayerProfile private constructor(
return profile
}
fun savePlayer(uuid: UUID) {
writeToHandler(uuid)
saveToHandler()
}
private fun writeToHandler(uuid: UUID) {
val profile = loaded[uuid] ?: return
for ((key, type) in keys) {
when (type) {
Type.INT -> handler.write(uuid, key, profile.read(key, 0))
Type.DOUBLE -> handler.write(uuid, key, profile.read(key, 0.0))
Type.STRING -> handler.write(uuid, key, profile.read(key, "Unknown Value"))
}
}
}
private fun saveToHandler() {
handler.save()
}
fun saveAll(async: Boolean) {
val saver = {
for ((uuid, profile) in loaded) {
for ((key, type) in keys) {
when (type) {
Type.INT -> handler.write(uuid, key, profile.read(key, 0))
Type.DOUBLE -> handler.write(uuid, key, profile.read(key, 0.0))
Type.STRING -> handler.write(uuid, key, profile.read(key, "Unknown Value"))
}
}
for ((uuid, _) in loaded) {
writeToHandler(uuid)
}
handler.save()
saveToHandler()
}
if (async) {