Java 类org.bukkit.configuration.file.YamlRepresenter 实例源码

项目:SCUtils    文件:Config.java   
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));
}
项目:TownyWands    文件:ConfigManager.java   
@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();
    }

}