Temp data rework

This commit is contained in:
Indyuce 2022-08-01 14:35:55 +02:00
parent 1ea785e4bb
commit a4eada2593
4 changed files with 26 additions and 40 deletions

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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}
* <p>
* 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());
}
}

View File

@ -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, () -> {