#1073 Add delay to email recovery command
- Add configurable cooldown period after sending an email for /email recovery - Change ExpiringMap to remove expired entries (like ExpiringSet) - Create method to translate durations via the messages file
This commit is contained in:
@@ -3,7 +3,6 @@ package fr.xephi.authme.util;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -71,36 +70,4 @@ public final class Utils {
|
||||
return Runtime.getRuntime().availableProcessors();
|
||||
}
|
||||
|
||||
public static Duration convertMillisToSuitableUnit(long duration) {
|
||||
TimeUnit targetUnit;
|
||||
if (duration > 1000L * 60L * 60L * 24L) {
|
||||
targetUnit = TimeUnit.DAYS;
|
||||
} else if (duration > 1000L * 60L * 60L) {
|
||||
targetUnit = TimeUnit.HOURS;
|
||||
} else if (duration > 1000L * 60L) {
|
||||
targetUnit = TimeUnit.MINUTES;
|
||||
} else if (duration > 1000L) {
|
||||
targetUnit = TimeUnit.SECONDS;
|
||||
} else {
|
||||
targetUnit = TimeUnit.MILLISECONDS;
|
||||
}
|
||||
|
||||
return new Duration(targetUnit, duration);
|
||||
}
|
||||
|
||||
public static final class Duration {
|
||||
|
||||
private final long duration;
|
||||
private final TimeUnit unit;
|
||||
|
||||
Duration(TimeUnit targetUnit, long durationMillis) {
|
||||
this(targetUnit, durationMillis, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
Duration(TimeUnit targetUnit, long sourceDuration, TimeUnit sourceUnit) {
|
||||
this.duration = targetUnit.convert(sourceDuration, sourceUnit);
|
||||
this.unit = targetUnit;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,7 +46,13 @@ public class ExpiringMap<K, V> {
|
||||
*/
|
||||
public V get(K key) {
|
||||
ExpiringEntry<V> value = entries.get(key);
|
||||
return value == null ? null : value.getValue();
|
||||
if (value == null) {
|
||||
return null;
|
||||
} else if (System.currentTimeMillis() > value.getExpiration()) {
|
||||
entries.remove(key);
|
||||
return null;
|
||||
}
|
||||
return value.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,7 +121,7 @@ public class ExpiringMap<K, V> {
|
||||
}
|
||||
|
||||
V getValue() {
|
||||
return System.currentTimeMillis() > expiration ? null : value;
|
||||
return value;
|
||||
}
|
||||
|
||||
long getExpiration() {
|
||||
|
||||
@@ -83,23 +83,22 @@ public class ExpiringSet<E> {
|
||||
|
||||
/**
|
||||
* Returns the duration of the entry until it expires (provided it is not removed or re-added).
|
||||
* If the entry does not exist, -1 is returned.
|
||||
* If the entry does not exist, a duration of -1 seconds is returned.
|
||||
*
|
||||
* @param entry the entry whose duration before it expires should be returned
|
||||
* @param unit the unit in which to return the duration
|
||||
* @return duration the entry will remain in the set (if there are not modifications)
|
||||
*/
|
||||
public long getExpiration(E entry, TimeUnit unit) {
|
||||
public Duration getExpiration(E entry) {
|
||||
Long expiration = entries.get(entry);
|
||||
if (expiration == null) {
|
||||
return -1;
|
||||
return new Duration(-1, TimeUnit.SECONDS);
|
||||
}
|
||||
long stillPresentMillis = expiration - System.currentTimeMillis();
|
||||
if (stillPresentMillis < 0) {
|
||||
entries.remove(entry);
|
||||
return -1;
|
||||
return new Duration(-1, TimeUnit.SECONDS);
|
||||
}
|
||||
return unit.convert(stillPresentMillis, TimeUnit.MILLISECONDS);
|
||||
return Duration.createWithSuitableUnit(stillPresentMillis, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.util.expiring;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -42,9 +41,10 @@ public class TimedCounter<K> extends ExpiringMap<K, Integer> {
|
||||
* @return the total of all valid entries
|
||||
*/
|
||||
public int total() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
return entries.values().stream()
|
||||
.filter(entry -> currentTime <= entry.getExpiration())
|
||||
.map(ExpiringEntry::getValue)
|
||||
.filter(Objects::nonNull)
|
||||
.reduce(0, Integer::sum);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user