Skip to content

Commit

Permalink
Fix cooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueris committed Jun 17, 2024
1 parent eeb6a02 commit b830fdf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import me.dueris.genesismc.factory.data.types.HudRender;
import me.dueris.genesismc.factory.powers.holder.PowerType;
import me.dueris.genesismc.registry.Registries;
import me.dueris.genesismc.util.ModifiableFloatPair;
import me.dueris.genesismc.util.Util;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.boss.KeyedBossBar;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -25,8 +25,8 @@
import static me.dueris.genesismc.factory.powers.apoli.Resource.serverLoadedBars;

public class Cooldown extends PowerType implements CooldownPower {
private static final ConcurrentHashMap<NamespacedKey, Double> incrementGetter = new ConcurrentHashMap<>();
public static ConcurrentHashMap<Player, List<Pair<KeyedBossBar, ResourcePower>>> cooldowns = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<KeyedBossBar, ModifiableFloatPair> timingsTracker = new ConcurrentHashMap<>();
private final HudRender hudRender;
private final int cooldown;

Expand All @@ -50,10 +50,9 @@ protected static void addCooldown(Player player, int amt, CooldownPower power, d
cooldowns.putIfAbsent(player, new ArrayList<>());
if (isInCooldown(player, power)) return; // Already in cooldown
Resource.Bar bar = new Resource.Bar(power, player);
incrementGetter.put(bar.renderedBar.getKey(), 1.0 / amt);
Resource.currentlyDisplayed.putIfAbsent(player, new ArrayList<>());
Resource.currentlyDisplayed.get(player).add(bar);
cooldowns.get(player).add(new Pair<>() {
Pair<KeyedBossBar, ResourcePower> pair = new Pair<>() {
@Override
public KeyedBossBar left() {
return bar.renderedBar;
Expand All @@ -63,7 +62,9 @@ public KeyedBossBar left() {
public ResourcePower right() {
return power;
}
});
};
cooldowns.get(player).add(pair);
timingsTracker.put(pair.key(), new ModifiableFloatPair(amt, amt));
}

public static boolean isInCooldown(Player player, ResourcePower power) {
Expand Down Expand Up @@ -91,9 +92,10 @@ public void preLoad(ServerLoadEvent e) {
public void tick() {
Util.collectValues(new ArrayList<>(cooldowns.values())).forEach((pair) -> {
KeyedBossBar bar = pair.left();
if (!incrementGetter.containsKey(bar.getKey())) return;
double increment = incrementGetter.get(bar.getKey());
if (bar.getProgress() - increment <= 0) {
ModifiableFloatPair floatPair = timingsTracker.get(bar);
float max = floatPair.a();
float cur = floatPair.setB(floatPair.b() - 1); // Decrease the tracker
if (cur <= 0) {
for (Player player : bar.getPlayers()) {
Resource.currentlyDisplayed.putIfAbsent(player, new ArrayList<>());
Resource.Bar b = null;
Expand All @@ -113,7 +115,7 @@ public void tick() {
Bukkit.getServer().removeBossBar(bar.getKey());
scheduleRemoval(pair);
} else {
bar.setProgress(bar.getProgress() - increment);
bar.setProgress(1 - (cur / max));
}
});
}
Expand All @@ -122,7 +124,6 @@ private void scheduleRemoval(Pair<KeyedBossBar, ResourcePower> pair) {
for (Player p : cooldowns.keySet()) {
if (cooldowns.get(p).contains(pair)) {
cooldowns.get(p).remove(pair);
incrementGetter.remove(pair.left().getKey());
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.dueris.genesismc.util;

public final class ModifiableFloatPair {
private float a;
private float b;

public ModifiableFloatPair(float a, float b) {
this.a = a;
this.b = b;
}

public float a() {
return a;
}

public float b() {
return b;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (ModifiableFloatPair) obj;
return Float.floatToIntBits(this.a) == Float.floatToIntBits(that.a) &&
Float.floatToIntBits(this.b) == Float.floatToIntBits(that.b);
}

public float setA(float a) {
this.a = a;
return a;
}

public float setB(float b) {
this.b = b;
return b;
}
}

0 comments on commit b830fdf

Please sign in to comment.