Created
June 15, 2020 03:49
-
-
Save CoreyShupe/5ff3d6f6f33b0d3ac16affa459b37893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.fixed.test; | |
import org.apache.commons.lang.Validate; | |
import org.bukkit.Bukkit; | |
import org.bukkit.configuration.InvalidConfigurationException; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.configuration.file.YamlConstructor; | |
import org.bukkit.configuration.file.YamlRepresenter; | |
import org.jetbrains.annotations.NotNull; | |
import org.yaml.snakeyaml.DumperOptions; | |
import org.yaml.snakeyaml.Yaml; | |
import org.yaml.snakeyaml.representer.Representer; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.Reader; | |
import java.util.Optional; | |
import java.util.logging.Level; | |
public class SingleResponsibilityYaml extends YamlConfiguration { | |
protected static final String BLANK_CONFIG = "{}\n"; | |
private final DumperOptions yamlOptions = new DumperOptions(); | |
private final Representer yamlRepresenter = new YamlRepresenter(); | |
private final Yaml yaml; | |
private Object object; | |
public SingleResponsibilityYaml() { | |
this.yaml = new Yaml(new YamlConstructor(), this.yamlRepresenter, this.yamlOptions); | |
} | |
public void setObject(Object object) { | |
this.object = object; | |
} | |
public @NotNull Object getObject() { | |
return object; | |
} | |
public <T> @NotNull Optional<T> getObjectAs() { | |
try { | |
//noinspection unchecked | |
return Optional.of((T)object); | |
} catch (ClassCastException ex) { | |
return Optional.empty(); | |
} | |
} | |
public @NotNull String saveToString() { | |
this.yamlOptions.setIndent(this.options().indent()); | |
this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
this.yamlOptions.setAllowUnicode(true); // lets allow unicode, no need to do anything else | |
this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
String dump = this.yaml.dump(getObject()); // we can just dump the object itself now | |
if (dump.equals(BLANK_CONFIG)) { | |
dump = ""; | |
} | |
return dump; | |
} | |
public void loadFromString(@NotNull String contents) { | |
Validate.notNull(contents, "Contents cannot be null"); | |
Object object = yaml.load(contents); // we load them from here | |
setObject(object); // now we set the object | |
} | |
public static SingleResponsibilityYaml loadConfiguration(@NotNull File file) { | |
Validate.notNull(file, "File cannot be null"); | |
SingleResponsibilityYaml config = new SingleResponsibilityYaml(); | |
try { | |
config.load(file); | |
} catch (IOException | InvalidConfigurationException ex) { | |
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex); | |
} | |
return config; | |
} | |
public static SingleResponsibilityYaml loadConfiguration(@NotNull Reader reader) { | |
Validate.notNull(reader, "Stream cannot be null"); | |
SingleResponsibilityYaml config = new SingleResponsibilityYaml(); | |
try { | |
config.load(reader); | |
} catch (IOException | InvalidConfigurationException ex) { | |
Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", ex); | |
} | |
return config; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment