Skip to content

Commit

Permalink
Rework cooldown system a bit (again...)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Oct 13, 2024
1 parent 96f4d5e commit f5335b8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 59 deletions.
67 changes: 37 additions & 30 deletions src/main/java/xyz/srnyx/annoyingapi/cooldown/AnnoyingCooldown.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import xyz.srnyx.javautilities.manipulation.DurationFormatter;
import xyz.srnyx.javautilities.parents.Stringable;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;


/**
* This class is used to create and manage cooldowns
Expand All @@ -22,31 +18,49 @@ public class AnnoyingCooldown extends Stringable {
* The {@link CooldownManager} that is managing this cooldown
*/
@NotNull private final CooldownManager manager;
/**
* A string indicating the type of cooldown (examples: {@code command.play}, {@code use_ability}, etc...)
*/
@NotNull public final String type;
/**
* The key that "owns" this cooldown
*/
@NotNull public final String key;
/**
* The {@link CooldownType} that was used to create this cooldown
* The duration of the cooldown (in milliseconds)
*/
@NotNull public final CooldownType type;
public final long duration;
/**
* The time that this cooldown will expire
* <br>{@code null} if the cooldown hasn't started
*/
private final long time;
@Nullable private Long expires;

/**
* Creates a new cooldown with the given key and type
* Creates a new cooldown with the given type, key, and duration
*
* @param plugin the plugin that is creating the cooldown (used to get the {@link #manager})
* @param key {@link #key}
* @param type {@link #type}
* @param manager {@link #manager}
* @param type {@link #type}
* @param key {@link #key}
* @param duration the duration of the cooldown (in milliseconds)
*/
public AnnoyingCooldown(@NotNull AnnoyingPlugin plugin, @NotNull String key, @NotNull CooldownType type) {
this.manager = plugin.cooldownManager;
public AnnoyingCooldown(@NotNull CooldownManager manager, @NotNull Object type, @NotNull String key, long duration) {
this.manager = manager;
this.key = key;
this.type = type;
this.time = System.currentTimeMillis() + type.getDuration();
this.type = type.toString();
this.duration = duration;
}

/**
* Creates a new cooldown with the given type, key, and duration
*
* @param plugin the plugin that is creating the cooldown (used to get the {@link #manager})
* @param type {@link #type}
* @param key {@link #key}
* @param duration the duration of the cooldown (in milliseconds)
*/
public AnnoyingCooldown(@NotNull AnnoyingPlugin plugin, @NotNull Object type, @NotNull String key, long duration) {
this(plugin.cooldownManager, type, key, duration);
}

/**
Expand All @@ -57,7 +71,7 @@ public AnnoyingCooldown(@NotNull AnnoyingPlugin plugin, @NotNull String key, @No
* @see DurationFormatter#formatDuration(long, String, boolean)
*/
public long getRemaining() {
return time - System.currentTimeMillis();
return expires == null ? 0 : expires - System.currentTimeMillis();
}

/**
Expand Down Expand Up @@ -98,23 +112,16 @@ public boolean isOnCooldownStart() {
* <br>If the cooldown is already started, it will be restarted
*/
public void start() {
final Set<AnnoyingCooldown> set = manager.cooldowns.get(key);
if (set == null) {
manager.cooldowns.put(key, new HashSet<>(Collections.singleton(this)));
return;
}
set.remove(this);
set.add(this);
expires = System.currentTimeMillis() + duration;
manager.cooldowns.add(this);
}

/**
* Stops the cooldown
*/
public void stop() {
final Set<AnnoyingCooldown> set = manager.cooldowns.get(key);
if (set == null) return;
set.remove(this);
if (set.isEmpty()) manager.cooldowns.remove(key);
expires = null;
manager.cooldowns.remove(this);
}

/**
Expand All @@ -130,17 +137,17 @@ public boolean equals(@Nullable Object other) {
if (this == other) return true;
if (!(other instanceof AnnoyingCooldown)) return false;
final AnnoyingCooldown cooldown = (AnnoyingCooldown) other;
return key.equals(cooldown.key) && type.equals(cooldown.type);
return type.equals(cooldown.type) && key.equals(cooldown.key);
}

/**
* Returns the hash code of this cooldown
* <br>It is the sum of the hash codes of the {@link #key} and {@link #type}
* <br>It is the sum of the hash codes of the {@link #type} and {@link #key}
*
* @return the hash code of this cooldown
*/
@Override
public int hashCode() {
return key.hashCode() + type.hashCode();
return type.hashCode() + key.hashCode();
}
}
65 changes: 50 additions & 15 deletions src/main/java/xyz/srnyx/annoyingapi/cooldown/CooldownManager.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package xyz.srnyx.annoyingapi.cooldown;

