Reworked everything to be much easier to read and just over all better (didnt rewrite the ranks part tho)

This commit is contained in:
2023-06-27 04:37:56 +02:00
parent fb41198073
commit 84e306f45f
39 changed files with 1500 additions and 1708 deletions
@@ -0,0 +1,120 @@
package com.alttd.boosterapi.data;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import java.util.Objects;
public class Booster implements Comparable<Booster> {
private final UUID boosterUUID;
private final String activatorName;
private Instant startingTime;
private Duration duration;
private final BoosterType boosterType;
private final Double multiplier;
private Boolean running;
public Booster(UUID boosterUUID, BoosterType boosterType, String reason, Duration duration, double multiplier) {
this.boosterUUID = boosterUUID;
this.boosterType = boosterType;
this.activatorName = reason;
this.duration = duration;
this.multiplier = multiplier;
this.running = false;
this.startingTime = Instant.now();
}
public Booster(BoosterType type, String playerName, Duration duration, double multiplier) {
this(UUID.randomUUID(), type, playerName, duration, multiplier);
}
public Booster(UUID boosterUUID, String activatorName, BoosterType boosterType, Instant startingTime,
Duration duration, double multiplier, boolean running) {
this.boosterUUID = boosterUUID;
this.activatorName = activatorName;
this.boosterType = boosterType;
this.startingTime = startingTime;
this.duration = duration;
this.multiplier = multiplier;
this.running = running;
}
public void updateDuration() {
Instant stopTime = Instant.now();
Duration elapsedTime = Duration.between(startingTime, stopTime);
duration = duration.minus(elapsedTime);
}
public double useMultiplier(double exp) {
return exp * (multiplier + 1);
}
public UUID getBoosterUUID() {
return boosterUUID;
}
public String getActivatorName() {
return activatorName;
}
public Instant getStartingTime() {
return startingTime;
}
public Duration getDuration() {
return duration;
}
public BoosterType getBoosterType() {
return boosterType;
}
public Double getMultiplier() {
return multiplier;
}
public Boolean getRunning() {
return running;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Booster other = (Booster) o;
return Objects.equals(boosterUUID, other.boosterUUID);
}
@Override
public int hashCode() {
return Objects.hash(boosterUUID);
}
@Override
public int compareTo(@NotNull Booster other) {
int multiplierComparison = Double.compare(other.multiplier, this.multiplier);
if (multiplierComparison != 0) {
return multiplierComparison;
}
return this.duration.compareTo(other.duration);
}
@Override
public String toString() {
return "Booster{" +
"boosterUUID=" + boosterUUID +
", activatorName='" + activatorName + '\'' +
", startingTime=" + startingTime +
", duration=" + duration +
", boosterType=" + boosterType +
", multiplier=" + multiplier +
", running=" + running +
'}';
}
}
@@ -0,0 +1,111 @@
package com.alttd.boosterapi.data;
import com.alttd.boosterapi.config.BoosterFileStorage;
import java.time.Duration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class BoosterCache {
private final HashMap<BoosterType, LinkedList<Booster>> boosters = new HashMap<>();
private final BoosterFileStorage boosterFileStorage;
public BoosterCache(BoosterFileStorage boosterFileStorage) {
this.boosterFileStorage = boosterFileStorage;
reloadBoosters();
}
public synchronized void reloadBoosters() {
boosters.clear();
List<Booster> allBoosters = boosterFileStorage.reload();
for (Booster booster : allBoosters) {
LinkedList<Booster> list = boosters.getOrDefault(booster.getBoosterType(), new LinkedList<>());
list.add(booster);
boosters.put(booster.getBoosterType(), list);
}
updateOrder();
}
private void updateOrder() {
for (BoosterType boosterType : boosters.keySet()) {
updateOrder(boosterType);
}
}
private void updateOrder(BoosterType boosterType) {
if (!boosters.containsKey(boosterType)) {
return;
}
LinkedList<Booster> list = boosters.get(boosterType);
list.sort(Booster::compareTo);
}
public synchronized Optional<Booster> getActiveBooster(BoosterType boosterType) {
if (!boosters.containsKey(boosterType))
return Optional.empty();
LinkedList<Booster> list = boosters.get(boosterType);
if (list.isEmpty())
return Optional.empty();
return Optional.of(list.get(0));
}
public synchronized List<Booster> getAllActiveBoosters() {
return boosters.values().stream()
.filter(list -> !list.isEmpty())
.map(list -> list.get(0))
.collect(Collectors.toList());
}
public synchronized List<Booster> getAllQueuedBoosters() {
return boosters.values().stream()
.filter(list -> list.size() > 1)
.map(list -> list.subList(1, list.size()))
.flatMap(List::stream)
.collect(Collectors.toList());
}
public synchronized void addNewBooster(BoosterType boosterType, String activatorName, Duration duration, double multiplier) {
List<BoosterType> childBoosters = boosterType.getChildBoosters();
if (!childBoosters.isEmpty()) {
addNewBoosters(childBoosters, activatorName, duration, multiplier);
return;
}
Booster booster = new Booster(boosterType, activatorName, duration, multiplier);
LinkedList<Booster> list = boosters.getOrDefault(boosterType, new LinkedList<>());
list.addLast(booster);
boosters.put(boosterType, list);
updateOrder(boosterType);
boosterFileStorage.saveBoosters(boosters.values().stream().flatMap(List::stream).collect(Collectors.toList()));
}
private void addNewBoosters(List<BoosterType> boosterTypes, String activatorName, Duration duration, double multiplier) {
for (BoosterType boosterType : boosterTypes) {
Booster booster = new Booster(boosterType, activatorName, duration, multiplier);
LinkedList<Booster> list = boosters.getOrDefault(boosterType, new LinkedList<>());
list.addLast(booster);
boosters.put(boosterType, list);
updateOrder(boosterType);
}
boosterFileStorage.saveBoosters(boosters.values().stream().flatMap(List::stream).collect(Collectors.toList()));
}
public synchronized void finishBooster(Booster booster) {
BoosterType boosterType = booster.getBoosterType();
LinkedList<Booster> list = boosters.get(boosterType);
if (list == null)
return;
list.removeIf(filterBooster -> filterBooster.getBoosterUUID().equals(booster.getBoosterUUID()));
boosters.put(boosterType, list);
updateOrder(boosterType);
boosterFileStorage.saveBoosters(boosters.values().stream().flatMap(List::stream).collect(Collectors.toList()));
}
public synchronized void updateAndSave() {
getAllActiveBoosters().forEach(Booster::updateDuration); //TODO test if this needs to be re-added to the map (it shouldn't afaik)
boosterFileStorage.saveBoosters(boosters.values().stream().flatMap(List::stream).collect(Collectors.toList()));
}
}
@@ -0,0 +1,80 @@
package com.alttd.boosterapi.data;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public enum BoosterType {
/**
* MCMMO - implies all mcmmo skills are boosted
*/
MCMMO("mcmmo", null),
ACROBATICS("acrobatics", MCMMO),
ALCHEMY("alchemy", MCMMO),
ARCHERY("archery", MCMMO),
AXES("axes", MCMMO),
EXCAVATION("excavation", MCMMO),
FISHING("fishing", MCMMO),
HERBALISM("herbalism", MCMMO),
MINING("mining", MCMMO),
REPAIR("repair", MCMMO),
SALVAGE("salvage", MCMMO),
SMELTING("smelting", MCMMO),
SWORDS("swords", MCMMO),
TAMING("taming", MCMMO),
UNARMED("unarmed", MCMMO),
WOODCUTTING("woodcutting", MCMMO),
/**
* MYPET - Boosts MyPet exp gains
*/
MYPET("mypet", null),
/**
* VANILLAXP - increases exp gained by killing mobs
*/
VANILLAXP("vanillaxp", null),
/**
* LUCK - Boosts luck based vanilla features
* Caps at max vanilla enchant + 1
* Boosts:
* - Mining with Fortune
* - Adds 1 extra looting level to any mob kills
* - Boosts luck of the sea by 1
*/
LUCK("luck", null),
/**
* PHANTOM - Disables phantom spawns while this booster is active
*/
PHANTOM("phantom", null),
/**
* IDK
*/
UNKNOWN("unknown", null);
public final String BoosterName;
public final BoosterType parent;
BoosterType(String BoosterName, BoosterType parent) {
this.BoosterName = BoosterName;
this.parent = parent;
}
public String getBoosterName() {
return this.BoosterName;
}
public static BoosterType getByName(String text) {
for (BoosterType type : BoosterType.values()) {
if (type.BoosterName.equalsIgnoreCase(text)) {
return type;
}
}
return UNKNOWN;
}
public List<BoosterType> getChildBoosters() {
return Arrays.stream(BoosterType.values()).filter(boosterType -> boosterType.parent == this).collect(Collectors.toList());
}
}