From a4eada25930d211d4a38bd894a7d1f15d6b5e778 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Mon, 1 Aug 2022 14:35:55 +0200 Subject: [PATCH] Temp data rework --- .../mmocore/api/player/PlayerData.java | 29 +++++++------------ .../api/player/TemporaryPlayerData.java | 11 +++++++ .../mmocore/listener/PlayerListener.java | 23 +++------------ .../manager/data/PlayerDataManager.java | 3 +- 4 files changed, 26 insertions(+), 40 deletions(-) create mode 100644 src/main/java/net/Indyuce/mmocore/api/player/TemporaryPlayerData.java diff --git a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java index 77dbeeba..f76e6427 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -2,7 +2,6 @@ package net.Indyuce.mmocore.api.player; import io.lumine.mythic.lib.MythicLib; import io.lumine.mythic.lib.api.player.MMOPlayerData; -import io.lumine.mythic.lib.player.TemporaryPlayerData; import io.lumine.mythic.lib.player.cooldown.CooldownMap; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.ConfigMessage; @@ -27,7 +26,6 @@ import net.Indyuce.mmocore.experience.ExperienceTableClaimer; import net.Indyuce.mmocore.experience.PlayerProfessions; import net.Indyuce.mmocore.experience.droptable.ExperienceItem; import net.Indyuce.mmocore.experience.droptable.ExperienceTable; -import net.Indyuce.mmocore.gui.api.GeneratedInventory; import net.Indyuce.mmocore.guild.provided.Guild; import net.Indyuce.mmocore.loot.chest.particle.SmallParticleEffect; import net.Indyuce.mmocore.party.AbstractParty; @@ -114,29 +112,23 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc /** * If the player data was loaded using temporary data. - * See {@link TemporaryPlayerData} for more info */ private final boolean usingTemporaryData; public PlayerData(MMOPlayerData mmoData) { - this(mmoData, false); - } - - public PlayerData(MMOPlayerData mmoData, TemporaryPlayerData tempData) { - this(mmoData, true); - - mana = tempData.getDouble("mana"); - stamina = tempData.getDouble("stamina"); - stellium = tempData.getDouble("stellium"); - } - - private PlayerData(MMOPlayerData mmoData, boolean usingTemporaryData) { super(mmoData.getUniqueId()); this.mmoData = mmoData; - this.playerStats = new PlayerStats(this); - this.questData = new PlayerQuests(this); - this.usingTemporaryData = usingTemporaryData; + questData = new PlayerQuests(this); + playerStats = new PlayerStats(this); + + // Load temporary data if necessary + final @Nullable TemporaryPlayerData tempData = mmoData.getExternalData("mmocore", TemporaryPlayerData.class); + if (usingTemporaryData = tempData != null) { + mana = tempData.mana; + stamina = tempData.stamina; + stellium = tempData.stellium; + } } /** @@ -247,7 +239,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc } - @Override public int getClaims(ExperienceObject object, ExperienceTable table, ExperienceItem item) { String key = object.getKey() + "." + table.getId() + "." + item.getId(); diff --git a/src/main/java/net/Indyuce/mmocore/api/player/TemporaryPlayerData.java b/src/main/java/net/Indyuce/mmocore/api/player/TemporaryPlayerData.java new file mode 100644 index 00000000..874c8764 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/player/TemporaryPlayerData.java @@ -0,0 +1,11 @@ +package net.Indyuce.mmocore.api.player; + +public class TemporaryPlayerData { + public final double mana, stamina, stellium; + + public TemporaryPlayerData(PlayerData player) { + this.mana = player.getMana(); + this.stamina = player.getStamina(); + this.stellium = player.getStellium(); + } +} diff --git a/src/main/java/net/Indyuce/mmocore/listener/PlayerListener.java b/src/main/java/net/Indyuce/mmocore/listener/PlayerListener.java index a5225c0b..c8669a7c 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/PlayerListener.java +++ b/src/main/java/net/Indyuce/mmocore/listener/PlayerListener.java @@ -1,11 +1,10 @@ package net.Indyuce.mmocore.listener; -import io.lumine.mythic.lib.api.event.TemporaryDataSavedEvent; -import io.lumine.mythic.lib.player.TemporaryPlayerData; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent; import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.api.player.TemporaryPlayerData; import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; import net.Indyuce.mmocore.gui.api.InventoryClickContext; import net.Indyuce.mmocore.gui.api.PluginInventory; @@ -44,8 +43,8 @@ public class PlayerListener implements Listener { if (event.getCurrentItem() != null && event.getCurrentItem().getItemMeta() != null) - ((PluginInventory) event.getInventory().getHolder()) - .whenClicked(new InventoryClickContext(slot,event.getCurrentItem(),event.getClick(),event)); + ((PluginInventory) event.getInventory().getHolder()) + .whenClicked(new InventoryClickContext(slot, event.getCurrentItem(), event.getClick(), event)); } } @@ -77,6 +76,7 @@ public class PlayerListener implements Listener { @EventHandler public void e(PlayerQuitEvent event) { PlayerData playerData = PlayerData.get(event.getPlayer()); + playerData.getMMOPlayerData().setExternalData("mmocore", new TemporaryPlayerData(playerData)); MMOCore.plugin.dataProvider.getDataManager().unregisterSafe(playerData); } @@ -99,19 +99,4 @@ public class PlayerListener implements Listener { event.setAmount(bukkitEvent.getAmount()); } } - - /** - * See {@link TemporaryPlayerData} and {@link TemporaryDataSavedEvent} - *

- * This temporarily saves: - * - player resources including mana, stamina and stellium - */ - @EventHandler - public void h(TemporaryDataSavedEvent event) { - TemporaryPlayerData temp = event.getTemporaryData(); - PlayerData playerData = PlayerData.get(event.getPlayer().getUniqueId()); - temp.set("mana", playerData.getMana()); - temp.set("stamina", playerData.getStamina()); - temp.set("stellium", playerData.getStellium()); - } } diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java b/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java index 1f3a91e7..dbd35a15 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java @@ -1,7 +1,6 @@ package net.Indyuce.mmocore.manager.data; import io.lumine.mythic.lib.api.player.MMOPlayerData; -import io.lumine.mythic.lib.player.TemporaryPlayerData; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.event.AsyncPlayerDataLoadEvent; import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent; @@ -88,7 +87,7 @@ public abstract class PlayerDataManager { // Load player data if it does not exist if (!data.containsKey(uniqueId)) { - PlayerData newData = TemporaryPlayerData.has(uniqueId) ? new PlayerData(MMOPlayerData.get(uniqueId), TemporaryPlayerData.get(uniqueId)) : new PlayerData(MMOPlayerData.get(uniqueId)); + PlayerData newData = new PlayerData(MMOPlayerData.get(uniqueId)); // Schedule async data loading Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {