mirror of
https://github.com/carm-outsource/TimeReward.git
synced 2026-06-04 23:39:45 +08:00
feat(save): 在保存时优先利用数据库数据进行计算。
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<artifactId>timereward</artifactId>
|
<artifactId>timereward</artifactId>
|
||||||
<version>3.1.1</version>
|
<version>3.1.2</version>
|
||||||
|
|
||||||
<name>TimeReward</name>
|
<name>TimeReward</name>
|
||||||
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
|
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cc.carm.plugin.timereward.listener;
|
package cc.carm.plugin.timereward.listener;
|
||||||
|
|
||||||
|
import cc.carm.plugin.timereward.Main;
|
||||||
import cc.carm.plugin.timereward.TimeRewardAPI;
|
import cc.carm.plugin.timereward.TimeRewardAPI;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -17,6 +18,10 @@ public class UserListener implements Listener {
|
|||||||
if (player == null || !player.isOnline()) {
|
if (player == null || !player.isOnline()) {
|
||||||
TimeRewardAPI.getUserManager().unload(data.getUserUUID());
|
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();
|
MySQLStorage storage = Main.getStorage();
|
||||||
|
|
||||||
|
@Nullable UserRewardData current = loadData(data.getUserUUID()); // 考虑读取时间差,优先利用数据库的当前数据计算
|
||||||
|
TimeRecord oldRecord = current == null ? data.getTimeRecord() : current.getTimeRecord();
|
||||||
|
|
||||||
// 只需要保存游玩时间数据,领取数据已经实时保存了
|
// 只需要保存游玩时间数据,领取数据已经实时保存了
|
||||||
LocalDate date = LocalDate.now();
|
Duration daily = IntervalType.DAILY.calculate(oldRecord, data.getJoinTime());
|
||||||
Duration daily = data.getOnlineDuration(IntervalType.DAILY);
|
Duration weekly = IntervalType.WEEKLY.calculate(oldRecord, data.getJoinTime());
|
||||||
Duration weekly = data.getOnlineDuration(IntervalType.WEEKLY);
|
Duration monthly = IntervalType.MONTHLY.calculate(oldRecord, data.getJoinTime());
|
||||||
Duration monthly = data.getOnlineDuration(IntervalType.MONTHLY);
|
Duration total = IntervalType.TOTAL.calculate(oldRecord, data.getJoinTime());
|
||||||
Duration total = data.getOnlineDuration(IntervalType.TOTAL);
|
TimeRecord newRecord = new TimeRecord(LocalDate.now(), daily, weekly, monthly, total);
|
||||||
TimeRecord newRecord = new TimeRecord(date, daily, weekly, monthly, total);
|
|
||||||
|
|
||||||
storage.savePlayTime(data.getUserUUID(), newRecord);
|
storage.savePlayTime(data.getUserUUID(), newRecord);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user