-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CooldownProvider - Version Increment
- Loading branch information
1 parent
b144d0e
commit 47d1ec1
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
plugin/src/main/java/com/iridium/iridiumcore/CooldownProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.iridium.iridiumcore; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Provides various cooldown features. | ||
* | ||
* @param <T> The type that can have this cooldown | ||
*/ | ||
public class CooldownProvider<T> { | ||
|
||
private final Map<T, Duration> cooldownTimes = new HashMap<>(); | ||
private final Duration duration; | ||
|
||
/** | ||
* The default constructor. | ||
* | ||
* @param duration The duration which cooldowns from this provider have. | ||
*/ | ||
public CooldownProvider(Duration duration) { | ||
this.duration = duration; | ||
} | ||
|
||
/** | ||
* Returns if the specified entity has a valid cooldown. | ||
* | ||
* @param t The entity which should be checked | ||
* @return True if the entity has a valid cooldown | ||
*/ | ||
public boolean isOnCooldown(T t) { | ||
return cooldownTimes.containsKey(t) && cooldownTimes.get(t).toMillis() > System.currentTimeMillis(); | ||
} | ||
|
||
/** | ||
* Returns the remaining duration of the cooldown for the provided entity. | ||
* ZERO if there is no cooldown. | ||
* | ||
* @param t The entity which should be checked | ||
* @return The duration of the cooldown, can be ZERO | ||
*/ | ||
public Duration getRemainingTime(T t) { | ||
if (!isOnCooldown(t)) return Duration.ZERO; | ||
|
||
return cooldownTimes.get(t).minusMillis(System.currentTimeMillis()); | ||
} | ||
|
||
/** | ||
* Reset the cooldown of the specified entity with the duration of this CooldownProvider. | ||
* {@link CooldownProvider#isOnCooldown(Object)} will return true after this. | ||
* | ||
* @param t The entity which should be checked | ||
*/ | ||
public void applyCooldown(T t) { | ||
cooldownTimes.put(t, duration.plusMillis(System.currentTimeMillis())); | ||
} | ||
|
||
} |