From 0b6e1ad3e4d3efe2e41943db5ec39fecb7e3fa86 Mon Sep 17 00:00:00 2001 From: carm Date: Wed, 14 Sep 2022 23:08:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(cooldown):=20=E6=B7=BB=E5=8A=A0=E4=BE=BF?= =?UTF-8?q?=E6=8D=B7=E7=9A=84=E5=86=B7=E5=8D=B4=E6=97=B6=E9=97=B4=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/command/pom.xml | 2 +- base/conf/pom.xml | 2 +- base/database/pom.xml | 2 +- base/gui/pom.xml | 2 +- base/listener/pom.xml | 2 +- base/main/pom.xml | 2 +- .../lib/easyplugin/utils/EasyCooldown.java | 96 +++++++++++++++++++ base/storage/pom.xml | 2 +- collection/all/pom.xml | 2 +- collection/bom/pom.xml | 2 +- collection/common/pom.xml | 2 +- extension/gh-checker/pom.xml | 2 +- extension/papi/pom.xml | 2 +- extension/vault/pom.xml | 2 +- pom.xml | 2 +- 15 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 base/main/src/main/java/cc/carm/lib/easyplugin/utils/EasyCooldown.java diff --git a/base/command/pom.xml b/base/command/pom.xml index 9f460da..49d3515 100644 --- a/base/command/pom.xml +++ b/base/command/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/base/conf/pom.xml b/base/conf/pom.xml index fab38b2..bae2627 100644 --- a/base/conf/pom.xml +++ b/base/conf/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/base/database/pom.xml b/base/database/pom.xml index 722685e..9425ae7 100644 --- a/base/database/pom.xml +++ b/base/database/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/base/gui/pom.xml b/base/gui/pom.xml index 41053aa..caf1fc8 100644 --- a/base/gui/pom.xml +++ b/base/gui/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/base/listener/pom.xml b/base/listener/pom.xml index cf1a71b..f7b46cf 100644 --- a/base/listener/pom.xml +++ b/base/listener/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/base/main/pom.xml b/base/main/pom.xml index b7225e4..5ca78de 100644 --- a/base/main/pom.xml +++ b/base/main/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/EasyCooldown.java b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/EasyCooldown.java new file mode 100644 index 0000000..75a53f7 --- /dev/null +++ b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/EasyCooldown.java @@ -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; + +/** + * Easy cooldown time utils. + * + * @param

Cooldown key provider + * @param Cooldown key + * @author CarmJos + */ +public class EasyCooldown { + + protected final NumberFormat numberFormatter; + + protected final @NotNull Map cooldown = new HashMap<>(); + protected final @NotNull Function provider; + + protected long defaultDuration; + + public EasyCooldown(@NotNull Function provider) { + this(defaultFormatter(), provider, 1000L); + } + + public EasyCooldown(@NotNull NumberFormat numberFormatter, + @NotNull Function provider) { + this(numberFormatter, provider, 1000L); + } + + public EasyCooldown(@NotNull NumberFormat numberFormatter, + @NotNull Function 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 consumer) { + NumberFormat format = NumberFormat.getInstance(); + consumer.accept(format); + return format; + } + + public static NumberFormat defaultFormatter() { + return createFormatter((f) -> f.setMaximumFractionDigits(2)); + } + + public static EasyCooldown playerCooldown(Function durationProvider) { + return new EasyCooldown(Player::getUniqueId) { + @Override + public long getDuration(@NotNull Player provider) { + return durationProvider.apply(provider); + } + }; + } + +} diff --git a/base/storage/pom.xml b/base/storage/pom.xml index 6b4617a..5337151 100644 --- a/base/storage/pom.xml +++ b/base/storage/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/collection/all/pom.xml b/collection/all/pom.xml index 00a5441..d79d336 100644 --- a/collection/all/pom.xml +++ b/collection/all/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/collection/bom/pom.xml b/collection/bom/pom.xml index 5457e56..bfe8339 100644 --- a/collection/bom/pom.xml +++ b/collection/bom/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/collection/common/pom.xml b/collection/common/pom.xml index 1ad40ab..00b3a18 100644 --- a/collection/common/pom.xml +++ b/collection/common/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/extension/gh-checker/pom.xml b/extension/gh-checker/pom.xml index 762c990..417ee50 100644 --- a/extension/gh-checker/pom.xml +++ b/extension/gh-checker/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/extension/papi/pom.xml b/extension/papi/pom.xml index 3800b79..5f317b0 100644 --- a/extension/papi/pom.xml +++ b/extension/papi/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/extension/vault/pom.xml b/extension/vault/pom.xml index bf95f1f..3d959f5 100644 --- a/extension/vault/pom.xml +++ b/extension/vault/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.4.14 + 1.4.15 ../../pom.xml 4.0.0 diff --git a/pom.xml b/pom.xml index 94c9b92..38650af 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ cc.carm.lib easyplugin-parent pom - 1.4.14 + 1.4.15 base/main