Updated Cooldown.java

This commit is contained in:
Németh Noel 2021-06-29 22:24:34 +02:00
parent 8f906d80d0
commit b98abe8dd5

View File

@ -2,18 +2,30 @@ package net.minestom.server.utils.time;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.time.temporal.TemporalUnit;
@SuppressWarnings("removal")
public final class Cooldown { public final class Cooldown {
private UpdateOption updateOption; private Duration duration;
private long lastUpdate; private long lastUpdate;
/**
* @deprecated Replaced by {@link #Cooldown(Duration)}
*/
@Deprecated(forRemoval = true)
public Cooldown(@NotNull UpdateOption updateOption) { public Cooldown(@NotNull UpdateOption updateOption) {
this.updateOption = updateOption; this.duration = Duration.ofMillis(updateOption.toMilliseconds());
this.lastUpdate = System.currentTimeMillis(); this.lastUpdate = System.currentTimeMillis();
} }
public UpdateOption getUpdateOption() { public Cooldown(Duration duration) {
return this.updateOption; this.duration = duration;
}
public Duration getDuration() {
return this.duration;
} }
public void refreshLastUpdate(long lastUpdate) { public void refreshLastUpdate(long lastUpdate) {
@ -21,7 +33,7 @@ public final class Cooldown {
} }
public boolean isReady(long time) { public boolean isReady(long time) {
return !hasCooldown(time, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); return !hasCooldown(time, lastUpdate, duration);
} }
/** /**
@ -32,12 +44,28 @@ public final class Cooldown {
* @param timeUnit the time unit of the cooldown * @param timeUnit the time unit of the cooldown
* @param cooldown the value of the cooldown * @param cooldown the value of the cooldown
* @return true if the cooldown is in progress, false otherwise * @return true if the cooldown is in progress, false otherwise
*
* @deprecated Replaced by {@link #hasCooldown(long, long, TemporalUnit, long)}
*/ */
@Deprecated(forRemoval = true)
public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull TimeUnit timeUnit, long cooldown) { public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull TimeUnit timeUnit, long cooldown) {
final long cooldownMs = timeUnit.toMilliseconds(cooldown); final long cooldownMs = timeUnit.toMilliseconds(cooldown);
return currentTime - lastUpdate < cooldownMs; return currentTime - lastUpdate < cooldownMs;
} }
/**
* Gets if something is in cooldown based on the current time.
*
* @param currentTime the current time in milliseconds
* @param lastUpdate the last update in milliseconds
* @param temporalUnit the time unit of the cooldown
* @param cooldown the value of the cooldown
* @return true if the cooldown is in progress, false otherwise
*/
public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull TemporalUnit temporalUnit, long cooldown) {
return hasCooldown(currentTime, lastUpdate, Duration.of(cooldown, temporalUnit));
}
/** /**
* Gets if something is in cooldown based on the current time. * Gets if something is in cooldown based on the current time.
* *
@ -45,11 +73,27 @@ public final class Cooldown {
* @param lastUpdate the last update in milliseconds * @param lastUpdate the last update in milliseconds
* @param updateOption the cooldown * @param updateOption the cooldown
* @return true if the cooldown is in progress, false otherwise * @return true if the cooldown is in progress, false otherwise
*
* @deprecated Replaced by {@link #hasCooldown(long, long, Duration)}
*/ */
@Deprecated(forRemoval = true)
public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull UpdateOption updateOption) { public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull UpdateOption updateOption) {
return hasCooldown(currentTime, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); return hasCooldown(currentTime, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue());
} }
/**
* Gets if something is in cooldown based on the current time.
*
* @param currentTime the current time in milliseconds
* @param lastUpdate the last update in milliseconds
* @param duration the cooldown
* @return true if the cooldown is in progress, false otherwise
*/
public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull Duration duration) {
final long cooldownMs = duration.toMillis();
return currentTime - lastUpdate < cooldownMs;
}
/** /**
* Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}). * Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}).
* *
@ -57,8 +101,23 @@ public final class Cooldown {
* @param timeUnit the time unit of the cooldown * @param timeUnit the time unit of the cooldown
* @param cooldown the value of the cooldown * @param cooldown the value of the cooldown
* @return true if the cooldown is in progress, false otherwise * @return true if the cooldown is in progress, false otherwise
*
* @deprecated Replaced by {@link #hasCooldown(long, TemporalUnit, int)}
*/ */
@Deprecated(forRemoval = true)
public static boolean hasCooldown(long lastUpdate, @NotNull TimeUnit timeUnit, int cooldown) { public static boolean hasCooldown(long lastUpdate, @NotNull TimeUnit timeUnit, int cooldown) {
return hasCooldown(System.currentTimeMillis(), lastUpdate, timeUnit, cooldown); return hasCooldown(System.currentTimeMillis(), lastUpdate, timeUnit, cooldown);
} }
/**
* Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}).
*
* @param lastUpdate the last update in milliseconds
* @param temporalUnit the time unit of the cooldown
* @param cooldown the value of the cooldown
* @return true if the cooldown is in progress, false otherwise
*/
public static boolean hasCooldown(long lastUpdate, @NotNull TemporalUnit temporalUnit, int cooldown) {
return hasCooldown(System.currentTimeMillis(), lastUpdate, temporalUnit, cooldown);
}
} }