126 lines
5.3 KiB
Java
126 lines
5.3 KiB
Java
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;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class ConsumeManage {
|
|
private File file;
|
|
private FileConfiguration yml;
|
|
private String dataKey = "ConsumeData";
|
|
private HashMap<String, PlayerData> dataHashMap = new HashMap<>();
|
|
|
|
public ConsumeManage() {
|
|
file = new File(Main.plugin.getDataFolder(), "consume.yml");
|
|
// 如果文件不存在,则创建新文件
|
|
if (!file.exists()) {
|
|
try {
|
|
file.getParentFile().mkdirs(); // 确保父文件夹存在
|
|
file.createNewFile(); // 创建新文件
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
yml = YamlConfiguration.loadConfiguration(file);
|
|
// 检查配置文件中是否包含 "ConsumeData" 节点
|
|
if (yml.getConfigurationSection(dataKey) != null) {
|
|
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() + "名");
|
|
}
|
|
|
|
public void saveData() {
|
|
yml.set(dataKey, null);
|
|
for (String name : dataHashMap.keySet()){
|
|
PlayerData playerData = dataHashMap.get(name);
|
|
for (TimeType timeType : playerData.getValueMap().keySet()){
|
|
int value = playerData.getValueData(timeType);
|
|
String timeKey = playerData.getTimeToString(timeType);
|
|
if (value >= 1) {
|
|
yml.set(dataKey + "." + name + "." + timeKey, value);
|
|
}
|
|
}
|
|
long lastOperation = playerData.getLastOperation();
|
|
if (lastOperation >= 1000) {
|
|
yml.set(dataKey+"."+name+".lastOperation",lastOperation);
|
|
}
|
|
}
|
|
// 保存配置文件
|
|
try {
|
|
yml.save(file);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public PlayerData getPlayerData(String name) {
|
|
if(dataHashMap.get(name) == null){
|
|
dataHashMap.put(name,new PlayerData(name,null));
|
|
return dataHashMap.get(name);
|
|
}
|
|
return dataHashMap.get(name);
|
|
}
|
|
}
|