mirror of
https://github.com/Auxilor/EcoSkills.git
synced 2025-12-31 21:06:40 +00:00
Switched DataHandler to be more abstracted in favour of using PlayerProfile to increase MySQL performance
This commit is contained in:
@@ -80,7 +80,7 @@ public class EcoSkillsPlugin extends EcoPlugin {
|
||||
|
||||
@Override
|
||||
protected void handleDisable() {
|
||||
dataHandler.save();
|
||||
SaveHandler.Companion.save(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<UUID, Double>()
|
||||
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
|
||||
|
||||
@@ -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<String> ->
|
||||
try {
|
||||
(plugin as EcoSkillsPlugin).dataHandler.save()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
PlayerProfile.saveAll()
|
||||
plugin.reload()
|
||||
sender.sendMessage(plugin.langYml.getMessage("reloaded"))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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!")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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<String, Any>
|
||||
) {
|
||||
fun <T : Any> 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<UUID, PlayerProfile>()
|
||||
private val keys = mutableMapOf<String, Type>()
|
||||
|
||||
fun load(uuid: UUID): PlayerProfile {
|
||||
val found = loaded[uuid]
|
||||
if (found != null) {
|
||||
return found
|
||||
}
|
||||
|
||||
val data = mutableMapOf<String, Any>()
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user