Stat formatting moved to MythicLib

This commit is contained in:
Indyuce 2022-07-29 00:52:57 +02:00
parent 79fb516f2e
commit 7d6e0b74cc
8 changed files with 35 additions and 64 deletions

View File

@ -1,6 +1,7 @@
package net.Indyuce.mmocore.api;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.manager.StatManager;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerActivity;
import net.Indyuce.mmocore.api.player.PlayerData;
@ -40,17 +41,17 @@ public class PlayerActionBar extends BukkitRunnable {
data.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(MMOCore.plugin.placeholderParser.parse(data.getPlayer(),
MythicLib.plugin.parseColors((data.getProfess().hasActionBar() ? data.getProfess().getActionBar() : config.format)
.replace("{health}", digit.format(data.getPlayer().getHealth()))
.replace("{max_health}", StatInfo.valueOf("MAX_HEALTH").format(data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()))
.replace("{max_health}", StatManager.format("MAX_HEALTH", data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()))
.replace("{mana_icon}", data.getProfess().getManaDisplay().getIcon())
.replace("{mana}", digit.format(data.getMana()))
.replace("{max_mana}", StatInfo.valueOf("MAX_MANA").format(data.getStats().getStat("MAX_MANA")))
.replace("{max_mana}", StatManager.format("MAX_MANA", data.getStats().getStat("MAX_MANA")))
.replace("{stamina}", digit.format(data.getStamina()))
.replace("{max_stamina}", StatInfo.valueOf("MAX_STAMINA").format(data.getStats().getStat("MAX_STAMINA")))
.replace("{max_stamina}", StatManager.format("MAX_STAMINA", data.getStats().getStat("MAX_STAMINA")))
.replace("{stellium}", digit.format(data.getStellium()))
.replace("{max_stellium}", StatInfo.valueOf("MAX_STELLIUM").format(data.getStats().getStat("MAX_STELLIUM")))
.replace("{max_stellium}", StatManager.format("MAX_STELLIUM", data.getStats().getStat("MAX_STELLIUM")))
.replace("{class}", data.getProfess().getName())
.replace("{xp}", MythicLib.plugin.getMMOConfig().decimal.format(data.getExperience()))
.replace("{armor}", StatInfo.valueOf("ARMOR").format(data.getPlayer().getAttribute(Attribute.GENERIC_ARMOR).getValue()))
.replace("{armor}", StatManager.format("ARMOR", data.getPlayer().getAttribute(Attribute.GENERIC_ARMOR).getValue()))
.replace("{level}", "" + data.getLevel())
.replace("{name}", data.getPlayer().getDisplayName())))));
}

View File

