diff --git a/src/main/java/me/Demon/DemonLevels/api/DLevelAPI.java b/src/main/java/me/Demon/DemonLevels/api/DLevelAPI.java index d3ef646..b1b04bb 100644 --- a/src/main/java/me/Demon/DemonLevels/api/DLevelAPI.java +++ b/src/main/java/me/Demon/DemonLevels/api/DLevelAPI.java @@ -8,13 +8,18 @@ import me.Demon.DemonLevels.event.UpLevelEvant; import me.Demon.DemonLevels.listener.MobDeath; import me.Demon.DemonLevels.util.ScriptEngineAPI; import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.serverct.ersha.jd.AttributeAPI; +import java.io.File; +import java.io.IOException; import java.math.BigInteger; import java.util.List; import java.util.Map; +import java.util.UUID; public abstract class DLevelAPI { @@ -260,6 +265,56 @@ public abstract class DLevelAPI { setTotalExp(p,totalExp); } + public static void setOffPlayerLevel(String uuid,int level){ + if(level <= 0){return;} + // 初始化经验值 + BigInteger defaultExp = BigInteger.ZERO; + // 循环计算到当前等级所需的经验 + for (int i = 1; i < level; i++) { + // 获取当前等级所需经验 + BigInteger currentLevelNeedExp = DLevelAPI.getLevelNeedExp(i); + defaultExp = defaultExp.add(currentLevelNeedExp); + } + // 计算并设置玩家的经验值 + // 由于经验值是从上一级到当前级别的剩余经验 + BigInteger totalExp = defaultExp; + // 设置玩家的总经验 + setOffPlayerTotalExp(uuid,totalExp); + } + + + public static void setOffPlayerTotalExp(String uuid, BigInteger exp){ + File file = new File(Main.plugin.getDataFolder()+"/PlayerData",uuid+".yml"); + FileConfiguration yml = YamlConfiguration.loadConfiguration(file); + BigInteger totalExp = new BigInteger(String.valueOf(exp)); + yml.set("totalExp",totalExp.toString()); + int level = 1; + // 获取当前等级1所需的经验 + BigInteger needExp = DLevelAPI.getLevelNeedExp(level); + // 循环检查总经验是否足够升级 + while (totalExp.compareTo(needExp) >= 0) { + // 从总经验中减去当前等级所需的经验 + totalExp = totalExp.subtract(needExp); + // 升级玩家等级 + level++; + // 获取下一个等级所需的经验 + needExp = DLevelAPI.getLevelNeedExp(level); + // 如果等级小于0,退出循环(虽然在实际中等级通常不会小于0) + if (level < 0) { + break; + } + } + // 更新玩家的数据 + yml.set("level",level); + yml.set("exp",totalExp.toString()); + try { + yml.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("[调试 - 等级保存] level = "+level+" exp = "+exp+" totalExp = "+new BigInteger(String.valueOf(exp))); + } + public static void updateThePlayerExperienceBar(Player p,PlayerData playerData){ new BukkitRunnable() { public void run() { diff --git a/src/main/java/me/Demon/DemonLevels/command/MainCmd.java b/src/main/java/me/Demon/DemonLevels/command/MainCmd.java index b718972..3d7e430 100644 --- a/src/main/java/me/Demon/DemonLevels/command/MainCmd.java +++ b/src/main/java/me/Demon/DemonLevels/command/MainCmd.java @@ -98,20 +98,34 @@ public class MainCmd implements CommandExecutor , TabCompleter { } Player player = Bukkit.getPlayer(name); int value = GameUtil.convertInt(args[2]); - if(player == null) { - DemonAPI.sendMessage(sender,"目标玩家不在线."); - return true; - } if(args[0].equalsIgnoreCase("give")){ + if(player == null) { + DemonAPI.sendMessage(sender,"目标玩家不在线."); + return true; + } DLevelAPI.addExp(player,value); DemonAPI.sendMessage(sender,"给予 §6"+name+" §a经验: §d+"+value); }else if(args[0].equalsIgnoreCase("take")){ + if(player == null) { + DemonAPI.sendMessage(sender,"目标玩家不在线."); + return true; + } DLevelAPI.takeExp(player,value); DemonAPI.sendMessage(sender,"扣除 §6"+name+" §a经验: §d-"+value); }else if(args[0].equalsIgnoreCase("setlevel")){ - DLevelAPI.setPlayerLevel(player,value); - DemonAPI.sendMessage(sender,"设置 §6"+name+" §a等级: §dLv."+value); + if(player == null) { + DLevelAPI.setOffPlayerLevel(uuid,value); + DemonAPI.sendMessage(sender,"设置 §6"+name+" §a等级: §dLv."+value); + }else { + DLevelAPI.setPlayerLevel(player, value); + DemonAPI.sendMessage(sender,"设置 §6"+name+" §a等级: §dLv."+value); + } + return true; }else if(args[0].equalsIgnoreCase("settotal")){ + if(player == null) { + DemonAPI.sendMessage(sender,"目标玩家不在线."); + return true; + } BigInteger bigInteger = new BigInteger(args[2]); DLevelAPI.setTotalExp(player,bigInteger); DemonAPI.sendMessage(sender,"设置 §6"+name+" §a总经验: §d"+bigInteger.toString()); diff --git a/src/main/java/me/Demon/DemonLevels/listener/JoinEvent.java b/src/main/java/me/Demon/DemonLevels/listener/JoinEvent.java index 1af4497..9ee3d46 100644 --- a/src/main/java/me/Demon/DemonLevels/listener/JoinEvent.java +++ b/src/main/java/me/Demon/DemonLevels/listener/JoinEvent.java @@ -27,7 +27,7 @@ public class JoinEvent implements Listener { FileConfiguration yml = Main.plugin.getConfig(); int totalExp = yml.getInt("levelstats."+name+".totalExp"); BigInteger bigInteger = BigInteger.valueOf(totalExp); - if(bigInteger.compareTo(playerData.getTotalExp()) > 0){ + if (bigInteger.compareTo(playerData.getTotalExp()) > 0) { System.out.println("[调试 - 修正] 玩家 "+name+" 经验出现错误,已被纠正. YmlExp = "+bigInteger.toString()+" playerdata = "+playerData.getTotalExp().toString()); DLevelAPI.setTotalExp(p,bigInteger); } diff --git a/src/main/java/me/Demon/DemonLevels/listener/MobDeath.java b/src/main/java/me/Demon/DemonLevels/listener/MobDeath.java index e6abf1d..c70212f 100644 --- a/src/main/java/me/Demon/DemonLevels/listener/MobDeath.java +++ b/src/main/java/me/Demon/DemonLevels/listener/MobDeath.java @@ -143,7 +143,7 @@ public class MobDeath implements Listener { // 检测玩家在队伍里但是不在共享名单里 int shareExperience = (int) (teamExp / playerList.size()); // 给予玩家经验值 - DLevelAPI.addExp(p,shareExperience); + DLevelAPI.addExp(player,shareExperience); experienceGainTips(player,shareExperience,magnificationDisplayString); if(magnificationDisplayString >= 1){ player.sendMessage("§f[§e提示§f] §7队伍经验分享你获得了§6" + shareExperience + "§7! [§a+" + magnificationDisplayString + "%§7]"); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2e72ad6..658aa72 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: DemonLevels main: me.Demon.DemonLevels.Main -version: 2.2.0 +version: 2.2.2 depend: [DemonTeam] commands: dlevel: \ No newline at end of file