Skip to content

Commit d2600d3

Browse files
committed
feat(update): 适配最新依赖版本。
1 parent 45346b0 commit d2600d3

8 files changed

Lines changed: 157 additions & 166 deletions

File tree

pom.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
<deps.easyplugin.version>1.5.2</deps.easyplugin.version>
1515
<deps.easysql.version>0.4.6</deps.easysql.version>
16-
<deps.mineconfig.version>2.3.3</deps.mineconfig.version>
16+
<deps.mineconfig.version>2.4.0</deps.mineconfig.version>
1717
</properties>
1818

1919
<groupId>cc.carm.plugin</groupId>
2020
<artifactId>timereward</artifactId>
21-
<version>1.3.0</version>
21+
<version>2.0.0</version>
2222

2323
<name>TimeReward</name>
2424
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
@@ -100,7 +100,16 @@
100100
<scope>compile</scope>
101101
<optional>true</optional>
102102
</dependency>
103-
103+
104+
<dependency>
105+
<groupId>cc.carm.lib</groupId>
106+
<artifactId>easyplugin-placeholderapi</artifactId>
107+
<version>${deps.easyplugin.version}</version>
108+
<scope>compile</scope>
109+
<optional>true</optional>
110+
</dependency>
111+
112+
<!--suppress VulnerableLibrariesLocal -->
104113
<dependency>
105114
<groupId>cc.carm.lib</groupId>
106115
<artifactId>easyplugin-githubchecker</artifactId>

src/main/java/cc/carm/plugin/timereward/Main.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import cc.carm.plugin.timereward.command.TimeRewardCommand;
99
import cc.carm.plugin.timereward.conf.PluginConfig;
1010
import cc.carm.plugin.timereward.conf.PluginMessages;
11-
import cc.carm.plugin.timereward.storage.database.MySQLStorage;
1211
import cc.carm.plugin.timereward.hooker.PAPIExpansion;
1312
import cc.carm.plugin.timereward.listener.UserListener;
1413
import cc.carm.plugin.timereward.manager.RewardManager;
1514
import cc.carm.plugin.timereward.manager.UserManager;
15+
import cc.carm.plugin.timereward.storage.database.MySQLStorage;
1616
import org.bstats.bukkit.Metrics;
1717
import org.bukkit.Bukkit;
1818

