1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-04 08:38:17 +08:00

feat(cooldown): 添加便捷的冷却时间工具类

This commit is contained in:
2022-09-14 23:08:06 +08:00
parent 6863c02611
commit 0b6e1ad3e4
15 changed files with 110 additions and 14 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -0,0 +1,96 @@
package cc.carm.lib.easyplugin.utils;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* <a href="https://gist.github.com/CarmJos/402cb5aad0ec14ab25c2fa0d21571703">Easy cooldown time utils.</a>
*
* @param <P> Cooldown key provider
* @param <K> Cooldown key
* @author CarmJos
*/
public class EasyCooldown<P, K> {
protected final NumberFormat numberFormatter;
protected final @NotNull Map<K, Long> cooldown = new HashMap<>();
protected final @NotNull Function<P, K> provider;
protected long defaultDuration;
public EasyCooldown(@NotNull Function<P, K> provider) {
this(defaultFormatter(), provider, 1000L);
}
public EasyCooldown(@NotNull NumberFormat numberFormatter,
@NotNull Function<P, K> provider) {
this(numberFormatter, provider, 1000L);
}
public EasyCooldown(@NotNull NumberFormat numberFormatter,
@NotNull Function<P, K> provider,
long defaultDuration) {
this.numberFormatter = numberFormatter;
this.provider = provider;
this.defaultDuration = defaultDuration;
}
public long getCooldown(@NotNull P provider) {
Long time = this.cooldown.get(this.provider.apply(provider));
if (time == null || time < 0) return 0;
long duration = getDuration(provider);
if (duration <= 0) return 0;
long past = System.currentTimeMillis() - time;
return duration - past;
}
public @NotNull String getCooldownSeconds(@NotNull P provider) {
return formatSeconds(getCooldown(provider));
}
public void updateTime(@NotNull P provider) {
this.cooldown.put(this.provider.apply(provider), System.currentTimeMillis());
}
public boolean isCoolingDown(@NotNull P provider) {
return getCooldown(provider) > 0;
}
public long getDuration(@NotNull P provider) {
return this.defaultDuration;
}
public @NotNull String formatSeconds(long cooldownMillis) {
return numberFormatter.format((double) cooldownMillis / 1000D);
}
public static NumberFormat createFormatter(Consumer<NumberFormat> consumer) {
NumberFormat format = NumberFormat.getInstance();
consumer.accept(format);
return format;
}
public static NumberFormat defaultFormatter() {
return createFormatter((f) -> f.setMaximumFractionDigits(2));
}
public static EasyCooldown<Player, UUID> playerCooldown(Function<Player, Long> durationProvider) {
return new EasyCooldown<Player, UUID>(Player::getUniqueId) {
@Override
public long getDuration(@NotNull Player provider) {
return durationProvider.apply(provider);
}
};
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.4.14</version>
<version>1.4.15</version>
<modules>
<module>base/main</module>