95 lines
2.5 KiB
Java
95 lines
2.5 KiB
Java
package com.yaohun.levelreward.data;
|
||
|
||
import org.bukkit.configuration.file.FileConfiguration;
|
||
import org.bukkit.configuration.file.YamlConfiguration;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @author Administrator
|
||
*/
|
||
public class PlayerData {
|
||
|
||
/**
|
||
* 玩家数据管理类
|
||
* 用于保存和管理玩家的游戏数据,特别是玩家的奖励信息
|
||
*/
|
||
private final File file;
|
||
private final FileConfiguration configuration;
|
||
|
||
private final String playerName;
|
||
|
||
/**
|
||
* 存储玩家奖励的列表
|
||
*/
|
||
private final List<String> rewardList;
|
||
|
||
/**
|
||
* 构造玩家数据对象
|
||
*
|
||
* @param playerName 玩家名称,用于标识和加载对应的玩家数据文件
|
||
*/
|
||
public PlayerData(String playerName){
|
||
this.playerName = playerName;
|
||
// 初始化文件路径
|
||
this.file = new File("plugins/AuData/LevelReward", playerName + ".yml");
|
||
// 检查并创建玩家数据文件的父目录
|
||
if(!file.getParentFile().exists()) {
|
||
file.getParentFile().mkdirs();
|
||
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||
} else {
|
||
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||
}
|
||
// 从配置中加载玩家的奖励列表
|
||
this.rewardList = this.configuration.getStringList("RewardList");
|
||
}
|
||
|
||
public String getPlayerName() {
|
||
return playerName;
|
||
}
|
||
|
||
/**
|
||
* 保存玩家数据到文件
|
||
* 将玩家的奖励列表保存到配置文件中,并写入磁盘
|
||
*/
|
||
public void savePlayerData(){
|
||
this.configuration.set("RewardList", rewardList);
|
||
try {
|
||
this.configuration.save(file);
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查玩家是否已有指定的奖励
|
||
*
|
||
* @param rewardKey 奖励的唯一键值
|
||
* @return 如果玩家已有指定的奖励,则返回true;否则返回false
|
||
*/
|
||
public boolean isHasReward(String rewardKey){
|
||
return rewardList.contains(rewardKey);
|
||
}
|
||
|
||
/**
|
||
* 给玩家添加新的奖励
|
||
*
|
||
* @param rewardKey 新增奖励的唯一键值
|
||
*/
|
||
public void addReward(String rewardKey){
|
||
rewardList.add(rewardKey);
|
||
}
|
||
|
||
/**
|
||
* 获取玩家的奖励列表
|
||
*
|
||
* @return 返回包含玩家所有奖励键值的列表
|
||
*/
|
||
public List<String> getRewardList() {
|
||
return rewardList;
|
||
}
|
||
|
||
}
|