9
0
mirror of https://github.com/Auxilor/EcoSkills.git synced 2025-12-31 21:06:40 +00:00

Rewrote data storage to use abstract DataHandler interface in preparation for SQL support

This commit is contained in:
Auxilor
2021-09-20 12:24:32 +01:00
parent 022b2a76cb
commit a97f01e488
9 changed files with 84 additions and 46 deletions

View File

@@ -5,12 +5,13 @@ import com.willfp.eco.core.command.impl.PluginCommand;
import com.willfp.eco.core.integrations.IntegrationLoader;
import com.willfp.ecoskills.commands.CommandEcoskills;
import com.willfp.ecoskills.commands.CommandSkills;
import com.willfp.ecoskills.config.EffectsYml;
import com.willfp.ecoskills.data.DataListener;
import com.willfp.ecoskills.data.LeaderboardHandler;
import com.willfp.ecoskills.data.PlayerBlockListener;
import com.willfp.ecoskills.data.SaveHandler;
import com.willfp.ecoskills.data.DataListener;
import com.willfp.ecoskills.config.DataYml;
import com.willfp.ecoskills.config.EffectsYml;
import com.willfp.ecoskills.data.LeaderboardHandler;
import com.willfp.ecoskills.data.storage.DataHandler;
import com.willfp.ecoskills.data.storage.YamlDataHandler;
import com.willfp.ecoskills.effects.Effect;
import com.willfp.ecoskills.effects.Effects;
import com.willfp.ecoskills.integrations.EcoEnchantsEnchantingLeveller;
@@ -26,7 +27,6 @@ import com.willfp.ecoskills.stats.Stats;
import com.willfp.ecoskills.stats.modifier.StatModifierListener;
import org.bukkit.event.Listener;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@@ -39,7 +39,7 @@ public class EcoSkillsPlugin extends EcoPlugin {
/**
* data.yml.
*/
private final DataYml dataYml;
private final DataHandler dataHandler;
/**
* effects.yml.
@@ -52,7 +52,7 @@ public class EcoSkillsPlugin extends EcoPlugin {
public EcoSkillsPlugin() {
super(1351, 12205, "&#ff00ae");
instance = this;
dataYml = new DataYml(this);
dataHandler = new YamlDataHandler(this);
effectsYml = new EffectsYml(this);
}
@@ -78,20 +78,16 @@ public class EcoSkillsPlugin extends EcoPlugin {
@Override
protected void handleDisable() {
try {
dataYml.save();
} catch (IOException e) {
e.printStackTrace();
}
dataHandler.save();
}
/**
* Get data.yml.
* Get data handler.
*
* @return data.yml.
* @return data handler.
*/
public DataYml getDataYml() {
return dataYml;
public DataHandler getDataHandler() {
return dataHandler;
}
/**

View File

@@ -93,11 +93,11 @@ fun Player.giveSkillExperience(skill: Skill, experience: Double, isOvershoot: Bo
}
fun OfflinePlayer.getSkillLevel(skill: Skill): Int {
return plugin.dataYml.getInt("player.${this.uniqueId}.${skill.id}", 0)
return plugin.dataHandler.readInt(this.uniqueId, skill.id)
}
fun OfflinePlayer.setSkillLevel(skill: Skill, level: Int) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", level)
plugin.dataHandler.write(this.uniqueId, skill.id, level)
}
fun OfflinePlayer.getSkillProgressToNextLevel(skill: Skill): Double {
@@ -109,27 +109,27 @@ fun OfflinePlayer.getSkillProgressRequired(skill: Skill): Int {
}
fun OfflinePlayer.getSkillProgress(skill: Skill): Double {
return plugin.dataYml.getDoubleOrNull("player.${this.uniqueId}.${skill.xpKey.key}") ?: 0.0
return plugin.dataHandler.readDouble(this.uniqueId, skill.xpKey.key)
}
fun OfflinePlayer.setSkillProgress(skill: Skill, level: Double) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.xpKey.key}", level)
plugin.dataHandler.write(this.uniqueId, skill.xpKey.key, level)
}
fun OfflinePlayer.getEffectLevel(effect: Effect): Int {
return plugin.dataYml.getInt("player.${this.uniqueId}.${effect.id}", 0)
return plugin.dataHandler.readInt(this.uniqueId, effect.id)
}
fun OfflinePlayer.setEffectLevel(effect: Effect, level: Int) {
plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", level)
plugin.dataHandler.write(this.uniqueId, effect.id, level)
}
fun OfflinePlayer.getStatLevel(stat: Stat): Int {
return plugin.dataYml.getInt("player.${this.uniqueId}.${stat.id}", 0)
return plugin.dataHandler.readInt(this.uniqueId, stat.id)
}
fun Player.setStatLevel(stat: Stat, level: Int) {
plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", level)
plugin.dataHandler.write(this.uniqueId, stat.id, level)
stat.updateStatLevel(this)
}

View File

@@ -17,7 +17,7 @@ class CommandReload(plugin: EcoPlugin) :
override fun getHandler(): CommandHandler {
return CommandHandler { sender: CommandSender, _: List<String> ->
try {
(plugin as EcoSkillsPlugin).dataYml.save()
(plugin as EcoSkillsPlugin).dataHandler.save()
} catch (e: IOException) {
e.printStackTrace()
}

View File

@@ -1,12 +0,0 @@
package com.willfp.ecoskills.config
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.yaml.YamlBaseConfig
class DataYml(
plugin: EcoPlugin
): YamlBaseConfig(
"data",
false,
plugin
)

View File

@@ -60,17 +60,18 @@ class DataListener : Listener {
private fun Player.convertFromLegacyData() {
for (effect in Effects.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", this.getEffectLevel(effect))
plugin.dataHandler.write(this.uniqueId, effect.id, this.getEffectLevel(effect))
}
for (stat in Stats.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", this.getStatLevel(stat))
plugin.dataHandler.write(this.uniqueId, stat.id, this.getStatLevel(stat))
}
for (skill in Skills.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", this.getSkillLevel(skill))
plugin.dataHandler.write(this.uniqueId, skill.id, this.getSkillLevel(skill))
val prog = this.persistentDataContainer.get(skill.xpKey, PersistentDataType.DOUBLE)
if (prog != null) {
plugin.dataYml.set(
"player.${this.uniqueId}.${skill.xpKey.key}",
plugin.dataHandler.write(
this.uniqueId,
skill.xpKey.key,
prog
)
this.persistentDataContainer.remove(skill.xpKey)

View File

@@ -13,7 +13,7 @@ class SaveHandler {
if (plugin.configYml.getBool("log-autosaves")) {
plugin.logger.info("Auto-Saving player data!")
}
plugin.dataYml.save()
plugin.dataHandler.save()
expMultiplierCache.clear()
if (plugin.configYml.getBool("log-autosaves")) {
plugin.logger.info("Saved data!")

View File

@@ -26,11 +26,11 @@ class SavedPlayerNameListener(
var OfflinePlayer.savedDisplayName: String
get() {
if (this is Player) {
plugin.dataYml.set("player.${this.uniqueId}.name", this.displayName)
plugin.dataHandler.write(this.uniqueId, "name", this.displayName)
}
return plugin.dataYml.getStringOrNull("player.${this.uniqueId}.name") ?: this.name ?: "Unknown Player"
return plugin.dataHandler.readString(this.uniqueId, "name", this.name ?: "Unknown Player")
}
set(value) {
plugin.dataYml.set("player.${this.uniqueId}.name", value)
plugin.dataHandler.write(this.uniqueId, "name", value)
}

View File

@@ -0,0 +1,13 @@
package com.willfp.ecoskills.data.storage
import java.util.*
interface DataHandler {
fun save()
fun <T> write(uuid: UUID, key: String, value: T)
fun readInt(uuid: UUID, key: String): Int
fun readDouble(uuid: UUID, key: String): Double
fun readString(uuid: UUID, key: String, default: String = ""): String
}

View File

@@ -0,0 +1,40 @@
package com.willfp.ecoskills.data.storage
import com.willfp.eco.core.config.yaml.YamlBaseConfig
import com.willfp.ecoskills.EcoSkillsPlugin
import java.util.*
@Suppress("UNCHECKED_CAST")
class YamlDataHandler(
private val plugin: EcoSkillsPlugin
) : DataHandler {
private val dataYml = DataYml(plugin)
override fun save() {
dataYml.save()
}
override fun <T> write(uuid: UUID, key: String, value: T) {
dataYml.set("player.$uuid.key", value)
}
override fun readInt(uuid: UUID, key: String): Int {
return dataYml.getInt("player.$uuid.$key", 0)
}
override fun readDouble(uuid: UUID, key: String): Double {
return dataYml.getDoubleOrNull("player.$uuid.$key") ?: 0.0
}
override fun readString(uuid: UUID, key: String, default: String): String {
return dataYml.getStringOrNull("player.$uuid.$key") ?: default
}
class DataYml(
plugin: EcoSkillsPlugin
) : YamlBaseConfig(
"data",
false,
plugin
)
}