mirror of
https://github.com/Auxilor/EcoSkills.git
synced 2026-01-03 06:12:21 +00:00
Added yaml storage for offline player API methods
This commit is contained in:
@@ -5,6 +5,7 @@ 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.data.DataYml;
|
||||
import com.willfp.ecoskills.effects.Effect;
|
||||
import com.willfp.ecoskills.effects.Effects;
|
||||
import com.willfp.ecoskills.skills.Skill;
|
||||
@@ -17,6 +18,7 @@ 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;
|
||||
|
||||
@@ -26,12 +28,18 @@ public class EcoSkillsPlugin extends EcoPlugin {
|
||||
*/
|
||||
private static EcoSkillsPlugin instance;
|
||||
|
||||
/**
|
||||
* data.yml.
|
||||
*/
|
||||
private final DataYml dataYml;
|
||||
|
||||
/**
|
||||
* Internal constructor called by bukkit on plugin load.
|
||||
*/
|
||||
public EcoSkillsPlugin() {
|
||||
super(0, 12205, "&#ff00ae");
|
||||
instance = this;
|
||||
dataYml = new DataYml(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,6 +58,24 @@ public class EcoSkillsPlugin extends EcoPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleDisable() {
|
||||
try {
|
||||
dataYml.save();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data.yml.
|
||||
*
|
||||
* @return data.yml.
|
||||
*/
|
||||
public DataYml getDataYml() {
|
||||
return dataYml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance of EcoSkills.
|
||||
*
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.willfp.ecoskills.effects.Effect;
|
||||
import com.willfp.ecoskills.skills.Skill;
|
||||
import com.willfp.ecoskills.stats.Stat;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -21,7 +22,7 @@ public interface EcoSkillsAPI {
|
||||
* @param skill The skill.
|
||||
* @return The level.
|
||||
*/
|
||||
int getSkillLevel(@NotNull Player player,
|
||||
int getSkillLevel(@NotNull OfflinePlayer player,
|
||||
@NotNull Skill skill);
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,14 +5,17 @@ import com.willfp.eco.util.NumberUtils
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent
|
||||
import com.willfp.ecoskills.effects.Effect
|
||||
import com.willfp.ecoskills.effects.Effects
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import com.willfp.ecoskills.skills.Skills
|
||||
import com.willfp.ecoskills.stats.Stat
|
||||
import com.willfp.ecoskills.stats.Stats
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.OfflinePlayer
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
object PlayerPlaceholders {
|
||||
object PlayerHelper {
|
||||
init {
|
||||
PlaceholderEntry(
|
||||
"average_skill_level",
|
||||
@@ -25,6 +28,8 @@ object PlayerPlaceholders {
|
||||
true
|
||||
).register()
|
||||
}
|
||||
|
||||
val plugin: EcoSkillsPlugin = EcoSkillsPlugin.getInstance()
|
||||
}
|
||||
|
||||
fun Player.getTotalSkillLevel(): Int {
|
||||
@@ -65,12 +70,17 @@ fun Player.giveSkillExperience(skill: Skill, experience: Double) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Player.getSkillLevel(skill: Skill): Int {
|
||||
return this.persistentDataContainer.getOrDefault(skill.key, PersistentDataType.INTEGER, 0)
|
||||
fun OfflinePlayer.getSkillLevel(skill: Skill): Int {
|
||||
return if (this !is Player) {
|
||||
PlayerHelper.plugin.dataYml.getInt("player.${this.uniqueId}.${skill.id}", 0)
|
||||
} else {
|
||||
this.persistentDataContainer.get(skill.key, PersistentDataType.INTEGER)
|
||||
?: PlayerHelper.plugin.dataYml.getInt("player.${this.uniqueId}.${skill.id}", 0)
|
||||
}
|
||||
}
|
||||
|
||||
fun Player.setSkillLevel(skill: Skill, level: Int) {
|
||||
this.persistentDataContainer.set(skill.key, PersistentDataType.INTEGER, level)
|
||||
PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", level)
|
||||
}
|
||||
|
||||
fun Player.getSkillProgressToNextLevel(skill: Skill): Double {
|
||||
@@ -104,4 +114,16 @@ fun Player.getStatLevel(stat: Stat): Int {
|
||||
fun Player.setStatLevel(stat: Stat, level: Int) {
|
||||
this.persistentDataContainer.set(stat.key, PersistentDataType.INTEGER, level)
|
||||
stat.updateStatLevel(this)
|
||||
}
|
||||
|
||||
fun Player.convertPersistentToYml() {
|
||||
for (effect in Effects.values()) {
|
||||
PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", this.getEffectLevel(effect))
|
||||
}
|
||||
for (stat in Stats.values()) {
|
||||
PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", this.getStatLevel(stat))
|
||||
}
|
||||
for (skill in Skills.values()) {
|
||||
PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", this.getSkillLevel(skill))
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,12 @@ import com.willfp.ecoskills.effects.Effect
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import com.willfp.ecoskills.stats.Stat
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.OfflinePlayer
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
object EcoSkillsAPIImpl: EcoSkillsAPI {
|
||||
override fun getSkillLevel(player: Player, skill: Skill): Int {
|
||||
override fun getSkillLevel(player: OfflinePlayer, skill: Skill): Int {
|
||||
return player.getSkillLevel(skill)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.willfp.ecoskills.data
|
||||
|
||||
import com.willfp.ecoskills.EcoSkillsPlugin
|
||||
import com.willfp.ecoskills.convertPersistentToYml
|
||||
import org.bukkit.event.EventHandler
|
||||
import org.bukkit.event.Listener
|
||||
import org.bukkit.event.player.PlayerJoinEvent
|
||||
import java.net.http.WebSocket
|
||||
|
||||
class DataConversionListener(
|
||||
private val plugin: EcoSkillsPlugin
|
||||
): Listener {
|
||||
@EventHandler
|
||||
fun onJoin(event: PlayerJoinEvent) {
|
||||
event.player.convertPersistentToYml()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.ecoskills.data
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin
|
||||
import com.willfp.eco.core.config.yaml.YamlBaseConfig
|
||||
|
||||
class DataYml(
|
||||
plugin: EcoPlugin
|
||||
): YamlBaseConfig(
|
||||
"data",
|
||||
false,
|
||||
plugin
|
||||
) {
|
||||
}
|
||||
Reference in New Issue
Block a user