Skip to content

Commit

Permalink
feat(update): 适配最新依赖版本。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Feb 22, 2023
1 parent 45346b0 commit d2600d3
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 166 deletions.
15 changes: 12 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

<deps.easyplugin.version>1.5.2</deps.easyplugin.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>

<groupId>cc.carm.plugin</groupId>
<artifactId>timereward</artifactId>
<version>1.3.0</version>
<version>2.0.0</version>

<name>TimeReward</name>
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
Expand Down Expand Up @@ -100,7 +100,16 @@
<scope>compile</scope>
<optional>true</optional>
</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>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-githubchecker</artifactId>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/cc/carm/plugin/timereward/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
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;

Expand All @@ -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;

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

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

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

if (MessageUtils.hasPlaceholderAPI()) {
log("注册变量...");
new PAPIExpansion(this).register();
new PAPIExpansion(this, "TimeReward").register();
} else {
log("未安装PlaceholderAPI,跳过变量注册...");
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public static Main getInstance() {
}

public static MySQLStorage getStorage() {
return getInstance().mySQLStorage;
return getInstance().storage;
}

public ConfigurationProvider<?> getConfigProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

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

Expand Down Expand Up @@ -113,7 +113,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case 1: {
allCompletes.add("reload");
allCompletes.add("user");
allCompletes.add("listUserData");
allCompletes.add("list");
if (sender instanceof Player) allCompletes.add("test");

break;
Expand Down
67 changes: 19 additions & 48 deletions src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@

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 {

Expand All @@ -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<String> NAME = ConfiguredValue.of(String.class, "&f[初级奖励] &e总在线时长 2小时");

@HeaderComment("该奖励自动领取需要的在线时长,单位为秒")
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({"奖励相关设定,包含以下设定:",
" [id] 配置键名即奖励ID,支持英文、数字与下划线。",
" | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
" | 如果更改,原先领取过该奖励的玩家将会自动再领取一次!",
" [name] 奖励的显示名称,可以是任意字符串",
" | 可以在 commands 中使用 %(name) 来获取该奖励的名称",
" | 也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称",
" [permission] 领取奖励时后台执行的指令",
" | 支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。",
" [commands] 该奖励领取权限,可以不设置。",
" | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。"
})
public static final ConfigValue<RewardContents.Group> REWARDS = ConfigValue.builder()
.asValue(RewardContents.Group.class).fromSection()
.parseValue((v, d) -> RewardContents.Group.parse(v))
.serializeValue(RewardContents.Group::serialize)
.defaults(RewardContents.Group.defaults())
.build();

@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();
}

}

}

}
106 changes: 36 additions & 70 deletions src/main/java/cc/carm/plugin/timereward/hooker/PAPIExpansion.java
Original file line number Diff line number Diff line change
@@ -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;

public class PAPIExpansion extends PlaceholderExpansion {

private static final List<String> PLACEHOLDERS = Arrays.asList(
"%TimeReward_time%",
"%TimeReward_reward_<奖励ID>%",
"%TimeReward_claimed_<奖励ID>%"
);

private final JavaPlugin plugin;

public PAPIExpansion(JavaPlugin plugin) {
this.plugin = plugin;
import java.util.Collections;
import java.util.function.BiFunction;
import java.util.function.Function;

public class PAPIExpansion extends EasyPlaceholder {

public PAPIExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
super(plugin, rootIdentifier);

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<String> getPlaceholders() {
return PLACEHOLDERS;
protected <R> PlaceholderHandler userHandler(Function<UserData, R> userFunction) {
return userHandler((user, args) -> userFunction.apply(user));
}

@Override
public boolean canRegister() {
return true;
protected <R> PlaceholderHandler userHandler(BiFunction<UserData, String[], R> 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 <R> PlaceholderHandler rewardHandler(Function<RewardContents, R> 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);
};
}

}
36 changes: 5 additions & 31 deletions src/main/java/cc/carm/plugin/timereward/manager/RewardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@
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;
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<String, RewardContents> rewards = new ConcurrentHashMap<>();
protected BukkitRunnable runnable;

public RewardManager(Main main) {
Expand Down Expand Up @@ -51,38 +47,16 @@ public void shutdown() {
this.runnable = null;
}

public Map<String, RewardContents> readRewards(@NotNull ConfigurationSection section) {
Map<String, RewardContents> 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<String, RewardContents> getRewardsMap() {
return Collections.unmodifiableMap(rewards);
protected Map<String, RewardContents> getRewards() {
return PluginConfig.REWARDS.getNotNull().getContents();
}

public @Nullable RewardContents getReward(String rewardID) {
return rewards.get(rewardID);
return getRewards().get(rewardID);
}

public Map<String, RewardContents> listRewards() {
return ImmutableMap.copyOf(getRewardsMap());
return Collections.unmodifiableMap(PluginConfig.REWARDS.getNotNull().getContents());
}

@Unmodifiable
Expand Down
Loading

0 comments on commit d2600d3

Please sign in to comment.