From 83ab7ef523c96ea7622d16611b51ec9e7e250345 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Fri, 29 Jul 2022 17:07:04 +0200 Subject: [PATCH] Fixed stats --- .../java/net/Indyuce/mmocore/MMOCore.java | 4 +- .../api/player/profess/PlayerClass.java | 4 ++ .../mmocore/api/player/stats/PlayerStats.java | 14 ++--- .../Indyuce/mmocore/manager/ClassManager.java | 7 +-- .../Indyuce/mmocore/manager/StatManager.java | 52 +++++++++++++------ .../resources/default/gui/player-stats.yml | 2 +- src/main/resources/default/stats.yml | 25 ++++----- 7 files changed, 68 insertions(+), 40 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/MMOCore.java b/src/main/java/net/Indyuce/mmocore/MMOCore.java index 90b04b1a..6017dad8 100644 --- a/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -300,12 +300,12 @@ public class MMOCore extends JavaPlugin { /* * 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 */ 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(); // Command diff --git a/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java b/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java index 3021d87e..54316b9a 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java @@ -380,6 +380,10 @@ public class PlayerClass extends PostLoadObject implements ExperienceObject { return skills.values(); } + public Set getStats() { + return stats.keySet(); + } + @NotNull private LinearValue getStatInfo(String stat) { LinearValue found = stats.get(stat); diff --git a/src/main/java/net/Indyuce/mmocore/api/player/stats/PlayerStats.java b/src/main/java/net/Indyuce/mmocore/api/player/stats/PlayerStats.java index 27d2b753..ff418954 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/stats/PlayerStats.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/stats/PlayerStats.java @@ -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.player.modifier.ModifierSource; 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.experience.Profession; import net.Indyuce.mmocore.player.stats.StatInfo; +import net.Indyuce.mmocore.skill.ClassSkill; public class PlayerStats { private final PlayerData data; @@ -70,14 +71,15 @@ public class PlayerStats { * see {@link PlayerData#update()} for more info */ public synchronized void updateStats() { - for (StatInstance instance : getMap().getInstances()) { - StatInstance.ModifierPacket packet = instance.newPacket(); + for (String stat : MMOCore.plugin.statManager.getRegistered()) { + final StatInstance instance = getMap().getInstance(stat); + final StatInstance.ModifierPacket packet = instance.newPacket(); - // Remove old stat modifiers - packet.removeIf(str -> str.equals("mmocoreClass")); + // Remove old stat modifier + packet.remove("mmocoreClass"); // Add newest one - double total = getBase(instance.getStat()) - instance.getBase(); + final double total = getBase(instance.getStat()) - instance.getBase(); if (total != 0) packet.addModifier(new StatModifier("mmocoreClass", instance.getStat(), total, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER)); diff --git a/src/main/java/net/Indyuce/mmocore/manager/ClassManager.java b/src/main/java/net/Indyuce/mmocore/manager/ClassManager.java index 8d0356c0..c1c1669d 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/ClassManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/ClassManager.java @@ -1,8 +1,8 @@ package net.Indyuce.mmocore.manager; 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.PlayerClass; import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler; import net.Indyuce.mmocore.api.player.profess.event.trigger.*; import org.apache.commons.lang.Validate; @@ -46,6 +46,7 @@ public class ClassManager implements MMOCoreManager { public void register(PlayerClass playerClass) { map.put(playerClass.getId(), playerClass); + MMOCore.plugin.statManager.getRegistered().addAll(playerClass.getStats()); } public boolean has(String id) { @@ -75,8 +76,8 @@ public class ClassManager implements MMOCoreManager { map.clear(); /* - * Do not clear the list of trigger listeners, since it's only setup - * once the server loads and it is never modified. + * Does not clear the list of trigger listeners, since it's + * only setup once the server loads and it is never modified. */ triggerHandlers.forEach(HandlerList::unregisterAll); } diff --git a/src/main/java/net/Indyuce/mmocore/manager/StatManager.java b/src/main/java/net/Indyuce/mmocore/manager/StatManager.java index 1afca5b6..dd341069 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/StatManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/StatManager.java @@ -1,28 +1,38 @@ package net.Indyuce.mmocore.manager; 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.experience.Profession; +import net.Indyuce.mmocore.player.stats.StatInfo; import org.bukkit.configuration.file.FileConfiguration; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; public class StatManager implements MMOCoreManager { private final Map 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. + *

+ * These stats appear at least once: + * - in a class definition + * - in stats.yml which defines default stat formulas + */ + private final Set usedStats = new HashSet<>(); + @Override public void initialize(boolean clearBefore) { - if (clearBefore) + if (clearBefore) { loaded.clear(); - - FileConfiguration config = new ConfigFile("stats").getConfig(); + usedStats.clear(); + } // Read default formulas + FileConfiguration config = new ConfigFile("stats").getConfig(); for (String key : config.getConfigurationSection("default").getKeys(false)) registerDefaultFormula(key, new LinearValue(config.getConfigurationSection("default." + key))); } @@ -31,6 +41,21 @@ public class StatManager implements MMOCoreManager { 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. + *

+ * 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 getRegistered() { + return usedStats; + } + @Nullable public StatInfo getInfo(String stat) { return loaded.get(stat); @@ -42,20 +67,15 @@ public class StatManager implements MMOCoreManager { public void registerDefaultFormula(String stat, LinearValue defaultFormula) { compute(stat).defaultInfo = defaultFormula; + usedStats.add(stat); } /** - * @return A stat info for the specified stat. If it doesn't - * exist when method is called, it is registered into the map + * @return A stat info for the specified stat. If it doesn't exist + * when method is called, it is registered into the map */ @NotNull private StatInfo compute(String stat) { - StatInfo found = loaded.get(stat); - if (found != null) - return found; - - StatInfo newInfo = new StatInfo(stat); - loaded.put(stat, newInfo); - return newInfo; + return loaded.computeIfAbsent(stat, StatInfo::new); } } diff --git a/src/main/resources/default/gui/player-stats.yml b/src/main/resources/default/gui/player-stats.yml index 8a635c5d..de1d38be 100644 --- a/src/main/resources/default/gui/player-stats.yml +++ b/src/main/resources/default/gui/player-stats.yml @@ -221,7 +221,7 @@ items: - '' - '✤ 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)' - - '✤ 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: slots: [34] function: stats diff --git a/src/main/resources/default/stats.yml b/src/main/resources/default/stats.yml index 80567133..39b0382b 100644 --- a/src/main/resources/default/stats.yml +++ b/src/main/resources/default/stats.yml @@ -1,3 +1,4 @@ +# Caution, these are very case sensitive! default: ATTACK_DAMAGE: base: 1 @@ -26,7 +27,7 @@ default: HEALTH_REGENERATION: base: .1 per-level: 0 - + # Max Resource MAX_MANA: base: 20 @@ -37,7 +38,7 @@ default: MAX_STELLIUM: base: 20 per-level: 0 - + # Resource Regeneration MANA_REGENERATION: base: .166 @@ -48,12 +49,12 @@ default: STELLIUM_REGENERATION: base: .01 per-level: 0 - + # Increases main class experience earned. ADDITIONAL_EXPERIENCE: base: 0 per-level: 0 - + # Reduces skill cooldowns by X% COOLDOWN_REDUCTION: base: 0 @@ -63,32 +64,32 @@ default: CHANCE: base: 0 per-level: 0 - + # Dealt by skills SKILL_DAMAGE: base: 0 per-level: 0 - + # Physical skill damage PHYSICAL_DAMAGE: base: 0 per-level: 0 - + # Magic skill damage MAGIC_DAMAGE: base: 0 per-level: 0 - + # Dealt by any weapon WEAPON_DAMAGE: base: 0 per-level: 0 - + # Dealt by projectile skills or weapons PROJECTILE_DAMAGE: base: 0 per-level: 0 - + # Reduces the amount of tugs needed to fish fishes. # (When set to 30, 30% of the tugs will be removed). FISHING_STRENGTH: @@ -96,14 +97,14 @@ default: per-level: 0.3 min: 0 max: 40 - + # Chance of instantly fishing. CRITICAL_FISHING_CHANCE: base: 5 per-level: 0 min: 0 max: 70 - + # Chance of being dragged in the waters # by the fish when trying to catch it, # once the player has NOT successfully