Addex exp tables to attributes

This commit is contained in:
Indyuce 2022-05-24 14:03:46 +02:00
parent f9cc6bfb33
commit 7d2af8d787
3 changed files with 111 additions and 49 deletions

View File

@ -3,57 +3,109 @@ package net.Indyuce.mmocore.api.player.attribute;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.stat.modifier.StatModifier;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.experience.EXPSource;
import net.Indyuce.mmocore.experience.ExpCurve;
import net.Indyuce.mmocore.experience.ExperienceObject;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
public class PlayerAttribute {
private final String id, name;
private final int max;
public class PlayerAttribute implements ExperienceObject {
private final String id, name;
private final int max;
private final ExperienceTable expTable;
/**
* Used to store stats using StatType, but attributes also need to access
* non basic MMOCore stats hence the string maps keys
*/
private final Set<StatModifier> buffs = new HashSet<>();
/**
* Used to store stats using StatType, but attributes also need to access
* non basic MMOCore stats hence the string maps keys
*/
private final Set<StatModifier> buffs = new HashSet<>();
public PlayerAttribute(ConfigurationSection config) {
Validate.notNull(config, "Could not load config");
id = config.getName().toLowerCase().replace("_", "-").replace(" ", "-");
public PlayerAttribute(ConfigurationSection config) {
Validate.notNull(config, "Could not load config");
id = config.getName().toLowerCase().replace("_", "-").replace(" ", "-");
name = MythicLib.plugin.parseColors(config.getString("name", "Attribute"));
max = config.contains("max-points") ? Math.max(1, config.getInt("max-points")) : 0;
name = MythicLib.plugin.parseColors(config.getString("name", "Attribute"));
max = config.contains("max-points") ? Math.max(1, config.getInt("max-points")) : 0;
if (config.contains("buff"))
for (String key : config.getConfigurationSection("buff").getKeys(false))
try {
String stat = key.toUpperCase().replace("-", "_").replace(" ", "_");
buffs.add(new StatModifier("attribute." + id, stat, config.getString("buff." + key)));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load buff '" + key + "' from attribute '" + id + "': " + exception.getMessage());
}
}
if (config.contains("buff"))
for (String key : config.getConfigurationSection("buff").getKeys(false))
try {
String stat = key.toUpperCase().replace("-", "_").replace(" ", "_");
buffs.add(new StatModifier("attribute." + id, stat, config.getString("buff." + key)));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load buff '" + key + "' from attribute '" + id + "': " + exception.getMessage());
}
public String getId() {
return id;
}
// Load exp table
ExperienceTable expTable = null;
if (config.contains("exp-table"))
try {
expTable = MMOCore.plugin.experience.loadExperienceTable(config.get("exp-table"));
} catch (RuntimeException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load exp table from class '" + id + "': " + exception.getMessage());
}
this.expTable = expTable;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public boolean hasMax() {
return max > 0;
}
public String getName() {
return name;
}
public int getMax() {
return max;
}
public boolean hasMax() {
return max > 0;
}
public Set<StatModifier> getBuffs() {
return buffs;
}
public int getMax() {
return max;
}
public Set<StatModifier> getBuffs() {
return buffs;
}
@Override
public String getKey() {
return "attribute:" + getId().replace("-", "_");
}
@NotNull
@Override
public ExperienceTable getExperienceTable() {
return Objects.requireNonNull(expTable);
}
@Override
public boolean hasExperienceTable() {
return expTable != null;
}
@Nullable
@Override
public ExpCurve getExpCurve() {
throw new RuntimeException("Attributes don't have experience");
}
@Override
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, @NotNull EXPSource source) {
throw new RuntimeException("Attributes don't have experience");
}
@Override
public boolean shouldHandle(PlayerData playerData) {
throw new RuntimeException("Attributes don't have experience");
}
}

View File

@ -129,8 +129,17 @@ public class PlayerAttributes {
update();
}
/**
* Adds X points to the base of the player attribute AND applies
* the attribute experience table.
*
* @param value Amount of attribute points spent in the attribute
*/
public void addBase(int value) {
PlayerAttribute attribute = MMOCore.plugin.attributeManager.get(id);
setBase(spent + value);
if (attribute.hasExperienceTable())
attribute.getExperienceTable().claim(data, spent, attribute);
}
/*
@ -159,14 +168,14 @@ public class PlayerAttributes {
return map.get(key);
}
public void addModifier(String key, double value) {
addModifier(new AttributeModifier(key, id, value, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));
public AttributeModifier addModifier(String key, double value) {
return addModifier(new AttributeModifier(key, id, value, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));
}
public void addModifier(AttributeModifier modifier) {
map.put(modifier.getKey(), modifier);
public AttributeModifier addModifier(AttributeModifier modifier) {
AttributeModifier mod = map.put(modifier.getKey(), modifier);
update();
return mod;
}
public Set<String> getKeys() {
@ -177,7 +186,7 @@ public class PlayerAttributes {
return map.containsKey(key);
}
public void removeModifier(String key) {
public AttributeModifier removeModifier(String key) {
AttributeModifier mod = map.remove(key);
/*
@ -190,12 +199,13 @@ public class PlayerAttributes {
((Closeable) mod).close();
update();
}
return mod;
}
public void update() {
PlayerAttribute attribute = MMOCore.plugin.attributeManager.get(id);
PlayerAttribute attr = MMOCore.plugin.attributeManager.get(id);
int total = getTotal();
attribute.getBuffs().forEach(buff -> buff.multiply(total).register(data.getMMOPlayerData()));
attr.getBuffs().forEach(buff -> buff.multiply(total).register(data.getMMOPlayerData()));
}
public String getId() {

View File

@ -36,13 +36,13 @@ public class ExperienceItem {
* where n is the amount of successive claiming fails
* @param triggers Actions cast when the exp item is claimed
*/
public ExperienceItem(String id, int period, int firstTrigger,double claimChance, double failReduction, List<Trigger> triggers) {
public ExperienceItem(String id, int period, int firstTrigger, double claimChance, double failReduction, List<Trigger> triggers) {
this.id = id;
this.period = period;
this.claimChance = claimChance;
this.failReduction = failReduction;
this.triggers = triggers;
this.firstTrigger=firstTrigger;
this.firstTrigger = firstTrigger;
}
public ExperienceItem(ConfigurationSection config) {
@ -51,7 +51,7 @@ public class ExperienceItem {
id = config.getName();
period = config.getInt("period", 0);
firstTrigger=config.getInt("first-trigger",period);
firstTrigger = config.getInt("first-trigger", period);
claimChance = config.getDouble("chance", 100) / 100;
failReduction = config.getDouble("fail-reduction", 80) / 100;
triggers = new ArrayList<>();
@ -71,7 +71,7 @@ public class ExperienceItem {
* account the randomness factor from the 'chance' parameter
*/
public boolean roll(int professionLevel, int timesCollected) {
int claimsRequired = professionLevel+1 - (firstTrigger-+timesCollected * period);
int claimsRequired = professionLevel + 1 - (firstTrigger - timesCollected * period);
if (claimsRequired < 1)
return false;