@ -45,7 +45,7 @@ public class PlayerStats {
}
/**
* MMOCore base stat value differs from the on in MythicLib.
* MMOCore base stat value differs from the one in MythicLib.
* <p>
* MythicLib: the base stat value is only defined for stats
* which are based on vanilla player attributes. It corresponds
@ -58,7 +58,7 @@ public class PlayerStats {
* @return MMOCore base stat value
*/
public double getBase(String stat) {
Profession profession = StatInfo.valueOf(stat).profession;
final Profession profession = StatInfo.valueOf(stat).profession;
return data.getProfess().calculateStat(stat, profession == null ? data.getLevel() : data.getCollectionSkills().getLevel(profession));
}
@ -70,17 +70,16 @@ public class PlayerStats {
* see {@link PlayerData#update()} for more info
*/
public synchronized void updateStats() {
for (StatType stat : StatType.values()) { // TODO fix
StatInstance instance = getMap().getInstance(stat.name());
for (StatInstance instance : getMap().getInstances()) {
StatInstance.ModifierPacket packet = instance.newPacket();
// Remove old stat modifiers
packet.removeIf(str -> str.equals("mmocoreClass"));
// Add newest one
double total = getBase(stat.name()) - instance.getBase();
double total = getBase(instance.getStat()) - instance.getBase();
if (total != 0)
packet.addModifier(new StatModifier("mmocoreClass", stat.name(), total, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));
packet.addModifier(new StatModifier("mmocoreClass", instance.getStat(), total, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));
// Then update the stat
packet.runUpdate();

View File

@ -2,11 +2,11 @@ package net.Indyuce.mmocore.comp.placeholder;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.manager.StatManager;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.quest.PlayerQuests;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.player.stats.StatInfo;
import net.Indyuce.mmocore.api.quest.PlayerQuests;
import net.Indyuce.mmocore.experience.PlayerProfessions;
import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.party.AbstractParty;
@ -71,13 +71,11 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return MythicLib.plugin.getMMOConfig().decimal.format(current / next * 100);
}
else if (identifier.equals("health") && player.isOnline()) {
return StatInfo.valueOf("MAX_HEALTH").format(player.getPlayer().getHealth());
}
else if (identifier.equals("health"))
return StatManager.format("MAX_HEALTH", player.getPlayer().getHealth());
else if (identifier.equals("max_health") && player.isOnline()) {
return StatInfo.valueOf("MAX_HEALTH").format(player.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
}
else if (identifier.equals("max_health"))
return StatManager.format("MAX_HEALTH", player.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
else if (identifier.equals("health_bar") && player.isOnline()) {
StringBuilder format = new StringBuilder();
@ -182,8 +180,8 @@ public class RPGPlaceholders extends PlaceholderExpansion {
}
else if (identifier.startsWith("stat_")) {
StatInfo info = StatInfo.valueOf(identifier.substring(5).toUpperCase());
return info.format(playerData.getStats().getStat(info.name));
final String stat = identifier.substring(5).toUpperCase();
return StatManager.format(identifier.substring(5), playerData.getStats().getStat(stat));
}
else if (identifier.equals("stellium"))

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.gui;
import io.lumine.mythic.lib.manager.StatManager;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.SoundEvent;
import net.Indyuce.mmocore.api.event.PlayerAttributeUseEvent;
@ -11,7 +12,6 @@ import net.Indyuce.mmocore.gui.api.GeneratedInventory;
import net.Indyuce.mmocore.gui.api.item.InventoryItem;
import net.Indyuce.mmocore.gui.api.item.Placeholders;
import net.Indyuce.mmocore.gui.api.item.SimplePlaceholderItem;
import net.Indyuce.mmocore.player.stats.StatInfo;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent;
@ -68,9 +68,9 @@ public class AttributeView extends EditableInventory {
holders.register("attribute_points", inv.getPlayerData().getAttributePoints());
holders.register("shift_points", shiftCost);
attribute.getBuffs().forEach(buff -> {
StatInfo info = StatInfo.valueOf(buff.getStat());
holders.register("buff_" + buff.getStat().toLowerCase(), info.format(buff.getValue()));
holders.register("total_" + buff.getStat().toLowerCase(), info.format(buff.multiply(total).getValue()));
final String stat = buff.getStat();
holders.register("buff_" + buff.getStat().toLowerCase(), StatManager.format(stat, buff.getValue()));
holders.register("total_" + buff.getStat().toLowerCase(), StatManager.format(stat, buff.multiply(total).getValue()));
});
return holders;

View File

@ -3,6 +3,7 @@ package net.Indyuce.mmocore.gui;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.stat.modifier.StatModifier;
import io.lumine.mythic.lib.manager.StatManager;
import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
@ -98,7 +99,7 @@ public class PlayerStats extends EditableInventory {
holders.register("percent", decimal.format(ratio * 100));
for (StatInfo stat : MMOCore.plugin.statManager.getLoaded())
if (Objects.equals(stat.profession, profession))
holders.register(stat.name.toLowerCase(), stat.format(stats.getStat(stat.name)));
holders.register(stat.name.toLowerCase(), StatManager.format(stat.name, stats.getStat(stat.name)));
return holders;
}
@ -126,17 +127,17 @@ public class PlayerStats extends EditableInventory {
final String holder = str.substring(str.indexOf("{") + 1, str.indexOf("}"));
String replaced;
if (holder.endsWith("_base")) {
StatInfo info = StatInfo.valueOf(UtilityMethods.enumName(holder.substring(0, holder.length() - 5)));
replaced = info.format(stats.getBase(info.name));
final String stat = UtilityMethods.enumName(holder.substring(0, holder.length() - 5));
replaced = StatManager.format(stat, stats.getBase(stat));
} else if (holder.endsWith("_extra")) {
StatInfo info = StatInfo.valueOf(UtilityMethods.enumName(holder.substring(0, holder.length() - 6)));
replaced = info.format(MythicLib.plugin.getStats().getTotalValue(info.name, stats.getMap()) - stats.getBase(info.name));
final String stat = UtilityMethods.enumName(holder.substring(0, holder.length() - 6));
replaced = StatManager.format(stat, MythicLib.plugin.getStats().getTotalValue(stat, stats.getMap()) - stats.getBase(stat));
} else if (holder.startsWith("attribute_")) {
PlayerAttribute attr = MMOCore.plugin.attributeManager.get(holder.substring(10).replace("_", "-").toLowerCase());
final PlayerAttribute attr = MMOCore.plugin.attributeManager.get(holder.substring(10).replace("_", "-").toLowerCase());
replaced = String.valueOf(inv.target.getAttributes().getAttribute(attr));
} else {
StatInfo info = StatInfo.valueOf(UtilityMethods.enumName(holder));
replaced = info.format(MythicLib.plugin.getStats().getTotalValue(info.name, stats.getMap()));
final String stat = UtilityMethods.enumName(holder);
replaced = StatManager.format(stat, MythicLib.plugin.getStats().getTotalValue(stat, stats.getMap()));
}
str = str.replace("{" + holder + "}", replaced);

View File

@ -1,6 +1,5 @@
package net.Indyuce.mmocore.manager;
import io.lumine.mythic.lib.MythicLib;
import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.player.stats.StatInfo;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
@ -9,7 +8,6 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@ -24,10 +22,6 @@ public class StatManager implements MMOCoreManager {
FileConfiguration config = new ConfigFile("stats").getConfig();
// Read decimal formats
for (String key : config.getConfigurationSection("decimal-format").getKeys(false))
registerDecimalFormat(key, MythicLib.plugin.getMMOConfig().newDecimalFormat(config.getString("decimal-format." + key)));
// Read default formulas
for (String key : config.getConfigurationSection("default").getKeys(false))
registerDefaultFormula(key, new LinearValue(config.getConfigurationSection("default." + key)));
@ -50,10 +44,6 @@ public class StatManager implements MMOCoreManager {
compute(stat).defaultInfo = defaultFormula;
}
public void registerDecimalFormat(String stat, DecimalFormat format) {
compute(stat).format = format;
}
/**
* @return A stat info for the specified stat. If it doesn't
* exist when method is called, it is registered into the map

View File

@ -1,11 +1,11 @@
package net.Indyuce.mmocore.player.stats;
import io.lumine.mythic.lib.manager.StatManager;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
import net.Indyuce.mmocore.experience.Profession;
import org.jetbrains.annotations.NotNull;
import java.text.DecimalFormat;
import java.util.Objects;
/**
@ -29,20 +29,14 @@ public class StatInfo {
*/
public LinearValue defaultInfo;
/**
* How that stat displays anywhere in GUIs
*/
public DecimalFormat format;
private static final DecimalFormat DEFAULT_DECIMAL_FORMAT = new DecimalFormat("0.#");
public StatInfo(String name) {
this.name = name;
}
@NotNull
@Deprecated
public String format(double d) {
return (format == null ? DEFAULT_DECIMAL_FORMAT : format).format(d);
return StatManager.format(name, d);
}
@NotNull

View File

@ -113,15 +113,3 @@ default:
per-level: -.01
min: 1
max: 100
# How much decimal places the different stats
# display in the GUIs (e.g in player stats).
# Default is "0.#" when none is specified.
# https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
decimal-format:
MOVEMENT_SPEED: '0.##'
ARMOR_TOUGHNESS: '0.###'
KNOCKBACK_RESISTANCE: '0.##'
HEALTH_REGENERATION: '0.##'
MANA_REGENERATION: '0.##'
STELLIUM_REGENERATION: '0.##'