Class stats now update when using /mmocore reload

This commit is contained in:
Indyuce 2021-08-25 21:25:52 +02:00
parent 63c837be2c
commit 8378babd83
2 changed files with 58 additions and 46 deletions

View File

@ -37,6 +37,7 @@ import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.*;
@ -104,6 +105,7 @@ public class PlayerData extends OfflinePlayerData {
try {
profess = profess == null ? null : MMOCore.plugin.classManager.get(profess.getId());
getStats().updateStats();
} catch (NullPointerException exception) {
MMOCore.log(Level.SEVERE, "[Userdata] Could not find class " + getProfess().getId() + " while refreshing player data.");
}
@ -516,6 +518,7 @@ public class PlayerData extends OfflinePlayerData {
}
@Override
@NotNull
public PlayerClass getProfess() {
return profess == null ? MMOCore.plugin.classManager.getDefaultClass() : profess;
}

View File

@ -1,66 +1,75 @@
package net.Indyuce.mmocore.api.player.stats;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.player.EquipmentSlot;
import io.lumine.mythic.lib.api.stat.StatInstance;
import io.lumine.mythic.lib.api.stat.StatMap;
import io.lumine.mythic.lib.api.stat.modifier.ModifierSource;
import io.lumine.mythic.lib.api.stat.modifier.ModifierType;
import io.lumine.mythic.lib.api.stat.modifier.StatModifier;
import net.Indyuce.mmocore.api.player.PlayerData;
public class PlayerStats {
private final PlayerData data;
private final PlayerData data;
/**
* Utilclass to easily manipulate the MMOLib stat map
*
* @param data Playerdata
*/
public PlayerStats(PlayerData data) {
this.data = data;
}
/**
* Utilclass to easily manipulate the MMOLib stat map
*
* @param data Playerdata
*/
public PlayerStats(PlayerData data) {
this.data = data;
}
public PlayerData getData() {
return data;
}
public PlayerData getData() {
return data;
}
public StatMap getMap() {
return data.getMMOPlayerData().getStatMap();
}
public StatMap getMap() {
return data.getMMOPlayerData().getStatMap();
}
public StatInstance getInstance(StatType stat) {
return getMap().getInstance(stat.name());
}
public StatInstance getInstance(StatType stat) {
return getMap().getInstance(stat.name());
}
public StatInstance getInstance(String stat) {
return getMap().getInstance(stat);
}
public StatInstance getInstance(String stat) {
return getMap().getInstance(stat);
}
/*
* applies relative attributes on the base stat too
*/
public double getStat(StatType stat) {
return getInstance(stat).getTotal();
}
/*
* applies relative attributes on the base stat too
*/
public double getStat(StatType stat) {
return getInstance(stat).getTotal();
}
public double getBase(StatType stat) {
return data.getProfess().calculateStat(stat,
stat.hasProfession() ? data.getCollectionSkills().getLevel(stat.getProfession()) : data.getLevel());
}
public double getBase(StatType stat) {
return data.getProfess().calculateStat(stat,
stat.hasProfession() ? data.getCollectionSkills().getLevel(stat.getProfession()) : data.getLevel());
}
/*
* used to update MMOCore stat modifiers due to class and send them over to
* MMOLib. must be ran everytime the player levels up or changes class.
*/
public synchronized void updateStats() {
getMap().getInstances().forEach(ins -> ins.remove("mmocoreClass"));
/**
* Used to update MMOCore stat modifiers due to class and send them over to
* MMOLib. Must be ran everytime the player levels up or changes class.
* <p>
* This is also called when reloading the plugin to make class setup easier,
* see {@link PlayerData#update()} for more info
*/
public synchronized void updateStats() {
for (StatType stat : StatType.values()) {
StatInstance instance = getMap().getInstance(stat.name());
StatInstance.ModifierPacket packet = instance.newPacket();
for (StatType stat : StatType.values()) {
StatInstance instance = getMap().getInstance(stat.name());
double total = getBase(stat) - instance.getBase();
// Remove old stat modifiers
packet.removeIf(str -> str.equals("mmocoreClass"));
if (total != 0)
instance.addModifier("mmocoreClass", new StatModifier(total));
}
// Add newest one
double total = getBase(stat) - instance.getBase();
if (total != 0)
packet.addModifier("mmocoreClass", new StatModifier(total, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));
MythicLib.plugin.getStats().runUpdates(getMap());
}
// Then update the stat
packet.runUpdate();
}
}
}