package com.yaohun.aurechargedata.manage; import com.yaohun.aurechargedata.data.PlayerData; import com.yaohun.aurechargedata.util.TimeType; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; 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"; private HashMap dataHashMap = new HashMap<>(); public ConsumeManage(){ file = new File("plugins/AuRechargeData", "consume.yml"); // 如果文件不存在,则创建新文件 if (!file.exists()) { try { file.getParentFile().mkdirs(); // 确保父文件夹存在 file.createNewFile(); // 创建新文件 } catch (IOException e) { e.printStackTrace(); } } 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)); } } 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); } } } // 保存配置文件 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); } }