diff --git a/pom.xml b/pom.xml
index 0e5907a..9e6b660 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,12 +13,12 @@
1.5.2
0.4.6
- 2.3.3
+ 2.4.0
cc.carm.plugin
timereward
- 1.3.0
+ 2.0.0
TimeReward
在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。
@@ -100,7 +100,16 @@
compile
true
-
+
+
+ cc.carm.lib
+ easyplugin-placeholderapi
+ ${deps.easyplugin.version}
+ compile
+ true
+
+
+
cc.carm.lib
easyplugin-githubchecker
diff --git a/src/main/java/cc/carm/plugin/timereward/Main.java b/src/main/java/cc/carm/plugin/timereward/Main.java
index 40b7c24..a611672 100644
--- a/src/main/java/cc/carm/plugin/timereward/Main.java
+++ b/src/main/java/cc/carm/plugin/timereward/Main.java
@@ -8,11 +8,11 @@ import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration;
import cc.carm.plugin.timereward.command.TimeRewardCommand;
import cc.carm.plugin.timereward.conf.PluginConfig;
import cc.carm.plugin.timereward.conf.PluginMessages;
-import cc.carm.plugin.timereward.storage.database.MySQLStorage;
import cc.carm.plugin.timereward.hooker.PAPIExpansion;
import cc.carm.plugin.timereward.listener.UserListener;
import cc.carm.plugin.timereward.manager.RewardManager;
import cc.carm.plugin.timereward.manager.UserManager;
+import cc.carm.plugin.timereward.storage.database.MySQLStorage;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
@@ -22,7 +22,7 @@ public class Main extends EasyPlugin {
protected ConfigurationProvider> configProvider;
protected ConfigurationProvider> messageProvider;
- protected MySQLStorage mySQLStorage;
+ protected MySQLStorage storage;
protected UserManager userManager;
protected RewardManager rewardManager;
@@ -43,9 +43,9 @@ public class Main extends EasyPlugin {
@Override
protected boolean initialize() {
log("初始化数据管理器...");
- this.mySQLStorage = new MySQLStorage();
+ this.storage = new MySQLStorage();
try {
- mySQLStorage.initialize();
+ storage.initialize();
} catch (Exception e) {
severe("初始化存储失败,请检查配置文件。");
setEnabled(false);
@@ -61,7 +61,7 @@ public class Main extends EasyPlugin {
log("加载奖励管理器...");
this.rewardManager = new RewardManager(this);
- log("加载了 " + this.rewardManager.listRewards().size() + " 个奖励配置。");
+ debug("加载了 " + this.rewardManager.listRewards().size() + " 个奖励配置。");
log("注册监听器...");
registerListener(new UserListener());
@@ -71,7 +71,7 @@ public class Main extends EasyPlugin {
if (MessageUtils.hasPlaceholderAPI()) {
log("注册变量...");
- new PAPIExpansion(this).register();
+ new PAPIExpansion(this, "TimeReward").register();
} else {
log("未安装PlaceholderAPI,跳过变量注册...");
}
@@ -128,7 +128,7 @@ public class Main extends EasyPlugin {
}
public static MySQLStorage getStorage() {
- return getInstance().mySQLStorage;
+ return getInstance().storage;
}
public ConfigurationProvider> getConfigProvider() {
diff --git a/src/main/java/cc/carm/plugin/timereward/command/TimeRewardCommand.java b/src/main/java/cc/carm/plugin/timereward/command/TimeRewardCommand.java
index 644a9c3..460743b 100644
--- a/src/main/java/cc/carm/plugin/timereward/command/TimeRewardCommand.java
+++ b/src/main/java/cc/carm/plugin/timereward/command/TimeRewardCommand.java
@@ -48,7 +48,7 @@ public class TimeRewardCommand implements CommandExecutor, TabCompleter {
}
return true;
- } else if (aim.equalsIgnoreCase("listUserData")) {
+ } else if (aim.equalsIgnoreCase("list")) {
Collection awards = TimeRewardAPI.getRewardManager().listRewards().values();
PluginMessages.LIST.HEADER.send(sender, awards.size());
@@ -113,7 +113,7 @@ public class TimeRewardCommand implements CommandExecutor, TabCompleter {
case 1: {
allCompletes.add("reload");
allCompletes.add("user");
- allCompletes.add("listUserData");
+ allCompletes.add("list");
if (sender instanceof Player) allCompletes.add("test");
break;
diff --git a/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java b/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java
index 029e770..c7fed11 100644
--- a/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java
+++ b/src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java
@@ -2,20 +2,9 @@ package cc.carm.plugin.timereward.conf;
import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
-import cc.carm.lib.configuration.core.util.MapFactory;
import cc.carm.lib.configuration.core.value.ConfigValue;
-import cc.carm.lib.configuration.core.value.type.ConfiguredList;
-import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
-import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
-import cc.carm.plugin.timereward.Main;
-import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.storage.RewardContents;
-import org.bukkit.configuration.ConfigurationSection;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
public class PluginConfig extends ConfigurationRoot {
@@ -39,43 +28,25 @@ public class PluginConfig extends ConfigurationRoot {
public static final class STORAGE extends ConfigurationRoot {
}
- @HeaderComment("奖励相关设定")
- public static final class REWARDS extends ConfigurationRoot {
-
- @HeaderComment({
- "配置键名即奖励ID,支持英文、数字与下划线。",
- "确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
- "如果更改,原先领取过该奖励的玩家将会自动再领取一次!"
- })
- public static final class EXAMPLE extends ConfigurationRoot {
-
- @HeaderComment({
- "奖励的显示名称,可以是任意字符串",
- "可以在 commands 中使用 %(name) 来获取该奖励的名称",
- "也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称"
- })
- public static final ConfigValue NAME = ConfiguredValue.of(String.class, "&f[初级奖励] &e总在线时长 2小时");
-
- @HeaderComment("该奖励自动领取需要的在线时长,单位为秒")
- public static final ConfigValue TIME = ConfiguredValue.of(Integer.class, 7200);
-
- @HeaderComment({
- "该奖励领取权限,可以不设置。",
- "若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
- })
- public static final ConfigValue PERMISSION = ConfiguredValue.of(String.class, "TimeReward.vip");
-
- @HeaderComment({
- "领取奖励时后台执行的指令",
- "支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。"
- })
- public static final ConfiguredList COMMANDS = ConfiguredList.builder(String.class)
- .fromString()
- .defaults("say &f恭喜 &b%player_name% &f领取了奖励 &r%(name) &f!")
- .build();
-
- }
-
- }
+ @HeaderComment({"奖励相关设定,包含以下设定:",
+ " [id] 配置键名即奖励ID,支持英文、数字与下划线。",
+ " | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
+ " | 如果更改,原先领取过该奖励的玩家将会自动再领取一次!",
+ " [name] 奖励的显示名称,可以是任意字符串",
+ " | 可以在 commands 中使用 %(name) 来获取该奖励的名称",
+ " | 也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称",
+ " [permission] 领取奖励时后台执行的指令",
+ " | 支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。",
+ " [commands] 该奖励领取权限,可以不设置。",
+ " | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
+ })
+ public static final ConfigValue REWARDS = ConfigValue.builder()
+ .asValue(RewardContents.Group.class).fromSection()
+ .parseValue((v, d) -> RewardContents.Group.parse(v))
+ .serializeValue(RewardContents.Group::serialize)
+ .defaults(RewardContents.Group.defaults())
+ .build();
}
+
+
diff --git a/src/main/java/cc/carm/plugin/timereward/hooker/PAPIExpansion.java b/src/main/java/cc/carm/plugin/timereward/hooker/PAPIExpansion.java
index d7cbb56..5fb9a95 100644
--- a/src/main/java/cc/carm/plugin/timereward/hooker/PAPIExpansion.java
+++ b/src/main/java/cc/carm/plugin/timereward/hooker/PAPIExpansion.java
@@ -1,88 +1,54 @@
package cc.carm.plugin.timereward.hooker;
+import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
+import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler;
import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.storage.UserData;
-import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
-import java.util.Arrays;
-import java.util.List;
+import java.util.Collections;
+import java.util.function.BiFunction;
+import java.util.function.Function;
-public class PAPIExpansion extends PlaceholderExpansion {
+public class PAPIExpansion extends EasyPlaceholder {
- private static final List PLACEHOLDERS = Arrays.asList(
- "%TimeReward_time%",
- "%TimeReward_reward_<奖励ID>%",
- "%TimeReward_claimed_<奖励ID>%"
- );
+ public PAPIExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
+ super(plugin, rootIdentifier);
- private final JavaPlugin plugin;
-
- public PAPIExpansion(JavaPlugin plugin) {
- this.plugin = plugin;
+ handle("time", userHandler(UserData::getAllSeconds));
+ handle("reward",
+ rewardHandler(RewardContents::getDisplayName),
+ Collections.singletonList("<奖励ID>")
+ );
+ handle("claimed", userHandler((user, args) -> {
+ if (args.length < 1) return "请填写奖励ID";
+ else return user.isClaimed(args[0]);
+ }), Collections.singletonList("<奖励ID>"));
+ handle("version", (player, args) -> getVersion());
}
- @Override
- public @NotNull List getPlaceholders() {
- return PLACEHOLDERS;
+ protected PlaceholderHandler userHandler(Function userFunction) {
+ return userHandler((user, args) -> userFunction.apply(user));
}
- @Override
- public boolean canRegister() {
- return true;
+ protected PlaceholderHandler userHandler(BiFunction userFunction) {
+ return (player, args) -> {
+ if (player == null || !player.isOnline()) return "加载中...";
+ return userFunction.apply(TimeRewardAPI.getUserManager().getData((Player) player), args);
+ };
}
- @Override
- public @NotNull String getAuthor() {
- return plugin.getDescription().getAuthors().toString();
- }
-
- @Override
- public @NotNull String getIdentifier() {
- return plugin.getDescription().getName();
- }
-
- @Override
- public @NotNull String getVersion() {
- return plugin.getDescription().getVersion();
- }
-
- @Override
- public String onPlaceholderRequest(Player player, @NotNull String identifier) {
- if (player == null) return "加载中...";
- String[] args = identifier.split("_");
-
- if (args.length < 1) {
- return "Error Params";
- }
-
- UserData user = TimeRewardAPI.getUserManager().getData(player);
-
- switch (args[0].toLowerCase()) {
- case "time": {
- return Long.toString(user.getAllSeconds());
- }
- case "reward": {
- if (args.length < 2) return "请填写奖励ID";
- String rewardName = args[1];
- RewardContents contents = TimeRewardAPI.getRewardManager().getReward(rewardName);
- if (contents == null) return "奖励不存在";
- return contents.getDisplayName();
- }
- case "claimed": {
- if (args.length < 2) return "请填写奖励ID";
- return Boolean.toString(user.isClaimed(args[1]));
- }
- case "version": {
- return getVersion();
- }
- default: {
- return "参数错误";
- }
- }
+ protected PlaceholderHandler rewardHandler(Function function) {
+ return (player, args) -> {
+ if (args.length < 1) return "请填写奖励ID";
+ String rewardName = args[0];
+ RewardContents contents = TimeRewardAPI.getRewardManager().getReward(rewardName);
+ if (contents == null) return "奖励不存在";
+ return function.apply(contents);
+ };
}
}
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 2b2c4fb..27ca4a3 100644
--- a/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java
+++ b/src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java
@@ -3,11 +3,10 @@ package cc.carm.plugin.timereward.manager;
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.PluginConfig;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.storage.UserData;
-import com.google.common.collect.ImmutableMap;
import org.bukkit.Bukkit;
-import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
@@ -15,15 +14,12 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Collections;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class RewardManager {
- protected final Map rewards = new ConcurrentHashMap<>();
protected BukkitRunnable runnable;
public RewardManager(Main main) {
@@ -51,38 +47,16 @@ public class RewardManager {
this.runnable = null;
}
- public Map readRewards(@NotNull ConfigurationSection section) {
- Map rewards = new HashMap<>();
- for (String rewardID : section.getKeys(false)) {
- ConfigurationSection rewardSection = section.getConfigurationSection(rewardID);
- if (rewardSection == null) continue;
- long time = rewardSection.getLong("time", -1);
- if (time <= 0) {
- Main.severe("奖励 " + rewardID + " 的时间配置错误,请检查配置文件。");
- continue;
- }
-
- rewards.put(rewardID, new RewardContents(
- rewardID, time,
- rewardSection.getString("name"),
- rewardSection.getString("permission"),
- rewardSection.getStringList("commands")
- ));
- }
- return rewards;
- }
-
- @Unmodifiable
- public Map getRewardsMap() {
- return Collections.unmodifiableMap(rewards);
+ protected Map getRewards() {
+ return PluginConfig.REWARDS.getNotNull().getContents();
}
public @Nullable RewardContents getReward(String rewardID) {
- return rewards.get(rewardID);
+ return getRewards().get(rewardID);
}
public Map listRewards() {
- return ImmutableMap.copyOf(getRewardsMap());
+ return Collections.unmodifiableMap(PluginConfig.REWARDS.getNotNull().getContents());
}
@Unmodifiable
diff --git a/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java b/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java
index f3e6887..93a6446 100644
--- a/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java
+++ b/src/main/java/cc/carm/plugin/timereward/storage/RewardContents.java
@@ -1,12 +1,13 @@
package cc.carm.plugin.timereward.storage;
+import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.easyplugin.utils.ColorParser;
+import cc.carm.plugin.timereward.Main;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
public class RewardContents {
@@ -60,6 +61,38 @@ public class RewardContents {
return requireSeconds >= getTime();
}
+ public Map serialize() {
+ Map map = new LinkedHashMap<>();
+ map.put("time", getTime());
+ if (getName() != null) map.put("name", getName());
+ if (getPermission() != null) map.put("permission", getPermission());
+ map.put("commands", getCommands());
+ return map;
+ }
+
+ public static RewardContents parse(String id, @NotNull ConfigurationWrapper> section) {
+ long time = section.getLong("time", -1L);
+ if (time <= 0) {
+ Main.severe("奖励 " + id + " 的时间配置错误,请检查配置文件。");
+ return null;
+ }
+
+ return new RewardContents(
+ id, time,
+ section.getString("name"),
+ section.getString("permission"),
+ section.getStringList("commands")
+ );
+ }
+
+ 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!")
+ );
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -73,5 +106,44 @@ public class RewardContents {
return Objects.hash(id);
}
+ public static final class Group {
+ final @NotNull Map contents;
+
+ public Group(@NotNull Map contents) {
+ this.contents = contents;
+ }
+
+ public @NotNull Map getContents() {
+ return contents;
+ }
+
+ public Map serialize() {
+ Map map = new LinkedHashMap<>();
+ for (Map.Entry entry : contents.entrySet()) {
+ map.put(entry.getKey(), entry.getValue().serialize());
+ }
+ return map;
+ }
+
+ public static Group parse(@NotNull ConfigurationWrapper> section) {
+ Map rewards = new LinkedHashMap<>();
+ for (String rewardID : section.getKeys(false)) {
+ ConfigurationWrapper> rewardSection = section.getConfigurationSection(rewardID);
+ if (rewardSection == null) continue;
+
+ RewardContents c = RewardContents.parse(rewardID, rewardSection);
+ if (c == null) continue;
+ rewards.put(rewardID, c);
+ }
+ return new Group(rewards);
+ }
+
+ public static Group defaults() {
+ Map rewards = new LinkedHashMap<>();
+ rewards.put("example", RewardContents.defaults("example"));
+ return new Group(rewards);
+ }
+
+ }
}
diff --git a/src/main/java/cc/carm/plugin/timereward/storage/database/MySQLStorage.java b/src/main/java/cc/carm/plugin/timereward/storage/database/MySQLStorage.java
index 8a869d1..3d3cb4e 100644
--- a/src/main/java/cc/carm/plugin/timereward/storage/database/MySQLStorage.java
+++ b/src/main/java/cc/carm/plugin/timereward/storage/database/MySQLStorage.java
@@ -62,8 +62,7 @@ public class MySQLStorage {
return new UserData(uuid, playTime, claimedData);
}
- @Nullable
- public Long loadPlayTime(@NotNull UUID uuid) throws Exception {
+ public long loadPlayTime(@NotNull UUID uuid) throws Exception {
return DatabaseTables.USER_TIMES.createQuery()
.selectColumns("uuid", "time")
.addCondition("uuid", uuid).setLimit(1).build()
@@ -71,7 +70,7 @@ public class MySQLStorage {
ResultSet resultSet = query.getResultSet();
if (resultSet == null || !resultSet.next()) return 0L;
return resultSet.getLong("time");
- });
+ }, 0L);
}
@NotNull