1
mirror of https://github.com/carm-outsource/TimeReward.git synced 2024-09-19 19:25:49 +00:00

feat(update): 适配最新依赖版本。

This commit is contained in:
Carm Jos 2023-02-22 20:32:36 +08:00
parent 45346b093a
commit d2600d308c
8 changed files with 155 additions and 164 deletions

15
pom.xml
View File

@ -13,12 +13,12 @@
<deps.easyplugin.version>1.5.2</deps.easyplugin.version> <deps.easyplugin.version>1.5.2</deps.easyplugin.version>
<deps.easysql.version>0.4.6</deps.easysql.version> <deps.easysql.version>0.4.6</deps.easysql.version>
<deps.mineconfig.version>2.3.3</deps.mineconfig.version> <deps.mineconfig.version>2.4.0</deps.mineconfig.version>
</properties> </properties>
<groupId>cc.carm.plugin</groupId> <groupId>cc.carm.plugin</groupId>
<artifactId>timereward</artifactId> <artifactId>timereward</artifactId>
<version>1.3.0</version> <version>2.0.0</version>
<name>TimeReward</name> <name>TimeReward</name>
<description>在线时长自动领奖插件通过指令发放奖励基于EasyPlugin实现。</description> <description>在线时长自动领奖插件通过指令发放奖励基于EasyPlugin实现。</description>
@ -100,7 +100,16 @@
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-placeholderapi</artifactId>
<version>${deps.easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!--suppress VulnerableLibrariesLocal -->
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-githubchecker</artifactId> <artifactId>easyplugin-githubchecker</artifactId>

View File

@ -8,11 +8,11 @@ import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration;
import cc.carm.plugin.timereward.command.TimeRewardCommand; import cc.carm.plugin.timereward.command.TimeRewardCommand;
import cc.carm.plugin.timereward.conf.PluginConfig; import cc.carm.plugin.timereward.conf.PluginConfig;
import cc.carm.plugin.timereward.conf.PluginMessages; 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.hooker.PAPIExpansion;
import cc.carm.plugin.timereward.listener.UserListener; import cc.carm.plugin.timereward.listener.UserListener;
import cc.carm.plugin.timereward.manager.RewardManager; import cc.carm.plugin.timereward.manager.RewardManager;
import cc.carm.plugin.timereward.manager.UserManager; import cc.carm.plugin.timereward.manager.UserManager;
import cc.carm.plugin.timereward.storage.database.MySQLStorage;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -22,7 +22,7 @@ public class Main extends EasyPlugin {
protected ConfigurationProvider<?> configProvider; protected ConfigurationProvider<?> configProvider;
protected ConfigurationProvider<?> messageProvider; protected ConfigurationProvider<?> messageProvider;
protected MySQLStorage mySQLStorage; protected MySQLStorage storage;
protected UserManager userManager; protected UserManager userManager;
protected RewardManager rewardManager; protected RewardManager rewardManager;
@ -43,9 +43,9 @@ public class Main extends EasyPlugin {
@Override @Override
protected boolean initialize() { protected boolean initialize() {
log("初始化数据管理器..."); log("初始化数据管理器...");
this.mySQLStorage = new MySQLStorage(); this.storage = new MySQLStorage();
try { try {
mySQLStorage.initialize(); storage.initialize();
} catch (Exception e) { } catch (Exception e) {
severe("初始化存储失败,请检查配置文件。"); severe("初始化存储失败,请检查配置文件。");
setEnabled(false); setEnabled(false);
@ -61,7 +61,7 @@ public class Main extends EasyPlugin {
log("加载奖励管理器..."); log("加载奖励管理器...");
this.rewardManager = new RewardManager(this); this.rewardManager = new RewardManager(this);
log("加载了 " + this.rewardManager.listRewards().size() + " 个奖励配置。"); debug("加载了 " + this.rewardManager.listRewards().size() + " 个奖励配置。");
log("注册监听器..."); log("注册监听器...");
registerListener(new UserListener()); registerListener(new UserListener());
@ -71,7 +71,7 @@ public class Main extends EasyPlugin {
if (MessageUtils.hasPlaceholderAPI()) { if (MessageUtils.hasPlaceholderAPI()) {
log("注册变量..."); log("注册变量...");
new PAPIExpansion(this).register(); new PAPIExpansion(this, "TimeReward").register();
} else { } else {
log("未安装PlaceholderAPI跳过变量注册..."); log("未安装PlaceholderAPI跳过变量注册...");
} }
@ -128,7 +128,7 @@ public class Main extends EasyPlugin {
} }
public static MySQLStorage getStorage() { public static MySQLStorage getStorage() {
return getInstance().mySQLStorage; return getInstance().storage;
} }
public ConfigurationProvider<?> getConfigProvider() { public ConfigurationProvider<?> getConfigProvider() {

View File

@ -48,7 +48,7 @@ public class TimeRewardCommand implements CommandExecutor, TabCompleter {
} }
return true; return true;
} else if (aim.equalsIgnoreCase("listUserData")) { } else if (aim.equalsIgnoreCase("list")) {
Collection<RewardContents> awards = TimeRewardAPI.getRewardManager().listRewards().values(); Collection<RewardContents> awards = TimeRewardAPI.getRewardManager().listRewards().values();
PluginMessages.LIST.HEADER.send(sender, awards.size()); PluginMessages.LIST.HEADER.send(sender, awards.size());
@ -113,7 +113,7 @@ public class TimeRewardCommand implements CommandExecutor, TabCompleter {
case 1: { case 1: {
allCompletes.add("reload"); allCompletes.add("reload");
allCompletes.add("user"); allCompletes.add("user");
allCompletes.add("listUserData"); allCompletes.add("list");
if (sender instanceof Player) allCompletes.add("test"); if (sender instanceof Player) allCompletes.add("test");
break; break;

View File

@ -2,20 +2,9 @@ package cc.carm.plugin.timereward.conf;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.HeaderComment; 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.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.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 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 { public class PluginConfig extends ConfigurationRoot {
@ -39,43 +28,25 @@ public class PluginConfig extends ConfigurationRoot {
public static final class STORAGE extends ConfigurationRoot { public static final class STORAGE extends ConfigurationRoot {
} }
@HeaderComment("奖励相关设定") @HeaderComment({"奖励相关设定,包含以下设定:",
public static final class REWARDS extends ConfigurationRoot { " [id] 配置键名即奖励ID支持英文、数字与下划线。",
" | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
@HeaderComment({ " | 如果更改,原先领取过该奖励的玩家将会自动再领取一次!",
"配置键名即奖励ID支持英文、数字与下划线。", " [name] 奖励的显示名称,可以是任意字符串",
"确定后请不要更改,因为该键值用于存储玩家是否领取的数据", " | 可以在 commands 中使用 %(name) 来获取该奖励的名称",
"如果更改,原先领取过该奖励的玩家将会自动再领取一次!" " | 也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称",
}) " [permission] 领取奖励时后台执行的指令",
public static final class EXAMPLE extends ConfigurationRoot { " | 支持PlaceholderAPI变量指令中可以使用 %(name) 来获取该奖励的名称。",
" [commands] 该奖励领取权限,可以不设置。",
@HeaderComment({ " | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
"奖励的显示名称,可以是任意字符串", })
"可以在 commands 中使用 %(name) 来获取该奖励的名称", public static final ConfigValue<RewardContents.Group> REWARDS = ConfigValue.builder()
"也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称" .asValue(RewardContents.Group.class).fromSection()
}) .parseValue((v, d) -> RewardContents.Group.parse(v))
public static final ConfigValue<String> NAME = ConfiguredValue.of(String.class, "&f[初级奖励] &e总在线时长 2小时"); .serializeValue(RewardContents.Group::serialize)
.defaults(RewardContents.Group.defaults())
@HeaderComment("该奖励自动领取需要的在线时长,单位为秒") .build();
public static final ConfigValue<Integer> TIME = ConfiguredValue.of(Integer.class, 7200);
@HeaderComment({
"该奖励领取权限,可以不设置。",
"若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
})
public static final ConfigValue<String> PERMISSION = ConfiguredValue.of(String.class, "TimeReward.vip");
@HeaderComment({
"领取奖励时后台执行的指令",
"支持PlaceholderAPI变量指令中可以使用 %(name) 来获取该奖励的名称。"
})
public static final ConfiguredList<String> COMMANDS = ConfiguredList.builder(String.class)
.fromString()
.defaults("say &f恭喜 &b%player_name% &f领取了奖励 &r%(name) &f")
.build();
}
}
} }

View File

@ -1,88 +1,54 @@
package cc.carm.plugin.timereward.hooker; 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.TimeRewardAPI;
import cc.carm.plugin.timereward.storage.RewardContents; import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.storage.UserData; import cc.carm.plugin.timereward.storage.UserData;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Collections;
import java.util.List; import java.util.function.BiFunction;
import java.util.function.Function;
public class PAPIExpansion extends PlaceholderExpansion { public class PAPIExpansion extends EasyPlaceholder {
private static final List<String> PLACEHOLDERS = Arrays.asList( public PAPIExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
"%TimeReward_time%", super(plugin, rootIdentifier);
"%TimeReward_reward_<奖励ID>%",
"%TimeReward_claimed_<奖励ID>%"
);
private final JavaPlugin plugin; handle("time", userHandler(UserData::getAllSeconds));
handle("reward",
public PAPIExpansion(JavaPlugin plugin) { rewardHandler(RewardContents::getDisplayName),
this.plugin = plugin; 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 protected <R> PlaceholderHandler userHandler(Function<UserData, R> userFunction) {
public @NotNull List<String> getPlaceholders() { return userHandler((user, args) -> userFunction.apply(user));
return PLACEHOLDERS;
} }
@Override protected <R> PlaceholderHandler userHandler(BiFunction<UserData, String[], R> userFunction) {
public boolean canRegister() { return (player, args) -> {
return true; if (player == null || !player.isOnline()) return "加载中...";
return userFunction.apply(TimeRewardAPI.getUserManager().getData((Player) player), args);
};
} }
@Override protected <R> PlaceholderHandler rewardHandler(Function<RewardContents, R> function) {
public @NotNull String getAuthor() { return (player, args) -> {
return plugin.getDescription().getAuthors().toString(); if (args.length < 1) return "请填写奖励ID";
} String rewardName = args[0];
RewardContents contents = TimeRewardAPI.getRewardManager().getReward(rewardName);
@Override if (contents == null) return "奖励不存在";
public @NotNull String getIdentifier() { return function.apply(contents);
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 "参数错误";
}
}
} }
} }

View File

@ -3,11 +3,10 @@ package cc.carm.plugin.timereward.manager;
import cc.carm.lib.easyplugin.utils.MessageUtils; import cc.carm.lib.easyplugin.utils.MessageUtils;
import cc.carm.plugin.timereward.Main; import cc.carm.plugin.timereward.Main;
import cc.carm.plugin.timereward.TimeRewardAPI; 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.RewardContents;
import cc.carm.plugin.timereward.storage.UserData; import cc.carm.plugin.timereward.storage.UserData;
import com.google.common.collect.ImmutableMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -15,15 +14,12 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable; import org.jetbrains.annotations.Unmodifiable;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class RewardManager { public class RewardManager {
protected final Map<String, RewardContents> rewards = new ConcurrentHashMap<>();
protected BukkitRunnable runnable; protected BukkitRunnable runnable;
public RewardManager(Main main) { public RewardManager(Main main) {
@ -51,38 +47,16 @@ public class RewardManager {
this.runnable = null; this.runnable = null;
} }
public Map<String, RewardContents> readRewards(@NotNull ConfigurationSection section) { protected Map<String, RewardContents> getRewards() {
Map<String, RewardContents> rewards = new HashMap<>(); return PluginConfig.REWARDS.getNotNull().getContents();
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<String, RewardContents> getRewardsMap() {
return Collections.unmodifiableMap(rewards);
} }
public @Nullable RewardContents getReward(String rewardID) { public @Nullable RewardContents getReward(String rewardID) {
return rewards.get(rewardID); return getRewards().get(rewardID);
} }
public Map<String, RewardContents> listRewards() { public Map<String, RewardContents> listRewards() {
return ImmutableMap.copyOf(getRewardsMap()); return Collections.unmodifiableMap(PluginConfig.REWARDS.getNotNull().getContents());
} }
@Unmodifiable @Unmodifiable

View File

@ -1,12 +1,13 @@
package cc.carm.plugin.timereward.storage; package cc.carm.plugin.timereward.storage;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.easyplugin.utils.ColorParser; import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.plugin.timereward.Main;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.*;
import java.util.Objects;
public class RewardContents { public class RewardContents {
@ -60,6 +61,38 @@ public class RewardContents {
return requireSeconds >= getTime(); return requireSeconds >= getTime();
} }
public Map<String, Object> serialize() {
Map<String, Object> 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -73,5 +106,44 @@ public class RewardContents {
return Objects.hash(id); return Objects.hash(id);
} }
public static final class Group {
final @NotNull Map<String, RewardContents> contents;
public Group(@NotNull Map<String, RewardContents> contents) {
this.contents = contents;
}
public @NotNull Map<String, RewardContents> getContents() {
return contents;
}
public Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
for (Map.Entry<String, RewardContents> entry : contents.entrySet()) {
map.put(entry.getKey(), entry.getValue().serialize());
}
return map;
}
public static Group parse(@NotNull ConfigurationWrapper<?> section) {
Map<String, RewardContents> 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<String, RewardContents> rewards = new LinkedHashMap<>();
rewards.put("example", RewardContents.defaults("example"));
return new Group(rewards);
}
}
} }

View File

@ -62,8 +62,7 @@ public class MySQLStorage {
return new UserData(uuid, playTime, claimedData); 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() return DatabaseTables.USER_TIMES.createQuery()
.selectColumns("uuid", "time") .selectColumns("uuid", "time")
.addCondition("uuid", uuid).setLimit(1).build() .addCondition("uuid", uuid).setLimit(1).build()
@ -71,7 +70,7 @@ public class MySQLStorage {
ResultSet resultSet = query.getResultSet(); ResultSet resultSet = query.getResultSet();
if (resultSet == null || !resultSet.next()) return 0L; if (resultSet == null || !resultSet.next()) return 0L;
return resultSet.getLong("time"); return resultSet.getLong("time");
}); }, 0L);
} }
@NotNull @NotNull