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,16 +3,26 @@ package net.Indyuce.mmocore.api.player.attribute;
import io.lumine.mythic.lib.MythicLib; import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.stat.modifier.StatModifier; import io.lumine.mythic.lib.api.stat.modifier.StatModifier;
import net.Indyuce.mmocore.MMOCore; 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.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
public class PlayerAttribute { public class PlayerAttribute implements ExperienceObject {
private final String id, name; private final String id, name;
private final int max; private final int max;
private final ExperienceTable expTable;
/** /**
* Used to store stats using StatType, but attributes also need to access * Used to store stats using StatType, but attributes also need to access
@ -35,6 +45,16 @@ public class PlayerAttribute {
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load buff '" + key + "' from attribute '" + id + "': " + exception.getMessage()); MMOCore.log(Level.WARNING, "Could not load buff '" + key + "' from attribute '" + id + "': " + exception.getMessage());
} }
// 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 getId() { public String getId() {
@ -56,4 +76,36 @@ public class PlayerAttribute {
public Set<StatModifier> getBuffs() { public Set<StatModifier> getBuffs() {
return buffs; 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(); 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) { public void addBase(int value) {
PlayerAttribute attribute = MMOCore.plugin.attributeManager.get(id);
setBase(spent + value); setBase(spent + value);
if (attribute.hasExperienceTable())
attribute.getExperienceTable().claim(data, spent, attribute);
} }
/* /*
@ -159,14 +168,14 @@ public class PlayerAttributes {
return map.get(key); return map.get(key);
} }
public void addModifier(String key, double value) { public AttributeModifier addModifier(String key, double value) {
addModifier(new AttributeModifier(key, id, value, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER)); return addModifier(new AttributeModifier(key, id, value, ModifierType.FLAT, EquipmentSlot.OTHER, ModifierSource.OTHER));
} }
public void addModifier(AttributeModifier modifier) { public AttributeModifier addModifier(AttributeModifier modifier) {
map.put(modifier.getKey(), modifier); AttributeModifier mod = map.put(modifier.getKey(), modifier);
update(); update();
return mod;
} }
public Set<String> getKeys() { public Set<String> getKeys() {
@ -177,7 +186,7 @@ public class PlayerAttributes {
return map.containsKey(key); return map.containsKey(key);
} }
public void removeModifier(String key) { public AttributeModifier removeModifier(String key) {
AttributeModifier mod = map.remove(key); AttributeModifier mod = map.remove(key);
/* /*
@ -190,12 +199,13 @@ public class PlayerAttributes {
((Closeable) mod).close(); ((Closeable) mod).close();
update(); update();
} }
return mod;
} }
public void update() { public void update() {
PlayerAttribute attribute = MMOCore.plugin.attributeManager.get(id); PlayerAttribute attr = MMOCore.plugin.attributeManager.get(id);
int total = getTotal(); 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() { public String getId() {

View File

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