From 8cecdeffeef1d26c3178716fffb19135a2b671df Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 27 Sep 2021 12:48:04 +0100 Subject: [PATCH] Switched DataHandler to be more abstracted in favour of using PlayerProfile to increase MySQL performance --- .../com/willfp/ecoskills/EcoSkillsPlugin.java | 2 +- .../com/willfp/ecoskills/EcoSkillsPlayer.kt | 24 +++-- .../ecoskills/commands/CommandReload.kt | 9 +- .../com/willfp/ecoskills/data/DataListener.kt | 28 +----- .../com/willfp/ecoskills/data/SaveHandler.kt | 3 +- .../ecoskills/data/SavedPlayerNameListener.kt | 8 +- .../ecoskills/data/storage/PlayerProfile.kt | 90 +++++++++++++++++++ 7 files changed, 116 insertions(+), 48 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/storage/PlayerProfile.kt diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java index 4024949..67451fa 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java @@ -80,7 +80,7 @@ public class EcoSkillsPlugin extends EcoPlugin { @Override protected void handleDisable() { - dataHandler.save(); + SaveHandler.Companion.save(this); } /** diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt index f2786a2..38b373a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt @@ -2,6 +2,7 @@ package com.willfp.ecoskills import com.willfp.ecoskills.api.PlayerSkillExpGainEvent import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent +import com.willfp.ecoskills.data.storage.PlayerProfile import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.skills.Skill import com.willfp.ecoskills.skills.Skills @@ -16,6 +17,11 @@ import java.util.* val expMultiplierCache = mutableMapOf() val plugin: EcoSkillsPlugin = EcoSkillsPlugin.getInstance() +val OfflinePlayer.profile: PlayerProfile + get() { + return PlayerProfile.load(this.uniqueId) + } + fun Player.getSkillExperienceMultiplier(): Double { if (expMultiplierCache.containsKey(this.uniqueId)) { return expMultiplierCache[this.uniqueId]!! @@ -93,11 +99,11 @@ fun Player.giveSkillExperience(skill: Skill, experience: Double, isOvershoot: Bo } fun OfflinePlayer.getSkillLevel(skill: Skill): Int { - return plugin.dataHandler.readInt(this.uniqueId, skill.id) + return profile.readInt(skill.id) } fun OfflinePlayer.setSkillLevel(skill: Skill, level: Int) { - plugin.dataHandler.write(this.uniqueId, skill.id, level) + return profile.write(skill.id, level) } fun OfflinePlayer.getSkillProgressToNextLevel(skill: Skill): Double { @@ -109,32 +115,32 @@ fun OfflinePlayer.getSkillProgressRequired(skill: Skill): Int { } fun OfflinePlayer.getSkillProgress(skill: Skill): Double { - return plugin.dataHandler.readDouble(this.uniqueId, skill.xpKey.key) + return profile.readDouble(skill.xpKey.key) } fun OfflinePlayer.setSkillProgress(skill: Skill, level: Double) { - plugin.dataHandler.write(this.uniqueId, skill.xpKey.key, level) + profile.write(skill.xpKey.key, level) } fun OfflinePlayer.getEffectLevel(effect: Effect): Int { - return plugin.dataHandler.readInt(this.uniqueId, effect.id) + return profile.readInt(effect.id) } fun OfflinePlayer.setEffectLevel(effect: Effect, level: Int) { - plugin.dataHandler.write(this.uniqueId, effect.id, level) + profile.write(effect.id, level) } fun OfflinePlayer.getStatLevel(stat: Stat): Int { - return plugin.dataHandler.readInt(this.uniqueId, stat.id) + return profile.readInt(stat.id) } fun Player.setStatLevel(stat: Stat, level: Int) { - plugin.dataHandler.write(this.uniqueId, stat.id, level) + profile.write(stat.id, level) stat.updateStatLevel(this) } fun Entity.tryAsPlayer(): Player? { - return when(this) { + return when (this) { is Projectile -> { val shooter = this.shooter if (shooter is Player) shooter else null diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReload.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReload.kt index 1ff836a..cef9768 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReload.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReload.kt @@ -3,9 +3,8 @@ package com.willfp.ecoskills.commands import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.CommandHandler import com.willfp.eco.core.command.impl.Subcommand -import com.willfp.ecoskills.EcoSkillsPlugin +import com.willfp.ecoskills.data.storage.PlayerProfile import org.bukkit.command.CommandSender -import java.io.IOException class CommandReload(plugin: EcoPlugin) : Subcommand( @@ -16,11 +15,7 @@ class CommandReload(plugin: EcoPlugin) : ) { override fun getHandler(): CommandHandler { return CommandHandler { sender: CommandSender, _: List -> - try { - (plugin as EcoSkillsPlugin).dataHandler.save() - } catch (e: IOException) { - e.printStackTrace() - } + PlayerProfile.saveAll() plugin.reload() sender.sendMessage(plugin.langYml.getMessage("reloaded")) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt index f632de7..66c9079 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt @@ -1,22 +1,19 @@ package com.willfp.ecoskills.data -import com.willfp.ecoskills.* import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.effects.Effects +import com.willfp.ecoskills.getSkillLevel +import com.willfp.ecoskills.setEffectLevel import com.willfp.ecoskills.skills.Skills import com.willfp.ecoskills.stats.Stats import org.bukkit.attribute.Attribute -import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent -import org.bukkit.persistence.PersistentDataType class DataListener : Listener { @EventHandler fun onJoin(event: PlayerJoinEvent) { - event.player.convertFromLegacyData() - for (skill in Skills.values()) { for (levelUpReward in skill.getLevelUpRewards()) { val obj = levelUpReward.obj @@ -56,25 +53,4 @@ class DataListener : Listener { stat.updateStatLevel(event.player) } } -} - -private fun Player.convertFromLegacyData() { - for (effect in Effects.values()) { - plugin.dataHandler.write(this.uniqueId, effect.id, this.getEffectLevel(effect)) - } - for (stat in Stats.values()) { - plugin.dataHandler.write(this.uniqueId, stat.id, this.getStatLevel(stat)) - } - for (skill in Skills.values()) { - plugin.dataHandler.write(this.uniqueId, skill.id, this.getSkillLevel(skill)) - val prog = this.persistentDataContainer.get(skill.xpKey, PersistentDataType.DOUBLE) - if (prog != null) { - plugin.dataHandler.write( - this.uniqueId, - skill.xpKey.key, - prog - ) - this.persistentDataContainer.remove(skill.xpKey) - } - } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt index 7331e69..c4c4fd3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt @@ -1,6 +1,7 @@ package com.willfp.ecoskills.data import com.willfp.ecoskills.EcoSkillsPlugin +import com.willfp.ecoskills.data.storage.PlayerProfile import com.willfp.ecoskills.expMultiplierCache import org.bukkit.Bukkit @@ -13,7 +14,7 @@ class SaveHandler { if (plugin.configYml.getBool("log-autosaves")) { plugin.logger.info("Auto-Saving player data!") } - plugin.dataHandler.save() + PlayerProfile.saveAll() expMultiplierCache.clear() if (plugin.configYml.getBool("log-autosaves")) { plugin.logger.info("Saved data!") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SavedPlayerNameListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SavedPlayerNameListener.kt index 33091e7..5f98ce4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SavedPlayerNameListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SavedPlayerNameListener.kt @@ -1,7 +1,7 @@ package com.willfp.ecoskills.data import com.willfp.eco.core.EcoPlugin -import com.willfp.ecoskills.plugin +import com.willfp.ecoskills.profile import org.bukkit.OfflinePlayer import org.bukkit.entity.Player import org.bukkit.event.EventHandler @@ -26,11 +26,11 @@ class SavedPlayerNameListener( var OfflinePlayer.savedDisplayName: String get() { if (this is Player) { - plugin.dataHandler.write(this.uniqueId, "name", this.displayName) + profile.write("name", this.displayName) } - return plugin.dataHandler.readString(this.uniqueId, "name", this.name ?: "Unknown Player") + return profile.readString("name", this.name ?: "Unknown Player") } set(value) { - plugin.dataHandler.write(this.uniqueId, "name", value) + return profile.write("name", value) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/storage/PlayerProfile.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/storage/PlayerProfile.kt new file mode 100644 index 0000000..7564b39 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/storage/PlayerProfile.kt @@ -0,0 +1,90 @@ +package com.willfp.ecoskills.data.storage + +import com.willfp.ecoskills.EcoSkillsPlugin +import com.willfp.ecoskills.effects.Effects +import com.willfp.ecoskills.skills.Skills +import com.willfp.ecoskills.stats.Stats +import java.util.* + +class PlayerProfile private constructor( + private val data: MutableMap +) { + fun write(key: String, value: T) { + data[key] = value + } + + fun readInt(key: String): Int { + return data[key] as Int? ?: 0 + } + + fun readDouble(key: String): Double { + return data[key] as Double? ?: 0.0 + } + + fun readString(key: String, default: String): String { + return data[key] as String? ?: default + } + + companion object { + private val handler = EcoSkillsPlugin.getInstance().dataHandler + private val loaded = mutableMapOf() + private val keys = mutableMapOf() + + fun load(uuid: UUID): PlayerProfile { + val found = loaded[uuid] + if (found != null) { + return found + } + + val data = mutableMapOf() + for ((key, type) in keys) { + when (type) { + Type.INT -> data[key] = handler.readInt(uuid, key) + Type.DOUBLE -> data[key] = handler.readDouble(uuid, key) + Type.STRING -> data[key] = handler.readString(uuid, key) + } + } + + val profile = PlayerProfile(data) + loaded[uuid] = profile + return profile + } + + fun saveAll() { + for ((uuid, profile) in loaded) { + for ((key, type) in keys) { + when (type) { + Type.INT -> handler.write(uuid, key, profile.readInt(key)) + Type.DOUBLE -> handler.write(uuid, key, profile.readDouble(key)) + Type.STRING -> handler.write(uuid, key, profile.readString(key, "Unknown Value")) + } + } + } + + handler.save() + } + + init { + keys["name"] = Type.STRING + + for (skill in Skills.values()) { + keys[skill.id] = Type.INT + keys[skill.xpKey.key] = Type.DOUBLE + } + + for (stat in Stats.values()) { + keys[stat.id] = Type.INT + } + + for (effect in Effects.values()) { + keys[effect.id] = Type.INT + } + } + } + + private enum class Type { + STRING, + DOUBLE, + INT + } +} \ No newline at end of file