From ec716658d5b2aefc3faccda633570e56f887290d Mon Sep 17 00:00:00 2001 From: yaohunya <31456652@qq.com> Date: Sun, 21 Jul 2024 02:16:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/Demon/DemonLevels/data/PlayerData.java | 70 ++++++++++++++++--- .../DemonLevels/manage/ServerManage.java | 19 ++++- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/Demon/DemonLevels/data/PlayerData.java b/src/main/java/me/Demon/DemonLevels/data/PlayerData.java index ea60dc9..1667bc8 100644 --- a/src/main/java/me/Demon/DemonLevels/data/PlayerData.java +++ b/src/main/java/me/Demon/DemonLevels/data/PlayerData.java @@ -15,13 +15,68 @@ public class PlayerData { private Player player; private UUID uuid; private int level; - private int exp; + private BigInteger exp; private BigInteger totalExp; private File file; private FileConfiguration fileConfiguration; public PlayerData(Player player){ + this.player = player; + this.uuid = player.getUniqueId(); + createPlayerData(); // 若没有这个玩家的数据则会创建一个默认文件 + this.level = fileConfiguration.getInt("level"); + String expString = fileConfiguration.getString("exp"); + this.exp = new BigInteger(expString); + String totalExpString = fileConfiguration.getString("totalExp"); + this.totalExp = new BigInteger(totalExpString); + } + public Player getPlayer() { + return player; + } + + public UUID getUuid() { + return uuid; + } + + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + public BigInteger getExp() { + return exp; + } + + public void setExp(BigInteger exp) { + this.exp = exp; + } + + public BigInteger getTotalExp() { + return totalExp; + } + + public void setTotalExp(BigInteger totalExp) { + this.totalExp = totalExp; + } + + public void savePlayerData(){ + FileConfiguration yml = this.fileConfiguration; + yml.set("level",this.level); + yml.set("exp",this.exp.toString()); + yml.set("totalExp",this.totalExp.toString()); + saveFile(); + } + + public void saveFile(){ + try { + this.fileConfiguration.save(this.file); + } catch (IOException e) { + e.printStackTrace(); + } } public void createPlayerData(){ @@ -39,19 +94,16 @@ public class PlayerData { yml.set("name",player.getName()); yml.set("uuid",player.getUniqueId().toString()); yml.set("level",1); + yml.set("exp",0); yml.set("totalExp",1); this.file = file; this.fileConfiguration = yml; saveFile(); System.out.println("[日志 - 等级] 玩家 "+player.getName()+" 已创建档案数据."); - } - } - - public void saveFile(){ - try { - this.fileConfiguration.save(this.file); - } catch (IOException e) { - e.printStackTrace(); + }else{ + FileConfiguration yml = YamlConfiguration.loadConfiguration(file); + this.file = file; + this.fileConfiguration = yml; } } } diff --git a/src/main/java/me/Demon/DemonLevels/manage/ServerManage.java b/src/main/java/me/Demon/DemonLevels/manage/ServerManage.java index 3cf668c..aa788aa 100644 --- a/src/main/java/me/Demon/DemonLevels/manage/ServerManage.java +++ b/src/main/java/me/Demon/DemonLevels/manage/ServerManage.java @@ -1,13 +1,17 @@ package me.Demon.DemonLevels.manage; +import me.Demon.DemonLevels.data.PlayerData; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + +import java.util.HashMap; public class ServerManage { + private HashMap dataHashMap = new HashMap<>(); private double expMultiplicity; // 当前经验倍率 - - private boolean LinkDemonVipSystem = false; + private boolean LinkDemonVipSystem = false; // 是否已关联 贵族系统 public ServerManage(FileConfiguration yml){ expMultiplicity = yml.getDouble("DoubleExp",1.0); @@ -23,4 +27,15 @@ public class ServerManage { public boolean isLinkDemonVipSystem() { return LinkDemonVipSystem; } + + public PlayerData getPlayerData(Player player){ + if(dataHashMap.get(player) == null){ + dataHashMap.put(player,new PlayerData(player)); + } + return dataHashMap.get(player); + } + + public HashMap getDataHashMap() { + return dataHashMap; + } }