Merging expDispensers with expObjects

This commit is contained in:
Jules 2022-04-24 08:42:27 +02:00
parent 958141f07b
commit 41ac1e95ac
9 changed files with 93 additions and 101 deletions

View File

@ -8,6 +8,7 @@ import io.lumine.mythic.lib.api.MMOLineConfig;
import io.lumine.mythic.lib.api.util.PostLoadObject;
import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.event.EventTrigger;
import net.Indyuce.mmocore.api.player.profess.resource.ManaDisplayOptions;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
@ -15,19 +16,18 @@ import net.Indyuce.mmocore.api.player.profess.resource.ResourceRegeneration;
import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
import net.Indyuce.mmocore.experience.EXPSource;
import net.Indyuce.mmocore.loot.chest.particle.CastingParticle;
import net.Indyuce.mmocore.experience.ExpCurve;
import net.Indyuce.mmocore.experience.ExperienceObject;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import net.Indyuce.mmocore.experience.dispenser.ExperienceDispenser;
import net.Indyuce.mmocore.experience.dispenser.ClassExperienceDispenser;
import net.Indyuce.mmocore.experience.source.type.ExperienceSource;
import net.Indyuce.mmocore.player.playerclass.ClassTrigger;
import net.Indyuce.mmocore.player.playerclass.ClassTriggerType;
import net.Indyuce.mmocore.skill.ClassSkill;
import net.Indyuce.mmocore.skill.RegisteredSkill;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
@ -156,10 +156,9 @@ public class PlayerClass extends PostLoadObject implements ExperienceObject {
}
if (config.contains("main-exp-sources")) {
ExperienceDispenser dispenser = new ClassExperienceDispenser(this);
for (String key : config.getStringList("main-exp-sources"))
try {
ExperienceSource<?> source = MMOCore.plugin.loadManager.loadExperienceSource(new MMOLineConfig(key), dispenser);
ExperienceSource<?> source = MMOCore.plugin.loadManager.loadExperienceSource(new MMOLineConfig(key), this);
MMOCore.plugin.experience.registerSource(source);
} catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load exp source '" + key + "' from class '"
@ -389,11 +388,6 @@ public class PlayerClass extends PostLoadObject implements ExperienceObject {
return stats.containsKey(type) ? stats.get(type) : type.getDefault();
}
@Override
public boolean equals(Object obj) {
return obj instanceof PlayerClass && ((PlayerClass) obj).id.equals(id);
}
public String getActionBar() {
return actionBarFormat;
}
@ -401,4 +395,29 @@ public class PlayerClass extends PostLoadObject implements ExperienceObject {
public boolean hasActionBar() {
return actionBarFormat != null;
}
@Override
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) {
hologramLocation = !MMOCore.plugin.getConfig().getBoolean("display-main-class-exp-holograms") ? null
: hologramLocation == null ? getPlayerLocation(playerData) : hologramLocation;
playerData.giveExperience(experience, source, hologramLocation, true);
}
@Override
public boolean shouldHandle(PlayerData playerData) {
return equals(playerData.getProfess());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlayerClass that = (PlayerClass) o;
return id.equals(that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@ -1,8 +1,6 @@
package net.Indyuce.mmocore.api.quest.trigger;
import net.Indyuce.mmocore.experience.dispenser.ExperienceDispenser;
import net.Indyuce.mmocore.experience.dispenser.ProfessionExperienceDispenser;
import net.Indyuce.mmocore.experience.dispenser.SimpleExperienceDispenser;
import net.Indyuce.mmocore.experience.ExperienceObject;
import org.apache.commons.lang.Validate;
import net.Indyuce.mmocore.MMOCore;
@ -11,14 +9,17 @@ import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.math.formula.RandomAmount;
import io.lumine.mythic.lib.api.MMOLineConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
public class ExperienceTrigger extends Trigger {
@NotNull
private final RandomAmount amount;
@NotNull
private final EXPSource source;
@NotNull
private final ExperienceDispenser dispenser;
@Nullable
private final ExperienceObject expObject;
public ExperienceTrigger(MMOLineConfig config) {
super(config);
@ -28,15 +29,15 @@ public class ExperienceTrigger extends Trigger {
if (config.contains("profession")) {
String id = config.getString("profession").toLowerCase().replace("_", "-");
Validate.isTrue(MMOCore.plugin.professionManager.has(id), "Could not find profession");
dispenser = new ProfessionExperienceDispenser(MMOCore.plugin.professionManager.get(id));
expObject = MMOCore.plugin.professionManager.get(id);
} else
dispenser = new SimpleExperienceDispenser();
expObject = null;
amount = new RandomAmount(config.getString("amount"));
source = config.contains("source") ? EXPSource.valueOf(config.getString("source").toUpperCase()) : EXPSource.QUEST;
}
@Override
public void apply(PlayerData player) {
dispenser.giveExperience(player, amount.calculateInt(), null, source);
Objects.requireNonNullElse(expObject, player.getProfess()).giveExperience(player, amount.calculateInt(), null, source);
}
}

View File

@ -1,6 +1,10 @@
package net.Indyuce.mmocore.experience;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.experience.dispenser.ExperienceDispenser;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
@ -10,7 +14,7 @@ import javax.annotation.Nullable;
*
* @author jules
*/
public interface ExperienceObject {
public interface ExperienceObject extends ExperienceDispenser {
String getKey();
@ -22,11 +26,40 @@ public interface ExperienceObject {
@Nullable
ExpCurve getExpCurve();
boolean hasExperienceTable();
/**
* @return Table read when leveling up
*/
@NotNull
ExperienceTable getExperienceTable();
boolean hasExperienceTable();
/**
* Called when experience is gained in main class/profession
*
* @param playerData Player gaining the experience
* @param experience Experience gained. Note that it is a double
* because it gets converted to an integer at
* the very last moment in MMOCore
* @param hologramLocation Location of displayed hologram. When set to null
* and if exp holograms are enabled it will take the
* player's location instead.
* @param source Why the EXP was gained
*/
void giveExperience(PlayerData playerData, double experience, @org.jetbrains.annotations.Nullable Location hologramLocation, @NotNull EXPSource source);
/**
* Experience sources handle both CLASS experience sources and PROFESSION
* experience sources. Professions have no problem because whatever
* class the player has chosen, he can get exp in that profession.
* <p>
* But class experience sources must first make sure that the player has
* the right class before giving exp to the player
*/
boolean shouldHandle(PlayerData playerData);
@Nullable
default Location getPlayerLocation(PlayerData player) {
return player.isOnline() ? MMOCoreUtils.getCenterLocation(player.getPlayer()) : null;
}
}

View File

@ -3,14 +3,15 @@ package net.Indyuce.mmocore.experience;
import io.lumine.mythic.lib.api.MMOLineConfig;
import io.lumine.mythic.lib.api.util.PostLoadObject;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import net.Indyuce.mmocore.experience.dispenser.ExperienceDispenser;
import net.Indyuce.mmocore.experience.dispenser.ProfessionExperienceDispenser;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
@ -64,10 +65,9 @@ public class Profession extends PostLoadObject implements ExperienceObject {
maxLevel = config.getInt("max-level");
if (config.contains("exp-sources")) {
ExperienceDispenser dispenser = new ProfessionExperienceDispenser(this);
for (String key : config.getStringList("exp-sources"))
try {
MMOCore.plugin.experience.registerSource(MMOCore.plugin.loadManager.loadExperienceSource(new MMOLineConfig(key), dispenser));
MMOCore.plugin.experience.registerSource(MMOCore.plugin.loadManager.loadExperienceSource(new MMOLineConfig(key), this));
} catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not register exp source '" + key + "' from profession '" + id + "': " + exception.getMessage());
@ -124,6 +124,18 @@ public class Profession extends PostLoadObject implements ExperienceObject {
return Objects.requireNonNull(expTable, "Profession has no exp table");
}
@Override
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) {
hologramLocation = !getOption(Profession.ProfessionOption.EXP_HOLOGRAMS) ? null
: hologramLocation == null ? getPlayerLocation(playerData) : hologramLocation;
playerData.getCollectionSkills().giveExperience(this, experience, EXPSource.SOURCE, hologramLocation);
}
@Override
public boolean shouldHandle(PlayerData playerData) {
return true;
}
public static enum ProfessionOption {
/**

View File

@ -1,28 +0,0 @@
package net.Indyuce.mmocore.experience.dispenser;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.experience.EXPSource;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;
public class ClassExperienceDispenser implements ExperienceDispenser {
private final PlayerClass profess;
public ClassExperienceDispenser(PlayerClass profess) {
this.profess = profess;
}
@Override
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) {
hologramLocation = !MMOCore.plugin.getConfig().getBoolean("display-main-class-exp-holograms") ? null
: hologramLocation == null ? getPlayerLocation(playerData) : hologramLocation;
playerData.giveExperience(experience, source, hologramLocation, true);
}
@Override
public boolean shouldHandle(PlayerData playerData) {
return playerData.getProfess().equals(profess);
}
}

View File

@ -11,7 +11,10 @@ import org.jetbrains.annotations.Nullable;
* Used to differenciate between the main class experience and
* experience given in a specific profession. Also being used to
* monitor EXP holograms.
*
* @deprecated Merged with {@link net.Indyuce.mmocore.experience.ExperienceObject}
*/
@Deprecated
public interface ExperienceDispenser {
/**

View File

@ -1,27 +0,0 @@
package net.Indyuce.mmocore.experience.dispenser;
import net.Indyuce.mmocore.experience.EXPSource;
import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;
public class ProfessionExperienceDispenser implements ExperienceDispenser {
private final Profession profession;
public ProfessionExperienceDispenser(Profession profession) {
this.profession = profession;
}
@Override
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) {
hologramLocation = !profession.getOption(Profession.ProfessionOption.EXP_HOLOGRAMS) ? null
: hologramLocation == null ? getPlayerLocation(playerData) : hologramLocation;
playerData.getCollectionSkills().giveExperience(profession, experience, EXPSource.SOURCE, hologramLocation);
}
@Override
public boolean shouldHandle(PlayerData playerData) {
return true;
}
}

View File

@ -1,22 +0,0 @@
package net.Indyuce.mmocore.experience.dispenser;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.experience.EXPSource;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;
public class SimpleExperienceDispenser implements ExperienceDispenser {
@Override
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) {
hologramLocation = !MMOCore.plugin.getConfig().getBoolean("display-main-class-exp-holograms") ? null
: hologramLocation == null ? getPlayerLocation(playerData) : hologramLocation;
playerData.giveExperience(experience, source, hologramLocation, true);
}
@Override
public boolean shouldHandle(PlayerData playerData) {
return true;
}
}

View File

@ -41,6 +41,7 @@ public class MMOLoadManager {
return load(Objective.class, config, loader -> loader.loadObjective(config, section));
}
@Deprecated
public ExperienceSource<?> loadExperienceSource(MMOLineConfig config, ExperienceDispenser dispenser) {
return load(ExperienceSource.class, config, loader -> loader.loadExperienceSource(config, dispenser));
}