From 1078c7e03dc7c2fc641d1e4af16b2bb4adea22d4 Mon Sep 17 00:00:00 2001 From: carm Date: Thu, 7 Sep 2023 05:09:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(time):=20=E6=94=AF=E6=8C=81=E5=9C=A8?= =?UTF-8?q?=E5=91=A8=E6=9C=9F=E5=86=85=E5=BE=AA=E7=8E=AF=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=8F=91=E5=A5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../plugin/timereward/conf/RewardsConfig.java | 6 +++++ .../plugin/timereward/data/IntervalType.java | 14 +++++------ .../timereward/data/RewardContents.java | 20 +++++++++------- .../timereward/manager/RewardManager.java | 24 +++++++++++++++---- .../storage/database/MySQLStorage.java | 3 +-- .../timereward/user/LockedRewardData.java | 3 +-- .../timereward/user/UserRewardData.java | 14 +++++------ 8 files changed, 55 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 7c94e8c..4fad6c3 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ cc.carm.plugin timereward - 3.0.0 + 3.1.0 TimeReward 在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。 diff --git a/src/main/java/cc/carm/plugin/timereward/conf/RewardsConfig.java b/src/main/java/cc/carm/plugin/timereward/conf/RewardsConfig.java index 69e1fdd..a1427e0 100644 --- a/src/main/java/cc/carm/plugin/timereward/conf/RewardsConfig.java +++ b/src/main/java/cc/carm/plugin/timereward/conf/RewardsConfig.java @@ -17,6 +17,12 @@ " [type] 奖励的类型序号", " | “0”代表总计时间奖励,“1”代表每日在线奖励,", " | “2”代表每周在线奖励,“3”代表每月在线奖励。", + " [time] 奖励发放要求的时间(秒)", + " [loop] 奖励是否按循环时间发放", + " | 可以填入 true 或 false", + " | 若启用,则在规定的周期内,每满足指定时间一次则发放一次奖励", + " | 如 类型为 “每周在线奖励” 时间为 “12小时” ", + " | 则 代表 代表在这周内每12小时发放一次奖励,下一周时间重新开始算", " [name] 奖励的显示名称,可以是任意字符串", " | 可以在 commands 中使用 %(name) 来获取该奖励的名称", " | 也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称", diff --git a/src/main/java/cc/carm/plugin/timereward/data/IntervalType.java b/src/main/java/cc/carm/plugin/timereward/data/IntervalType.java index bb80d08..e9620b0 100644 --- a/src/main/java/cc/carm/plugin/timereward/data/IntervalType.java +++ b/src/main/java/cc/carm/plugin/timereward/data/IntervalType.java @@ -60,14 +60,14 @@ public enum IntervalType { ); private final int id; - private final @NotNull Predicate reclaimPreficate; + private final @NotNull Predicate periodChangePredicate; private final @NotNull BiFunction calculator; IntervalType(int id, - @NotNull Predicate reclaimablePredicate, + @NotNull Predicate periodChangePredicate, @NotNull BiFunction calculator) { this.id = id; - this.reclaimPreficate = reclaimablePredicate; + this.periodChangePredicate = periodChangePredicate; this.calculator = calculator; } @@ -79,16 +79,16 @@ public int getID() { return calculator; } - public @NotNull Predicate getReclaimablePredicate() { - return reclaimPreficate; + public @NotNull Predicate getPreiodChangePredicate() { + return periodChangePredicate; } public Duration calculate(@NotNull TimeRecord timeRecord, @NotNull LocalDateTime joinTime) { return calculator.apply(timeRecord, joinTime); } - public boolean isReclaimable(@NotNull LocalDateTime claimedDate) { - return reclaimPreficate.test(claimedDate); + public boolean isPeriodChanged(@NotNull LocalDateTime claimedDate) { + return periodChangePredicate.test(claimedDate); } public static IntervalType parse(String input) { diff --git a/src/main/java/cc/carm/plugin/timereward/data/RewardContents.java b/src/main/java/cc/carm/plugin/timereward/data/RewardContents.java index 20b5179..94ab4f1 100644 --- a/src/main/java/cc/carm/plugin/timereward/data/RewardContents.java +++ b/src/main/java/cc/carm/plugin/timereward/data/RewardContents.java @@ -15,6 +15,7 @@ public class RewardContents { public final @NotNull IntervalType type; private final long time; + private final boolean loop; private final @Nullable String name; private final @Nullable String permission; @@ -22,12 +23,13 @@ public class RewardContents { private final boolean auto; - public RewardContents(@NotNull String id, @NotNull IntervalType type, long time, + public RewardContents(@NotNull String id, @NotNull IntervalType type, long time, boolean loop, @Nullable String name, @Nullable String permission, @NotNull List commands, boolean auto) { this.id = id; this.type = type; this.time = time; + this.loop = loop; this.name = name; this.permission = permission; this.commands = commands; @@ -42,6 +44,10 @@ public RewardContents(@NotNull String id, @NotNull IntervalType type, long time, return type; } + public boolean isLoop() { + return loop; + } + public long getTime() { return time; } @@ -70,11 +76,11 @@ public boolean checkPermission(@NotNull Player player) { return permission == null || player.hasPermission(permission); } - public Map serialize() { Map map = new LinkedHashMap<>(); map.put("time", getTime()); map.put("type", getType().getID()); + map.put("loop", isLoop()); if (getName() != null) map.put("name", getName()); if (getPermission() != null) map.put("permission", getPermission()); map.put("commands", getCommands()); @@ -96,17 +102,15 @@ public static RewardContents parse(String id, @NotNull ConfigurationWrapper s } return new RewardContents( - id, intervalType, time, - section.getString("name"), - section.getString("permission"), - section.getStringList("commands"), - section.getBoolean("auto", false) + id, intervalType, time, section.getBoolean("loop", false), + section.getString("name"), section.getString("permission"), + section.getStringList("commands"), section.getBoolean("auto", false) ); } public static RewardContents defaults(String id) { return new RewardContents( - id, IntervalType.TOTAL, 7200, + id, IntervalType.TOTAL, 7200, false, "&f[初级奖励] &e总在线时长 2小时", "TimeReward.vip", Collections.singletonList("say &f恭喜 &b%player_name% &f领取了奖励 &r%(name) &f!"), true 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 38db6a1..a9a541e 100644 --- a/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java +++ b/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java @@ -4,6 +4,7 @@ import cc.carm.plugin.timereward.Main; import cc.carm.plugin.timereward.TimeRewardAPI; import cc.carm.plugin.timereward.conf.RewardsConfig; +import cc.carm.plugin.timereward.data.IntervalType; import cc.carm.plugin.timereward.data.RewardContents; import cc.carm.plugin.timereward.user.UserRewardData; import org.bukkit.Bukkit; @@ -13,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; +import java.time.Duration; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -67,11 +69,25 @@ public Map listRewards() { } public boolean isClaimable(Player player, RewardContents reward) { + if (!reward.checkPermission(player)) return false; // 满足权限 + UserRewardData user = TimeRewardAPI.getUserManager().get(player); + IntervalType intervalType = reward.getType(); + LocalDateTime lastClaimed = user.getClaimedDate(reward); - return !user.isClaimed(reward) // 未曾领取 - && user.isTimeEnough(reward)// 时间足够 - && reward.checkPermission(player); // 满足权限 + if (reward.isLoop()) { // 循环奖励 + if (lastClaimed == null || intervalType.isPeriodChanged(lastClaimed)) { + // 无上次领取记录或上次领取的时间不在一个周期内,则直接判断时间是否足够一次循环领取的时间 + return user.getOnlineDuration(intervalType).getSeconds() > reward.getTime(); + } else { + // 有上次领取记录,且在同一周期内,则直接判断相隔时间是否满足一个周期 + return Duration.between(lastClaimed, LocalDateTime.now()).getSeconds() > reward.getTime(); + } + } else { // 非循环奖励 + if (lastClaimed == null || intervalType.isPeriodChanged(lastClaimed)) { // 无上次领取记录,则直接判断时间是否足够领取的时间 + return user.getOnlineDuration(intervalType).getSeconds() > reward.getTime(); + } else return false; // 有同一周期内的领取记录,则玩家不得重复领取了 + } } public CompletableFuture claimReward(Player player, RewardContents reward, boolean check) { @@ -91,7 +107,7 @@ public CompletableFuture claimRewards(Player player, Collection claimedData = loadClaimedData(uuid); return new UserRewardData(uuid, recordDate, claimedData); } @@ -85,7 +84,7 @@ public TimeRecord loadTimeRecord(@NotNull UUID uuid) throws Exception { public Map loadClaimedData(@NotNull UUID uuid) throws Exception { return DatabaseTables.USER_CLAIMED.createQuery() .addCondition("uuid", uuid).build() - .executeFunction((query) -> { + .executeFunction(query -> { ResultSet rs = query.getResultSet(); Map map = new LinkedHashMap<>(); diff --git a/src/main/java/cc/carm/plugin/timereward/user/LockedRewardData.java b/src/main/java/cc/carm/plugin/timereward/user/LockedRewardData.java index 1f8e9a7..e224913 100644 --- a/src/main/java/cc/carm/plugin/timereward/user/LockedRewardData.java +++ b/src/main/java/cc/carm/plugin/timereward/user/LockedRewardData.java @@ -30,8 +30,7 @@ public boolean isTimeEnough(RewardContents reward) { } @Override - public boolean addClaimedReward(@NotNull RewardContents reward) { - return false; + public void updateClaimed(@NotNull RewardContents reward) { } } diff --git a/src/main/java/cc/carm/plugin/timereward/user/UserRewardData.java b/src/main/java/cc/carm/plugin/timereward/user/UserRewardData.java index 981c1a9..367b3c8 100644 --- a/src/main/java/cc/carm/plugin/timereward/user/UserRewardData.java +++ b/src/main/java/cc/carm/plugin/timereward/user/UserRewardData.java @@ -5,6 +5,7 @@ import cc.carm.plugin.timereward.data.RewardContents; import cc.carm.plugin.timereward.data.TimeRecord; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.time.Duration; import java.time.LocalDateTime; @@ -64,21 +65,20 @@ public boolean isTimeEnough(RewardContents reward) { return getOnlineDuration(reward.getType()).getSeconds() >= reward.getTime(); } - public @NotNull Map getClaimedRewards() { return claimedRewards; } + public @Nullable LocalDateTime getClaimedDate(@NotNull RewardContents reward) { + return claimedRewards.get(reward.getRewardID()); + } + public boolean isClaimed(@NotNull RewardContents reward) { - LocalDateTime claimedDate = claimedRewards.get(reward.getRewardID()); - if (claimedDate == null) return false; - return !reward.getType().isReclaimable(claimedDate); + return claimedRewards.containsKey(reward.getRewardID()); } - public boolean addClaimedReward(@NotNull RewardContents reward) { - if (isClaimed(reward)) return false; // 已经领取过了 + public void updateClaimed(@NotNull RewardContents reward) { this.claimedRewards.put(reward.getRewardID(), LocalDateTime.now()); - return true; } }