From 6284cff152dd9952a94f7536b012d358ea218a38 Mon Sep 17 00:00:00 2001 From: CNLuminous Date: Fri, 8 Aug 2025 01:56:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugin):=20=E4=B8=BA=20LimboBackSpawn=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增配置文件加载和保存功能 - 添加 maxX、minY 和 maxZ 配置项 - 实现配置文件不存在时自动创建默认配置 - 优化插件启用流程,注册事件和命令 --- .../java/com/blockfantasy/LimboBackSpawn.java | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/blockfantasy/LimboBackSpawn.java b/src/main/java/com/blockfantasy/LimboBackSpawn.java index 201a29e..0d0d85b 100644 --- a/src/main/java/com/blockfantasy/LimboBackSpawn.java +++ b/src/main/java/com/blockfantasy/LimboBackSpawn.java @@ -10,6 +10,12 @@ import com.loohp.limbo.location.Location; import com.loohp.limbo.player.Player; import com.loohp.limbo.plugins.LimboPlugin; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.Properties; + public class LimboBackSpawn extends LimboPlugin implements Listener { public static LimboBackSpawn instance; public static Location spawn; @@ -21,9 +27,13 @@ public class LimboBackSpawn extends LimboPlugin implements Listener { @Override public void onEnable() { instance = this; + + loadConfig(); + spawn = instance.getServer().getServerProperties().getWorldSpawn(); - Limbo.getInstance().getEventsManager().registerEvents(this,new LimboBackSpawnListener()); - Limbo.getInstance().getPluginManager().registerCommands(this,new GetLocationCommand()); + + instance.getServer().getEventsManager().registerEvents(this,new LimboBackSpawnListener()); + instance.getServer().getPluginManager().registerCommands(this,new GetLocationCommand()); } @@ -78,4 +88,37 @@ public class LimboBackSpawn extends LimboPlugin implements Listener { } } } + + private void loadConfig() { + File configFile = new File(getDataFolder(), "config.properties"); + if (!configFile.exists()) { + saveDefaultConfig(configFile); + } + Properties props = new Properties(); + try { + props.load(Files.newInputStream(configFile.toPath())); + + maxX = Integer.parseInt(props.getProperty("maxX", "50")); + minY = Integer.parseInt(props.getProperty("minY", "-10")); + maxZ = Integer.parseInt(props.getProperty("maxZ", "50")); + + } catch (IOException | NumberFormatException e) { + instance.getServer().getConsole().sendMessage("无法加载配置文件,使用默认值"); + maxX = 50; + minY = -10; + maxZ = 50; + } + } + private void saveDefaultConfig(File configFile) { + try { + configFile.getParentFile().mkdirs(); + Properties props = new Properties(); + props.setProperty("maxX", "50"); + props.setProperty("minY", "-10"); + props.setProperty("maxZ", "50"); + props.store(Files.newOutputStream(configFile.toPath()), "LimboBackSpawn"); + } catch (IOException e) { + instance.getServer().getConsole().sendMessage("无法创建默认配置文件"); + } + } }