MMOPlayerData refactor

Please use this version of MMOLib
https://github.com/mmopluginteam/mmolib/releases/tag/1.2.5
This commit is contained in:
Indyuce 2020-07-20 15:44:23 +02:00
parent 87c75588fa
commit 6fc5f8f657
5 changed files with 82 additions and 84 deletions

Binary file not shown.

View File

@ -53,8 +53,7 @@ import net.Indyuce.mmocore.listener.SpellCast.SkillCasting;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.player.MMOData; import net.mmogroup.mmolib.api.player.MMOPlayerData;
import net.mmogroup.mmolib.api.stat.StatMap;
import net.mmogroup.mmolib.version.VersionSound; import net.mmogroup.mmolib.version.VersionSound;
public class PlayerData extends OfflinePlayerData { public class PlayerData extends OfflinePlayerData {
@ -64,7 +63,7 @@ public class PlayerData extends OfflinePlayerData {
* player is offline so the plugin can use #isOnline to check if the player * player is offline so the plugin can use #isOnline to check if the player
* is online * is online
*/ */
private Player player; private final MMOPlayerData mmoData;
/* /*
* 'profess' can be null, you need to retrieve the player class using the * 'profess' can be null, you need to retrieve the player class using the
@ -88,7 +87,7 @@ public class PlayerData extends OfflinePlayerData {
private final PlayerAttributes attributes = new PlayerAttributes(this); private final PlayerAttributes attributes = new PlayerAttributes(this);
private final Map<String, SavedClassInformation> classSlots = new HashMap<>(); private final Map<String, SavedClassInformation> classSlots = new HashMap<>();
private long lastWaypoint, lastLogin, lastFriendRequest, actionBarTimeOut, lastLootChest; private long lastWaypoint, lastFriendRequest, actionBarTimeOut, lastLootChest;
/* /*
* NON-FINAL player data stuff made public to facilitate field change * NON-FINAL player data stuff made public to facilitate field change
@ -98,12 +97,13 @@ public class PlayerData extends OfflinePlayerData {
public boolean nocd; public boolean nocd;
public CombatRunnable combat; public CombatRunnable combat;
public PlayerData(Player player) { public PlayerData(MMOPlayerData mmoData) {
super(player.getUniqueId()); super(mmoData.getUniqueId());
mmoData.setMMOCore(this);
setPlayer(player); this.mmoData = mmoData;
playerStats = new PlayerStats(this); this.playerStats = new PlayerStats(this);
questData = new PlayerQuests(this); this.questData = new PlayerQuests(this);
} }
/* /*
@ -115,7 +115,10 @@ public class PlayerData extends OfflinePlayerData {
private PlayerData() { private PlayerData() {
super(UUID.randomUUID()); super(UUID.randomUUID());
playerStats = new PlayerStats(this, new StatMap(new MMOData().setMMOCore(this))); mmoData = new MMOPlayerData(null, null);
mmoData.setMMOCore(this);
playerStats = new PlayerStats(this);
questData = new PlayerQuests(this, null); questData = new PlayerQuests(this, null);
} }
@ -143,21 +146,8 @@ public class PlayerData extends OfflinePlayerData {
} }
} }
public static PlayerData get(OfflinePlayer player) { public MMOPlayerData getMMOPlayerData() {
return get(player.getUniqueId()); return mmoData;
}
public static PlayerData get(UUID uuid) {
return MMOCore.plugin.dataProvider.getDataManager().get(uuid);
}
public static Collection<PlayerData> getAll() {
return MMOCore.plugin.dataProvider.getDataManager().getLoaded();
}
public void setPlayer(Player player) {
this.player = player;
this.lastLogin = System.currentTimeMillis();
} }
public List<UUID> getFriends() { public List<UUID> getFriends() {
@ -173,12 +163,12 @@ public class PlayerData extends OfflinePlayerData {
} }
public Player getPlayer() { public Player getPlayer() {
return player; return mmoData.getPlayer();
} }
@Override @Override
public long getLastLogin() { public long getLastLogin() {
return lastLogin; return mmoData.getLastLogin();
} }
public long getLastFriendRequest() { public long getLastFriendRequest() {
@ -229,6 +219,10 @@ public class PlayerData extends OfflinePlayerData {
return attributeReallocationPoints; return attributeReallocationPoints;
} }
public boolean isOnline() {
return mmoData.isOnline();
}
public boolean hasParty() { public boolean hasParty() {
return party != null; return party != null;
} }
@ -237,9 +231,9 @@ public class PlayerData extends OfflinePlayerData {
return guild != null; return guild != null;
} }
public boolean isOnline() { // public boolean isOnline() {
return player.isOnline(); // return player.isOnline();
} // }
public void setLevel(int level) { public void setLevel(int level) {
this.level = Math.max(1, level); this.level = Math.max(1, level);
@ -260,8 +254,8 @@ public class PlayerData extends OfflinePlayerData {
public void refreshVanillaExp() { public void refreshVanillaExp() {
if (MMOCore.plugin.configManager.overrideVanillaExp) { if (MMOCore.plugin.configManager.overrideVanillaExp) {
player.setLevel(getLevel()); getPlayer().setLevel(getLevel());
player.setExp(Math.max(0, Math.min(1, (float) experience / (float) getLevelUpExperience()))); getPlayer().setExp(Math.max(0, Math.min(1, (float) experience / (float) getLevelUpExperience())));
} }
} }
@ -339,8 +333,8 @@ public class PlayerData extends OfflinePlayerData {
} }
public void heal(double heal) { public void heal(double heal) {
double newest = Math.max(0, Math.min(player.getHealth() + heal, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue())); double newest = Math.max(0, Math.min(getPlayer().getHealth() + heal, getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
if (player.getHealth() == newest) if (getPlayer().getHealth() == newest)
return; return;
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.HEALTH, heal); PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.HEALTH, heal);
@ -374,7 +368,7 @@ public class PlayerData extends OfflinePlayerData {
} }
public void log(Level level, String message) { public void log(Level level, String message) {
MMOCore.plugin.getLogger().log(level, "[Userdata:" + player.getName() + "] " + message); MMOCore.plugin.getLogger().log(level, "[Userdata:" + getPlayer().getName() + "] " + message);
} }
public void setLastFriendRequest(long ms) { public void setLastFriendRequest(long ms) {
@ -402,32 +396,33 @@ public class PlayerData extends OfflinePlayerData {
giveStellium(-waypoint.getStelliumCost()); giveStellium(-waypoint.getStelliumCost());
new BukkitRunnable() { new BukkitRunnable() {
int x = player.getLocation().getBlockX(), y = player.getLocation().getBlockY(), z = player.getLocation().getBlockZ(), t; int x = getPlayer().getLocation().getBlockX(), y = getPlayer().getLocation().getBlockY(), z = getPlayer().getLocation().getBlockZ(), t;
public void run() { public void run() {
if (player.getLocation().getBlockX() != x || player.getLocation().getBlockY() != y || player.getLocation().getBlockZ() != z) { if (getPlayer().getLocation().getBlockX() != x || getPlayer().getLocation().getBlockY() != y
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, .5f); || getPlayer().getLocation().getBlockZ() != z) {
MMOCore.plugin.configManager.getSimpleMessage("warping-canceled").send(player); getPlayer().playSound(getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, .5f);
MMOCore.plugin.configManager.getSimpleMessage("warping-canceled").send(getPlayer());
giveStellium(waypoint.getStelliumCost()); giveStellium(waypoint.getStelliumCost());
cancel(); cancel();
return; return;
} }
MMOCore.plugin.configManager.getSimpleMessage("warping-comencing", "left", "" + ((120 - t) / 20)).send(player); MMOCore.plugin.configManager.getSimpleMessage("warping-comencing", "left", "" + ((120 - t) / 20)).send(getPlayer());
if (t++ >= 100) { if (t++ >= 100) {
player.teleport(waypoint.getLocation()); getPlayer().teleport(waypoint.getLocation());
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20, 1, false, false)); getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20, 1, false, false));
player.playSound(player.getLocation(), VersionSound.ENTITY_ENDERMAN_TELEPORT.toSound(), 1, .5f); getPlayer().playSound(getPlayer().getLocation(), VersionSound.ENTITY_ENDERMAN_TELEPORT.toSound(), 1, .5f);
cancel(); cancel();
return; return;
} }
player.playSound(player.getLocation(), VersionSound.BLOCK_NOTE_BLOCK_BELL.toSound(), 1, (float) (t / Math.PI * .015 + .5)); getPlayer().playSound(getPlayer().getLocation(), VersionSound.BLOCK_NOTE_BLOCK_BELL.toSound(), 1, (float) (t / Math.PI * .015 + .5));
double r = Math.sin((double) t / 100 * Math.PI); double r = Math.sin((double) t / 100 * Math.PI);
for (double j = 0; j < Math.PI * 2; j += Math.PI / 4) for (double j = 0; j < Math.PI * 2; j += Math.PI / 4)
MMOLib.plugin.getVersion().getWrapper().spawnParticle(Particle.REDSTONE, MMOLib.plugin.getVersion().getWrapper().spawnParticle(Particle.REDSTONE,
player.getLocation().add(Math.cos((double) t / 20 + j) * r, (double) t / 50, Math.sin((double) t / 20 + j) * r), 1.25f, getPlayer().getLocation().add(Math.cos((double) t / 20 + j) * r, (double) t / 50, Math.sin((double) t / 20 + j) * r),
Color.PURPLE); 1.25f, Color.PURPLE);
} }
}.runTaskTimer(MMOCore.plugin, 0, 1); }.runTaskTimer(MMOCore.plugin, 0, 1);
} }
@ -476,9 +471,9 @@ public class PlayerData extends OfflinePlayerData {
if (level > oldLevel) { if (level > oldLevel) {
Bukkit.getPluginManager().callEvent(new PlayerLevelUpEvent(this, null, oldLevel, level)); Bukkit.getPluginManager().callEvent(new PlayerLevelUpEvent(this, null, oldLevel, level));
new ConfigMessage("level-up").addPlaceholders("level", "" + level).send(player); new ConfigMessage("level-up").addPlaceholders("level", "" + level).send(getPlayer());
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1); getPlayer().playSound(getPlayer().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
new SmallParticleEffect(player, Particle.SPELL_INSTANT); new SmallParticleEffect(getPlayer(), Particle.SPELL_INSTANT);
getStats().updateStats(); getStats().updateStats();
} }
@ -583,7 +578,7 @@ public class PlayerData extends OfflinePlayerData {
public void displayActionBar(String message) { public void displayActionBar(String message) {
setActionBarTimeOut(60); setActionBarTimeOut(60);
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
} }
@Deprecated @Deprecated
@ -735,13 +730,13 @@ public class PlayerData extends OfflinePlayerData {
if (!cast.isSuccessful()) { if (!cast.isSuccessful()) {
if (!skill.getSkill().isPassive()) { if (!skill.getSkill().isPassive()) {
if (cast.getCancelReason() == CancelReason.LOCKED) if (cast.getCancelReason() == CancelReason.LOCKED)
MMOCore.plugin.configManager.getSimpleMessage("not-unlocked-skill").send(player); MMOCore.plugin.configManager.getSimpleMessage("not-unlocked-skill").send(getPlayer());
if (cast.getCancelReason() == CancelReason.MANA) if (cast.getCancelReason() == CancelReason.MANA)
MMOCore.plugin.configManager.getSimpleMessage("casting.no-mana").send(player); MMOCore.plugin.configManager.getSimpleMessage("casting.no-mana").send(getPlayer());
if (cast.getCancelReason() == CancelReason.COOLDOWN) if (cast.getCancelReason() == CancelReason.COOLDOWN)
MMOCore.plugin.configManager.getSimpleMessage("casting.on-cooldown").send(player); MMOCore.plugin.configManager.getSimpleMessage("casting.on-cooldown").send(getPlayer());
} }
return cast; return cast;
@ -762,4 +757,16 @@ public class PlayerData extends OfflinePlayerData {
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj != null && obj instanceof PlayerData && ((PlayerData) obj).getUniqueId().equals(getUniqueId()); return obj != null && obj instanceof PlayerData && ((PlayerData) obj).getUniqueId().equals(getUniqueId());
} }
public static PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId());
}
public static PlayerData get(UUID uuid) {
return MMOCore.plugin.dataProvider.getDataManager().get(uuid);
}
public static Collection<PlayerData> getAll() {
return MMOCore.plugin.dataProvider.getDataManager().getLoaded();
}
} }

View File

@ -1,28 +1,18 @@
package net.Indyuce.mmocore.api.player.stats; package net.Indyuce.mmocore.api.player.stats;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.mmogroup.mmolib.api.player.MMOData;
import net.mmogroup.mmolib.api.stat.StatInstance; import net.mmogroup.mmolib.api.stat.StatInstance;
import net.mmogroup.mmolib.api.stat.StatMap; import net.mmogroup.mmolib.api.stat.StatMap;
import net.mmogroup.mmolib.api.stat.modifier.StatModifier; import net.mmogroup.mmolib.api.stat.modifier.StatModifier;
public class PlayerStats { public class PlayerStats {
private final PlayerData data; private final PlayerData data;
private final StatMap map;
/* /*
* util class to manipulate more easily stat data from MMOLib * util class to manipulate more easily stat data from MMOLib
*/ */
public PlayerStats(PlayerData data) { public PlayerStats(PlayerData data) {
this.data = data; this.data = data;
map = MMOData.get(data.getPlayer()).setMMOCore(data).getStatMap();
}
@Deprecated
public PlayerStats(PlayerData data, StatMap map) {
this.data = data;
this.map = map;
} }
public PlayerData getData() { public PlayerData getData() {
@ -30,7 +20,7 @@ public class PlayerStats {
} }
public StatMap getMap() { public StatMap getMap() {
return map; return data.getMMOPlayerData().getStatMap();
} }
public StatInstance getInstance(StatType stat) { public StatInstance getInstance(StatType stat) {
@ -38,7 +28,7 @@ public class PlayerStats {
} }
public StatInstance getInstance(String stat) { public StatInstance getInstance(String stat) {
return map.getInstance(stat); return getMap().getInstance(stat);
} }
/* /*
@ -58,16 +48,16 @@ public class PlayerStats {
* MMOLib. must be ran everytime the player levels up or changes class. * MMOLib. must be ran everytime the player levels up or changes class.
*/ */
public void updateStats() { public void updateStats() {
map.getInstances().forEach(ins -> ins.removeIf(key -> key.equals("mmocoreClass"))); getMap().getInstances().forEach(ins -> ins.removeIf(key -> key.equals("mmocoreClass")));
for (StatType stat : StatType.values()) { for (StatType stat : StatType.values()) {
StatInstance instance = map.getInstance(stat.name()); StatInstance instance = getMap().getInstance(stat.name());
double total = getBase(stat) - instance.getVanilla(); double total = getBase(stat) - instance.getVanilla();
if (total != 0) if (total != 0)
instance.addModifier("mmocoreClass", new StatModifier(total)); instance.addModifier("mmocoreClass", new StatModifier(total));
} }
map.updateAll(); getMap().updateAll();
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -73,7 +74,7 @@ public class DropTableManager extends MMOManager {
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load drop table file '" + file.getName() + "': " + exception.getMessage()); MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load drop table file '" + file.getName() + "': " + exception.getMessage());
} }
map.values().forEach(table -> table.postLoad()); Bukkit.getScheduler().runTask(MMOCore.plugin, () -> map.values().forEach(table -> table.postLoad()));
} }
@Override @Override

View File

@ -1,9 +1,8 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -13,20 +12,23 @@ import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent; import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
import net.Indyuce.mmocore.api.player.OfflinePlayerData; import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.mmogroup.mmolib.api.player.MMOPlayerData;
public abstract class PlayerDataManager { public abstract class PlayerDataManager {
private final Map<UUID, PlayerData> map = new HashMap<>(); // private final Map<UUID, PlayerData> map = new HashMap<>();
public PlayerData get(OfflinePlayer player) { public PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId()); return get(player.getUniqueId());
} }
public PlayerData get(UUID uuid) { public PlayerData get(UUID uuid) {
return map.getOrDefault(uuid, PlayerData.NOT_LOADED); PlayerData found = MMOPlayerData.get(uuid).getMMOCore();
return found == null ? PlayerData.NOT_LOADED : found;
} }
public void remove(UUID uuid) { public void remove(UUID uuid) {
map.remove(uuid); if (MMOPlayerData.isLoaded(uuid))
MMOPlayerData.get(uuid).setMMOCore(null);
} }
public abstract OfflinePlayerData getOffline(UUID uuid); public abstract OfflinePlayerData getOffline(UUID uuid);
@ -37,9 +39,9 @@ public abstract class PlayerDataManager {
* setup playerData based on loadData method to support both MySQL and * setup playerData based on loadData method to support both MySQL and
* YAML data storage * YAML data storage
*/ */
if (!map.containsKey(player.getUniqueId())) { MMOPlayerData mmoData = MMOPlayerData.get(player);
PlayerData generated = new PlayerData(player); if (mmoData.getMMOCore() == null) {
map.put(player.getUniqueId(), generated); PlayerData generated = new PlayerData(mmoData);
/* /*
* loads player data and ONLY THEN refresh the player statistics and * loads player data and ONLY THEN refresh the player statistics and
@ -51,16 +53,14 @@ public abstract class PlayerDataManager {
generated.getStats().updateStats(); generated.getStats().updateStats();
}); });
} }
get(player).setPlayer(player);
} }
public boolean isLoaded(UUID uuid) { public boolean isLoaded(UUID uuid) {
return map.containsKey(uuid); return MMOPlayerData.isLoaded(uuid) && MMOPlayerData.get(uuid).getMMOCore() != null;
} }
public Collection<PlayerData> getLoaded() { public Collection<PlayerData> getLoaded() {
return map.values(); return MMOPlayerData.getLoaded().stream().filter(data -> data.getMMOCore() != null).map(data -> data.getMMOCore()).collect(Collectors.toSet());
} }
public abstract void loadData(PlayerData data); public abstract void loadData(PlayerData data);