java类org.bukkit.configuration.file.YamlRepresenter的实例源码

Config.java 文件源码 项目:SCUtils 阅读 23 收藏 0 点赞 0 评论 0
public YamlConfigurationR() throws Exception {
  Field yamlOptionsField = YamlConfiguration.class.getDeclaredField("yamlOptions");
  yamlOptionsField.setAccessible(true);
  DumperOptionsR yamlOptions = new DumperOptionsR();
  yamlOptionsField.set(this, yamlOptions);

  Field yamlRepresenterField = YamlConfiguration.class.getDeclaredField("yamlRepresenter");
  yamlRepresenterField.setAccessible(true);
  YamlRepresenter yamlRepresenter = new YamlRepresenter();
  yamlRepresenterField.set(this, yamlRepresenter);

  Field yamlField = YamlConfiguration.class.getDeclaredField("yaml");
  yamlField.setAccessible(true);
  yamlField.set(this, new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions));
}
ConfigManager.java 文件源码 项目:TownyWands 阅读 29 收藏 0 点赞 0 评论 0
@SneakyThrows
public static void saveYAML(@NonNull YamlConfiguration config, @NonNull File file) {

    Method buildHeader = Reflect.getMethod(config.getClass(), "buildHeader");

    DumperOptions yamlOptions = (DumperOptions) Reflect.getField(config.getClass(), "yamlOptions").get(config);
    YamlRepresenter yamlRepresenter = (YamlRepresenter) Reflect.getField(config.getClass(), "yamlRepresenter").get(config);
    Yaml yaml = (Yaml) Reflect.getField(config.getClass(), "yaml").get(config);

    yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

    yamlOptions.setIndent(config.options().indent());
    yamlOptions.setAllowUnicode(true);

    String configHeader = (String) buildHeader.invoke(config);
    String yamlDump = yaml.dump(config.getValues(false));
    String blankConfig = (String) Reflect.getField(config.getClass(), "BLANK_CONFIG").get(null);

    if (yamlDump.equalsIgnoreCase(blankConfig)) yamlDump = "";
    String data = StringEscapeUtils.unescapeJava(new String(new StringBuilder(configHeader).append(yamlDump).toString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));

    try(OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
        writer.write(data);
    } catch(IOException e) {
        e.printStackTrace();
    }

}


问题


面经


文章

微信
公众号

扫码关注公众号