Skip to content

Commit

Permalink
feat(auto): 支持针对单间物品的自动领取配置以支持”按时提醒“功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Apr 6, 2023
1 parent 7e598d1 commit 6585f7f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 38 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<deps.easyplugin.version>1.5.2</deps.easyplugin.version>
<deps.easysql.version>0.4.6</deps.easysql.version>
<deps.mineconfig.version>2.4.0</deps.mineconfig.version>
<deps.easyplugin.version>1.5.5</deps.easyplugin.version>
<deps.easysql.version>0.4.7</deps.easysql.version>
<deps.mineconfig.version>2.5.1</deps.mineconfig.version>
</properties>

<groupId>cc.carm.plugin</groupId>
<artifactId>timereward</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>

<name>TimeReward</name>
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/cc/carm/plugin/timereward/conf/FunctionConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public class PluginConfig extends ConfigurationRoot {
"检查更新为异步操作,绝不会影响性能与使用体验。"
})
public static final ConfigValue<Boolean> CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true);

public static final Class<?> FUNCTIONS = FunctionConfig.class;


@HeaderComment({"奖励相关设定,包含以下设定:",
" [id] 配置键名即奖励ID,支持英文、数字与下划线。",
" | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
Expand All @@ -36,7 +34,9 @@ public class PluginConfig extends ConfigurationRoot {
" [permission] 领取奖励时后台执行的指令",
" | 支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。",
" [commands] 该奖励领取权限,可以不设置。",
" | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
" | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。",
" [auto] 该奖励是否自动领取,可以不设置,默认为true。",
" | 若关闭自动领取,则需要玩家手动输入/tr claim 领取奖励。",
})
public static final ConfigValue<RewardContents.Group> REWARDS = ConfigValue.builder()
.asValue(RewardContents.Group.class).fromSection()
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RewardContents> unclaimedRewards = getUnclaimedRewards(player);
if (unclaimedRewards.isEmpty()) continue;

main.getScheduler().run(() -> unclaimedRewards.forEach(
// 在同步进程中为玩家发放奖励
unclaimedReward -> claimReward(player, unclaimedReward, false)
));
List<RewardContents> rewards = getUnclaimedRewards(player).stream()
.filter(RewardContents::isAutoClaimed)
.collect(Collectors.toList());
if (rewards.isEmpty()) continue;

main.getScheduler().run(() -> rewards.forEach(r -> {
// 在同步进程中为玩家发放奖励
claimReward(player, r, true); // 二次检查避免重复发奖
}));
}
}
};
Expand Down Expand Up @@ -68,7 +68,6 @@ public Map<String, RewardContents> listRewards() {
.collect(Collectors.toList());
}


public boolean isClaimable(Player player, RewardContents reward) {
UserData user = TimeRewardAPI.getUserManager().getData(player);
return !user.isClaimed(reward.getRewardID()) // 未曾领取
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ public class RewardContents {
private final @Nullable String permission;
private final @NotNull List<String> commands;

private final boolean auto;


public RewardContents(@NotNull String id, long time,
@Nullable String name, @Nullable String permission,
@NotNull List<String> commands) {
@NotNull List<String> commands, boolean auto) {
this.id = id;
this.time = time;
this.name = name;
this.permission = permission;
this.commands = commands;
this.auto = auto;
}

public String getRewardID() {
Expand All @@ -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);
}
Expand All @@ -67,6 +74,7 @@ public Map<String, Object> 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;
}

Expand All @@ -81,15 +89,17 @@ 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)
);
}

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
);
}

Expand Down

0 comments on commit 6585f7f

Please sign in to comment.