《BungeeCord插件开发》中文翻译
首页
捐助译者 (opens new window)
原文链接 (opens new window)
GitHub (opens new window)
首页
捐助译者 (opens new window)
原文链接 (opens new window)
GitHub (opens new window)
  • 教程

    • 首页
    • 第一个插件
    • 常见问题
    • 创建指令
    • 事件API
    • 配置API
      • 载入配置文件
      • 使用 Configuration 实例
      • 保存配置文件
      • 一些不提供的功能
        • 保存默认配置文件
        • 重载配置文件
    • 聊天组件API
    • 插件消息频道
    • 传送玩家
    • 记分板API
  • 资源

    • BungeeCord API (Github) (opens new window)
    • BungeeCord API (JavaDoc) (opens new window)
    • BungeeCord API (Chat JavaDoc) (opens new window)

BungeeCord 配置 API

# BungeeCord 配置 API

BungeeCord 包含一个简单的配置 API,支持 YAML 文件。

# 载入配置文件

不像 Bukkit 你可以使用 YamlConfiguration.loadConfiguration() 来载入配置文件。BungeeCord API 需要获取一个配置文件提供器。YAML 提供器叫做 YamlConfiguration。你不能自己创建这个类的实例,你必须从 ConfigurationProvider 获取它。

Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(getDataFolder(), "config.yml"));
1

跟Bukkit不同的是,这个方法在不能读入文件时会抛出 IOException,因此你需要第一时间创建文件。

# 使用 Configuration 实例

使用 Configuration 实例应该是非常简单的。最常用的部分API已经实现,例如 getString。

# 保存配置文件

如果你知道怎么载入配置文件,这应该是无用的:

ConfigurationProvider.getProvider(YamlConfiguration.class).save(configuration, new File(getDataFolder(), "config.yml"));
1

# 一些不提供的功能

# 保存默认配置文件

这是很简单的,代码如下:

            if (!getDataFolder().exists())
                getDataFolder().mkdir();

            File file = new File(getDataFolder(), "config.yml");

         
            if (!file.exists()) {
                try (InputStream in = getResourceAsStream("config.yml")) {
                    Files.copy(in, file.toPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
1
2
3
4
5
6
7
8
9
10
11
12
13

# 重载配置文件

再加载一次配置文件。

编辑此页面 (opens new window)
事件API
聊天组件API

← 事件API 聊天组件API→

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式