From 6585f7f21dbc7b6c11dc09e7d39926b94f3e4cae Mon Sep 17 00:00:00 2001 From: carm Date: Thu, 6 Apr 2023 19:59:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(auto):=20=E6=94=AF=E6=8C=81=E9=92=88?= =?UTF-8?q?=E5=AF=B9=E5=8D=95=E9=97=B4=E7=89=A9=E5=93=81=E7=9A=84=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E9=A2=86=E5=8F=96=E9=85=8D=E7=BD=AE=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E2=80=9D=E6=8C=89=E6=97=B6=E6=8F=90=E9=86=92=E2=80=9C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 ++++---- .../timereward/conf/FunctionConfig.java | 17 ----------------- .../plugin/timereward/conf/PluginConfig.java | 8 ++++---- .../timereward/manager/RewardManager.java | 19 +++++++++---------- .../timereward/storage/RewardContents.java | 16 +++++++++++++--- 5 files changed, 30 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/cc/carm/plugin/timereward/conf/FunctionConfig.java diff --git a/pom.xml b/pom.xml index bea921c..70551a9 100644 --- a/pom.xml +++ b/pom.xml @@ -11,14 +11,14 @@ UTF-8 UTF-8 - 1.5.2 - 0.4.6 - 2.4.0 + 1.5.5 + 0.4.7 + 2.5.1 cc.carm.plugin timereward - 2.1.0 + 2.2.0 TimeReward 在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。 diff --git a/src/main/java/cc/carm/plugin/timereward/conf/FunctionConfig.java b/src/main/java/cc/carm/plugin/timereward/conf/FunctionConfig.java deleted file mode 100644 index 76444cf..0000000 --- a/src/main/java/cc/carm/plugin/timereward/conf/FunctionConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package cc.carm.plugin.timereward.conf; - -import cc.carm.lib.configuration.core.ConfigurationRoot; -import cc.carm.lib.configuration.core.annotation.HeaderComment; -import cc.carm.lib.configuration.core.value.ConfigValue; -import cc.carm.lib.configuration.core.value.type.ConfiguredValue; - -public class FunctionConfig extends ConfigurationRoot { - - @HeaderComment({ - "是否启用自动领取", - "启用后,玩家将会在满足奖励领取条件时自动领取奖励。", - "若关闭,则玩家需要手动输入指令领取奖励。" - }) - public static final ConfigValue AUTO_CLAIM = ConfiguredValue.of(Boolean.class, true); - -} diff --git a/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java b/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java index 1498fbe..a5627cf 100644 --- a/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java +++ b/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java @@ -23,9 +23,7 @@ public class PluginConfig extends ConfigurationRoot { "检查更新为异步操作,绝不会影响性能与使用体验。" }) public static final ConfigValue CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true); - - public static final Class FUNCTIONS = FunctionConfig.class; - + @HeaderComment({"奖励相关设定,包含以下设定:", " [id] 配置键名即奖励ID,支持英文、数字与下划线。", " | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据", @@ -36,7 +34,9 @@ public class PluginConfig extends ConfigurationRoot { " [permission] 领取奖励时后台执行的指令", " | 支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。", " [commands] 该奖励领取权限,可以不设置。", - " | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。" + " | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。", + " [auto] 该奖励是否自动领取,可以不设置,默认为true。", + " | 若关闭自动领取,则需要玩家手动输入/tr claim 领取奖励。", }) public static final ConfigValue REWARDS = ConfigValue.builder() .asValue(RewardContents.Group.class).fromSection() diff --git a/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java b/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java index 12aee5d..2517dcc 100644 --- a/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java +++ b/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java @@ -3,7 +3,6 @@ import cc.carm.lib.easyplugin.utils.MessageUtils; import cc.carm.plugin.timereward.Main; import cc.carm.plugin.timereward.TimeRewardAPI; -import cc.carm.plugin.timereward.conf.FunctionConfig; import cc.carm.plugin.timereward.conf.PluginConfig; import cc.carm.plugin.timereward.storage.RewardContents; import cc.carm.plugin.timereward.storage.UserData; @@ -27,17 +26,18 @@ public RewardManager(Main main) { this.runnable = new BukkitRunnable() { @Override public void run() { - if (!FunctionConfig.AUTO_CLAIM.getNotNull()) return; if (Bukkit.getOnlinePlayers().isEmpty()) return; for (Player player : Bukkit.getOnlinePlayers()) { - List unclaimedRewards = getUnclaimedRewards(player); - if (unclaimedRewards.isEmpty()) continue; - - main.getScheduler().run(() -> unclaimedRewards.forEach( - // 在同步进程中为玩家发放奖励 - unclaimedReward -> claimReward(player, unclaimedReward, false) - )); + List rewards = getUnclaimedRewards(player).stream() + .filter(RewardContents::isAutoClaimed) + .collect(Collectors.toList()); + if (rewards.isEmpty()) continue; + + main.getScheduler().run(() -> rewards.forEach(r -> { + // 在同步进程中为玩家发放奖励 + claimReward(player, r, true); // 二次检查避免重复发奖 + })); } } }; @@ -68,7 +68,6 @@ public Map listRewards() { .collect(Collectors.toList()); } - public boolean isClaimable(Player player, RewardContents reward) { UserData user = TimeRewardAPI.getUserManager().getData(player); return !user.isClaimed(reward.getRewardID()) // 未曾领取 diff --git a/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java b/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java index 93a6446..69e123f 100644 --- a/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java +++ b/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java @@ -18,15 +18,18 @@ public class RewardContents { private final @Nullable String permission; private final @NotNull List commands; + private final boolean auto; + public RewardContents(@NotNull String id, long time, @Nullable String name, @Nullable String permission, - @NotNull List commands) { + @NotNull List commands, boolean auto) { this.id = id; this.time = time; this.name = name; this.permission = permission; this.commands = commands; + this.auto = auto; } public String getRewardID() { @@ -53,6 +56,10 @@ public long getTime() { return commands; } + public boolean isAutoClaimed() { + return auto; + } + public boolean checkPermission(@NotNull Player player) { return permission == null || player.hasPermission(permission); } @@ -67,6 +74,7 @@ public Map serialize() { if (getName() != null) map.put("name", getName()); if (getPermission() != null) map.put("permission", getPermission()); map.put("commands", getCommands()); + map.put("auto", auto); return map; } @@ -81,7 +89,8 @@ public static RewardContents parse(String id, @NotNull ConfigurationWrapper s id, time, section.getString("name"), section.getString("permission"), - section.getStringList("commands") + section.getStringList("commands"), + section.getBoolean("auto", false) ); } @@ -89,7 +98,8 @@ public static RewardContents defaults(String id) { return new RewardContents( id, 7200, "&f[初级奖励] &e总在线时长 2小时", "TimeReward.vip", - Collections.singletonList("say &f恭喜 &b%player_name% &f领取了奖励 &r%(name) &f!") + Collections.singletonList("say &f恭喜 &b%player_name% &f领取了奖励 &r%(name) &f!"), + true ); }