如何在Spigot中进行自定义配置

时间:2016-02-06 22:25:32

标签: java config bukkit

我总是想知道是否有更简单的方法来创建自定义配置或自定义YAML文件与Spigot / Bukkit api请回答我,但说如果有一个更简单和更简单的方法:D谢谢

2 个答案:

答案 0 :(得分:1)

如果我理解你是对的,你想用自己的东西创建一个Yaml配置文件。这很简单。

saveDefaultConfig();并且只需在项目中包含一个config.yml(只要语法仍然正确),它就会保存并加载该Yaml文件。

答案 1 :(得分:0)

我在我的主要课程中使用它:

        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();
    }

我希望这可以帮助你,并为我糟糕的英语抱歉;)