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 69670310..5aa99ed2 100644 --- a/src/main/java/net/Indyuce/mmocore/api/experience/Booster.java +++ b/src/main/java/net/Indyuce/mmocore/api/experience/Booster.java @@ -4,11 +4,15 @@ import java.util.UUID; public class Booster { private final UUID uuid = UUID.randomUUID(); - private final long date = System.currentTimeMillis(), length; + private final long date = System.currentTimeMillis(); private final Profession profession; private final double extra; private final String author; + // length not final because boosters can stack, this allows to reduce the + // amount of boosters + private long length; + public Booster(double extra, long length) { this(null, null, extra, length); } @@ -40,6 +44,10 @@ public class Booster { return length; } + public void addLength(long length) { + this.length += length; + } + public boolean hasProfession() { return profession != null; } @@ -67,4 +75,8 @@ public class Booster { public String getAuthor() { return author; } + + 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/manager/social/BoosterManager.java b/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java index cc24a641..5bfcd259 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/social/BoosterManager.java @@ -11,7 +11,16 @@ public class BoosterManager { private List map = new ArrayList<>(); public void register(Booster booster) { + + // always flush booster list to reduce future calculations flush(); + + for (Booster active : map) + if (active.canStackWith(booster)) { + active.addLength(booster.getLength()); + return; + } + map.add(booster); } @@ -25,17 +34,20 @@ public class BoosterManager { public int calculateExp(Profession profession, double exp) { flush(); + for (Booster booster : map) if (booster.getProfession() == profession) exp = booster.calculateExp(exp); + return (int) exp; } public List getBoosters() { flush(); + return map; } - + public Booster get(int index) { return map.get(index); }