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

View File

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