feat(plugin): 为 LimboBackSpawn 插件添加配置文件支持

- 新增配置文件加载和保存功能
- 添加 maxX、minY 和 maxZ 配置项
- 实现配置文件不存在时自动创建默认配置
- 优化插件启用流程,注册事件和命令
This commit is contained in:
CNLuminous 2025-08-08 01:56:44 +08:00
parent f27b176096
commit 6284cff152
1 changed files with 45 additions and 2 deletions

View File

@ -10,6 +10,12 @@ import com.loohp.limbo.location.Location;
import com.loohp.limbo.player.Player; import com.loohp.limbo.player.Player;
import com.loohp.limbo.plugins.LimboPlugin; 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 class LimboBackSpawn extends LimboPlugin implements Listener {
public static LimboBackSpawn instance; public static LimboBackSpawn instance;
public static Location spawn; public static Location spawn;
@ -21,9 +27,13 @@ public class LimboBackSpawn extends LimboPlugin implements Listener {
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
loadConfig();
spawn = instance.getServer().getServerProperties().getWorldSpawn(); 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("无法创建默认配置文件");
}
}
} }