Fixed stats

This commit is contained in:
Indyuce 2022-07-29 17:07:04 +02:00
parent 4a94dbd269
commit 83ab7ef523
7 changed files with 68 additions and 40 deletions

View File

@ -300,12 +300,12 @@ public class MMOCore extends JavaPlugin {
/* /*
* Initialize player data from all online players. This is very important to do * Initialize player data from all online players. This is very important to do
* that after registering all the professses otherwise the player datas can't * that after registering all the classes otherwise the player datas can't
* recognize what profess the player has and professes will be lost * recognize what profess the player has and professes will be lost
*/ */
Bukkit.getOnlinePlayers().forEach(player -> dataProvider.getDataManager().setup(player.getUniqueId())); Bukkit.getOnlinePlayers().forEach(player -> dataProvider.getDataManager().setup(player.getUniqueId()));
// load guild data after loading player data // Load guild data after loading player data
dataProvider.getGuildManager().load(); dataProvider.getGuildManager().load();
// Command // Command

View File

@ -380,6 +380,10 @@ public class PlayerClass extends PostLoadObject implements ExperienceObject {
return skills.values(); return skills.values();
} }
public Set<String> getStats() {
return stats.keySet();
}
@NotNull @NotNull
private LinearValue getStatInfo(String stat) { private LinearValue getStatInfo(String stat) {
LinearValue found = stats.get(stat); LinearValue found = stats.get(stat);

View File

@ -6,10 +6,11 @@ import io.lumine.mythic.lib.api.stat.StatMap;
import io.lumine.mythic.lib.api.stat.modifier.StatModifier; import io.lumine.mythic.lib.api.stat.modifier.StatModifier;
import io.lumine.mythic.lib.player.modifier.ModifierSource; import io.lumine.mythic.lib.player.modifier.ModifierSource;
import io.lumine.mythic.lib.player.modifier.ModifierType; import io.lumine.mythic.lib.player.modifier.ModifierType;
import net.Indyuce.mmocore.skill.ClassSkill; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.experience.Profession; import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.player.stats.StatInfo; import net.Indyuce.mmocore.player.stats.StatInfo;
import net.Indyuce.mmocore.skill.ClassSkill;
public class PlayerStats { public class PlayerStats {
private final PlayerData data; private final PlayerData data;
@ -70,14 +71,15 @@ public class PlayerStats {
* see {@link PlayerData#update()} for more info * see {@link PlayerData#update()} for more info
*/ */
public synchronized void updateStats() { public synchronized void updateStats() {
for (StatInstance instance : getMap().getInstances()) { for (String stat : MMOCore.plugin.statManager.getRegistered()) {
StatInstance.ModifierPacket packet = instance.newPacket(); final StatInstance instance = getMap().getInstance(stat);
final StatInstance.ModifierPacket packet = instance.newPacket();
// Remove old stat modifiers // Remove old stat modifier
packet.removeIf(str -> str.equals("mmocoreClass")); packet.remove("mmocoreClass");
// Add newest one // Add newest one
double total = getBase(instance.getStat()) - instance.getBase(); final double total = getBase(instance.getStat()) - instance.getBase();
if (total != 0) if (total != 0)
packet.addModifier(new StatModifier("mmocoreClass", instance.getStat(), total, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER)); packet.addModifier(new StatModifier("mmocoreClass", instance.getStat(), total, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));

View File

@ -1,8 +1,8 @@
package net.Indyuce.mmocore.manager; package net.Indyuce.mmocore.manager;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.ClassOption; import net.Indyuce.mmocore.api.player.profess.ClassOption;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler; import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
import net.Indyuce.mmocore.api.player.profess.event.trigger.*; import net.Indyuce.mmocore.api.player.profess.event.trigger.*;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
@ -46,6 +46,7 @@ public class ClassManager implements MMOCoreManager {
public void register(PlayerClass playerClass) { public void register(PlayerClass playerClass) {
map.put(playerClass.getId(), playerClass); map.put(playerClass.getId(), playerClass);
MMOCore.plugin.statManager.getRegistered().addAll(playerClass.getStats());
} }
public boolean has(String id) { public boolean has(String id) {
@ -75,8 +76,8 @@ public class ClassManager implements MMOCoreManager {
map.clear(); map.clear();
/* /*
* Do not clear the list of trigger listeners, since it's only setup * Does not clear the list of trigger listeners, since it's
* once the server loads and it is never modified. * only setup once the server loads and it is never modified.
*/ */
triggerHandlers.forEach(HandlerList::unregisterAll); triggerHandlers.forEach(HandlerList::unregisterAll);
} }

View File

@ -1,28 +1,38 @@
package net.Indyuce.mmocore.manager; package net.Indyuce.mmocore.manager;
import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.player.stats.StatInfo;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue; import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
import net.Indyuce.mmocore.experience.Profession; import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.player.stats.StatInfo;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
public class StatManager implements MMOCoreManager { public class StatManager implements MMOCoreManager {
private final Map<String, StatInfo> loaded = new HashMap<>(); private final Map<String, StatInfo> loaded = new HashMap<>();
/**
* Keeps track of all the item stats used so far in the plugin.
* It is not a constant since users can freely add or even create
* new stats.
* <p>
* These stats appear at least once:
* - in a class definition
* - in stats.yml which defines default stat formulas
*/
private final Set<String> usedStats = new HashSet<>();
@Override @Override
public void initialize(boolean clearBefore) { public void initialize(boolean clearBefore) {
if (clearBefore) if (clearBefore) {
loaded.clear(); loaded.clear();
usedStats.clear();
FileConfiguration config = new ConfigFile("stats").getConfig(); }
// Read default formulas // Read default formulas
FileConfiguration config = new ConfigFile("stats").getConfig();
for (String key : config.getConfigurationSection("default").getKeys(false)) for (String key : config.getConfigurationSection("default").getKeys(false))
registerDefaultFormula(key, new LinearValue(config.getConfigurationSection("default." + key))); registerDefaultFormula(key, new LinearValue(config.getConfigurationSection("default." + key)));
} }
@ -31,6 +41,21 @@ public class StatManager implements MMOCoreManager {
return loaded.values(); return loaded.values();
} }
/**
* Keeps track of all the item stats used so far in the plugin.
* It is not a constant since users can freely add or even create
* new stats.
* <p>
* These stats appear at least once:
* - in a class definition
* - in stats.yml which defines default stat formulas
*
* @return A list of stats that must be taken into account in MMOCore
*/
public Set<String> getRegistered() {
return usedStats;
}
@Nullable @Nullable
public StatInfo getInfo(String stat) { public StatInfo getInfo(String stat) {
return loaded.get(stat); return loaded.get(stat);
@ -42,20 +67,15 @@ public class StatManager implements MMOCoreManager {
public void registerDefaultFormula(String stat, LinearValue defaultFormula) { public void registerDefaultFormula(String stat, LinearValue defaultFormula) {
compute(stat).defaultInfo = defaultFormula; compute(stat).defaultInfo = defaultFormula;
usedStats.add(stat);
} }
/** /**
* @return A stat info for the specified stat. If it doesn't * @return A stat info for the specified stat. If it doesn't exist
* exist when method is called, it is registered into the map * when method is called, it is registered into the map
*/ */
@NotNull @NotNull
private StatInfo compute(String stat) { private StatInfo compute(String stat) {
StatInfo found = loaded.get(stat); return loaded.computeIfAbsent(stat, StatInfo::new);
if (found != null)
return found;
StatInfo newInfo = new StatInfo(stat);
loaded.put(stat, newInfo);
return newInfo;
} }
} }

View File

@ -221,7 +221,7 @@ items:
- '' - ''
- '✤ Knockback Resistance: &f{knockback_resistance} &7(&f{knockback_resistance_base} &7+ &f{knockback_resistance_extra}&7)' - '✤ Knockback Resistance: &f{knockback_resistance} &7(&f{knockback_resistance_base} &7+ &f{knockback_resistance_extra}&7)'
- '✤ Movement Speed: &f{movement_speed} &7(&f{movement_speed_base} &7+ &f{movement_speed_extra}&7)' - '✤ Movement Speed: &f{movement_speed} &7(&f{movement_speed_base} &7+ &f{movement_speed_extra}&7)'
- '✤ Speed Malus Reduction: &f{speed_malus_reduction} &7(&f{speed_malus_reduction_base} &7+ &f{speed_malus_reduction_extra}&7)' - '✤ Speed Malus Reduction: &f{speed_malus_reduction}% &7(&f{speed_malus_reduction_base} &7+ &f{speed_malus_reduction_extra}&7)'
int: int:
slots: [34] slots: [34]
function: stats function: stats

View File

@ -1,3 +1,4 @@
# Caution, these are very case sensitive!
default: default:
ATTACK_DAMAGE: ATTACK_DAMAGE:
base: 1 base: 1