From 64afcad8c7d63d1024a0b63e7e8505e43183b72c Mon Sep 17 00:00:00 2001 From: expvintl Date: Wed, 6 Sep 2023 20:47:24 +0800 Subject: [PATCH] keepinv supoort --- src/main/java/expvintl/tools/Tools.java | 130 ++++++++++-------- .../tools/{ => listeners}/LoginHandler.java | 5 +- src/main/resources/plugin.yml | 10 +- 3 files changed, 77 insertions(+), 68 deletions(-) rename src/main/java/expvintl/tools/{ => listeners}/LoginHandler.java (96%) diff --git a/src/main/java/expvintl/tools/Tools.java b/src/main/java/expvintl/tools/Tools.java index c347625..af1cfa4 100644 --- a/src/main/java/expvintl/tools/Tools.java +++ b/src/main/java/expvintl/tools/Tools.java @@ -1,5 +1,6 @@ package expvintl.tools; +import expvintl.tools.listeners.LoginHandler; import org.bukkit.*; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -8,7 +9,6 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; -import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; import java.util.HashMap; @@ -20,37 +20,57 @@ import java.util.regex.Pattern; public final class Tools extends JavaPlugin { private LoginHandler loginHandler; public static YamlConfiguration loginConfig; + public static YamlConfiguration settingsCfg; + private boolean enableLogin; //玩家登录数据 - private final String cfgPath=getDataFolder().getPath()+"/PlayerLoginData.yml"; + private final String loginDataPath =getDataFolder().getPath()+"/PlayerLoginData.yml"; + private final String settingsPath=getDataFolder().getPath()+"/settings.yml"; Logger logger=getLogger(); //传送请求队列 private final Map reqQueue=new HashMap<>(); + private void initConfigs(){ + if(!getDataFolder().exists()) getDataFolder().mkdir(); + File loginData=new File(this.loginDataPath); + File settingsPath=new File(this.settingsPath); + try { + if(!loginData.exists()) { + if (!loginData.createNewFile()) { + logger.warning("无法创建登录数据文件!"); + return; + } + } + if(!settingsPath.exists()){ + if (!settingsPath.createNewFile()) { + logger.warning("无法创建配置文件!"); + return; + } + //初始化参数 + settingsCfg=new YamlConfiguration(); + settingsCfg.load(this.settingsPath); + settingsCfg.set("enableLogin",true); + settingsCfg.save(settingsPath); + settingsCfg=null; + } + loginConfig = new YamlConfiguration(); + settingsCfg=new YamlConfiguration(); + settingsCfg.load(this.settingsPath); + loginConfig.load(this.loginDataPath); + logger.info("已加载配置!"); + enableLogin=settingsCfg.getBoolean("enableLogin"); + if(enableLogin){ + //注册事件监听器 + loginHandler=new LoginHandler(this); + getServer().getPluginManager().registerEvents(loginHandler,this); + } + } catch (Exception e) { + logger.warning(e.getMessage()); + } + } @Override public void onEnable() { // Plugin startup logic logger.info("expvintl:Tools 插件已启用!"); - loginHandler=new LoginHandler(this); - getServer().getPluginManager().registerEvents(loginHandler,this); - } - - @Override - public void onLoad() { - if(!getDataFolder().exists()) getDataFolder().mkdir(); - File conf=new File(cfgPath); - if(!conf.exists()) { - try { - if(!conf.createNewFile()) return; - } catch (IOException e) { - logger.warning(e.getMessage()); - } - } - try { - loginConfig = new YamlConfiguration(); - loginConfig.load(cfgPath); - logger.info("已加载配置!"); - } catch (Exception e) { - logger.warning(e.getMessage()); - } + initConfigs(); } @Override @@ -62,6 +82,10 @@ public final class Tools extends JavaPlugin { } switch(label) { case "reg": + if(!enableLogin) { + sender.sendMessage("未启用登录功能!"); + return true; + } if (args.length == 2) { if (loginHandler.isRegistered(sender.getName())||loginHandler.isLogged(sender.getName())) { sender.sendMessage("你已经注册过了!"); @@ -83,7 +107,7 @@ public final class Tools extends JavaPlugin { MessageDigest md=MessageDigest.getInstance("MD5"); String passMd=new BigInteger(1,md.digest(args[0].getBytes())).toString(16); loginConfig.set(sender.getName(), passMd); - loginConfig.save(cfgPath); + loginConfig.save(loginDataPath); } catch (Exception e) { logger.warning(e.getMessage()); sender.sendMessage("密码保存失败!"); @@ -98,6 +122,10 @@ public final class Tools extends JavaPlugin { return false; } case "l": + if(!enableLogin) { + sender.sendMessage("未启用登录功能!"); + return true; + } if(args.length==1){ if(loginHandler.isLogged(sender.getName())){ sender.sendMessage("你似乎已经登录过了?"); @@ -129,6 +157,10 @@ public final class Tools extends JavaPlugin { return false; } case "cp": + if(!enableLogin) { + sender.sendMessage("未启用登录功能!"); + return true; + } if(args.length==1){ if(loginHandler.isRegistered(sender.getName())&&loginHandler.isLogged(sender.getName())){ if (args[0].length() < 6 || args[0].length() > 16) { @@ -143,7 +175,7 @@ public final class Tools extends JavaPlugin { MessageDigest md=MessageDigest.getInstance("MD5"); String passMd=new BigInteger(1,md.digest(args[0].getBytes())).toString(16); loginConfig.set(sender.getName(),passMd); - loginConfig.save(cfgPath); + loginConfig.save(loginDataPath); } catch (Exception e) { logger.warning(e.getMessage()); sender.sendMessage("密码保存失败!"); @@ -158,40 +190,6 @@ public final class Tools extends JavaPlugin { sender.sendMessage("缺少参数!"); return false; } - case "kill": - if(args.length>0){ - sender.sendMessage(args[0]+" 死了"); - return true; - } - player.setHealth(0); - return true; - case "tp": - if(args.length==4){ - try { - double x = Double.parseDouble(args[1]); - double y = Double.parseDouble(args[2]); - double z = Double.parseDouble(args[3]); - player.teleport(new Location(player.getWorld(),x,y,z)); - getServer().broadcastMessage(String.format("%s 传送到了 X:%.2f Y:%.2f Z:%.2f",player.getName(),x,y,z)); - }catch (NumberFormatException f) { - sender.sendMessage("错误的数字格式!"); - } - }else if(args.length==3) { - try { - double x = Double.parseDouble(args[0]); - double y = Double.parseDouble(args[1]); - double z = Double.parseDouble(args[2]); - player.teleport(new Location(player.getWorld(), x, y, z)); - getServer().broadcastMessage(String.format("%s 传送到了 X:%.2f Y:%.2f Z:%.2f", player.getName(), x, y, z)); - } catch (NumberFormatException f) { - sender.sendMessage("错误的数字格式!"); - } - }else{ - sender.sendMessage("缺少应有的坐标参数!"); - return false; - } - return true; - case "tpa": if(args.length==1){ Player target=getServer().getPlayer(args[0]); @@ -263,6 +261,18 @@ public final class Tools extends JavaPlugin { } sender.sendMessage("没有传送请求或请求已过期!"); return true; + case "keepinv": + if(args.length==1){ + Bukkit.getWorlds().forEach(world -> { + boolean result=Boolean.parseBoolean(args[0]); + world.setGameRule(GameRule.KEEP_INVENTORY,result); + sender.sendMessage(String.format("已为 %s %s死亡不掉落",world.getName(), Boolean.TRUE.equals(world.getGameRuleValue(GameRule.KEEP_INVENTORY)) ?"开启":"关闭")); + }); + }else{ + sender.sendMessage("参数不够!"); + return false; + } + return true; } return true; } diff --git a/src/main/java/expvintl/tools/LoginHandler.java b/src/main/java/expvintl/tools/listeners/LoginHandler.java similarity index 96% rename from src/main/java/expvintl/tools/LoginHandler.java rename to src/main/java/expvintl/tools/listeners/LoginHandler.java index d05a437..79edafe 100644 --- a/src/main/java/expvintl/tools/LoginHandler.java +++ b/src/main/java/expvintl/tools/listeners/LoginHandler.java @@ -1,5 +1,6 @@ -package expvintl.tools; +package expvintl.tools.listeners; +import expvintl.tools.Tools; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.EventHandler; @@ -15,7 +16,7 @@ import java.util.List; public class LoginHandler implements Listener { private final List loggedPlayer=new ArrayList<>(); - private final YamlConfiguration cfg=Tools.loginConfig; + private final YamlConfiguration cfg= Tools.loginConfig; private final Plugin plugin; public LoginHandler(Plugin plugin){ this.plugin=plugin; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6ef7160..d0f6b0e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,12 +5,10 @@ api-version: 1.15 authors: [ expvintl ] description: Tools commands: - kill: #鎸囦护鍚 - description: "鏉姝昏嚜宸" #鎸囦护鐨勬敞瑙 - usage: "鐢ㄦ硶: /kill" - tp: - description: "浼犻佸埌鏌愪釜浣嶇疆" - usage: "鐢ㄦ硶: /tp <鐜╁鍚> " + keepinv: + description: "寮鍏虫墍鏈変笘鐣岀殑姝讳骸涓嶆帀钀" + usage: "鐢ㄦ硶: /keepInv [true/false]" + permission: op reg: description: "娉ㄥ唽鐢ㄦ埛" usage: "鐢ㄦ硶: /reg 瀵嗙爜 纭瀵嗙爜"