Added option to store plugin data locally, even if eco is set to use a database

This commit is contained in:
Auxilor
2023-05-10 15:19:15 +01:00
parent 861f076c11
commit 5ef244f0bc
10 changed files with 100 additions and 12 deletions

View File

@@ -258,6 +258,7 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
override fun addNewPlugin(plugin: EcoPlugin) {
loadedEcoPlugins[plugin.name.lowercase()] = plugin
loadedEcoPlugins[plugin.id] = plugin
}
override fun getLoadedPlugins(): List<String> =

View File

@@ -258,7 +258,8 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
profileHandler.migrateIfNeeded()
}
ProfileSaver(this, profileHandler)
ProfileSaver(this, profileHandler).startTicking()
this.scheduler.runTimer(
{ getProxy(PacketHandlerProxy::class.java).clearDisplayFrames() },
this.configYml.getInt("display-frame-ttl").toLong(),

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.internal.spigot.data
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.data.PlayerProfile
import com.willfp.eco.core.data.Profile
import com.willfp.eco.core.data.ServerProfile
@@ -11,7 +12,8 @@ import java.util.concurrent.ConcurrentHashMap
abstract class EcoProfile(
val data: MutableMap<PersistentDataKey<*>, Any>,
val uuid: UUID,
private val handler: DataHandler
private val handler: DataHandler,
private val localHandler: DataHandler
) : Profile {
override fun <T : Any> write(key: PersistentDataKey<T>, value: T) {
this.data[key] = value
@@ -25,7 +27,12 @@ abstract class EcoProfile(
return this.data[key] as T
}
this.data[key] = handler.read(uuid, key) ?: key.defaultValue
this.data[key] = if (key.isLocal) {
localHandler.read(uuid, key)
} else {
handler.read(uuid, key)
} ?: key.defaultValue
return read(key)
}
@@ -49,8 +56,9 @@ abstract class EcoProfile(
class EcoPlayerProfile(
data: MutableMap<PersistentDataKey<*>, Any>,
uuid: UUID,
handler: DataHandler
) : EcoProfile(data, uuid, handler), PlayerProfile {
handler: DataHandler,
localHandler: DataHandler
) : EcoProfile(data, uuid, handler, localHandler), PlayerProfile {
override fun toString(): String {
return "EcoPlayerProfile{uuid=$uuid}"
}
@@ -58,9 +66,13 @@ class EcoPlayerProfile(
class EcoServerProfile(
data: MutableMap<PersistentDataKey<*>, Any>,
handler: DataHandler
) : EcoProfile(data, serverProfileUUID, handler), ServerProfile {
handler: DataHandler,
localHandler: DataHandler
) : EcoProfile(data, serverProfileUUID, handler, localHandler), ServerProfile {
override fun toString(): String {
return "EcoServerProfile"
}
}
private val PersistentDataKey<*>.isLocal: Boolean
get() = EcoPlugin.getPlugin(this.key.namespace)?.isUsingLocalStorage == true

View File

@@ -23,8 +23,10 @@ class ProfileHandler(
) {
private val loaded = mutableMapOf<UUID, EcoProfile>()
private val localHandler = YamlDataHandler(plugin, this)
val handler: DataHandler = when (type) {
HandlerType.YAML -> YamlDataHandler(plugin, this)
HandlerType.YAML -> localHandler
HandlerType.MYSQL -> MySQLDataHandler(plugin, this)
HandlerType.MONGO -> MongoDataHandler(plugin, this)
}
@@ -41,7 +43,7 @@ class ProfileHandler(
val data = mutableMapOf<PersistentDataKey<*>, Any>()
val profile = if (uuid == serverProfileUUID)
EcoServerProfile(data, handler) else EcoPlayerProfile(data, uuid, handler)
EcoServerProfile(data, handler, localHandler) else EcoPlayerProfile(data, uuid, handler, localHandler)
loaded[uuid] = profile
return profile
@@ -57,6 +59,11 @@ class ProfileHandler(
fun saveKeysFor(uuid: UUID, keys: Set<PersistentDataKey<*>>) {
handler.saveKeysFor(uuid, keys)
// Don't save to local handler if it's the same handler.
if (localHandler != handler) {
localHandler.saveKeysFor(uuid, keys)
}
}
fun unloadPlayer(uuid: UUID) {
@@ -65,6 +72,10 @@ class ProfileHandler(
fun save() {
handler.save()
if (localHandler != handler) {
localHandler.save()
}
}
fun migrateIfNeeded() {
@@ -147,5 +158,8 @@ class ProfileHandler(
fun initialize() {
handler.initialize()
if (localHandler != handler) {
localHandler.initialize()
}
}
}

View File

@@ -100,6 +100,18 @@ class MongoDataHandler(
profile
}
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
return other is MongoDataHandler
}
override fun hashCode(): Int {
return type.hashCode()
}
}
private data class UUIDProfile(

View File

@@ -149,4 +149,16 @@ class MySQLDataHandler(
SchemaUtils.createMissingTablesAndColumns(table, withLogs = false)
}
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
return other is MySQLDataHandler
}
override fun hashCode(): Int {
return type.hashCode()
}
}

View File

@@ -5,10 +5,10 @@ import com.willfp.eco.internal.spigot.data.EcoProfile
import com.willfp.eco.internal.spigot.data.ProfileHandler
class ProfileSaver(
plugin: EcoPlugin,
handler: ProfileHandler
private val plugin: EcoPlugin,
private val handler: ProfileHandler
) {
init {
fun startTicking() {
val interval = plugin.configYml.getInt("save-interval").toLong()
plugin.scheduler.runTimer(20, interval) {

View File

@@ -48,4 +48,16 @@ class YamlDataHandler(
private fun doWrite(uuid: UUID, key: NamespacedKey, value: Any) {
dataYml.set("player.$uuid.$key", value)
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
return other is YamlDataHandler
}
override fun hashCode(): Int {
return type.hashCode()
}
}