【问题标题】:How to make a custom config in Spigot如何在 Spigot 中进行自定义配置
【发布时间】:2016-02-06 22:25:31
【问题描述】:

我一直想知道是否有更简单的方法来使用 Spigot/Bukkit api 创建自定义配置或自定义 YAML 文件,请回答我,但说是否有更简单的方法来做到这一点:D 谢谢

【问题讨论】:

  • 查找 Bukkit/spigot 论坛,有很多人在问这个问题,甚至还有更多答案。简而言之,要创建自定义配置,您将必须使用与 Bukkit 一起使用的 YAMLConfiguration API,此外还可以将空白文本文件解析为 YAML 文件,然后您可以随意编辑/添加/删除值,然后将它们保存在文件。除了文件创建之外的所有内容都是通过 YAML apit 完成的。
  • Np :- ) 如果您有任何其他问题,请随时询问。还可以考虑从“答案”部分中批准最适合该主题的答案。
  • @Hydrox 欢迎使用 StackOverflow。不要编辑您的标题以说明它已回答,请使用回答您问题的答案下方的复选框。

标签: java config bukkit


【解决方案1】:

如果我的理解正确,你想创建一个包含你自己的东西的 Yaml 配置文件。这很简单。

【讨论】:

  • 不,我不是这个意思,我不是说如何使用我可以用作配置的 bukkit/spigot 库在 java 中创建自定义的实际 YAML 文件
  • 这确实会进行配置,但如果您想要另一个 Yaml 文件,请查看 FileConfiguration 类。
【解决方案2】:

我在我的主类中使用它:

        File locations = new File("plugins/GlobalSystem", "locations.yml");
        if (!locations.exists()) {
            try {
                locations.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            FileConfiguration loc = YamlConfiguration.loadConfiguration(locations);
            loc.set("spawn.Welt", "Welt");
            loc.set("spawn.X", 0);
            loc.set("spawn.Y", 100);
            loc.set("spawn.Z", 0);
            loc.set("spawn.Yaw", 0);
            loc.set("spawn.Pitch", 0);
            loc.set("reallife.Welt", "Welt");
            loc.set("reallife.X", 0);
            loc.set("reallife.Y", 100);
            loc.set("reallife.Z", 0);
            loc.set("reallife.Yaw", 0);
            loc.set("reallife.Pitch", 0);
            loc.set("acidisland.Welt", "Welt");
            loc.set("acidisland.X", 0);
            loc.set("acidisland.Y", 100);
            loc.set("acidisland.Z", 0);
            loc.set("acidisland.Yaw", 0);
            loc.set("acidisland.Pitch", 0);
            loc.set("skypvp.Welt", "Welt");
            loc.set("skypvp.X", 0);
            loc.set("skypvp.Y", 100);
            loc.set("skypvp.Z", 0);
            loc.set("skypvp.Yaw", 0);
            loc.set("skypvp.Pitch", 0);
            loc.set("spiele.Welt", "Welt");
            loc.set("spiele.X", 0);
            loc.set("spiele.Y", 100);
            loc.set("spiele.Z", 0);
            loc.set("spiele.Yaw", 0);
            loc.set("spiele.Pitch", 0);
            loc.set("silenthub.Welt", "Welt");
            loc.set("silenthub.X", 0);
            loc.set("silenthub.Y", 100);
            loc.set("silenthub.Z", 0);
            loc.set("silenthub.Yaw", 0);
            loc.set("silenthub.Pitch", 0);
            try {
                loc.save(locations);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

如果你现在想读出一些东西,你可以使用例如这个:

    FileConfiguration cfg = YamlConfiguration.loadConfiguration(new File("plugins/GlobalSystem", "locations.yml"));
    Location loc = new Location(Bukkit.getWorld(cfg.getString("spawn.Welt")), cfg.getDouble("spawn.X"), cfg.getDouble("spawn.Y"), cfg.getDouble("spawn.Z"));
    loc.setYaw((float) cfg.getDouble("spawn.Yaw"));
    loc.setPitch((float) cfg.getDouble("spawn.Pitch"));

写入文件:

    File file = new File("plugins/GlobalSystem", "locations.yml");
    FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    Location loc = Player.getLocation();
    cfg.set("spawn.Welt", loc.getWorld().getName());
    cfg.set("spawn.X", loc.getX());
    cfg.set("spawn.Y", loc.getY());
    cfg.set("spawn.Z", loc.getZ());
    cfg.set("spawn.Yaw", (double) loc.getYaw());
    cfg.set("spawn.Pitch", (double) loc.getYaw());
    try {
        cfg.save(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

我希望这对您有所帮助,并为我的英语不好感到抱歉;)

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 2015-04-27
    • 2015-11-23
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2019-11-24
    相关资源
    最近更新 更多