Created
June 15, 2020 04:43
-
-
Save CoreyShupe/ee3718294cae19d29ec86d41f6d7350b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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.error.YAMLException; | |
| import org.yaml.snakeyaml.representer.Representer; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.Reader; | |
| import java.util.logging.Level; | |
| public class SingleResponsibilityYaml extends YamlConfiguration { | |
| private final DumperOptions yamlOptions; | |
| private final Representer yamlRepresenter; | |
| private final Yaml yaml; | |
| private Object object; | |
| public SingleResponsibilityYaml() { | |
| this.yamlOptions = new DumperOptions(); | |
| this.yamlRepresenter = new YamlRepresenter(); | |
| this.yaml = new Yaml(new YamlConstructor(), this.yamlRepresenter, this.yamlOptions); | |
| } | |
| public Object getObject() { | |
| return object; | |
| } | |
| public void setObject(Object object) { | |
| this.object = object; | |
| } | |
| @SuppressWarnings("unchecked") // we do check it with an exception | |
| public <T> T getObjectAs() { | |
| try { | |
| return (T) getObject(); | |
| } catch (ClassCastException ex) { | |
| return null; | |
| } | |
| } | |
| @Override | |
| @NotNull | |
| public String saveToString() { | |
| yamlOptions.setIndent(options().indent()); | |
| yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
| yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
| String dump = yaml.dump(getObject()); | |
| if (dump.equals("{}\n")) { | |
| return ""; | |
| } else { | |
| return dump; | |
| } | |
| } | |
| @Override | |
| public void loadFromString(@NotNull String contents) throws InvalidConfigurationException { | |
| Validate.notNull(contents, "Contents cannot be null"); | |
| try { | |
| setObject(yaml.load(contents)); | |
| } catch (YAMLException e) { | |
| throw new InvalidConfigurationException(e); | |
| } | |
| } | |
| 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