|
1 | 1 | package cc.carm.plugin.timereward.hooker; |
2 | 2 |
|
| 3 | +import cc.carm.lib.easyplugin.papi.EasyPlaceholder; |
| 4 | +import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler; |
3 | 5 | import cc.carm.plugin.timereward.TimeRewardAPI; |
4 | 6 | import cc.carm.plugin.timereward.storage.RewardContents; |
5 | 7 | import cc.carm.plugin.timereward.storage.UserData; |
6 | | -import me.clip.placeholderapi.expansion.PlaceholderExpansion; |
7 | 8 | import org.bukkit.entity.Player; |
8 | 9 | import org.bukkit.plugin.java.JavaPlugin; |
9 | 10 | import org.jetbrains.annotations.NotNull; |
10 | 11 |
|
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()); |
26 | 31 | } |
27 | 32 |
|
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)); |
31 | 35 | } |
32 | 36 |
|
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 | + }; |
36 | 42 | } |
37 | 43 |
|
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 | + }; |
86 | 52 | } |
87 | 53 |
|
88 | 54 | } |
0 commit comments