mirror of
https://github.com/carm-outsource/TimeReward.git
synced 2026-06-04 15:28:16 +08:00
feat(save): 新增定时自动保存功能。
This commit is contained in:
@@ -17,6 +17,7 @@ import cc.carm.plugin.timereward.storage.database.MySQLStorage;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -31,6 +32,8 @@ public class Main extends EasyPlugin {
|
||||
protected UserManager userManager;
|
||||
protected RewardManager rewardManager;
|
||||
|
||||
protected BukkitRunnable autoSaveTask;
|
||||
|
||||
public Main() {
|
||||
instance = this;
|
||||
}
|
||||
@@ -96,11 +99,32 @@ public class Main extends EasyPlugin {
|
||||
info("已禁用检查更新,跳过。");
|
||||
}
|
||||
|
||||
if (PluginConfig.AUTO_SAVE.resolve() > 0) {
|
||||
long period = PluginConfig.AUTO_SAVE.resolve() * 20L;
|
||||
this.autoSaveTask = new BukkitRunnable() {
|
||||
int i = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
i = i == Integer.MAX_VALUE ? 0 : i + 1;
|
||||
debugging("第" + i + "次自动保存用户在线数据...");
|
||||
userManager.saveAll();
|
||||
}
|
||||
};
|
||||
this.autoSaveTask.runTaskTimerAsynchronously(this, period, period);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void shutdown() {
|
||||
|
||||
if (this.autoSaveTask != null && !this.autoSaveTask.isCancelled()) {
|
||||
this.autoSaveTask.cancel();
|
||||
this.autoSaveTask = null;
|
||||
}
|
||||
|
||||
info("终止奖励发放进程...");
|
||||
this.rewardManager.shutdown();
|
||||
|
||||
|
||||
@@ -33,6 +33,11 @@ public class UserCommand extends SubCommand<MainCommand> {
|
||||
}
|
||||
|
||||
UserRewardData user = TimeRewardAPI.getUserManager().get(player);
|
||||
if (user == null) {
|
||||
PluginMessages.LOADING.sendTo(sender, args[0]);
|
||||
return null;
|
||||
}
|
||||
|
||||
PluginMessages.USER_INFO.prepare(
|
||||
player.getName(),
|
||||
user.getOnlineDuration(IntervalType.DAILY).getSeconds(),
|
||||
|
||||
@@ -25,6 +25,13 @@ public interface PluginConfig extends Configuration {
|
||||
"检查更新为异步操作,绝不会影响性能与使用体验。"
|
||||
})
|
||||
ConfiguredValue<Boolean> CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true);
|
||||
|
||||
@HeaderComments({
|
||||
"自动保存设定,用于设置自动保存的时间间隔,单位为秒(小于等于0则关闭)。",
|
||||
"一般来说,玩家会在退出游戏时进行保存。",
|
||||
"但如果您希望额外的定期保存数据以避免数据丢失,可以选择开启。",
|
||||
})
|
||||
ConfiguredValue<Long> AUTO_SAVE = ConfiguredValue.of(Long.class, 60L);
|
||||
|
||||
@HeaderComments("周起始日,用于判断周度奖励的结算日期。")
|
||||
ConfiguredValue<DayOfWeek> WEEK_FIRST_DAY = ConfiguredValue.builderOf(DayOfWeek.class)
|
||||
|
||||
@@ -79,6 +79,10 @@ public interface PluginMessages extends Configuration {
|
||||
"&f玩家 &e%(player) &f并不在线。"
|
||||
).params("player").build();
|
||||
|
||||
ConfiguredMessage<BaseComponent[]> LOADING = list().defaults(
|
||||
"&e&l请稍候... &f您的在线数据正在加载中。"
|
||||
).params("player").build();
|
||||
|
||||
ConfiguredMessage<BaseComponent[]> NOT_EXISTS = list().defaults(
|
||||
"&f奖励 &e%(award) &f并不存在。"
|
||||
).params("award").build();
|
||||
|
||||
@@ -10,50 +10,21 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.ToLongFunction;
|
||||
|
||||
public class PAPIExpansion extends EasyPlaceholder {
|
||||
|
||||
public PAPIExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
|
||||
super(plugin, rootIdentifier);
|
||||
|
||||
handle("seconds", userHandler((user, args) -> {
|
||||
if (args.length < 1) return "请填写时间类型";
|
||||
IntervalType type = IntervalType.parse(args[0]);
|
||||
|
||||
if (type == null) return "时间类型不存在";
|
||||
|
||||
return user.getOnlineDuration(type).getSeconds();
|
||||
}), Collections.singletonList("<时间类型>"), "time");
|
||||
|
||||
handle("minutes", userHandler((user, args) -> {
|
||||
if (args.length < 1) return "请填写时间类型";
|
||||
IntervalType type = IntervalType.parse(args[0]);
|
||||
|
||||
if (type == null) return "时间类型不存在";
|
||||
|
||||
return user.getOnlineDuration(type).toMinutes();
|
||||
}), Collections.singletonList("<时间类型>"));
|
||||
|
||||
handle("hours", userHandler((user, args) -> {
|
||||
if (args.length < 1) return "请填写时间类型";
|
||||
IntervalType type = IntervalType.parse(args[0]);
|
||||
|
||||
if (type == null) return "时间类型不存在";
|
||||
|
||||
return user.getOnlineDuration(type).toHours();
|
||||
}), Collections.singletonList("<时间类型>"));
|
||||
|
||||
handle("days", userHandler((user, args) -> {
|
||||
if (args.length < 1) return "请填写时间类型";
|
||||
IntervalType type = IntervalType.parse(args[0]);
|
||||
|
||||
if (type == null) return "时间类型不存在";
|
||||
|
||||
return user.getOnlineDuration(type).toDays();
|
||||
}), Collections.singletonList("<时间类型>"));
|
||||
handle("seconds", timeHandler(Duration::getSeconds), Collections.singletonList("<时间类型>"), "time");
|
||||
handle("minutes", timeHandler(Duration::toMinutes), Collections.singletonList("<时间类型>"));
|
||||
handle("hours", timeHandler(Duration::toHours), Collections.singletonList("<时间类型>"));
|
||||
handle("days", timeHandler(Duration::toDays), Collections.singletonList("<时间类型>"));
|
||||
|
||||
handle("reward",
|
||||
rewardHandler(RewardContents::getDisplayName),
|
||||
@@ -89,10 +60,21 @@ public class PAPIExpansion extends EasyPlaceholder {
|
||||
protected <R> PlaceholderHandler userHandler(BiFunction<UserRewardData, String[], R> userFunction) {
|
||||
return (player, args) -> {
|
||||
if (player == null || !player.isOnline()) return "加载中...";
|
||||
return userFunction.apply(TimeRewardAPI.getUserManager().get((Player) player), args);
|
||||
UserRewardData data = TimeRewardAPI.getUserManager().get((Player) player);
|
||||
if (data == null) return "加载中...";
|
||||
return userFunction.apply(data, args);
|
||||
};
|
||||
}
|
||||
|
||||
protected PlaceholderHandler timeHandler(ToLongFunction<Duration> timeFunction) {
|
||||
return userHandler((user, args) -> {
|
||||
if (args.length < 1) return "请填写时间类型";
|
||||
IntervalType type = IntervalType.parse(args[0]);
|
||||
if (type == null) return "时间类型不存在";
|
||||
return timeFunction.applyAsLong(user.getOnlineDuration(type));
|
||||
});
|
||||
}
|
||||
|
||||
protected <R> PlaceholderHandler rewardHandler(Function<RewardContents, R> function) {
|
||||
return (player, args) -> {
|
||||
if (args.length < 1) return "请填写奖励ID";
|
||||
|
||||
@@ -72,6 +72,8 @@ public class RewardManager {
|
||||
if (!reward.checkPermission(player)) return false; // 满足权限
|
||||
|
||||
UserRewardData user = TimeRewardAPI.getUserManager().get(player);
|
||||
if (user == null) return false; // 玩家数据加载中
|
||||
|
||||
IntervalType intervalType = reward.getType();
|
||||
LocalDateTime lastClaimed = user.getClaimedDate(reward);
|
||||
|
||||
@@ -106,6 +108,8 @@ public class RewardManager {
|
||||
try {
|
||||
|
||||
UserRewardData user = TimeRewardAPI.getUserManager().get(player);
|
||||
if (user == null) return false; // 玩家数据加载中
|
||||
|
||||
Main.getStorage().addClaimedData(player.getUniqueId(), map);
|
||||
contents.forEach(user::updateClaimed);
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ public class UserManager extends UserDataManager<UUID, UserRewardData> {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
public @NotNull UserRewardData get(Player player) {
|
||||
return get(player.getUniqueId());
|
||||
public @Nullable UserRewardData get(Player player) {
|
||||
return getNullable(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user