fix data desync

This commit is contained in:
Cyramek
2023-04-23 14:35:17 +02:00
parent 58316c2a06
commit 4ce2850138
3 changed files with 16 additions and 8 deletions

View File

@@ -16,9 +16,7 @@ abstract class EcoProfile(
override fun <T : Any> write(key: PersistentDataKey<T>, value: T) {
this.data[key] = value
val changedKeys = CHANGE_MAP[uuid] ?: mutableSetOf()
changedKeys.add(key)
CHANGE_MAP[uuid] = changedKeys
CHANGE_MAP.add(uuid)
}
override fun <T : Any> read(key: PersistentDataKey<T>): T {
@@ -44,7 +42,7 @@ abstract class EcoProfile(
}
companion object {
val CHANGE_MAP: MutableMap<UUID, MutableSet<PersistentDataKey<*>>> = ConcurrentHashMap()
val CHANGE_MAP: MutableSet<UUID> = ConcurrentHashMap.newKeySet()
}
}

View File

@@ -22,7 +22,7 @@ class ProfileHandler(
private val type: HandlerType,
private val plugin: EcoSpigotPlugin
) {
private val loaded = mutableMapOf<UUID, Profile>()
val loaded = mutableMapOf<UUID, Profile>()
val handler: DataHandler = when (type) {
HandlerType.YAML -> YamlDataHandler(plugin, this)

View File

@@ -12,10 +12,20 @@ class ProfileSaver(
val interval = plugin.configYml.getInt("save-interval").toLong()
plugin.scheduler.runTimer(20, interval) {
for ((uuid, set) in EcoProfile.CHANGE_MAP) {
handler.saveKeysFor(uuid, set)
val iterator = EcoProfile.CHANGE_MAP.iterator()
while (iterator.hasNext()) {
val uuid = iterator.next()
iterator.remove()
val profile = handler.loaded[uuid] ?: continue
if (profile !is EcoProfile) {
continue
}
handler.saveKeysFor(uuid, profile.data.keys)
}
EcoProfile.CHANGE_MAP.clear()
}
}
}