From 72a2864b7ce5ad22eb60e677ef30c90039af3b25 Mon Sep 17 00:00:00 2001 From: yaohunya <31456652@qq.com> Date: Sun, 14 Jul 2024 21:30:10 +0800 Subject: [PATCH] test111 --- .../aurechargedata/data/PlayerData.java | 1 + .../aurechargedata/manage/ConsumeManage.java | 60 +++++++++++++++++-- .../aurechargedata/manage/RechargeManage.java | 58 ++++++++++++++++-- 3 files changed, 110 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/yaohun/aurechargedata/data/PlayerData.java b/src/main/java/com/yaohun/aurechargedata/data/PlayerData.java index a5e53e7..1b0acda 100644 --- a/src/main/java/com/yaohun/aurechargedata/data/PlayerData.java +++ b/src/main/java/com/yaohun/aurechargedata/data/PlayerData.java @@ -1,6 +1,7 @@ package com.yaohun.aurechargedata.data; import com.yaohun.aurechargedata.util.TimeType; +import com.yaohun.aurechargedata.util.UpdataTime; import org.bukkit.configuration.ConfigurationSection; import java.util.HashMap; diff --git a/src/main/java/com/yaohun/aurechargedata/manage/ConsumeManage.java b/src/main/java/com/yaohun/aurechargedata/manage/ConsumeManage.java index afc3c0a..db0969d 100644 --- a/src/main/java/com/yaohun/aurechargedata/manage/ConsumeManage.java +++ b/src/main/java/com/yaohun/aurechargedata/manage/ConsumeManage.java @@ -1,11 +1,14 @@ package com.yaohun.aurechargedata.manage; +import com.yaohun.aurechargedata.Main; import com.yaohun.aurechargedata.data.PlayerData; import com.yaohun.aurechargedata.util.TimeType; +import com.yaohun.aurechargedata.util.UpdataTime; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.scheduler.BukkitRunnable; import java.io.File; import java.io.IOException; @@ -30,12 +33,59 @@ public class ConsumeManage { } } yml = YamlConfiguration.loadConfiguration(file); - // 检查配置文件中是否包含 "RechargeData" 节点 + // 检查配置文件中是否包含 "ConsumeData" 节点 if (yml.getConfigurationSection(dataKey) != null) { - // 遍历 "RechargeData" 节点下的所有键(用户名) - for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { - ConfigurationSection section = yml.getConfigurationSection(dataKey+"."+name); - dataHashMap.put(name,new PlayerData(name,section)); + boolean updated = false; // 标记是否进行了数据更新 + if(UpdataTime.isDayDataUpdata()){ + // 遍历 "ConsumeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + yml.set(dataKey+"."+name+".daydata",null);// 将每位玩家的每日充值数据置为空 + } + // 保存配置文件 + try { + yml.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + updated = true; // 标记数据已更新 + } + if(UpdataTime.isMonthDataUpdata()){ + // 遍历 "ConsumeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + yml.set(dataKey+"."+name+".monthdata",null);// 将每位玩家的每日充值数据置为空 + } + // 保存配置文件 + try { + yml.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + updated = true; // 标记数据已更新 + } + // 如果数据已更新,异步定时任务重新加载数据 + if(updated){ + new BukkitRunnable() { + @Override + public void run() { + // 遍历 "ConsumeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + // 获取每位玩家的配置节 + ConfigurationSection section = yml.getConfigurationSection(dataKey + "." + name); + // 创建玩家数据对象并存入数据映射 + dataHashMap.put(name, new PlayerData(name, section)); + } + cancel();// 取消定时任务 + } + }.runTaskLaterAsynchronously(Main.plugin,20L); + }else { + // 数据未更新时直接加载数据 + // 遍历 "ConsumeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + // 获取每位玩家的配置节 + ConfigurationSection section = yml.getConfigurationSection(dataKey + "." + name); + // 创建玩家数据对象并存入数据映射 + dataHashMap.put(name, new PlayerData(name, section)); + } } } Bukkit.getConsoleSender().sendMessage("§6[充值数据] §f已有消费记录的玩家: §a" + dataHashMap.size() + "名"); diff --git a/src/main/java/com/yaohun/aurechargedata/manage/RechargeManage.java b/src/main/java/com/yaohun/aurechargedata/manage/RechargeManage.java index c7f67ad..a843912 100644 --- a/src/main/java/com/yaohun/aurechargedata/manage/RechargeManage.java +++ b/src/main/java/com/yaohun/aurechargedata/manage/RechargeManage.java @@ -1,12 +1,15 @@ package com.yaohun.aurechargedata.manage; +import com.yaohun.aurechargedata.Main; import com.yaohun.aurechargedata.data.PlayerData; import com.yaohun.aurechargedata.util.TimeType; +import com.yaohun.aurechargedata.util.UpdataTime; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; import java.io.File; import java.io.IOException; @@ -34,10 +37,57 @@ public class RechargeManage { yml = YamlConfiguration.loadConfiguration(file); // 检查配置文件中是否包含 "RechargeData" 节点 if (yml.getConfigurationSection(dataKey) != null) { - // 遍历 "RechargeData" 节点下的所有键(用户名) - for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { - ConfigurationSection section = yml.getConfigurationSection(dataKey+"."+name); - dataHashMap.put(name,new PlayerData(name,section)); + boolean updated = false; // 标记是否进行了数据更新 + if(UpdataTime.isDayDataUpdata()){ + // 遍历 "RechargeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + yml.set(dataKey+"."+name+".daydata",null);// 将每位玩家的每日充值数据置为空 + } + // 保存配置文件 + try { + yml.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + updated = true; // 标记数据已更新 + } + if(UpdataTime.isMonthDataUpdata()){ + // 遍历 "RechargeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + yml.set(dataKey+"."+name+".monthdata",null);// 将每位玩家的每日充值数据置为空 + } + // 保存配置文件 + try { + yml.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + updated = true; // 标记数据已更新 + } + // 如果数据已更新,异步定时任务重新加载数据 + if(updated){ + new BukkitRunnable() { + @Override + public void run() { + // 遍历 "RechargeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + // 获取每位玩家的配置节 + ConfigurationSection section = yml.getConfigurationSection(dataKey + "." + name); + // 创建玩家数据对象并存入数据映射 + dataHashMap.put(name, new PlayerData(name, section)); + } + cancel();// 取消定时任务 + } + }.runTaskLaterAsynchronously(Main.plugin,20L); + }else { + // 数据未更新时直接加载数据 + // 遍历 "RechargeData" 节点下的所有键(用户名) + for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) { + // 获取每位玩家的配置节 + ConfigurationSection section = yml.getConfigurationSection(dataKey + "." + name); + // 创建玩家数据对象并存入数据映射 + dataHashMap.put(name, new PlayerData(name, section)); + } } } Bukkit.getConsoleSender().sendMessage("§6[充值数据] §f已有充值记录的玩家: §a" + dataHashMap.size() + "名");