@@ -22,7 +22,7 @@ public class Main extends EasyPlugin {
2222
protected ConfigurationProvider<?> configProvider;
2323
protected ConfigurationProvider<?> messageProvider;
2424

25-
protected MySQLStorage mySQLStorage;
25+
protected MySQLStorage storage;
2626
protected UserManager userManager;
2727
protected RewardManager rewardManager;
2828

@@ -43,9 +43,9 @@ protected void load() {
4343
@Override
4444
protected boolean initialize() {
4545
log("初始化数据管理器...");
46-
this.mySQLStorage = new MySQLStorage();
46+
this.storage = new MySQLStorage();
4747
try {
48-
mySQLStorage.initialize();
48+
storage.initialize();
4949
} catch (Exception e) {
5050
severe("初始化存储失败,请检查配置文件。");
5151
setEnabled(false);
@@ -61,7 +61,7 @@ protected boolean initialize() {
6161

6262
log("加载奖励管理器...");
6363
this.rewardManager = new RewardManager(this);
64-
log("加载了 " + this.rewardManager.listRewards().size() + " 个奖励配置。");
64+
debug("加载了 " + this.rewardManager.listRewards().size() + " 个奖励配置。");
6565

6666
log("注册监听器...");
6767
registerListener(new UserListener());
@@ -71,7 +71,7 @@ protected boolean initialize() {
7171

7272
if (MessageUtils.hasPlaceholderAPI()) {
7373
log("注册变量...");
74-
new PAPIExpansion(this).register();
74+
new PAPIExpansion(this, "TimeReward").register();
7575
} else {
7676
log("未安装PlaceholderAPI,跳过变量注册...");
7777
}
@@ -128,7 +128,7 @@ public static Main getInstance() {
128128
}
129129

130130
public static MySQLStorage getStorage() {
131-
return getInstance().mySQLStorage;
131+
return getInstance().storage;
132132
}
133133

134134
public ConfigurationProvider<?> getConfigProvider() {

src/main/java/cc/carm/plugin/timereward/command/TimeRewardCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
4848
}
4949

5050
return true;
51-
} else if (aim.equalsIgnoreCase("listUserData")) {
51+
} else if (aim.equalsIgnoreCase("list")) {
5252
Collection<RewardContents> awards = TimeRewardAPI.getRewardManager().listRewards().values();
5353
PluginMessages.LIST.HEADER.send(sender, awards.size());
5454

@@ -113,7 +113,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
113113
case 1: {
114114
allCompletes.add("reload");
115115
allCompletes.add("user");
116-
allCompletes.add("listUserData");
116+
allCompletes.add("list");
117117
if (sender instanceof Player) allCompletes.add("test");
118118

119119
break;

src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,9 @@
22

33
import cc.carm.lib.configuration.core.ConfigurationRoot;
44
import cc.carm.lib.configuration.core.annotation.HeaderComment;
5-
import cc.carm.lib.configuration.core.util.MapFactory;
65
import cc.carm.lib.configuration.core.value.ConfigValue;
7-
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
8-
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
9-
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
106
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
11-
import cc.carm.plugin.timereward.Main;
12-
import cc.carm.plugin.timereward.TimeRewardAPI;
137
import cc.carm.plugin.timereward.storage.RewardContents;
14-
import org.bukkit.configuration.ConfigurationSection;
15-
16-
import java.util.Collections;
17-
import java.util.HashMap;
18-
import java.util.List;
198

209
public class PluginConfig extends ConfigurationRoot {
2110

@@ -39,43 +28,25 @@ public class PluginConfig extends ConfigurationRoot {
3928
public static final class STORAGE extends ConfigurationRoot {
4029
}
4130

42-
@HeaderComment("奖励相关设定")
43-
public static final class REWARDS extends ConfigurationRoot {
44-
45-
@HeaderComment({
46-
"配置键名即奖励ID,支持英文、数字与下划线。",
47-
"确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
48-
"如果更改,原先领取过该奖励的玩家将会自动再领取一次!"
49-
})
50-
public static final class EXAMPLE extends ConfigurationRoot {
51-
52-
@HeaderComment({
53-
"奖励的显示名称,可以是任意字符串",
54-
"可以在 commands 中使用 %(name) 来获取该奖励的名称",
55-
"也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称"
56-
})
57-
public static final ConfigValue<String> NAME = ConfiguredValue.of(String.class, "&f[初级奖励] &e总在线时长 2小时");
58-
59-
@HeaderComment("该奖励自动领取需要的在线时长,单位为秒")
60-
public static final ConfigValue<Integer> TIME = ConfiguredValue.of(Integer.class, 7200);
61-
62-
@HeaderComment({
63-
"该奖励领取权限,可以不设置。",
64-
"若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
65-
})
66-
public static final ConfigValue<String> PERMISSION = ConfiguredValue.of(String.class, "TimeReward.vip");
31+
@HeaderComment({"奖励相关设定,包含以下设定:",
32+
" [id] 配置键名即奖励ID,支持英文、数字与下划线。",
33+
" | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
34+
" | 如果更改,原先领取过该奖励的玩家将会自动再领取一次!",
35+
" [name] 奖励的显示名称,可以是任意字符串",
36+
" | 可以在 commands 中使用 %(name) 来获取该奖励的名称",
37+
" | 也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称",
38+
" [permission] 领取奖励时后台执行的指令",
39+
" | 支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。",
40+
" [commands] 该奖励领取权限,可以不设置。",
41+
" | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
42+
})
43+
public static final ConfigValue<RewardContents.Group> REWARDS = ConfigValue.builder()
44+
.asValue(RewardContents.Group.class).fromSection()
45+
.parseValue((v, d) -> RewardContents.Group.parse(v))
46+
.serializeValue(RewardContents.Group::serialize)
47+
.defaults(RewardContents.Group.defaults())
48+
.build();
6749

68-
@HeaderComment({
69-
"领取奖励时后台执行的指令",
70-
"支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。"
71-
})
72-
public static final ConfiguredList<String> COMMANDS = ConfiguredList.builder(String.class)
73-
.fromString()
74-
.defaults("say &f恭喜 &b%player_name% &f领取了奖励 &r%(name) &f!")
75-
.build();
50+
}
7651

77-
}
7852

79-
}
80-
81-
}
Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,54 @@
11
package cc.carm.plugin.timereward.hooker;
22

3+
import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
4+
import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler;
35
import cc.carm.plugin.timereward.TimeRewardAPI;
46
import cc.carm.plugin.timereward.storage.RewardContents;
57
import cc.carm.plugin.timereward.storage.UserData;
6-
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
78
import org.bukkit.entity.Player;
89
import org.bukkit.plugin.java.JavaPlugin;
910
import org.jetbrains.annotations.NotNull;
1011

11-
import java.util.Arrays;
12-
import java.util.List;
13-
14-
public class PAPIExpansion extends PlaceholderExpansion {
15-
16-
private static final List<String> PLACEHOLDERS = Arrays.asList(
17-
"%TimeReward_time%",
18-
"%TimeReward_reward_<奖励ID>%",
19-
"%TimeReward_claimed_<奖励ID>%"
20-
);
21-
22-
private final JavaPlugin plugin;
23-
24-
public PAPIExpansion(JavaPlugin plugin) {
25-
this.plugin = plugin;
12+
import java.util.Collections;
13+
import java.util.function.BiFunction;
14+
import java.util.function.Function;
15+
16+
public class PAPIExpansion extends EasyPlaceholder {
17+
18+
public PAPIExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
19+
super(plugin, rootIdentifier);
20+
21+
handle("time", userHandler(UserData::getAllSeconds));
22+
handle("reward",
23+
rewardHandler(RewardContents::getDisplayName),
24+
Collections.singletonList("<奖励ID>")
25+
);
26+
handle("claimed", userHandler((user, args) -> {
27+
if (args.length < 1) return "请填写奖励ID";
28+
else return user.isClaimed(args[0]);
29+
}), Collections.singletonList("<奖励ID>"));
30+
handle("version", (player, args) -> getVersion());
2631
}
2732

28-
@Override
29-
public @NotNull List<String> getPlaceholders() {
30-
return PLACEHOLDERS;
33+
protected <R> PlaceholderHandler userHandler(Function<UserData, R> userFunction) {
34+
return userHandler((user, args) -> userFunction.apply(user));
3135
}
3236

33-
@Override
34-
public boolean canRegister() {
35-
return true;
37+
protected <R> PlaceholderHandler userHandler(BiFunction<UserData, String[], R> userFunction) {
38+
return (player, args) -> {
39+
if (player == null || !player.isOnline()) return "加载中...";
40+
return userFunction.apply(TimeRewardAPI.getUserManager().getData((Player) player), args);
41+
};
3642
}
3743

38-
@Override
39-
public @NotNull String getAuthor() {
40-
return plugin.getDescription().getAuthors().toString();
41-
}
42-
43-
@Override
44-
public @NotNull String getIdentifier() {
45-
return plugin.getDescription().getName();
46-
}
47-
48-
@Override
49-
public @NotNull String getVersion() {
50-
return plugin.getDescription().getVersion();
51-
}
52-
53-
@Override
54-
public String onPlaceholderRequest(Player player, @NotNull String identifier) {
55-
if (player == null) return "加载中...";
56-
String[] args = identifier.split("_");
57-
58-
if (args.length < 1) {
59-
return "Error Params";
60-
}
61-
62-
UserData user = TimeRewardAPI.getUserManager().getData(player);
63-
64-
switch (args[0].toLowerCase()) {
65-
case "time": {
66-
return Long.toString(user.getAllSeconds());
67-
}
68-
case "reward": {
69-
if (args.length < 2) return "请填写奖励ID";
70-
String rewardName = args[1];
71-
RewardContents contents = TimeRewardAPI.getRewardManager().getReward(rewardName);
72-
if (contents == null) return "奖励不存在";
73-
return contents.getDisplayName();
74-
}
75-
case "claimed": {
76-
if (args.length < 2) return "请填写奖励ID";
77-
return Boolean.toString(user.isClaimed(args[1]));
78-
}
79-
case "version": {
80-
return getVersion();
81-
}
82-
default: {
83-
return "参数错误";
84-
}
85-
}
44+
protected <R> PlaceholderHandler rewardHandler(Function<RewardContents, R> function) {
45+
return (player, args) -> {
46+
if (args.length < 1) return "请填写奖励ID";
47+
String rewardName = args[0];
48+
RewardContents contents = TimeRewardAPI.getRewardManager().getReward(rewardName);
49+
if (contents == null) return "奖励不存在";
50+
return function.apply(contents);
51+
};
8652
}
8753

8854
}

src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,23 @@
33
import cc.carm.lib.easyplugin.utils.MessageUtils;
44
import cc.carm.plugin.timereward.Main;
55
import cc.carm.plugin.timereward.TimeRewardAPI;
6+
import cc.carm.plugin.timereward.conf.PluginConfig;
67
import cc.carm.plugin.timereward.storage.RewardContents;
78
import cc.carm.plugin.timereward.storage.UserData;
8-
import com.google.common.collect.ImmutableMap;
99
import org.bukkit.Bukkit;
10-
import org.bukkit.configuration.ConfigurationSection;
1110
import org.bukkit.entity.Player;
1211
import org.bukkit.scheduler.BukkitRunnable;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
1514
import org.jetbrains.annotations.Unmodifiable;
1615

1716
import java.util.Collections;
18-
import java.util.HashMap;
1917
import java.util.List;
2018
import java.util.Map;
21-
import java.util.concurrent.ConcurrentHashMap;
2219
import java.util.stream.Collectors;
2320

2421
public class RewardManager {
2522

26-
protected final Map<String, RewardContents> rewards = new ConcurrentHashMap<>();
2723
protected BukkitRunnable runnable;
2824

2925
public RewardManager(Main main) {
@@ -51,38 +47,16 @@ public void shutdown() {
5147
this.runnable = null;
5248
}
5349

54-
public Map<String, RewardContents> readRewards(@NotNull ConfigurationSection section) {
55-
Map<String, RewardContents> rewards = new HashMap<>();
56-
for (String rewardID : section.getKeys(false)) {
57-
ConfigurationSection rewardSection = section.getConfigurationSection(rewardID);
58-
if (rewardSection == null) continue;
59-
long time = rewardSection.getLong("time", -1);
60-
if (time <= 0) {
61-
Main.severe("奖励 " + rewardID + " 的时间配置错误,请检查配置文件。");
62-
continue;
63-
}
64-
65-
rewards.put(rewardID, new RewardContents(
66-
rewardID, time,
67-
rewardSection.getString("name"),
68-
rewardSection.getString("permission"),
69-
rewardSection.getStringList("commands")
70-
));
71-
}
72-
return rewards;
73-
}
74-
75-
@Unmodifiable
76-
public Map<String, RewardContents> getRewardsMap() {
77-
return Collections.unmodifiableMap(rewards);
50+
protected Map<String, RewardContents> getRewards() {
51+
return PluginConfig.REWARDS.getNotNull().getContents();
7852
}
7953

8054
public @Nullable RewardContents getReward(String rewardID) {
81-
return rewards.get(rewardID);
55+
return getRewards().get(rewardID);
8256
}
8357

8458
public Map<String, RewardContents> listRewards() {
85-
return ImmutableMap.copyOf(getRewardsMap());
59+
return Collections.unmodifiableMap(PluginConfig.REWARDS.getNotNull().getContents());
8660
}
8761

8862
@Unmodifiable

0 commit comments

Comments
 (0)