From 6f749e268443ada8fb1c44a31dfadcf2a0732bef Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sat, 15 Aug 2020 13:39:55 +0200 Subject: [PATCH] Added %mmocore_exp__
% --- .../mmocore/api/experience/Booster.java | 90 ++++++++++++------- .../comp/placeholder/RPGPlaceholders.java | 29 +++--- .../manager/social/BoosterManager.java | 46 +++++++--- 3 files changed, 108 insertions(+), 57 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/experience/Booster.java b/src/main/java/net/Indyuce/mmocore/api/experience/Booster.java index 5aa99ed2..a016badf 100644 --- a/src/main/java/net/Indyuce/mmocore/api/experience/Booster.java +++ b/src/main/java/net/Indyuce/mmocore/api/experience/Booster.java @@ -9,18 +9,44 @@ public class Booster { private final double extra; private final String author; - // length not final because boosters can stack, this allows to reduce the - // amount of boosters + /** + * Length is not final because boosters can stacks. This allows to reduce + * the amount of boosters displayed in the main player menu + */ private long length; + /** + * @param extra + * 1 for +100% experience, 3 for 300% etc. + * @param length + * Booster length in milliseconds + */ public Booster(double extra, long length) { this(null, null, extra, length); } + /** + * @param author + * The booster creator + * @param extra + * 1 for +100% experience, 3 for 300% etc. + * @param length + * Booster length in milliseconds + */ public Booster(String author, double extra, long length) { this(author, null, extra, length); } + /** + * @param author + * The booster creator + * @param profession + * Either null for main level boosters or a specific profession + * @param extra + * 1 for +100% experience, 3 for 300% etc. + * @param length + * Booster length in milliseconds + */ public Booster(String author, Profession profession, double extra, long length) { this.author = author; this.length = length * 1000; @@ -28,42 +54,10 @@ public class Booster { this.extra = extra; } - public boolean isTimedOut() { - return date + length < System.currentTimeMillis(); - } - - public long getLeft() { - return Math.max(0, date + length - System.currentTimeMillis()); - } - - public long getCreationDate() { - return date; - } - - public long getLength() { - return length; - } - - public void addLength(long length) { - this.length += length; - } - - public boolean hasProfession() { - return profession != null; - } - - public Profession getProfession() { - return profession; - } - public UUID getUniqueId() { return uuid; } - public double calculateExp(double exp) { - return exp * (1 + extra); - } - public double getExtra() { return extra; } @@ -76,6 +70,34 @@ public class Booster { return author; } + public long getCreationDate() { + return date; + } + + public boolean hasProfession() { + return profession != null; + } + + public Profession getProfession() { + return profession; + } + + public boolean isTimedOut() { + return date + length < System.currentTimeMillis(); + } + + public long getLeft() { + return Math.max(0, date + length - System.currentTimeMillis()); + } + + public long getLength() { + return length; + } + + public void addLength(long length) { + this.length += length; + } + public boolean canStackWith(Booster booster) { return extra == booster.extra && (profession != null ? profession.equals(booster.getProfession()) : booster.getProfession() == null); } diff --git a/src/main/java/net/Indyuce/mmocore/comp/placeholder/RPGPlaceholders.java b/src/main/java/net/Indyuce/mmocore/comp/placeholder/RPGPlaceholders.java index a6710cd6..1a276b33 100644 --- a/src/main/java/net/Indyuce/mmocore/comp/placeholder/RPGPlaceholders.java +++ b/src/main/java/net/Indyuce/mmocore/comp/placeholder/RPGPlaceholders.java @@ -100,16 +100,27 @@ public class RPGPlaceholders extends PlaceholderExpansion { return "" + PlayerData.get(player).getAttributeReallocationPoints(); else if (identifier.startsWith("attribute_")) - return String.valueOf(PlayerData.get(player).getAttributes().getAttribute( - MMOCore.plugin.attributeManager.get(identifier.substring(10).toLowerCase().replace("_", "-")))); + return String.valueOf(PlayerData.get(player).getAttributes() + .getAttribute(MMOCore.plugin.attributeManager.get(identifier.substring(10).toLowerCase().replace("_", "-")))); else if (identifier.equals("mana")) return MMOCore.plugin.configManager.decimal.format(PlayerData.get(player).getMana()); else if (identifier.equals("mana_bar")) { PlayerData data = PlayerData.get(player); - return data.getProfess().getManaDisplay().generateBar(data.getMana(), - data.getStats().getStat(StatType.MAX_MANA)); + return data.getProfess().getManaDisplay().generateBar(data.getMana(), data.getStats().getStat(StatType.MAX_MANA)); + } + + else if (identifier.startsWith("exp_multiplier_")) { + String format = identifier.substring(15).toLowerCase().replace("_", "-").replace(" ", "-"); + Profession profession = format.equals("main") ? null : MMOCore.plugin.professionManager.get(format); + return MMOCore.plugin.configManager.decimal.format(MMOCore.plugin.boosterManager.getMultiplier(profession) * 100); + } + + else if (identifier.startsWith("exp_boost_")) { + String format = identifier.substring(10).toLowerCase().replace("_", "-").replace(" ", "-"); + Profession profession = format.equals("main") ? null : MMOCore.plugin.professionManager.get(format); + return MMOCore.plugin.configManager.decimal.format((MMOCore.plugin.boosterManager.getMultiplier(profession) - 1) * 100); } else if (identifier.equals("stamina")) @@ -121,8 +132,7 @@ public class RPGPlaceholders extends PlaceholderExpansion { double ratio = 20 * data.getStamina() / data.getStats().getStat(StatType.MAX_STAMINA); for (double j = 1; j < 20; j++) format += (ratio >= j ? MMOCore.plugin.configManager.staminaFull - : ratio >= j - .5 ? MMOCore.plugin.configManager.staminaHalf - : MMOCore.plugin.configManager.staminaEmpty) + : ratio >= j - .5 ? MMOCore.plugin.configManager.staminaHalf : MMOCore.plugin.configManager.staminaEmpty) + AltChar.listSquare; return format; } @@ -140,8 +150,7 @@ public class RPGPlaceholders extends PlaceholderExpansion { PlayerData data = PlayerData.get(player); double ratio = 20 * data.getStellium() / data.getStats().getStat(StatType.MAX_STELLIUM); for (double j = 1; j < 20; j++) - format += (ratio >= j ? ChatColor.BLUE : ratio >= j - .5 ? ChatColor.AQUA : ChatColor.WHITE) - + AltChar.listSquare; + format += (ratio >= j ? ChatColor.BLUE : ratio >= j - .5 ? ChatColor.AQUA : ChatColor.WHITE) + AltChar.listSquare; return format; } @@ -153,8 +162,8 @@ public class RPGPlaceholders extends PlaceholderExpansion { else if (identifier.equals("quest_progress")) { PlayerQuests data = PlayerData.get(player).getQuestData(); return data.hasCurrent() - ? MMOCore.plugin.configManager.decimal.format((int) (double) data.getCurrent().getObjectiveNumber() - / data.getCurrent().getQuest().getObjectives().size() * 100) + ? MMOCore.plugin.configManager.decimal + .format((int) (double) data.getCurrent().getObjectiveNumber() / data.getCurrent().getQuest().getObjectives().size() * 100) : "0"; } diff --git a/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java b/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java index 5bfcd259..7639b049 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java @@ -8,11 +8,19 @@ import net.Indyuce.mmocore.api.experience.Booster; import net.Indyuce.mmocore.api.experience.Profession; public class BoosterManager { - private List map = new ArrayList<>(); + private final List map = new ArrayList<>(); + /** + * If MMOCore can find a booster with the same profession and value, the two + * boosters will stack to reduce the amount of boosters displayed at the + * same time. Otherwise, booster is registered + * + * @param booster + * Booster to register + */ public void register(Booster booster) { - // always flush booster list to reduce future calculations + // flushes booster list to reduce future calculations flush(); for (Booster active : map) @@ -24,6 +32,13 @@ public class BoosterManager { map.add(booster); } + public Booster get(int index) { + return map.get(index); + } + + /** + * Cleans timed out boosters from the MMOCore registry + */ public void flush() { for (Iterator iterator = map.iterator(); iterator.hasNext();) { Booster next = iterator.next(); @@ -32,23 +47,28 @@ public class BoosterManager { } } - public int calculateExp(Profession profession, double exp) { - flush(); + /** + * @return Sums all current experience boosters values + */ + public double getMultiplier(Profession profession) { + double d = 1; for (Booster booster : map) - if (booster.getProfession() == profession) - exp = booster.calculateExp(exp); + if (booster.getProfession() == profession && !booster.isTimedOut()) + d += booster.getExtra(); - return (int) exp; + return d; } - public List getBoosters() { - flush(); + public int calculateExp(Profession profession, double exp) { + return (int) (exp * getMultiplier(profession)); + } + /** + * @return Collection of currently registered boosters. Some of them can be + * expired but are not unregistered yet! + */ + public List getBoosters() { return map; } - - public Booster get(int index) { - return map.get(index); - } }