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:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user