AuRechargeData/src/main/java/com/yaohun/aurechargedata/manage/ConsumeManage.java

72 lines
2.6 KiB
Java
Raw Normal View History

2024-07-14 12:04:14 +00:00
package com.yaohun.aurechargedata.manage;
2024-07-14 13:20:39 +00:00
import com.yaohun.aurechargedata.data.PlayerData;
import com.yaohun.aurechargedata.util.TimeType;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
2024-07-14 12:04:14 +00:00
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
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";
2024-07-14 13:20:39 +00:00
private HashMap<String, PlayerData> dataHashMap = new HashMap<>();
2024-07-14 12:04:14 +00:00
public ConsumeManage(){
file = new File("plugins/AuRechargeData", "consume.yml");
2024-07-14 13:20:39 +00:00
// 如果文件不存在,则创建新文件
if (!file.exists()) {
try {
file.getParentFile().mkdirs(); // 确保父文件夹存在
file.createNewFile(); // 创建新文件
} catch (IOException e) {
e.printStackTrace();
}
}
2024-07-14 12:04:14 +00:00
yml = YamlConfiguration.loadConfiguration(file);
// 检查配置文件中是否包含 "RechargeData" 节点
if (yml.getConfigurationSection(dataKey) != null) {
// 遍历 "RechargeData" 节点下的所有键(用户名)
for (String name : yml.getConfigurationSection(dataKey).getKeys(false)) {
2024-07-14 13:20:39 +00:00
ConfigurationSection section = yml.getConfigurationSection(dataKey+"."+name);
dataHashMap.put(name,new PlayerData(name,section));
2024-07-14 12:04:14 +00:00
}
}
2024-07-14 13:20:39 +00:00
Bukkit.getConsoleSender().sendMessage("§6[充值数据] §f已有消费记录的玩家: §a" + dataHashMap.size() + "");
2024-07-14 12:04:14 +00:00
}
public void SaveData(){
yml.set(dataKey,null);
2024-07-14 13:20:39 +00:00
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);
}
}
2024-07-14 12:04:14 +00:00
}
// 保存配置文件
try {
yml.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
2024-07-14 13:20:39 +00:00
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);
}
2024-07-14 12:04:14 +00:00
}