|
10 | 10 | import org.bukkit.plugin.java.JavaPlugin; |
11 | 11 | import org.jetbrains.annotations.NotNull; |
12 | 12 |
|
| 13 | +import java.time.Duration; |
13 | 14 | import java.util.Collections; |
14 | 15 | import java.util.function.BiFunction; |
15 | 16 | import java.util.function.Function; |
| 17 | +import java.util.function.ToLongFunction; |
16 | 18 |
|
17 | 19 | public class PAPIExpansion extends EasyPlaceholder { |
18 | 20 |
|
19 | 21 | public PAPIExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) { |
20 | 22 | super(plugin, rootIdentifier); |
21 | 23 |
|
22 | | - handle("seconds", userHandler((user, args) -> { |
23 | | - if (args.length < 1) return "请填写时间类型"; |
24 | | - IntervalType type = IntervalType.parse(args[0]); |
25 | | - |
26 | | - if (type == null) return "时间类型不存在"; |
27 | | - |
28 | | - return user.getOnlineDuration(type).getSeconds(); |
29 | | - }), Collections.singletonList("<时间类型>"), "time"); |
30 | | - |
31 | | - handle("minutes", userHandler((user, args) -> { |
32 | | - if (args.length < 1) return "请填写时间类型"; |
33 | | - IntervalType type = IntervalType.parse(args[0]); |
34 | | - |
35 | | - if (type == null) return "时间类型不存在"; |
36 | | - |
37 | | - return user.getOnlineDuration(type).toMinutes(); |
38 | | - }), Collections.singletonList("<时间类型>")); |
39 | | - |
40 | | - handle("hours", userHandler((user, args) -> { |
41 | | - if (args.length < 1) return "请填写时间类型"; |
42 | | - IntervalType type = IntervalType.parse(args[0]); |
43 | | - |
44 | | - if (type == null) return "时间类型不存在"; |
45 | | - |
46 | | - return user.getOnlineDuration(type).toHours(); |
47 | | - }), Collections.singletonList("<时间类型>")); |
48 | | - |
49 | | - handle("days", userHandler((user, args) -> { |
50 | | - if (args.length < 1) return "请填写时间类型"; |
51 | | - IntervalType type = IntervalType.parse(args[0]); |
52 | | - |
53 | | - if (type == null) return "时间类型不存在"; |
54 | | - |
55 | | - return user.getOnlineDuration(type).toDays(); |
56 | | - }), Collections.singletonList("<时间类型>")); |
| 24 | + handle("seconds", timeHandler(Duration::getSeconds), Collections.singletonList("<时间类型>"), "time"); |
| 25 | + handle("minutes", timeHandler(Duration::toMinutes), Collections.singletonList("<时间类型>")); |
| 26 | + handle("hours", timeHandler(Duration::toHours), Collections.singletonList("<时间类型>")); |
| 27 | + handle("days", timeHandler(Duration::toDays), Collections.singletonList("<时间类型>")); |
57 | 28 |
|
58 | 29 | handle("reward", |
59 | 30 | rewardHandler(RewardContents::getDisplayName), |
@@ -89,10 +60,21 @@ protected <R> PlaceholderHandler userHandler(Function<UserRewardData, R> userFun |
89 | 60 | protected <R> PlaceholderHandler userHandler(BiFunction<UserRewardData, String[], R> userFunction) { |
90 | 61 | return (player, args) -> { |
91 | 62 | if (player == null || !player.isOnline()) return "加载中..."; |
92 | | - return userFunction.apply(TimeRewardAPI.getUserManager().get((Player) player), args); |
| 63 | + UserRewardData data = TimeRewardAPI.getUserManager().get((Player) player); |
| 64 | + if (data == null) return "加载中..."; |
| 65 | + return userFunction.apply(data, args); |
93 | 66 | }; |
94 | 67 | } |
95 | 68 |
|
| 69 | + protected PlaceholderHandler timeHandler(ToLongFunction<Duration> timeFunction) { |
| 70 | + return userHandler((user, args) -> { |
| 71 | + if (args.length < 1) return "请填写时间类型"; |
| 72 | + IntervalType type = IntervalType.parse(args[0]); |
| 73 | + if (type == null) return "时间类型不存在"; |
| 74 | + return timeFunction.applyAsLong(user.getOnlineDuration(type)); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
96 | 78 | protected <R> PlaceholderHandler rewardHandler(Function<RewardContents, R> function) { |
97 | 79 | return (player, args) -> { |
98 | 80 | if (args.length < 1) return "请填写奖励ID"; |
|
0 commit comments