1
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:
2025-01-10 08:43:14 +08:00
parent f8aafa0a1b
commit ef145803fa
3 changed files with 14 additions and 7 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
<groupId>cc.carm.plugin</groupId>
<artifactId>timereward</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<name>TimeReward</name>
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
@@ -1,5 +1,6 @@
package cc.carm.plugin.timereward.listener;
import cc.carm.plugin.timereward.Main;
import cc.carm.plugin.timereward.TimeRewardAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@@ -17,6 +18,10 @@ public class UserListener implements Listener {
if (player == null || !player.isOnline()) {
TimeRewardAPI.getUserManager().unload(data.getUserUUID());
} // 不在线自动卸载
}).exceptionally(ex -> {
Main.severe("加载用户在线时长数据失败: " + event.getPlayer().getName());
ex.printStackTrace();
return null;
});
}
@@ -38,13 +38,15 @@ public class UserManager extends UserDataManager<UUID, UserRewardData> {
MySQLStorage storage = Main.getStorage();
@Nullable UserRewardData current = loadData(data.getUserUUID()); // 考虑读取时间差,优先利用数据库的当前数据计算
TimeRecord oldRecord = current == null ? data.getTimeRecord() : current.getTimeRecord();
// 只需要保存游玩时间数据,领取数据已经实时保存了
LocalDate date = LocalDate.now();
Duration daily = data.getOnlineDuration(IntervalType.DAILY);
Duration weekly = data.getOnlineDuration(IntervalType.WEEKLY);
Duration monthly = data.getOnlineDuration(IntervalType.MONTHLY);
Duration total = data.getOnlineDuration(IntervalType.TOTAL);
TimeRecord newRecord = new TimeRecord(date, daily, weekly, monthly, total);
Duration daily = IntervalType.DAILY.calculate(oldRecord, data.getJoinTime());
Duration weekly = IntervalType.WEEKLY.calculate(oldRecord, data.getJoinTime());
Duration monthly = IntervalType.MONTHLY.calculate(oldRecord, data.getJoinTime());
Duration total = IntervalType.TOTAL.calculate(oldRecord, data.getJoinTime());
TimeRecord newRecord = new TimeRecord(LocalDate.now(), daily, weekly, monthly, total);
storage.savePlayTime(data.getUserUUID(), newRecord);
}