Fixed extra attributes not working on MI

This commit is contained in:
Indyuce 2022-11-29 22:02:45 +01:00
parent a64a2d0ab8
commit 4cb58398b0
3 changed files with 249 additions and 191 deletions

View File

@ -0,0 +1,44 @@
package net.Indyuce.mmocore.api.player.attribute;
import io.lumine.mythic.lib.api.stat.StatMap;
import io.lumine.mythic.lib.api.stat.handler.StatHandler;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
/**
* This fixes an issue where registering new stat modifiers in ML
* to add extra attribute points does NOT update the stats granted
* by the attribute.
* <p>
* This stat handler MAY call subsequent stat handlers. There might
* be infinite recursion problems if another attr. grants extra attribute pts.
*/
public class MMOCoreAttributeStatHandler implements StatHandler {
private final PlayerAttribute attr;
private final String statName;
public MMOCoreAttributeStatHandler(PlayerAttribute attr) {
this.attr = attr;
this.statName = "ADDITIONAL_" + attr.getId().toUpperCase().replace("-", "_");
}
public String getStat() {
return statName;
}
@Override
public void runUpdate(StatMap statMap) {
final PlayerData playerData = MMOCore.plugin.dataProvider.getDataManager().get(statMap.getPlayerData().getUniqueId());
playerData.getAttributes().getInstance(attr).updateStats();
}
@Override
public double getBaseValue(StatMap statMap) {
return 0;
}
@Override
public double getTotalValue(StatMap statMap) {
return statMap.getStat(statName);
}
}

View File

@ -132,7 +132,7 @@ public class PlayerAttributes {
spent = Math.max(0, value);
if (data.isOnline())
update();
updateStats();
}
public void addBase(int value) {
@ -174,9 +174,13 @@ public class PlayerAttributes {
}
public AttributeModifier addModifier(AttributeModifier modifier) {
AttributeModifier mod = map.put(modifier.getKey(), modifier);
update();
return mod;
final AttributeModifier current = map.put(modifier.getKey(), modifier);
if (current != null && current instanceof Closeable)
((Closeable) current).close();
updateStats();
return current;
}
public Set<String> getKeys() {
@ -188,7 +192,7 @@ public class PlayerAttributes {
}
public AttributeModifier removeModifier(String key) {
AttributeModifier mod = map.remove(key);
final AttributeModifier mod = map.remove(key);
/*
* Closing stat is really important with temporary stats because
@ -198,14 +202,14 @@ public class PlayerAttributes {
if (mod != null) {
if (mod instanceof Closeable)
((Closeable) mod).close();
update();
updateStats();
}
return mod;
}
public void update() {
PlayerAttribute attr = MMOCore.plugin.attributeManager.get(id);
int total = getTotal();
public void updateStats() {
final PlayerAttribute attr = MMOCore.plugin.attributeManager.get(id);
final int total = getTotal();
attr.getBuffs().forEach(buff -> buff.multiply(total).register(data.getMMOPlayerData()));
}

View File

@ -1,16 +1,18 @@
package net.Indyuce.mmocore.manager;
import io.lumine.mythic.lib.MythicLib;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.player.attribute.MMOCoreAttributeStatHandler;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
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.logging.Level;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
import net.Indyuce.mmocore.api.ConfigFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class AttributeManager implements MMOCoreManager {
private final Map<String, PlayerAttribute> map = new HashMap<>();
@ -30,10 +32,12 @@ public class AttributeManager implements MMOCoreManager {
@Override
public void initialize(boolean clearBefore) {
if (clearBefore)
if (clearBefore) {
map.clear();
MythicLib.plugin.getStats().clearRegisteredStats(handler -> handler instanceof MMOCoreAttributeStatHandler);
}
ConfigFile config = new ConfigFile("attributes");
final ConfigFile config = new ConfigFile("attributes");
for (String key : config.getConfig().getKeys(false))
try {
String path = key.toLowerCase().replace("_", "-").replace(" ", "-");
@ -41,5 +45,11 @@ public class AttributeManager implements MMOCoreManager {
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load attribute '" + key + "': " + exception.getMessage());
}
for (PlayerAttribute attr : getAll()) {
final MMOCoreAttributeStatHandler handler = new MMOCoreAttributeStatHandler(attr);
MythicLib.plugin.getStats().registerStat(handler.getStat(), handler);
MythicLib.plugin.getStats().registerStat(handler.getStat() + "_PERCENT", handler);
}
}
}