diff --git a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java index 25f91422..696d688c 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java +++ b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java @@ -1150,6 +1150,16 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist return this.getMetadataValueFactory().create(value); } + /** + * Get if all {@link com.willfp.eco.core.data.keys.PersistentDataKey}'s for this + * plugin should be saved locally (via data.yml.) even if eco is using a database. + * + * @return If using local storage. + */ + public boolean isUsingLocalStorage() { + return this.configYml.isUsingLocalStorage(); + } + @Override @NotNull public final String getID() { diff --git a/eco-api/src/main/java/com/willfp/eco/core/config/base/ConfigYml.java b/eco-api/src/main/java/com/willfp/eco/core/config/base/ConfigYml.java index c81adcc7..435c6e23 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/config/base/ConfigYml.java +++ b/eco-api/src/main/java/com/willfp/eco/core/config/base/ConfigYml.java @@ -9,6 +9,11 @@ import org.jetbrains.annotations.NotNull; * Default plugin config.yml. */ public class ConfigYml extends BaseConfig { + /** + * The use local storage key. + */ + public static final String KEY_USES_LOCAL_STORAGE = "use-local-storage"; + /** * Config.yml. * @@ -52,4 +57,13 @@ public class ConfigYml extends BaseConfig { final boolean removeUnused) { super(name, plugin, removeUnused, ConfigType.YAML); } + + /** + * Get if the plugin is using local storage. + * + * @return The prefix. + */ + public boolean isUsingLocalStorage() { + return this.getBool(KEY_USES_LOCAL_STORAGE); + } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt index 17220a1c..7ef58409 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt @@ -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 = diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt index 384a9d8c..d36d61b4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt @@ -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(), diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfile.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfile.kt index 00761b20..de2c52ed 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfile.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfile.kt @@ -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, Any>, val uuid: UUID, - private val handler: DataHandler + private val handler: DataHandler, + private val localHandler: DataHandler ) : Profile { override fun write(key: PersistentDataKey, 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, 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, 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 diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/ProfileHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/ProfileHandler.kt index 79daaf48..42ae2de2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/ProfileHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/ProfileHandler.kt @@ -23,8 +23,10 @@ class ProfileHandler( ) { private val loaded = mutableMapOf() + 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, 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>) { 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() + } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt index dee3cd21..d99d0949 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt @@ -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( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt index deca0be7..dd8627ed 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt @@ -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() + } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt index 0a9820b5..b44017c8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt @@ -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) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt index 251c53ee..87c32d9e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt @@ -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() + } }