Attributes now support non basic stats

This commit is contained in:
Jules 2020-02-19 20:03:14 +01:00
parent 1c1bbcd20c
commit 1d79d80199
5 changed files with 25 additions and 19 deletions

View File

@ -2,6 +2,7 @@ package net.Indyuce.mmocore.api.player.attribute;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
@ -10,13 +11,17 @@ import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.stats.StatType;
import net.mmogroup.mmolib.api.stat.modifier.StatModifier; import net.mmogroup.mmolib.api.stat.modifier.StatModifier;
public class PlayerAttribute { public class PlayerAttribute {
private final String id, name; private final String id, name;
private final int max; private final int max;
private final Map<StatType, StatModifier> buffs = new HashMap<>();
/*
* used to store stats using StatType, but attributes also need to access non
* basic MMOCore stats hence the string maps keys
*/
private final Map<String, StatModifier> buffs = new HashMap<>();
public PlayerAttribute(ConfigurationSection config) { public PlayerAttribute(ConfigurationSection config) {
Validate.notNull(config, "Could not load config"); Validate.notNull(config, "Could not load config");
@ -30,10 +35,11 @@ public class PlayerAttribute {
if (config.contains("buff")) if (config.contains("buff"))
for (String key : config.getConfigurationSection("buff").getKeys(false)) for (String key : config.getConfigurationSection("buff").getKeys(false))
try { try {
StatType stat = StatType.valueOf(key.toUpperCase().replace("-", "_").replace(" ", "_")); String stat = key.toUpperCase().replace("-", "_").replace(" ", "_");
buffs.put(stat, new StatModifier(config.getString("buff." + key))); buffs.put(stat, new StatModifier(config.getString("buff." + key)));
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "[PlayerAttributes:" + id + "] Could not load buff '" + key + "': " + exception.getMessage()); MMOCore.log(Level.WARNING,
"Could not load buff '" + key + "' from attribute '" + id + "': " + exception.getMessage());
} }
} }
@ -53,11 +59,7 @@ public class PlayerAttribute {
return max; return max;
} }
public Set<StatType> getStats() { public Set<Entry<String, StatModifier>> getBuffs() {
return buffs.keySet(); return buffs.entrySet();
}
public StatModifier getBuff(StatType stat) {
return buffs.get(stat);
} }
} }

View File

@ -156,7 +156,7 @@ public class PlayerAttributes {
public void update() { public void update() {
PlayerAttribute attribute = MMOCore.plugin.attributeManager.get(id); PlayerAttribute attribute = MMOCore.plugin.attributeManager.get(id);
int total = getTotal(); int total = getTotal();
attribute.getStats().forEach(stat -> data.getStats().getInstance(stat).addModifier("attribute." + attribute.getId(), attribute.getBuff(stat).multiply(total))); attribute.getBuffs().forEach(buff -> data.getStats().getInstance(buff.getKey()).addModifier("attribute." + attribute.getId(), buff.getValue().multiply(total)));
} }
public String getId() { public String getId() {

View File

@ -36,7 +36,11 @@ public class PlayerStats {
} }
public StatInstance getInstance(StatType stat) { public StatInstance getInstance(StatType stat) {
return map.getInstance(stat.name()); return getInstance(stat.name());
}
public StatInstance getInstance(String stat) {
return map.getInstance(stat);
} }
/* /*

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.gui; package net.Indyuce.mmocore.gui;
import java.util.Map.Entry;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
@ -8,7 +10,6 @@ import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute; import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttributes.AttributeInstance; import net.Indyuce.mmocore.api.player.attribute.PlayerAttributes.AttributeInstance;
import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.gui.api.EditableInventory; import net.Indyuce.mmocore.gui.api.EditableInventory;
import net.Indyuce.mmocore.gui.api.GeneratedInventory; import net.Indyuce.mmocore.gui.api.GeneratedInventory;
import net.Indyuce.mmocore.gui.api.PluginInventory; import net.Indyuce.mmocore.gui.api.PluginInventory;
@ -59,15 +60,14 @@ public class AttributeView extends EditableInventory {
Placeholders holders = new Placeholders(); Placeholders holders = new Placeholders();
holders.register("name", attribute.getName()); holders.register("name", attribute.getName());
holders.register("buffs", attribute.getStats().size()); holders.register("buffs", attribute.getBuffs().size());
holders.register("spent", inv.getPlayerData().getAttributes().getInstance(attribute).getBase()); holders.register("spent", inv.getPlayerData().getAttributes().getInstance(attribute).getBase());
holders.register("max", attribute.getMax()); holders.register("max", attribute.getMax());
holders.register("current", total); holders.register("current", total);
holders.register("attribute_points", inv.getPlayerData().getAttributePoints()); holders.register("attribute_points", inv.getPlayerData().getAttributePoints());
for (StatType stat : attribute.getStats()) { for (Entry<String, StatModifier> entry : attribute.getBuffs()) {
StatModifier buff = attribute.getBuff(stat); holders.register("buff_" + entry.getKey().toLowerCase(), entry.getValue());
holders.register("buff_" + stat.name().toLowerCase(), buff); holders.register("total_" + entry.getKey().toLowerCase(), entry.getValue().multiply(total));
holders.register("total_" + stat.name().toLowerCase(), buff.multiply(total));
} }
return holders; return holders;
} }

View File

@ -33,7 +33,7 @@ public class AttributeManager extends MMOManager {
String path = key.toLowerCase().replace("_", "-").replace(" ", "-"); String path = key.toLowerCase().replace("_", "-").replace(" ", "-");
map.put(path, new PlayerAttribute(config.getConfig().getConfigurationSection(key))); map.put(path, new PlayerAttribute(config.getConfig().getConfigurationSection(key)));
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "[PlayerAttributes] Could not load '" + key + "': " + exception.getMessage()); MMOCore.log(Level.WARNING, "Could not load attribute '" + key + "': " + exception.getMessage());
} }
} }