135 lines
3.6 KiB
Java
135 lines
3.6 KiB
Java
package com.yaohun.playermail.data;
|
||
|
||
import com.yaohun.playermail.MailMain;
|
||
import me.Demon.DemonPlayerMail.Api.DMailAPI;
|
||
import org.bukkit.Location;
|
||
import org.bukkit.configuration.ConfigurationSection;
|
||
import org.bukkit.configuration.file.FileConfiguration;
|
||
import org.bukkit.configuration.file.YamlConfiguration;
|
||
import org.bukkit.inventory.ItemStack;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.HashMap;
|
||
|
||
public class PlayerData {
|
||
|
||
/**
|
||
* 玩家数据类,用于管理玩家的邮件数据
|
||
*/
|
||
private File file;
|
||
private FileConfiguration config;
|
||
|
||
/**
|
||
* 玩家名称,用于标识和加载玩家数据
|
||
*/
|
||
private String playerName;
|
||
|
||
/**
|
||
* 存储邮件的HashMap,键为邮件过期时间,值为邮件物品堆
|
||
*/
|
||
private HashMap<Long, ItemStack> stackHashMap = new HashMap<>();
|
||
|
||
/**
|
||
* 构造函数,初始化玩家数据
|
||
* @param playerName 玩家名称
|
||
*/
|
||
public PlayerData(String playerName) {
|
||
this.playerName = playerName;
|
||
this.file = new File("plugins/AuData/MailData",playerName+".yml");
|
||
if(!file.exists()) {
|
||
this.file.getParentFile().mkdirs();
|
||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||
} else {
|
||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||
}
|
||
loadMailStack();
|
||
}
|
||
|
||
/**
|
||
* 加载邮件数据从配置文件到HashMap
|
||
*/
|
||
private void loadMailStack(){
|
||
ConfigurationSection section = config.getConfigurationSection("MailData");
|
||
if(section == null){
|
||
return;
|
||
}
|
||
for(String key : section.getKeys(false)){
|
||
long expiryTime = Long.parseLong(key);
|
||
ItemStack itemStack = section.getItemStack(key);
|
||
stackHashMap.put(expiryTime,itemStack);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存邮件数据从HashMap到配置文件
|
||
*/
|
||
public void saveMailStack(){
|
||
config.set("MailData",null);
|
||
if(!stackHashMap.isEmpty()) {
|
||
long nowTime = System.currentTimeMillis();
|
||
for (Long key : stackHashMap.keySet()) {
|
||
if (nowTime >= key) {
|
||
continue;
|
||
}
|
||
ItemStack itemStack = stackHashMap.get(key);
|
||
config.set("MailData." + key, itemStack);
|
||
}
|
||
}
|
||
try {
|
||
config.save(file);
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取存储邮件的HashMap
|
||
* @return 存储邮件的HashMap
|
||
*/
|
||
public HashMap<Long, ItemStack> getStackHashMap() {
|
||
return stackHashMap;
|
||
}
|
||
|
||
/**
|
||
* 获取玩家邮件的数量
|
||
* @return 邮件数量
|
||
*/
|
||
public int getMailAmount(){
|
||
return stackHashMap.size();
|
||
}
|
||
|
||
/**
|
||
* 重置玩家邮件数据,清空所有邮件
|
||
*/
|
||
public void resetMailData(){
|
||
stackHashMap.clear();
|
||
}
|
||
|
||
/**
|
||
* 根据过期时间获取邮件物品堆
|
||
* @param expiryTime 邮件过期时间
|
||
* @return 邮件物品堆
|
||
*/
|
||
public ItemStack getMailStack(long expiryTime){
|
||
return stackHashMap.get(expiryTime);
|
||
}
|
||
|
||
/**
|
||
* 根据过期时间移除邮件
|
||
* @param expiryTime 邮件过期时间
|
||
*/
|
||
public void removeMailStack(long expiryTime){
|
||
stackHashMap.remove(expiryTime);
|
||
}
|
||
|
||
/**
|
||
* 添加邮件到玩家数据中
|
||
* @param expiryTime 邮件过期时间
|
||
* @param stack 邮件物品堆
|
||
*/
|
||
public void addMailStack(long expiryTime,ItemStack stack){
|
||
stackHashMap.put(expiryTime,stack);
|
||
}
|
||
}
|