import com.google.common.collect.ImmutableSet;

import org.jetbrains.annotations.NotNull;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import xyz.srnyx.annoyingapi.parents.Annoyable;

import xyz.srnyx.javautilities.parents.Stringable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;


/**
Expand All @@ -26,9 +23,9 @@ public class CooldownManager extends Stringable implements Annoyable {
*/
@NotNull private final AnnoyingPlugin plugin;
/**
* A map of all cooldowns, with the key being the key of the cooldowns
* A set of all cooldowns
*/
@NotNull public final Map<String, Set<AnnoyingCooldown>> cooldowns = new HashMap<>();
@NotNull public final Set<AnnoyingCooldown> cooldowns = new HashSet<>();

/**
* Creates a new cooldown manager with the given plugin
Expand All @@ -44,6 +41,21 @@ public AnnoyingPlugin getAnnoyingPlugin() {
return plugin;
}

/**
* Get all cooldowns with the given type
*
* @param type the type of the cooldowns
*
* @return all cooldowns with the given type
*/
@NotNull
public Set<AnnoyingCooldown> getCooldownsByType(@NotNull Object type) {
final String typeString = type.toString();
return cooldowns.stream()
.filter(cooldown -> cooldown.type.equals(typeString))
.collect(Collectors.toSet());
}

/**
* Get all cooldowns with the given key
*
Expand All @@ -52,9 +64,11 @@ public AnnoyingPlugin getAnnoyingPlugin() {
* @return all cooldowns with the given key
*/
@NotNull
public ImmutableSet<AnnoyingCooldown> getCooldowns(@NotNull Object key) {
final Set<AnnoyingCooldown> set = cooldowns.get(key.toString());
return set == null ? ImmutableSet.of() : ImmutableSet.copyOf(set);
public Set<AnnoyingCooldown> getCooldownsByKey(@NotNull Object key) {
final String keyString = key.toString();
return cooldowns.stream()
.filter(cooldown -> cooldown.key.equals(keyString))
.collect(Collectors.toSet());
}

/**
Expand All @@ -66,10 +80,31 @@ public ImmutableSet<AnnoyingCooldown> getCooldowns(@NotNull Object key) {
* @return the cooldown with the given key and type
*/
@NotNull
public AnnoyingCooldown getCooldown(@NotNull Object key, @NotNull CooldownType type) {
return getCooldowns(key).stream()
.filter(cooldown -> cooldown.type.equals(type))
.findFirst()
.orElse(new AnnoyingCooldown(plugin, key.toString(), type));
public Optional<AnnoyingCooldown> getCooldown(@NotNull Object key, @NotNull Object type) {
final String keyString = key.toString();
final String typeString = type.toString();
return cooldowns.stream()
.filter(cooldown -> cooldown.key.equals(keyString) && cooldown.type.equals(typeString))
.findAny();
}

/**
* Get a cooldown with the given key and type
* <br>If the cooldown doesn't exist yet, it will create a new one with the given duration
*
* @param key the key of the cooldown
* @param type the type of the cooldown
* @param duration the duration of the cooldown (in milliseconds) if the cooldown doesn't exist yet
*
* @return the cooldown with the given key and type or the newly created cooldown
*/
@NotNull
public AnnoyingCooldown getCooldown(@NotNull Object key, @NotNull Object type, long duration) {
final String keyString = key.toString();
final String typeString = type.toString();
return cooldowns.stream()
.filter(cooldown -> cooldown.key.equals(keyString) && cooldown.type.equals(typeString))
.findAny()
.orElse(new AnnoyingCooldown(this, type, keyString, duration));
}
}
14 changes: 0 additions & 14 deletions src/main/java/xyz/srnyx/annoyingapi/cooldown/CooldownType.java

This file was deleted.

0 comments on commit f5335b8

Please sign in to comment.